10/13/2015it 3271 tow kinds of predictive parsers: bottom-up: the syntax tree is built up from the...

7
07/20/22 IT 327 1 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

Upload: horatio-price

Post on 31-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 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

04/19/23 IT 327 1

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 scanningRightmost derivations1 symbol look-ahead

Left to right scanningLeftmost derivations1 symbol look-ahead

Page 2: 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

04/19/23 IT 327 2

LR(1) parserLeft to right scanningRightmost derivations1 symbol look-ahead

Advantage (over LL(1))

1.Allowing left-recursion2.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 get2.Implementation of the parser is slightly more involved

Page 3: 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

04/19/23 IT 327 3

1. E E + T2. E T3. T T * F4. T F5. 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

Page 4: 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

04/19/23 IT 327 4

LR(1) parser

Current state of the parserCurrent symbol to handle

From input tokensfrom reduction

Actions: 1.Reduce a production rule2.Push the token to the stack and transfer state

Push the reduced symbol and transfer state

Page 5: 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

04/19/23 IT 327 5

How so?

Bottom-Up:

The syntax tree is built up from the leaves

Example: LR(1) parser Left to right scanningRightmost derivations1 symbol look-ahead

1. Bottom up2. 1 look-ahead symbol3. Left to right scanning4. Rightmost derivations

LR(1) parser This will enforce left association

Page 6: 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

04/19/23 IT 327 6

Arithmetic Expression

(12/2*3+(1+(2-3)))/3-(2/3)

Example: Evaluation Arithmetic Expression

Using operator precedence

A special kind of LR(1) parsing

(in, ) << +, - << *, / << ^ << (out

Page 7: 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

04/19/23 IT 327 7

Operator Precedence Relation

1. E E + T2. E E - T3. E T4. T T * F5. T T / F6. T P7. T F^P8. F ( E )9. F n

( n + - * / ^ )

( <<

<<

<<

<<

<<

<<

<<

=

n >>

>>

>>

>>

>>

>>

+ <<

<<

>>

>>

<<

<<

<<

>>

- <<

<<

>>

>>

<<

<<

<<

>>

* <<

<<

>>

>>

>>

>>

<<

>>

/ <<

<<

>>

>>

>>

>>

<<

>>

^ <<

<<

>>

>>

>>

>>

>>

>>

) >>

>>

>>

>>

>>

>>

Yes, we do have an algorithm to find this relation