1 / 12

Syntax One - Hybrid

Syntax One - Hybrid. CMSC 331. Parse Trees. PS → P | P PS P → e | '(' PS ')' |

egan
Download Presentation

Syntax One - Hybrid

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. Syntax One - Hybrid CMSC 331

  2. Parse Trees PS → P | P PS P → e | '(' PS ')' | '<' PS '>' | '[' PS ']' What’s the parse tree for this statement ? < [ ] [ < > ] >

  3. Derivation for < [ ] [ < > ] > PS → P → < PS > → P PS > → < [ PS ] PS >’ → < [P] PS >’ → < [ e] PS > → <‘[ ] PS> → < [ ] P > → < [ ] [ PS ] > → < [ ] [ P] > → < [ ] [‘<PS > ] > → < [ ] [‘<P> ] >’ → < [ ] [<e’> ] > → < [ ] [ < > ] >

  4. Ambiguity • Two parse trees for the same expression • Consider the following grammar String → String + String | String - String | DIGIT What are the two trees for 9 - 5 + 2

  5. String String + DIGIT(2) DIGIT(9) - DIGIT(5) Parse Tree 1

  6. String DIGIT(9) - String DIGIT(5) * DIGIT(2) Parse Tree 2

  7. EBNF - Extended BNF • Like BNF except that • Non-terminals start w/ uppercase • Parens are used for grouping terminals • Braces {} represent zero or more occurrences (iteration ) • Brackets [] represent an optional construct , that is a construct that appears either once or not at all.

  8. EBNF example Exp → Term { ('+' | '-') Term } Term → Factor { ('*' | '/') Factor } Factor → '(' Exp ')' | variable | constant

  9. EBNF/BNF • EBNF and BNF are equivalent • How can {} be expressed in BNF? • How can ( ) be expressed? • How can [ ] be expressed?

  10. EBNF for {} • X-> a { b } g • BNF • X -> a X’ • X’ -> b X’ | g • EBNF for [] • X-> a [b ] g • BNF • X -> a b g | a g

  11. Syntax Graphs Terminal in circles Non- terminals in rectangles; Syntax Graphs - put the terminals in circles or ellipses and put the nonterminals in rectangles; connect with lines with arrowheads e.g., Pascal type declarations type_identifier ( identifier ) , constant .. constant

  12. Recursive Descent • An easy way to build a parser • Example • Does work in the face of left recursion • Purge left recursion

More Related