A CFG is AMBIGUOUS if
Ait has more than one variable
Bthe grammar has rules but no terminals
Ca string in $L(G)$ has two or more distinct parse trees
Dthe grammar accepts the empty string
Answer & Solution
Correct answer: C. a string in $L(G)$ has two or more distinct parse trees
1. Ambiguity in a CFG: a string in $L(G)$ has multiple structurally distinct parse trees.
2. Classic example: arithmetic expressions. Grammar $E \to E + E \mid E \cdot E \mid \text{id}$. The string '$id + id \cdot id$' has TWO parse trees:
- $(id + id) \cdot id$ (top-level $\cdot$, left subtree is sum)
- $id + (id \cdot id)$ (top-level +, right subtree is product)
3. Each parse tree assigns a different MEANING, so ambiguity is a SEMANTIC PROBLEM for parsers (which interpretation should the compiler choose?).
4. SOLUTION: rewrite grammars to enforce precedence, e.g. $E \to E + T \mid T$; $T \to T \cdot F \mid F$; $F \to id$. This makes the grammar unambiguous.
5. Other options don't define ambiguity.
_Source: Jeff Erickson, "Models of Computation", §5.7 (Ambiguity in CFGs)._
Keep practising Theory of Computation
Browse all Theory of Computation questions for GATE CSE, or explore more Computer Science topics.
Related questions
Which class of grammars generates ALL recursively enumerable languages?Which of the following languages is CONTEXT-FREE?Consider the grammar $S \to (S) \mid SS \mid \varepsilon$. The language generated isEvery regular language is also context-free. The PROOF construction isThe CYK algorithm parses a CFG in CNF. Its time complexity for a string of length $n$ isContext-free languages are RECOGNISED by which type of machine?Consider the CFG with rule $S \to aSb \mid \varepsilon$. The language generated isWhich of these is an example of an IRREGULAR (non-regular) language?