Compiler Design · Left Recursion and Left Factoring
Eliminating Left Recursion: General Method and Examples
Overview
Left recursion occurs when a production's right-hand side begins with the same non-terminal as its left-hand side, which causes infinite loops in top-down parsing.
Any left recursive grammar can be mechanically converted into an equivalent right recursive grammar that generates exactly the same language without looping.
Key concepts
- Left recursion: A production is left recursive if the leftmost symbol on the RHS is the same as the non-terminal on the LHS, i.e. it has the form .
- Left recursive alternative (): the alternative whose RHS begins with .
- Remaining alternative (): the alternative whose RHS does not begin with .
- Right recursive grammar: the result of the conversion; it is non-left recursive and generates the same language as the original grammar.
- New non-terminal (): introduced by the conversion to hold the repetition of .
How it works
- Examine each production and check whether the leftmost symbol of the RHS equals the LHS. If yes, the production is left recursive.
- Write the production in the standard form and identify , and .
- Replace the production using the conversion:
- The resulting grammar is non-left recursive (right recursive) and equivalent to the original: it generates exactly the same language, but without infinite loops.
- Only the left recursive productions are replaced; the remaining productions are left unchanged.
Formulas
-
For a production of the form :
-
-
-
= the non-terminal being defined
-
= the part of the RHS that follows in the left recursive alternative
-
= the alternative that does not start with
-
= a new non-terminal introduced to make the grammar right recursive
-
= the empty string
Worked examples
Example 1: Expression grammar
Grammar:
- The leftmost symbol of the RHS of is , which equals the LHS, so this is left recursion.
- Form with , , .
- Applying the conversion:
- gives
- gives
- Resulting equivalent non-left recursive (right recursive) grammar:
Example 2: Grammar
- Check for left recursion: the leftmost symbol of the RHS of is , the same as the LHS, so it is left recursive; is the other alternative.
- It is in the form , identifying:
- Substituting into the formula:
- gives
- gives
- This is the elimination for this grammar.
Example 3: Grammar
- The other productions are fine (no left recursion); only is left recursive.
- Form with , , .
- Applying the conversion , :
- These two productions replace the two original productions; the rest of the grammar stays the same.
Revision summary
- Left recursion means the leftmost RHS symbol equals the LHS: .
- Conversion: and .
- The converted grammar is right recursive, non-left recursive, and generates the same language as the original.
- Eliminating left recursion prevents infinite loops in top-down parsing.
- Identify , and first, then substitute into the formula; derive the formula rather than memorising it.
- Only the left recursive productions are replaced; the remaining productions are left unchanged.
- A production whose RHS starts with a symbol different from the LHS (e.g. ) is not left recursive; it plays the role of and is not rewritten itself.
Previous-year exam insights
- Left recursion elimination must be applied to all non-terminals, including indirect left recursion: first order the non-terminals A1..An, substitute earlier productions into later ones to expose immediate left recursion, then eliminate immediate left recursion for each Ai in order.
Asked inGATE 2016GATE 2017 - Removing left recursion alone does not make an arbitrary CFG LL(1); left factoring (and possibly other transformations) is also needed, and some grammars cannot be made LL(1) at all.
Asked inGATE 2003UGC NET 2012 - Every left-recursive grammar can be converted to an equivalent right-recursive (non-left-recursive) grammar, and vice versa; the conversion preserves the generated language but not necessarily the parse tree shape.
Asked inGATE 2008 - A grammar is free from left recursion only if no non-terminal A can derive a sentential form beginning with A through a chain of productions (i.e., check indirect recursion via other non-terminals, not just immediate A → Aα).
Asked inGATE 2016 - For a production with multiple left-recursive alternatives A → Aα1 | Aα2 | β1 | β2, the general conversion is A → β1A' | β2A' and A' → α1A' | α2A' | ε.
Asked inGATE 2017 - Left factoring (A → αβ1 | αβ2 becomes A → αA', A' → β1 | β2) is a separate transformation used to remove common prefixes; it does not remove left recursion, and left recursion removal does not remove common prefixes.
Asked inGATE 2003UGC NET 2012