CS212 Spring 2002 3/27/2002 Lecture 10: No More FBR! ; Translation ; Abstract Syntax Trees ------------------------------------------------------------------------------- [0] Announcements: + reading: Brookshear 5.1, 5.4, 7.5 + extended due date 4/15, 1:00:00 pm + some useful online notes: http://www.slc.edu/~msiff/courses/compilers/notes/absyn.html http://www.cs.wisc.edu/~cs536-1/Notes/1.OVERVIEW ------------------------------------------------------------------------------- [1] Summary from last time: + no need to continue with FBRs -- I think you understand it by now :-) ------------------------------------------------------------------------------- [2] Overview for today: + more details on compilers and intro to syntax trees + CS211 introduces trees on Thurs 4/4, so we can do that now ------------------------------------------------------------------------------- [3] Language Implementation + see section 5.4 in optional textbook + showing a bit more detail on what's going on + TRANSLATOR vs COMPILER? see pg 228 translator: convert language into another language compiler: convert a high-level language into assembly/machine code ------------------------------------------------------------------------------- [4] Translation + TRANSLATION: converting a program from one language to another SOURCE PROGRAM gets translated into OBJECT PROGRAM + process of translation (pg 255, Fig 5.12): source lexical parser code object program analyzer generator program + note: steps are usually intertwined + see also http://www.cs.wisc.edu/~cs536-1/Notes/1.OVERVIEW for another description and terms ------------------------------------------------------------------------------- [5] Lexical Analysis + LEXICAL ANALYSIS: recognizing which strings of symbols from the source program represent a single entity + TOKEN: lexical unit, or one of the entities recognized by the lex analyzer ------------------------------------------------------------------------------- [6] Parsing + PARSING: process of identifying the grammatical structure of the program and recognizing the role of each component + formatting: FIXED-FORMAT LANGUAGE: appearance and position matters for parser FREE-FORMAT LANGUAGE: positioning of statements is irrelevant |=================================| Page 1 |=================================| [7] Parsing continued + SYNTAX DIAGRAMS: - see pp 257-258 - graphical ways of representing the syntax rules - TERMINALS ("finished" nodes) and NONTERMINALS (need further expansion) + PARSE TREE: - see pp 259-260 - graphical ways of representing how code conforms to syntax - represents parser's understanding of program's grammar ex) x + y + SYMBOL TABLE: - storing variable info - see book for details (pg 261) ------------------------------------------------------------------------------- [8] Abstract Syntax Trees + Program Hierarchy public static void xor(boolean x, boolean y) { if (x != y) return true; return false; } hierarchy/flow? statement block: __________ selection statement: __________ terminals: __________ + AST: - high-level description of program hierarchy - relations of program syntax, not punctuation & other elements - effectively "condenses" a parse tree - helps to build your compiler! + Examples) ( x + 1 ); parse tree AST |=================================| Page 2 |=================================| [11] Abstract Syntax Trees continued x := 1 ; y := (x + 1) ; See also Example 1 on http://www.slc.edu/~msiff/courses/compilers/notes/absyn.html parse tree AST if ( ( x > 1 ) ) return x ; else return 0 ; while ( ( x < 4 ) ) do { y := f( ( x + 1 ) ); x := ( x + 1 ); } + more examples? - there are trees and lists for each "blah->something" in the grammar - see next lecture and Part 4 notes (forthcoming) ------------------------------------------------------------------------------- [10] Code Generation + CODE GENERATION: process of constructing the machine-language instructions to simulate the statements recognized by the parser ------------------------------------------------------------------------------- [11] Linking and Loading + terms pop up quite a bit with C/C++ programming + see pp 262-264 + LINKING: connections to needed operating system utilities + LOADING: put complete executable program into memory |=================================| Page 3 |=================================|