Eliminating Left Recursion/Left Factoring and Testing Suitability for Top-Down Parsing

Eliminating Left Recursion/Left Factoring and Testing Suitability for Top-Down Parsing

Verified Sources
Sep 13, 2026

We are given the context-free grammar:

  • SSS+SSaS \rightarrow SS + \mid SS * \mid a

Our goals are to:

  1. Eliminate left recursion (more precisely: direct and indirect left recursion).
  2. Eliminate left factoring (to help make decisions with limited lookahead).
  3. Decide whether these removals make the grammar suitable for top-down parsing (typically meaning an LL-family parser, such as LL(1), that relies on lookahead).

Key definitions used below:

  • left recursion
  • left factoring
  • LL(1) grammar
  • top-down parsing

Note: With only SS as a nonterminal, the analysis is tight: eliminating left recursion may still leave ambiguity and/or FIRST/FOLLOW conflicts that prevent LL(1) parsing.

1) Identify left recursion

In the grammar

SSS+SSaS \rightarrow SS + \mid SS * \mid a

both alternatives SS+SS + and SSSS * begin with SS (because the first symbol on the right-hand side is SS). Therefore SS is directly left-recursive:

SSS+SαS \Rightarrow SS+ \Rightarrow \cdots \Rightarrow S\alpha

for α{S+,S}\alpha \in \{S+, S*\} (and the recursion can repeat because the leading SS can again expand to SS+SS+ or SSSS*).

left recursion is present, so a top-down recursive-descent parser without extra machinery would loop.

Eliminate left recursion (direct case)

  1. 1
    Step 1

    Here A = S. Take α1 = S+ and α2 = S*. The non-recursive alternative is β = a. So S → S(S+) | S(S*) | a.

  2. 2
    Step 2

    Introduce S' (written as S1S_1 or SS').

  3. 3
    Step 3

    Use the standard construction: S → β S' and S' → α1 S' | α2 S' | ε.

  4. 4
    Step 4

    α1 = S+ and α2 = S*. So: SaSS \rightarrow aS' and S(S+)S(S)SϵS' \rightarrow (S+)S' \mid (S*)S' \mid \epsilon.

  5. 5
    Step 5

    A left-recursion-free grammar is:

    SaS SSS+SSSSϵS \rightarrow aS' \ S' \rightarrow SS+S' \mid SS*S' \mid \epsilon

    (Parentheses indicate grouping; the concatenation order is what matters.)

2) Simplify/interpret the transformed grammar

After elimination we get:

SaSS \rightarrow aS'

and

SSS+SSSSϵS' \rightarrow SS+S' \mid SS*S' \mid \epsilon

This grammar is now free of direct left recursion (a derivation from SS no longer immediately starts with SS on the right-hand side; it must start with aa because of SaSS \rightarrow aS').

But note an important structural fact:

  • The productions for SS' still begin with SS (because SSS+SS' \rightarrow SS+S' starts with SS).
  • However, left recursion is assessed per nonterminal: we no longer have a situation where SS derives strings starting with SS without first producing a terminal (or without passing through SS' in a way that reintroduces SS at the front). Here, SS is anchored by the initial aa.

So left recursion is removed successfully, but this does not automatically imply LL(1) suitability.

3) Eliminate left factoring

Left factoring is needed when a nonterminal has alternatives with common prefixes (e.g., Aγβ1γβ2A \rightarrow \gamma\beta_1 \mid \gamma\beta_2).

In the transformed grammar:

  • For SS, alternatives are just one: SaSS \rightarrow aS'. No factoring.
  • For SS', alternatives are:
    • SS+SSS+S'
    • SSSSS*S'
    • ϵ\epsilon

The first two alternatives share prefix SSSS (i.e., both start with SS then immediately continue with either ++ or *). This is not “left factoring” in the classic single-step syntactic-prefix sense until you consider how an LL parser would decide after seeing what it can derive from the prefix SS.

Concretely:

  • An LL parser must decide between SSS+SS' \rightarrow SS+S' and SSSSS' \rightarrow SS*S' based on lookahead.
  • After the parser expands the first SS and consumes whatever terminals it produces from that SS, the next terminal is either ++ or *.
  • Because both alternatives share the same derivation prefix “SS”, the decision can require lookahead beyond what would be available at the moment you choose the production for SS'.

A typical factoring attempt would introduce intermediate nonterminals to delay the decision until after the SS part is recognized; but due to the recursive nature of SS, the grammar still may not become LL(1).

type="tip" title="Left recursion vs. left factoring" content="Eliminating left recursion mainly prevents infinite top-down recursion (termination). Left factoring mainly improves predictability of choices with limited lookahead (often LL(1) decisions). Removing left recursion alone does not guarantee LL(1) suitability."

Does this make the grammar suitable for top-down parsing?

What “suitable for top-down parsing” usually means

For a predictive recursive-descent parser (LL parsing), a common sufficient condition is that the grammar be LL(1), meaning:

  • For each nonterminal AA, the parser can choose the correct production using a single lookahead terminal.
  • This is governed by FIRST and FOLLOW sets and constraints that avoid conflicts.

Why left recursion removal is necessary but not sufficient

Even if left recursion is eliminated:

  • The grammar can still be ambiguous (multiple parse trees).
  • Or it can still produce FIRST/FOLLOW conflicts, where LL(1) cannot decide with limited lookahead.
  • Or the factoring is insufficient to make decisions deterministic.

In our transformed grammar:

  • SS always begins with terminal aa (since SaSS \rightarrow aS'). So FIRST(S)={a}FIRST(S)=\{a\}.
  • But SS' can produce ϵ\epsilon and also can begin with SS; since SS begins with aa, FIRST(S)FIRST(S') will also contain aa.
  • Therefore, when the parser is at $S'`, the choice between:
    • expanding SS' to SS+SSS+S'
    • expanding SS' to SSSSS*S'
    • or using ϵ\epsilon can require lookahead that may not be resolvable with 11 token in all contexts.

Additionally, SSS+SSSSS' \rightarrow SS+S' \mid SS*S' implies an expression-like structure where operands come from SS again, and the operator (++ or *) occurs after parsing an entire SS. That tends to increase the need for lookahead or grammar refactoring to ensure deterministic prediction.

Bottom line (justification)

  • Left recursion elimination makes the grammar terminating under naive top-down expansion (no immediate S+SαS \Rightarrow^+ S\alpha loop).
  • Left factoring, if performed, may improve decision points, but it does not by itself guarantee LL(1).
  • For this grammar, the remaining nondeterminism stems from the fact that alternatives for SS' share a long shared prefix derived from SS, and SS' also has an ϵ\epsilon option—both of which are classic sources of LL parsing conflicts.

Therefore:

  • The resulting grammar is not guaranteed suitable for predictive top-down parsing (LL(1)).
  • It may still require either:
    • more than one-token lookahead (LL(k) with k>1k>1),
    • or parser techniques like backtracking,
    • or a redesign into a conventional expression grammar with explicit precedence/associativity.

From given grammar to parsing suitability

Direct left recursion present

Given grammar

SSS+SSaS \rightarrow SS+ \mid SS* \mid a causes infinite expansion for top-down without a guard."

Anchored start

After left-recursion elimination

SaSS \rightarrow aS' ensures derivations from SS start with aa."

Prediction still may be ambiguous

After left factoring (if applied)

SS' has alternatives sharing prefix derived from SS and includes ϵ\epsilon."

Likely not LL(1)

LL(1) suitability check

Shared-prefix and ϵ\epsilon choices can create FIRST/FOLLOW conflicts."

FAQ: Edge cases and deeper justification

Decision power after transformation (conceptual)

Qualitative view: left recursion affects termination; left factoring affects predictability with lookahead.

Knowledge Check

Question 1 of 4
Q1Single choice

The grammar SSS+SSaS \rightarrow SS+ \mid SS* \mid a is considered left-recursive because it can derive strings starting with which form?