Home › GATE CSE › computerscience › Theory of Computation › A LEFT-RECURSIVE rule in a CFG has the form $A \…
A LEFT-RECURSIVE rule in a CFG has the form $A \to A\alpha$ for some non-empty $\alpha$. Why is left recursion problematic?
AIt causes the grammar to be ambiguous
BTop-down LL parsers loop forever on it
CIt makes the grammar context-sensitive
DIt increases the language size to infinity
Answer & Solution
Correct answer: B. Top-down LL parsers loop forever on it
1. TOP-DOWN parsers (like LL(k) and recursive descent) attempt to MATCH a non-terminal by applying a production and recursively parsing the result.
2. For a rule $A \to A\alpha$: the parser tries to parse $A$ by parsing $A$ (which tries to parse $A$, which...) — an INFINITE LOOP.
3. SOLUTION: rewrite left recursion as right recursion. Replace $A \to A\alpha \mid \beta$ with $A \to \beta A'$ and $A' \to \alpha A' \mid \varepsilon$. This 'unrolls' the recursion to a form LL parsers can handle.
4. Bottom-up parsers (LR) DO handle left recursion natively (they build trees from the leaves up).
5. Option A is wrong — left recursion is independent of ambiguity. Option C and D are wrong.
_Source: Jeff Erickson, "Models of Computation", §5.5 (Parsing CFGs, top-down vs bottom-up)._
Related questions
Which class of grammars generates ALL recursively enumerable languages?What is a LEFTMOST DERIVATION?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 CLOSED under which of the following operations?Is the language $L = \{a^n b^n c^n \mid n \geq 0\}$ context-free?