Q3Compiler Design
Question
What is Ambiguous Grammar?
Answer
An Ambiguous Grammar is a grammar for which some string in its language has more than one parse tree (or equivalently, more than one leftmost or rightmost derivation).
A Context-Free Grammar G is ambiguous if there exists at least one string w in L(G) that has two or more distinct parse trees. Equivalently, w has two or more distinct leftmost derivations or two or more distinct rightmost derivations.
Classic example: The grammar E → E + E | E E | id | (E) is ambiguous because the string 'id + id id' has two parse trees — one that groups as (id+id)id and another as id+(idid). Ambiguity is undesirable because it creates uncertainty in the meaning of programs. Compilers use disambiguation rules (operator precedence and associativity) or rewrite the grammar to be unambiguous.