
- Automata Theory - Applications
- Automata Terminology
- Basics of String in Automata
- Set Theory for Automata
- Finite Sets and Infinite Sets
- Algebraic Operations on Sets
- Relations Sets in Automata Theory
- Graph and Tree in Automata Theory
- Transition Table in Automata
- What is Queue Automata?
- Compound Finite Automata
- Complementation Process in DFA
- Closure Properties in Automata
- Concatenation Process in DFA
- Language and Grammars
- Language and Grammar
- Grammars in Theory of Computation
- Language Generated by a Grammar
- Chomsky Classification of Grammars
- Context-Sensitive Languages
- Finite Automata
- What is Finite Automata?
- Finite Automata Types
- Applications of Finite Automata
- Limitations of Finite Automata
- Two-way Deterministic Finite Automata
- Deterministic Finite Automaton (DFA)
- Non-deterministic Finite Automaton (NFA)
- NDFA to DFA Conversion
- Equivalence of NFA and DFA
- Dead State in Finite Automata
- Minimization of DFA
- Automata Moore Machine
- Automata Mealy Machine
- Moore vs Mealy Machines
- Moore to Mealy Machine
- Mealy to Moore Machine
- Myhill–Nerode Theorem
- Mealy Machine for 1’s Complement
- Finite Automata Exercises
- Complement of DFA
- Regular Expressions
- Regular Expression in Automata
- Regular Expression Identities
- Applications of Regular Expression
- Regular Expressions vs Regular Grammar
- Kleene Closure in Automata
- Arden’s Theorem in Automata
- Convert Regular Expression to Finite Automata
- Conversion of Regular Expression to DFA
- Equivalence of Two Finite Automata
- Equivalence of Two Regular Expressions
- Convert Regular Expression to Regular Grammar
- Convert Regular Grammar to Finite Automata
- Pumping Lemma in Theory of Computation
- Pumping Lemma for Regular Grammar
- Pumping Lemma for Regular Expression
- Pumping Lemma for Regular Languages
- Applications of Pumping Lemma
- Closure Properties of Regular Set
- Closure Properties of Regular Language
- Decision Problems for Regular Languages
- Decision Problems for Automata and Grammars
- Conversion of Epsilon-NFA to DFA
- Regular Sets in Theory of Computation
- Context-Free Grammars
- Context-Free Grammars (CFG)
- Derivation Tree
- Parse Tree
- Ambiguity in Context-Free Grammar
- CFG vs Regular Grammar
- Applications of Context-Free Grammar
- Left Recursion and Left Factoring
- Closure Properties of Context Free Languages
- Simplifying Context Free Grammars
- Removal of Useless Symbols in CFG
- Removal Unit Production in CFG
- Removal of Null Productions in CFG
- Linear Grammar
- Chomsky Normal Form (CNF)
- Greibach Normal Form (GNF)
- Pumping Lemma for Context-Free Grammars
- Decision Problems of CFG
- Pushdown Automata
- Pushdown Automata (PDA)
- Pushdown Automata Acceptance
- Deterministic Pushdown Automata
- Non-deterministic Pushdown Automata
- Construction of PDA from CFG
- CFG Equivalent to PDA Conversion
- Pushdown Automata Graphical Notation
- Pushdown Automata and Parsing
- Two-stack Pushdown Automata
- Turing Machines
- Basics of Turing Machine (TM)
- Representation of Turing Machine
- Examples of Turing Machine
- Turing Machine Accepted Languages
- Variations of Turing Machine
- Multi-tape Turing Machine
- Multi-head Turing Machine
- Multitrack Turing Machine
- Non-Deterministic Turing Machine
- Semi-Infinite Tape Turing Machine
- K-dimensional Turing Machine
- Enumerator Turing Machine
- Universal Turing Machine
- Restricted Turing Machine
- Convert Regular Expression to Turing Machine
- Two-stack PDA and Turing Machine
- Turing Machine as Integer Function
- Post–Turing Machine
- Turing Machine for Addition
- Turing Machine for Copying Data
- Turing Machine as Comparator
- Turing Machine for Multiplication
- Turing Machine for Subtraction
- Modifications to Standard Turing Machine
- Linear-Bounded Automata (LBA)
- Church's Thesis for Turing Machine
- Recursively Enumerable Language
- Computability & Undecidability
- Turing Language Decidability
- Undecidable Languages
- Turing Machine and Grammar
- Kuroda Normal Form
- Converting Grammar to Kuroda Normal Form
- Decidability
- Undecidability
- Reducibility
- Halting Problem
- Turing Machine Halting Problem
- Rice's Theorem in Theory of Computation
- Post’s Correspondence Problem (PCP)
- Types of Functions
- Recursive Functions
- Injective Functions
- Surjective Function
- Bijective Function
- Partial Recursive Function
- Total Recursive Function
- Primitive Recursive Function
- μ Recursive Function
- Ackermann’s Function
- Russell’s Paradox
- Gödel Numbering
- Recursive Enumerations
- Kleene's Theorem
- Kleene's Recursion Theorem
- Advanced Concepts
- Matrix Grammars
- Probabilistic Finite Automata
- Cellular Automata
- Reduction of CFG
- Reduction Theorem
- Regular expression to ∈-NFA
- Quotient Operation
- Parikh’s Theorem
- Ladner’s Theorem
Left Recursion (LR) and Left Factoring (LF)
Context-free grammars play a vital role in in compilers and programming languages. CFGs are used to define how valid programs should be organized. We use recursion in parsing, whereby examining a sequence of tokens to its grammatical structure, is greatly dependent on these grammars.
In this chapter, we will cover two important characteristics of CFG which are left recursion and left factoring. Let us understand them through examples.
Left Recursion
A context-free grammar is said to be left recursive if it contains a production rule where the non-terminal on the left-hand side of the rule also appears as the first symbol on the right-hand side. In other words, the grammar is trying to define a non-terminal in terms of itself, creating a recursive loop.
This can be represented formally as −
$$\mathrm{A \:\rightarrow\: A\: \alpha \: |\: \beta}$$
Where −
- A is a non-terminal symbol.
- α represents a sequence of terminals and/or non-terminals.
- β represents another sequence of terminals and/or non-terminals.
The most important part here is the presence of A on both sides of the production rule, with it appearing first on the right-hand side.
To visualize this, consider the following parse tree −

It is generated by a left-recursive grammar. As the grammar recursively expands the non-terminal 'A' on the left, the tree grows indefinitely downwards on the left side. This continuous expansion makes it unsuitable for top-down parsing, as the parser could get trapped in an infinite loop, trying to expand 'A' repeatedly.
Problem of Left Recursion for Top-Down Parsing
As we have seen, the top-down parsing works by starting with the start symbol of the grammar and attempting to derive the input string by applying production rules.
When encountering a left-recursive rule, the parser keeps expanding the same non-terminal, leading to an infinite loop. This inability to handle left recursion directly is a significant drawback of top-down parsing methods.
Eliminating Left Recursion
To solve this we can eliminate immediate left recursion from a grammar without altering the language it generates. The general approach involves introducing a new non-terminal and rewriting the recursive rules.
Let's illustrate this with our previous example −
$$\mathrm{A \:\rightarrow\: A\: \alpha \: |\: \beta}$$
We can eliminate the left recursion by introducing a new non-terminal 'A'` and rewriting the rule as follows −
$$\mathrm{A \:\rightarrow\: \beta\:A'}$$
$$\mathrm{A' \:\rightarrow\: \alpha \:A'\: |\: \varepsilon}$$
In this transformed grammar −
- A now derives the non-recursive part 'β' followed by the new non-terminal A'.
- A' handles the recursion, deriving either α A' (continuing the recursion) or ϵ (empty string), which terminates the recursion.
Let us see this through an example for a better view
Consider a simplified arithmetic expression grammar −
$$\mathrm{E \:\rightarrow\: E\: +\: T\: |\: T}$$
$$\mathrm{T \:\rightarrow\: T\: * \: F\: |\: F}$$
$$\mathrm{F \:\rightarrow\: (E)\: | \: id}$$
This grammar contains left recursion in the rules for both `E` and `T`. Let's eliminate it −
- Eliminating Left Recursion in `E`:
$$\mathrm{E \: \rightarrow\: TE'}$$
$$\mathrm{E' \: \rightarrow\: +TE' \: |\:\varepsilon}$$
- Eliminating Left Recursion in `T`:
$$\mathrm{T \: \rightarrow\: FT'}$$
$$\mathrm{T' \: \rightarrow\: * FT' \: |\:\varepsilon}$$
The final transformed grammar, free from left recursion, becomes −
$$\mathrm{E \: \rightarrow \: TE'}$$
$$\mathrm{E' \: \rightarrow \: +TE'\:|\:\varepsilon}$$
$$\mathrm{T \: \rightarrow \: FT'}$$
$$\mathrm{T' \: \rightarrow\: * FT' \: |\:\varepsilon}$$
$$\mathrm{F \: \rightarrow\: (E) \:|\:id}$$
Left Factoring
After the left recursion, let us see the idea of left factoring. While left recursion presents a parsing challenge, left factoring is a desirable property for top-down parsing. It involves restructuring the grammar to eliminate common prefixes in production rules.
Importance of left factoring
When a grammar has multiple production rules for a non-terminal with a common prefix, the parser faces ambiguity during the parsing process. It has to choose between these rules without knowing which one will ultimately lead to a successful parse.
Left factoring helps in resolving this ambiguity by postponing the decision point, making the parsing process more efficient.
Performing Left Factoring
The process of left factoring involves identifying the longest common prefix (α) in the alternatives for a non-terminal and rewriting the rules to factor out this common prefix.
Consider a grammar with the following rules −
$$\mathrm{A\:\rightarrow\: \alpha\beta_{1} \:|\:\alpha\beta_{2}\:|\:\dotso\:|\:\alpha\beta_{n}\:|\:\gamma}$$
Here, 'α' is the longest common prefix in the first 'n' alternatives for non-terminal 'A'.
We can left factor this as follows −
$$\mathrm{A \: \rightarrow \: \alpha A' \:|\: \gamma}$$
$$\mathrm{A\:\rightarrow\: \beta_{1} \:|\: \beta_{2}\:|\:\dotso\:|\: \beta_{n}}$$
In this factored grammar −
- A now derives either the common prefix 'α' followed by the new non-terminal 'A'` or the alternative 'γ'.
- A'` encapsulates the different options that were originally prefixed by 'α'. This defers the decision of which alternative to choose until more of the input has been processed.
Let us understand this through an example. Let's consider a simple grammar −
$$\mathrm{S\:\rightarrow\:\: \text{if E then S, else S}}$$
$$\mathrm{S\:\rightarrow\:\: \text{if E then S}}$$
$$\mathrm{S\:\rightarrow\: A}$$
Here, the first two rules for 'S' have a common prefix, "if E then". We can apply left factoring to obtain −
$$\mathrm{S\:\rightarrow\:\: \text{if E then S S' | A}}$$
$$\mathrm{S'\:\rightarrow\:\: \text{else S | $\varepsilon$}}$$
This factored grammar is more suitable for top-down parsing as it avoids the initial choice between the first two rules.
Conclusion
Left recursion and left factoring are two important concepts in compiler design and more specifically for context-free grammars and parsing.
Left recursion can be problematic for top-down parsing and needs to be eliminated. Left factoring, on the other hand, improves the efficiency of top-down parsing by reducing ambiguity.