Course Section: Left Factoring, Eliminating Left Recursion, and Top-Down Parsing Suitability for
We study the grammar:
- CFG
- where denotes the empty string.
This grammar is left-recursive because the production begins with on the right-hand side. Left recursion prevents standard top-down parsers (like predictive/LL(1) parsers) from directly making progress.
Key transformation goals:
- Left factoring the grammar (useful for LL(1) when alternatives share prefixes).
- Eliminating left recursion so top-down parsing becomes possible.
- Decide whether the resulting grammar is suitable for Top-down parsing.
A useful mental model is:
- Left factoring addresses choice ambiguity among alternatives (common prefixes).
- Left recursion elimination addresses infinite descent (a nonterminal calling itself as the leftmost step).
Eliminating Left Recursion (LL Parser Preparation)
First observations
- The grammar has only one nonterminal: .
- Its productions are:
Because there is only one production with as the left-hand side, left factoring in the usual sense (when multiple alternatives share a common prefix) is not really applicable: there are no two competing right-hand sides that begin with the same terminal/nonterminal prefix that can be factored to make an LL decision earlier.
However, to be rigorous: left factoring looks for alternatives of the form: where both start with a common prefix . Here, the alternatives for are:
- (begins with )
- (begins with “nothing”)
There is no pair of non-empty alternatives sharing a common prefix to factor in the standard way.
Key takeaway: (i) Left factoring does not change the grammar.
Step (i) Left Factor the Grammar
- 1Step 1
Alternatives are and .
- 2Step 2
There is no pair of alternatives with a shared non-empty prefix like and .
- 3Step 3
Therefore, the left-factored grammar is the same as the original: .
Why left factoring is often about LL(1)
LL(1) parsers fail when, for some nonterminal , multiple alternatives have overlapping FIRST sets (and also potentially FOLLOW conditions). Left factoring attempts to rewrite: into:
In our grammar, there is no such pair of alternatives with a shared prefix to rewrite.
Step (ii) Eliminate Left Recursion
- 1Step 1
We have which is of the form with .
- 2Step 2
Left-recursive: . Non-left-recursive: .
- 3Step 3
Introduce a new nonterminal . Then: and where is the non-left-recursive alternative (here, and .
- 4Step 4
so effectively ; and .
- 5Step 5
Equivalent left-recursion-free grammar: , and .
Final grammar after eliminating left recursion
A clean way to present it is to directly inline the trivial :
Let the transformed grammar be:
But that presentation accidentally duplicates structure if you inline too aggressively. The standard, unambiguous result from the construction is:
This is left-recursion-free: no production has a nonterminal on the immediate left of its own right-hand side.
type="tip" title="Pro Tip: Track the and pieces" content="For a left-recursive rule , always set to the part after the leading , and to the non-left-recursive alternatives. This prevents common algebra mistakes when introducing ."
(iii) Is the resulting grammar suitable for top-down parsing?
Clarify what “suitable” usually means
For top-down predictive parsing (LL(1)-style), the grammar typically needs:
- No left recursion (so recursive descent can progress).
- Left factoring (so one-token lookahead can choose the correct alternative).
Even after eliminating left recursion, a grammar might still be not LL(1) if choices are ambiguous with one-token lookahead.
Check for remaining predictive-choice issues
In the transformed grammar:
- only (deterministic)
The alternatives are:
- starting with terminal '(' (because the first symbol of is '(')
For an LL(1) parser, selection is based on FIRST sets:
- FIRST of is .
- FIRST of contributes via FOLLOW conditions (i.e., when lookahead indicates the parser can safely take ).
Since one alternative clearly starts with '(' and the other is , this is a common pattern that can be LL(1) provided the FOLLOW sets do not conflict.
At minimum, the grammar is amenable to top-down parsing in the sense that it is not left-recursive and has a very structured single-choice at .
type="warning" title="Warning: Eliminating left recursion is necessary but may not be sufficient" content="No left recursion removes infinite descent, but LL(1) suitability also requires that the lookahead sets for alternatives do not overlap. Many grammars still require further refactoring or accept that the parser is LL(k) rather than LL(1)."
A compact decision
- After step (ii), the grammar is left-recursion-free, so it is generally suitable for top-down parsing algorithms that require no left recursion (e.g., standard recursive descent without left-recursive loops).
- For LL(1) specifically, the grammar’s structure suggests it can be predictive because the non- production begins with a fixed terminal '('; the option is handled via FOLLOW-based prediction.
So, yes: the transformed grammar is suitable for top-down parsing, and it is plausibly LL(1) due to the clear terminal prefix separation—though a full LL(1) proof would require computing FIRST/FOLLOW sets.
Transformation Roadmap
Given grammar
Start(left-recursive)"
Left factoring
Step (i)No applicable common-prefix alternatives; grammar unchanged"
Eliminate left recursion
Step (ii)Introduce ; rewrite into , "
Top-down suitability
Step (iii)Left-recursion-free; alternatives are prefix-separated, enabling predictive descent"
Key Checks and Edge Cases
Transformation Impact on Grammar Properties
Qualitative comparison before and after elimination of left recursion
Self-Check Flashcards
Knowledge Check
Which part of the grammar is left-recursive?