Practice free →
HomeGATE CSEcomputerscienceTheory of Computation › A CFG is AMBIGUOUS if

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)._
Solve this in the app — GATE CSE practice & 24k+ MCQs →
Related questions