Compiler Design · Left Recursion and Left Factoring
Left Recursion vs Right Recursion
Overview
Recursion in a grammar occurs when a non-terminal calls itself, and it comes in two forms: left recursion and right recursion, distinguished by where the self-reference sits in the right-hand side.
Left recursion generates a different language than right recursion and causes infinite loops in top-down parsers, so it must be eliminated while preserving the original language.
Key concepts
- Recursion in a grammar: a variable (non-terminal) is calling itself. Any production where a symbol on the RHS equals the LHS is recursion; the position of that symbol decides whether it is left or right recursion.
- Left recursion: a production of the form , i.e. the leftmost symbol of the RHS equals the LHS. The variable calls itself before doing any other work.
- Right recursion: a production of the form , i.e. the rightmost symbol of the RHS equals the LHS. Here does some work () first and then calls itself.
- Anchor condition: in right recursion, acts as a condition check that stops the recursion, so there is no way to fall into an infinite loop.
- Language preservation: eliminating left recursion must keep the generated language unchanged; naive conversion to right recursion changes the language and is not acceptable.
How it works
- Language of a left recursive grammar : stop immediately and derive ; or apply once then stop (); or apply it repeatedly, giving followed by any number of s. Language: .
- Why left recursion is a problem: treating as a function, in the function calls first without doing anything and only afterwards does the work .
This leads to an infinite loop — keeps calling and never gets to do any work. - Why right recursion is safe: in , the function does the work first and then calls ; acts as an anchor condition that stops the recursion.
- Language of a right recursive grammar : stop immediately and derive ; or apply any number of times and then stop with , giving any number of s followed by . Language: .
- Eliminating left recursion while keeping the language :
- A grammar must not contain star () or such operators, so cannot be written directly.
- Introduce a new non-terminal (A-dash): , where generates any number of s, including zero.
- (zero s) or (one followed by any number of s). The production is a right recursion, which is acceptable.
- Result: , generates exactly , the same language as , but without left recursion.
Formulas
- Left recursive form: , language — is the non-terminal being defined, is the part after the recursive symbol, is the alternative (base) production.
- Right recursive form: , language .
- Equivalent non-left-recursive grammar: and .
- means zero or more repetitions of ; means the empty string.
Worked examples
Example 1: Left recursive grammar and its language
- Grammar:
- Derivations: ; ; ; …
- Language:
Example 2: Right recursive grammar and its language
- Grammar:
- Derivations: ; ; ; …
- Language:
- Conclusion: this is not the same language as Example 1.
Example 3: Removing left recursion from
- Step 1: generates followed by :
- Step 2: generates zero or more s:
- Check: (zero s); (one ); (two s); …
- Language: — identical to the left recursive grammar, but now right recursive and safe for top-down parsing.
Comparison
| Feature | Left recursion () | Right recursion () |
|---|---|---|
| Recursive symbol position | Leftmost symbol of RHS = LHS | Rightmost symbol of RHS = LHS |
| Language generated | ||
| Function view | Calls first, then does work | Does work first, then calls |
| Behaviour | Falls into infinite loop | acts as anchor/condition check; no infinite loop |
| Suitability for top-down parsers | Not acceptable | Acceptable |
Revision summary
- Left recursion: leftmost symbol of RHS equals LHS (); right recursion: rightmost symbol of RHS equals LHS ().
- Left recursion generates ; right recursion generates — the languages differ, so naive conversion is wrong.
- Left recursion causes an infinite loop in a recursive-descent (top-down) parser because calls before doing any work; right recursion's acts as an anchor condition.
- To eliminate left recursion while preserving the language : , .
- A grammar must not contain operators like star (); the star is expressed through recursion and epsilon.
- Most parsers, especially top-down parsers, do not accept left recursion, so it must be eliminated without changing the language.
Previous-year exam insights
- A non-terminal can be both left and right recursive (e.g. A → Aa | bA | c); having both recursions does not by itself decide ambiguity, so for an unspecified grammar 'information is not sufficient' is the right stance, although this particular grammar is ambiguous (bca has two parse trees: b(ca) and (bc)a).
Asked inGATE 1999 - General left-recursion elimination for A→Aα1|Aα2|…|Aαm|β1|…|βn (no βi begins with A) is: A→β1A′|…|βnA′, A′→α1A′|…|αmA′|ε;
if some βi = ε then A→A′ and A′→α1A′|…|αmA′|ε. - Indirect (mutual) left recursion, e.g. A→Bα, B→Aβ, must first be converted to direct left recursion (by substitution/ordering of non-terminals) before the standard elimination rule can be applied.
- Left recursion is a problem only for top-down (recursive-descent/LL) parsers; bottom-up parsers (LR, SLR, LALR, canonical LR) handle left-recursive grammars fine, and left recursion is in fact often preferred there because it gives left associativity with less stack growth.
- Left factoring (A→αβ1|αβ2 becomes A→αA′, A′→β1|β2) is a separate transformation used to remove the common-prefix problem for LL(1) parsing; it does not remove left recursion, and left-recursion elimination does not remove the need for left factoring.
- A grammar is left recursive iff there is a non-terminal A with a derivation A ⇒+ Aα for some α (directly, A→Aα, or indirectly through other non-terminals); this derivation-based definition is what exams test, not just the shape of a single production.