SLIDE 3 Another grammar for the same language: E → E+E | E*E | (E) | a This grammar is ambiguous: there is a string in L(G5) with two different parse trees, or, equivalently, with 2 different leftmost
- derivations. Note the pragmatic difference:
in general, (a+a)*a != a+(a*a); which is “right”? Fig 2.6: Two parse trees for a+a!a in grammar G5
9
Fig 2.6: Two parse trees for a+a!a in grammar G5
10
The “E, P” grammar again
This grammar is unambiguous.
(Why? Very informally, the 3 E rules generate P((‘+’∪’*’)P)* and only via a parse tree that “hangs to the right”, as shown.)
But it has another undesirable feature: Parse tree structure does not reflect the usual precedence of * over +. E.g., tree at lower right suggests “a * a + a == a * (a + a)”
11
A more complex grammar, again the same language. This one is unambiguous and its parse trees reflect usual precedence/associativity of plus and times.
12