← RGPV CS-501 Theory of Computation December 2025 Solved Question Paper
Question
Explain grammar ambiguity and methods for removing it.
Asked in: CS-501 | December 2025 | 5(a)
Answer
A CFG is ambiguous if some terminal word has two distinct parse trees. Equivalently, it has two distinct leftmost derivations for the same word. Ambiguity is problematic in compilers because different trees can imply different meanings.
Example
Consider E→E+E | E*E | id. The string id+id*id can mean (id+id)*id or id+(id*id). Both interpretations follow the grammar. The first has multiplication at the root; the second has addition at the root.
Removal by precedence and associativity
Use E→E+T | T, T→T*F | F, F→(E) | id. The separate levels enforce multiplication before addition, while left recursion makes each binary operator left associative. Parentheses explicitly override precedence. The resulting expression grammar has a unique parse for each valid expression.
Other techniques include separating matched and unmatched statements to resolve dangling else, and restructuring overlapping productions. Left factoring helps predictive parsing but does not automatically remove ambiguity; removing left recursion is likewise not a general ambiguity cure.
There is no algorithm that decides ambiguity for every CFG. Some context-free languages are inherently ambiguous, meaning every CFG generating them is ambiguous. Therefore grammar rewriting is a language-specific technique, not a universal solution.

← RGPV CS-501 Theory of Computation December 2025 Solved Question Paper
Discussion
Ask a question, share your thoughts or report an error.
Sign in to participate in the discussion.
Join the discussion