1. Gyanastra
  2. Compiler Design
  3. Left Recursion and Left Factoring
  4. Eliminating Left Recursion: General Method and Examples
Free lesson, no account needed 5 min read 9 practice questions after it
Asked in past papers6 times
  • GATE CSE2017201620082003
  • ISRO2013
  • UGC NET2012

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 A→Aα∣βA \to A\alpha \mid \beta.
  • Left recursive alternative (AαA\alpha): the alternative whose RHS begins with AA.
  • Remaining alternative (β\beta): the alternative whose RHS does not begin with AA.
  • 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 (A′A'): introduced by the conversion to hold the repetition of α\alpha.

How it works

  1. Examine each production and check whether the leftmost symbol of the RHS equals the LHS. If yes, the production is left recursive.
  2. Write the production in the standard form A→Aα∣βA \to A\alpha \mid \beta and identify AA, α\alpha and β\beta.
  3. Replace the production using the conversion:
    • A→βA′A \to \beta A'
    • A′→αA′∣ϵA' \to \alpha A' \mid \epsilon
  4. The resulting grammar is non-left recursive (right recursive) and equivalent to the original: it generates exactly the same language, but without infinite loops.
  5. Only the left recursive productions are replaced; the remaining productions are left unchanged.

Formulas

  • For a production of the form A→Aα∣βA \to A\alpha \mid \beta:

  • A→βA′A \to \beta A'

  • A′→αA′∣ϵA' \to \alpha A' \mid \epsilon

  • AA = the non-terminal being defined

  • α\alpha = the part of the RHS that follows AA in the left recursive alternative

  • β\beta = the alternative that does not start with AA

  • A′A' = a new non-terminal introduced to make the grammar right recursive

  • ϵ\epsilon = the empty string

Worked examples

Example 1: Expression grammar

Grammar: E→E+T∣TE \to E + T \mid T

  • The leftmost symbol of the RHS of E→E+TE \to E + T is EE, which equals the LHS, so this is left recursion.
  • Form A→Aα∣βA \to A\alpha \mid \beta with A=EA = E, α=+ T\alpha = +\,T, β=T\beta = T.
  • Applying the conversion:
    • A→βA′A \to \beta A' gives E→TE′E \to T E'
    • A′→αA′∣ϵA' \to \alpha A' \mid \epsilon gives E′→+ TE′∣ϵE' \to +\,T E' \mid \epsilon
  • Resulting equivalent non-left recursive (right recursive) grammar:
    • E→TE′E \to T E'
    • E′→+ TE′∣ϵE' \to +\,T E' \mid \epsilon

Example 2: Grammar S→S0S1S∣01S \to S0S1S \mid 01

  • Check for left recursion: the leftmost symbol of the RHS of S→S0S1SS \to S0S1S is SS, the same as the LHS, so it is left recursive; S→01S \to 01 is the other alternative.
  • It is in the form A→Aα∣βA \to A\alpha \mid \beta, identifying:
    • A=SA = S
    • α=0S1S\alpha = 0S1S
    • β=01\beta = 01
  • Substituting into the formula:
    • S→βS′S \to \beta S' gives S→01 S′S \to 01\,S'
    • S′→ϵ∣αS′S' \to \epsilon \mid \alpha S' gives S′→ϵ∣0S1S S′S' \to \epsilon \mid 0S1S\,S'
  • This is the elimination for this grammar.

Example 3: Grammar L→L,S∣SL \to L, S \mid S

  • The other productions are fine (no left recursion); only L→L,SL \to L, S is left recursive.
  • Form A→Aα∣βA \to A\alpha \mid \beta with A=LA = L, α=,S\alpha = ,S, β=S\beta = S.
  • Applying the conversion A→βA′A \to \beta A', A′→αA′∣ϵA' \to \alpha A' \mid \epsilon:
    • L→SL′L \to S L'
    • L′→,SL′∣ϵL' \to ,S L' \mid \epsilon
  • 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: A→Aα∣βA \to A\alpha \mid \beta.
  • Conversion: A→βA′A \to \beta A' and A′→αA′∣ϵA' \to \alpha A' \mid \epsilon.
  • 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 AA, α\alpha and β\beta 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. S→01S \to 01) is not left recursive; it plays the role of β\beta 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
Pages 1–2 of 2