Practice free →
HomeGATE CSEcomputerscienceCompiler Design › An AMBIGUOUS grammar is one where

An AMBIGUOUS grammar is one where

Adifferent programs produce the same output
Bthe grammar generates infinitely many strings
Cthe grammar is incomplete
Dsome string has TWO OR MORE distinct parse trees
Answer & Solution
Correct answer: D. some string has TWO OR MORE distinct parse trees
1. AMBIGUITY: a CFG that admits more than one parse tree for the same string. 2. Each parse tree means a DIFFERENT INTERPRETATION of the program — bad for compilers. 3. Classic example: arithmetic expression $a + b * c$. Without precedence, it could parse as $(a + b) * c$ or $a + (b * c)$. 4. SOLUTION: rewrite the grammar to encode precedence as nesting levels: - $E \to E + T \mid T$ - $T \to T * F \mid F$ - $F \to id \mid (E)$ 5. Other options misstate ambiguity. _Source: Bob Nystrom, "Crafting Interpreters", Ch 6.1 (Ambiguity and the Parsing Game)._
Solve this in the app — GATE CSE practice & 24k+ MCQs →
Related questions