Left Recursion in Context-Free Grammars and Its Elimination (LL Parsing)
In context-free grammars, left recursion occurs when a nonterminal can derive a string that begins with itself—i.e., there is a derivation of the form for some . This prevents a top-down predictive parser (e.g., an LL(1) parser) from deciding which production to use, because the parser can keep expanding without consuming any input.
For direct left recursion, a production has the specific form: where does not begin with . The classic transformation rewrites the grammar into an equivalent one that removes left recursion by introducing a new nonterminal :
This ensures recursive repetition happens after the parser consumes a -prefix from the input, enabling predictive parsing.
We will apply this to the grammar:
Key terms: left recursion , direct left recursion , LL(1) parser , [-production]{def="A production that derives the empty string"}.
Footnotes
-
(Example authoritative source on left recursion elimination / parsing) Wikipedia: "Left recursion" and related parsing/LL transformation notes—https://en.wikipedia.org/wiki/Left_recursion - Defines left recursion and standard transformation patterns used in top-down parsing. ↩ ↩2
Eliminating Left Recursion (Direct Left Recursion) — Theory + Example
Step-by-step elimination strategy
We handle each nonterminal with direct left-recursive alternatives separately:
In our grammar, the problematic parts are:
- (direct left recursion in )
- (direct left recursion in )
- (no left recursion)
Key terms: direct recursion elimination , auxiliary nonterminal , lookahead.
Footnotes
-
(Example authoritative source on left recursion elimination / parsing) Wikipedia: "Left recursion" and related parsing/LL transformation notes—https://en.wikipedia.org/wiki/Left_recursion - Defines left recursion and standard transformation patterns used in top-down parsing. ↩
Eliminate left recursion from the given grammar
- 1Step 1
Here, has the form and has . The nonterminal has no left recursion since its alternatives begin with or .
- 2Step 2
Match to with , , and . Introduce to get: and .
- 3Step 3
Match with , , and . Introduce to obtain: and .
- 4Step 4
Since has no left-recursive alternative, we retain it as-is.
- 5Step 5
Combine the rewritten rules to form the new grammar without left recursion.
Resulting grammar (left-recursion free)
The eliminated grammar is:
This grammar preserves the language of the original grammar while enabling LL-style top-down parsing because recursion now appears in and after consuming the initial or token sequence.
Footnotes
-
(Example authoritative source on left recursion elimination / parsing) Wikipedia: "Left recursion" and related parsing/LL transformation notes—https://en.wikipedia.org/wiki/Left_recursion - Defines left recursion and standard transformation patterns used in top-down parsing. ↩
What changed during left-recursion elimination?
Direct recursion moved from E and T into E' and T' with ε-termination.
Common checks and pitfalls
Pro Tip
When you see in a grammar you plan to parse with LL(1), rewrite it using the standard + pattern. Then re-check FIRST/FOLLOW to ensure LL(1) predictability.
Footnotes
-
(Example authoritative source on left recursion elimination / parsing) Wikipedia: "Left recursion" and related parsing/LL transformation notes—https://en.wikipedia.org/wiki/Left_recursion - Defines left recursion and standard transformation patterns used in top-down parsing. ↩
Common mistake
A frequent error is to drop the alternative in . Without , derivations cannot stop, and the grammar may generate only infinitely long recursive patterns (or become incomplete).
Footnotes
-
(Example authoritative source on left recursion elimination / parsing) Wikipedia: "Left recursion" and related parsing/LL transformation notes—https://en.wikipedia.org/wiki/Left_recursion - Defines left recursion and standard transformation patterns used in top-down parsing. ↩
Conceptual workflow for handling left recursion
Detect
1Look for productions where a nonterminal can derive a string starting with itself: ."
Classify
2Identify direct left recursion ()."
Transform
3Rewrite using and ."
Validate for LL(1)
4Check FIRST/FOLLOW conflicts for predictive parsing."
Knowledge Check
Which of the following best describes left recursion?