130 likes | 287 Views
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. Parse trees {week 05}. from Concepts of Programming Languages , 9th edition by Robert W. Sebesta, Addison-Wesley, 2010, ISBN 0-13-607347-6. Compilation. a parse tree represents
E N D
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. Parse trees{week 05} from Concepts of Programming Languages, 9th edition by Robert W. Sebesta, Addison-Wesley, 2010, ISBN 0-13-607347-6
Compilation a parse tree represents the syntactic structure of the program lexical analysis groupscharacters into lexical units syntax analysis transformslexical units into parse trees analyzes parse trees to produce intermediate code transforms intermediate code into executable machine code
BNF structure • Syntax of an assignment statement in BNF: • BNF rule or production defining <assign>: <assign> <var> = <expression> ; abstractionbeing defined definitionof <assign> • The definition consists of other abstractions, as well as lexemes and tokens
Example language <program> begin <stmts> end <stmts> <stmt> | <stmt> ; <stmts> <stmt> <var> = <expr> <var> a | b | c | d | e <expr> <term> + <term> | <term> - <term> <term> <var> | literal-integer-value a vertical bar indicates an OR a token, which is simplya grouping of lexemes • Write a sentence that conforms to this grammar
<program> begin <stmts> end <stmts> <stmt> | <stmt> ; <stmts> <stmt> <var> = <expr> <var> a | b | c | d | e <expr> <term> + <term> | <term> - <term> <term> <var> | literal-integer-value Derivations • A derivation is a repeated application of rules • Start with a start symbol and end with a sentence <program> => begin <stmts> end => begin <stmt> end => begin <var> = <expr> end => begin b = <expr> end => begin b = <term> + <term> end => begin b = <var> + <term> end => begin b = c + <term> end => begin b = c + 123 end • Many possible (often infinite) derivations
<assign> <var> = <expr> <var> A | B | C | D <expr> <expr> + <expr> | <expr> * <expr> | ( <expr> ) | <var> Parse trees (i) • A parse tree represents the structure ofa derivation • Every internalnode is anon-terminalabstraction • Every leaf nodeis a terminal symbol • Draw a parse tree for • B = A * C + D <assign> <var> = <expr> C <expr> * <expr> <var> <var> A B
<assign> <var> = <expr> <var> A | B | C | D <expr> <expr> + <expr> | <expr> * <expr> | ( <expr> ) | <var> Parse trees (ii) <assign> B = A * C + D <var> = <expr> B <expr> * <expr> <var> <expr> + <expr> <var> A <var> D C
<assign> <var> = <expr> <var> A | B | C | D <expr> <expr> + <expr> | <expr> * <expr> | ( <expr> ) | <var> Parse trees (iii) <assign> B = A * C + D <var> = <expr> B <expr> + <expr> <var> <expr> * <expr> <var> <var> D C A
Ambiguous grammars • A grammar that generates a sentential form for which there are two or more distinct parse trees is an ambiguous grammar • Ambiguity in a grammar leads to problems... • ...because compilers oftenbase semantics on parse trees • operator precedence • if-else parse trees are analyzed to produce intermediate code
Unambiguous grammar Ambiguous grammar Unambiguous grammar <assign> <var> = <expr> <var> A | B | C | D <expr> <var> + <expr> | <var> * <expr> | ( <expr> ) | <var> <assign> <var> = <expr> <var> A | B | C | D <expr> <expr> + <term> | <term> <term> <term> * <factor> | <factor> <factor> ( <expr> ) | <var> • An unambiguous grammar has exactly one derivation and parse tree for each unique sentential form
What next? • Read and study Chapter 3 • Do Exercises at the end of Chapter 3 • Do the What next? exercises from theWeek 3 lecture notes • DUE NEXT CLASS!