1 / 11

Parse trees {week 05}

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

nami
Download Presentation

Parse trees {week 05}

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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

  4. 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

  5. <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

  6. <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

  7. <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

  8. <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

  9. 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

  10. 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

  11. 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!

More Related