














































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!















































2
G E
E op^ E x – E op (^) E
(^2) * y
Leftmost derivation
4
G E op
evaluation order (x – 2 ) (^) * y
E
x –
E
E op^ E
2
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