10/13/2015it 3271 tow kinds of predictive parsers: bottom-up: the syntax tree is built up from the...
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](https://reader035.vdocuments.mx/reader035/viewer/2022072015/56649eb55503460f94bbd74e/html5/thumbnails/1.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022072015/56649eb55503460f94bbd74e/html5/thumbnails/2.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022072015/56649eb55503460f94bbd74e/html5/thumbnails/3.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022072015/56649eb55503460f94bbd74e/html5/thumbnails/4.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022072015/56649eb55503460f94bbd74e/html5/thumbnails/5.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022072015/56649eb55503460f94bbd74e/html5/thumbnails/6.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022072015/56649eb55503460f94bbd74e/html5/thumbnails/7.jpg)
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