Presentation is loading. Please wait.

Presentation is loading. Please wait.

Syntax One - Hybrid CMSC 331.

Similar presentations


Presentation on theme: "Syntax One - Hybrid CMSC 331."— Presentation transcript:

1 Syntax One - Hybrid CMSC 331

2 Parse Trees What’s the parse tree for this statement ?
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

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

6 Parse Tree 2 String DIGIT(9) - DIGIT(5) DIGIT(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?

10 EBNF for {} BNF EBNF for [] X-> a { b } g X -> a X’
X’ -> b X’ | g EBNF for [] X-> a [b ] g 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


Download ppt "Syntax One - Hybrid CMSC 331."

Similar presentations


Ads by Google