Presentation is loading. Please wait.

Presentation is loading. Please wait.

10/13/2015IT 3271 Tow kinds of predictive parsers: Bottom-Up: The syntax tree is built up from the leaves Example: LR(1) parser Top-Down The syntax tree.

Similar presentations


Presentation on theme: "10/13/2015IT 3271 Tow kinds of predictive parsers: Bottom-Up: The syntax tree is built up from the leaves Example: LR(1) parser Top-Down The syntax tree."— Presentation transcript:

1 10/13/2015IT 3271 Tow kinds of predictive parsers: Bottom-Up: The syntax tree is built up from the leaves Example: LR(1) parser Top-Down The syntax tree is built up from the root Example: LL(1) parser Left to right scanning Rightmost derivations 1 symbol look-ahead Left to right scanning Leftmost derivations 1 symbol look-ahead

2 10/13/2015IT 3272 LR(1) parser Left to right scanning Rightmost derivations 1 symbol look-ahead Advantage (over LL(1)) 1.Allowing left-recursion 2.More efficient (no recursion) 3.Easier to implement code generation 4.More powerful (LL(k) combined) Disadvantage (over LL(1)) 1.The parsing table is much more difficult to get 2.Implementation of the parser is slightly more involved

3 10/13/2015IT 3273 1.E  E + T 2.E  T 3.T  T * F 4.T  F 5.F  ( E ) 6.F  id 1.E  T E’ 2.E’  + T E’ 3.E’  4.T  F T’ 5.T’  * F T’ 6.T’  7.F  ( E ) 8.F  id Are we happy with this? Try the parse tree for id+(id+id) using 2 different grammars

4 10/13/2015IT 3274 LR(1) parser Current state of the parser Current symbol to handle From input tokens from reduction Actions: 1.Reduce a production rule 2.Push the token to the stack and transfer state Push the reduced symbol and transfer state

5 10/13/2015IT 3275 How so? Bottom-Up: The syntax tree is built up from the leaves Example: LR(1) parser Left to right scanning Rightmost derivations 1 symbol look-ahead 1.Bottom up 2.1 look-ahead symbol 3.Left to right scanning 4.Rightmost derivations LR(1) parser This will enforce left association

6 10/13/2015IT 3276 Arithmetic Expression (12/2*3+(1+(2-3)))/3-(2/3) Example: Evaluation Arithmetic ExpressionEvaluation Arithmetic Expression Using operator precedence A special kind of LR(1) parsing ( in, ) << +, - << *, / << ^ << ( out

7 10/13/2015IT 3277 Operator Precedence Relation 1.E  E + T 2.E  E - T 3.E  T 4.T  T * F 5.T  T / F 6.T  P 7.T  F^P 8.F  ( E ) 9.F  n (n+-*/^) ( << = n >> + << >> << >> - << >> << >> * << >> <<>> / << >> <<>> ^ << >> ) Yes, we do have an algorithm to find this relation


Download ppt "10/13/2015IT 3271 Tow kinds of predictive parsers: Bottom-Up: The syntax tree is built up from the leaves Example: LR(1) parser Top-Down The syntax tree."

Similar presentations


Ads by Google