- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# What is Non-Immediate Left Recursion in compiler design?

A Grammar G (V, T, P, S) is left recursive if it has a production in the form.

A → A α |β.

The above Grammar is left recursive because the left of production is occurring at a first position on the right side of production. It can eliminate left recursion by replacing a pair of production with

A → βA′

A → αA′|ϵ

The general form for left recursion is

A → Aα_{1}|Aα_{2}| … . |Aα_{m}|β_{1}|β_{2}| … . . β_{n}

can be replaced by

A → β_{1}A′|β_{2}A′| … . . | … . . |β_{n}A′

A → α_{1}A′|α_{2}A′| … . . |α_{m}A′|ε

In the following grammar, it does not have the immediate or direct left recursion. But here, it can have non-immediate left recursion.

S → Ab

A → Sc|d

Here, S is left Recursive, because S ⇒ A b ⇒ S c b

**Algorithm to Remove Left-Recursion**

**Input** − Grammar G with no cycles or ε − productions.

**Output** − Equivalent Grammar with no Left Recursion.

**Method**

Order the non-terminals as A

_{1}, A_{2}, … … … . . A_{n}

for i = 1 to n { for j = 1 to i – 1 { replace each production A_{i}→ A_{j}γ by productions A_{i}→ δ_{1}γ| δ_{2}γ| … … . . |δ_{K}γ where A_{j}→ δ_{1}|δ_{2}| … … . |δ_{K}} Remove immediate left Recursion among A_{i}productions. }

**Example1** − Remove or Eliminate Left Recursion from the following Grammar.

S → Ab

A → Sc | d

**Solution**

These productions don’t have immediate left Recursion. So it cannot be eliminated directly. We can use the algorithm here to remove Left Recursion.

**Step1** − Order the non-terminals S, A as A_{1}, A_{2}i.e., Mark S =A_{1}, A = A_{2}

Substituting values

∴ A_{1} → A_{2}b …………… (1)

A_{2} → A_{1}c|d …………… (2)

**Step2** − Put values of (1) in (2)

∴ A_{1} → A_{2}b

A_{2} → A_{1}bc|d

**Step3** − Again substitutes A_{1} = S and A_{2} = A

∴ S → A b ……………. (3)

A → A b c | d ……………. (4)

**Step4** − Remove Immediate Left recursion in A → A b c | d

So,

**Example2** − Remove left recursion from the following Grammar.

S → Aa |b

A → Ac | Sd|e

**Solution**

As we don’t have immediate left recursion so, we have to apply the algorithm here.

**Step1** − Rename S, A as A_{1}, A_{2} respectively.

A_{1} → A_{2} a | b …………… (1)

A_{2} → A_{2}c | A1d|e ……………. (2)

**Step2** − Substitute value of A_{1} of the statement (1) in R.H.S of the statement (2).

A_{1} → A_{2} a | b

A_{2} → A_{2} c |A_{2} a d|bd|e.

**Step3** − Again, substitute A_{1} = S, A_{2} = A

S → Aa |b …………….. (3)

A → Ac |A a d|bd|e ……………… (4)

**Step4** − Statement (4) has an immediate left recursion. Removing immediate left recursion we get

- Related Questions & Answers
- What is Compiler Design?
- What is Design of Lexical Analysis in Compiler Design?
- What is the difference between Procedural and Non-Procedural Languages in compiler design?
- What is Chomsky Hierarchy in compiler design?
- What is error handling in compiler design?
- What is Input Buffering in Compiler Design?
- What is Finite Automata in Compiler Design?
- What is Language Processing Systems in Compiler Design?
- What is minimizing of DFA in compiler design?
- What is binding and binding time in compiler design?
- What is the Representation of DFA in compiler design?
- What is Operator Precedence Parsing Algorithm in compiler design?
- What is Components of LR Parsers in compiler design?
- What is types of LR Parser in compiler design?
- What is translation of control statements in compiler design?