Docsity

Parse Trees, Parsing Techniques - Compiler Construction - Lecture Slides, Slides of Compiler Construction

Partial preview of the text

Download Parse Trees, Parsing Techniques - Compiler Construction - Lecture Slides and more Slides Compiler Construction in PDF only on Docsity!

Compiler

Construction

Lecture 12

2

Parse Trees

G E

E op^ E x – E op (^) E

(^2) * y

Leftmost derivation

4

Parse Trees

G E op

evaluation order (x – 2 ) (^) * y

E

x –

E

E op^ E

2

  • y

Rightmost derivation

5

 These two derivations point out a problem with the grammar

 It has no notion of precedence , or implied order of evaluation

7

To add precedence

 Create a non-terminal for each level of precedence

 Isolate corresponding part of grammar

8

To add precedence

 Create a non-terminal for each level of precedence

 Isolate corresponding part of grammar

10

To add precedence

 Force parser to recognize high precedence subexpressions first.

11

For algebraic expressions

 Multiplication and division, first. (level one)

 Subtraction and addition, next (level two)

13

For algebraic expressions

 Multiplication and division, first. (level one)

 Subtraction and addition, next (level two)

14

1 Goal → expr 2 expr → expr + term 3 | expr – term 4 | term 5 term → term ∗ factor 6 | term / factor 7 | factor 8 factor → number 9 | Id

16

This grammar is larger

 Takes more rewriting to reach some of the terminal symbols

 But it encodes expected precedence

17

This grammar is larger

 Takes more rewriting to reach some of the terminal symbols

 But it encodes expected precedence

19

 Produces same parse tree under leftmost and rightmost derivations

 Let’s see how it parses x – 2 (^) * y

20

 Produces same parse tree under leftmost and rightmost derivations

 Let’s see how it parses x – 2 (^) * y