1. Gyanastra
  2. Compiler Design
  3. Left Recursion and Left Factoring
  4. Left Recursion vs Right Recursion
Free lesson, no account needed 7 min read 7 practice questions after it
Asked in past papers1 time
  • GATE CSE1999

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 A→Aα∣βA \to A\alpha \mid \beta, i.e. the leftmost symbol of the RHS equals the LHS. The variable AA calls itself before doing any other work.
  • Right recursion: a production of the form A→αA∣βA \to \alpha A \mid \beta, i.e. the rightmost symbol of the RHS equals the LHS. Here AA does some work (α\alpha) first and then calls itself.
  • Anchor condition: in right recursion, α\alpha 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

  1. Language of a left recursive grammar A→Aα∣βA \to A\alpha \mid \beta: stop immediately and derive β\beta; or apply A→AαA \to A\alpha once then stop (A⇒Aα⇒βαA \Rightarrow A\alpha \Rightarrow \beta\alpha); or apply it repeatedly, giving β\beta followed by any number of α\alphas. Language: βα∗\beta\alpha^*.
  2. Why left recursion is a problem: treating AA as a function, in A→AαA \to A\alpha the function AA calls AA first without doing anything and only afterwards does the work α\alpha.
    This leads to an infinite loop — AA keeps calling AA and never gets to do any work.
  3. Why right recursion is safe: in A→αAA \to \alpha A, the function AA does the work α\alpha first and then calls AA; α\alpha acts as an anchor condition that stops the recursion.
  4. Language of a right recursive grammar A→αA∣βA \to \alpha A \mid \beta: stop immediately and derive β\beta; or apply A→αAA \to \alpha A any number of times and then stop with β\beta, giving any number of α\alphas followed by β\beta. Language: α∗β\alpha^*\beta.
  5. Eliminating left recursion while keeping the language βα∗\beta\alpha^*:
    • A grammar must not contain star (∗*) or such operators, so A→βα∗A \to \beta\alpha^* cannot be written directly.
    • Introduce a new non-terminal A′A' (A-dash): A→βA′A \to \beta A', where A′A' generates any number of α\alphas, including zero.
    • A′→ϵA' \to \epsilon (zero α\alphas) or A′→αA′A' \to \alpha A' (one α\alpha followed by any number of A′A's). The production A′→αA′A' \to \alpha A' is a right recursion, which is acceptable.
    • Result: A→βA′A \to \beta A', A′→αA′∣ϵA' \to \alpha A' \mid \epsilon generates exactly βα∗\beta\alpha^*, the same language as A→Aα∣βA \to A\alpha \mid \beta, but without left recursion.

Formulas

  • Left recursive form: A→Aα∣βA \to A\alpha \mid \beta, language =βα∗= \beta\alpha^* — AA is the non-terminal being defined, α\alpha is the part after the recursive symbol, β\beta is the alternative (base) production.
  • Right recursive form: A→αA∣βA \to \alpha A \mid \beta, language =α∗β= \alpha^*\beta.
  • Equivalent non-left-recursive grammar: A→βA′A \to \beta A' and A′→αA′∣ϵA' \to \alpha A' \mid \epsilon.
  • α∗\alpha^* means zero or more repetitions of α\alpha; ϵ\epsilon means the empty string.

Worked examples

Example 1: Left recursive grammar and its language

  • Grammar: A→Aα∣βA \to A\alpha \mid \beta
  • Derivations: A⇒βA \Rightarrow \beta; A⇒Aα⇒βαA \Rightarrow A\alpha \Rightarrow \beta\alpha; A⇒Aα⇒Aαα⇒βααA \Rightarrow A\alpha \Rightarrow A\alpha\alpha \Rightarrow \beta\alpha\alpha; …
  • Language: βα∗\beta\alpha^*

Example 2: Right recursive grammar and its language

  • Grammar: A→αA∣βA \to \alpha A \mid \beta
  • Derivations: A⇒βA \Rightarrow \beta; A⇒αA⇒αβA \Rightarrow \alpha A \Rightarrow \alpha\beta; A⇒αA⇒ααA⇒ααβA \Rightarrow \alpha A \Rightarrow \alpha\alpha A \Rightarrow \alpha\alpha\beta; …
  • Language: α∗β\alpha^*\beta
  • Conclusion: this is not the same language as Example 1.

Example 3: Removing left recursion from A→Aα∣βA \to A\alpha \mid \beta

  • Step 1: AA generates β\beta followed by A′A': A→βA′A \to \beta A'
  • Step 2: A′A' generates zero or more α\alphas: A′→αA′∣ϵA' \to \alpha A' \mid \epsilon
  • Check: A⇒βA′⇒βA \Rightarrow \beta A' \Rightarrow \beta (zero α\alphas); A⇒βA′⇒βαA′⇒βαA \Rightarrow \beta A' \Rightarrow \beta\alpha A' \Rightarrow \beta\alpha (one α\alpha); A⇒βA′⇒βαA′⇒βααA′⇒βααA \Rightarrow \beta A' \Rightarrow \beta\alpha A' \Rightarrow \beta\alpha\alpha A' \Rightarrow \beta\alpha\alpha (two α\alphas); …
  • Language: βα∗\beta\alpha^* — identical to the left recursive grammar, but now right recursive and safe for top-down parsing.

Comparison

FeatureLeft recursion (A→Aα∣βA \to A\alpha \mid \beta)Right recursion (A→αA∣βA \to \alpha A \mid \beta)
Recursive symbol positionLeftmost symbol of RHS = LHSRightmost symbol of RHS = LHS
Language generatedβα∗\beta\alpha^*α∗β\alpha^*\beta
Function viewCalls AA first, then does work α\alphaDoes work α\alpha first, then calls AA
BehaviourFalls into infinite loopα\alpha acts as anchor/condition check; no infinite loop
Suitability for top-down parsersNot acceptableAcceptable

Revision summary

  • Left recursion: leftmost symbol of RHS equals LHS (A→Aα∣βA \to A\alpha \mid \beta); right recursion: rightmost symbol of RHS equals LHS (A→αA∣βA \to \alpha A \mid \beta).
  • Left recursion generates βα∗\beta\alpha^*; right recursion generates α∗β\alpha^*\beta — the languages differ, so naive conversion is wrong.
  • Left recursion causes an infinite loop in a recursive-descent (top-down) parser because AA calls AA before doing any work; right recursion's α\alpha acts as an anchor condition.
  • To eliminate left recursion while preserving the language βα∗\beta\alpha^*: A→βA′A \to \beta A', A′→αA′∣ϵA' \to \alpha A' \mid \epsilon.
  • 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.
Pages 1–2 of 2