0% found this document useful (0 votes)
25 views

Left Recursion

The document discusses left recursion in grammar, defining it as a situation where the leftmost variable of a production's right-hand side (RHS) matches the left-hand side (LHS) variable. It explains the elimination of left recursion by converting it into right recursion, which does not pose issues for top-down parsers. Additionally, the document provides various examples and practice problems demonstrating the elimination of left recursion from different grammars.

Uploaded by

sowjnaya samala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Left Recursion

The document discusses left recursion in grammar, defining it as a situation where the leftmost variable of a production's right-hand side (RHS) matches the left-hand side (LHS) variable. It explains the elimination of left recursion by converting it into right recursion, which does not pose issues for top-down parsers. Additionally, the document provides various examples and practice problems demonstrating the elimination of left recursion from different grammars.

Uploaded by

sowjnaya samala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

1.

Left Recursion-

• A production of grammar is said to have left recursion if the leftmost variable of its RHS is
same as variable of its LHS.
• A grammar containing a production having left recursion is called as Left Recursive Grammar.

A→ Aa / b
(Left Recursive Grammar)
• Left recursion is considered to be a problematic situation for Top down parsers.
• Therefore, left recursion has to be eliminated from the grammar.

Elimination of Left Recursion
Left recursion is eliminated by converting the grammar into a right recursive grammar.

If we have the left-recursive pair of productions-


A → Aα / β
(Left Recursive Grammar)
where β does not begin with an A.

Prepared by Sallauddin Md,Assistant Professor,SREC Page 1


Then, we can eliminate left recursion by replacing the pair of productions with-
A → βA’
A’ → αA’ / ∈
(Right Recursive Grammar)

Right Recursion-

• A production of grammar is said to have right recursion if the rightmost variable of its RHS
is same as variable of its LHS.
• A grammar containing a production having right recursion is called as Right Recursive
Grammar.

Example-

A → aA / ∈

Prepared by Sallauddin Md,Assistant Professor,SREC Page 2


(Right Recursive Grammar)

• Right recursion does not create any problem for the Top down parsers.
• Therefore, there is no need of eliminating right recursion from the grammar.

3. General Recursion-
• The recursion which is neither left recursion nor right recursion is called as general recursion.
Example-

A → aAb / ∈
PRACTICE PROBLEMS :
Problem-01:

Consider the following grammar and eliminate left recursion-


A → ABd / Aa / a
B → Be / b

Solution-

The grammar after eliminating left recursion is-


A → aA’
A’ → BdA’ / aA’ / ∈
B → bB’
B’ → eB’ / ∈

Problem-02:

Consider the following grammar and eliminate left recursion-


E→E+E/ExE/a

Solution-

The grammar after eliminating left recursion is-


E → aA
A → +EA / xEA / ∈

Problem-03:

Consider the following grammar and eliminate left recursion-


E→E+T/T
T→TxF/F
F → id

Solution-

The grammar after eliminating left recursion is-

Prepared by Sallauddin Md,Assistant Professor,SREC Page 3


E → TE’
E’ → +TE’ / ∈
T → FT’
T’ → xFT’ / ∈
F → id

Problem-04:

Consider the following grammar and eliminate left recursion-


S → (L) / a
L→L,S/S
Solution-
The grammar after eliminating left recursion is-
S → (L) / a
L → SL’
L’ → ,SL’ / ∈

Problem-05:

Consider the following grammar and eliminate left recursion-


S → S0S1S / 01
Solution-
The grammar after eliminating left recursion is-
S → 01A
A → 0S1SA / ∈
Problem-06:
Consider the following grammar and eliminate left recursion-
S→A
A → Ad / Ae / aB / ac
B → bBc / f
Solution-
The grammar after eliminating left recursion is-
S→A
A → aBA’ / acA’
A’ → dA’ / eA’ / ∈
B → bBc / f

Problem-07:

Consider the following grammar and eliminate left recursion-


A → AAα / β
Solution-

The grammar after eliminating left recursion is-


A → βA’
A’ → AαA’ / ∈

Prepared by Sallauddin Md,Assistant Professor,SREC Page 4


Problem-08:

Consider the following grammar and eliminate left recursion-


A → Ba / Aa / c
B → Bb / Ab / d

Solution-
This is a case of indirect left recursion.
Step-01:
First let us eliminate left recursion from A → Ba / Aa / c
Eliminating left recursion from here, we get-
A → BaA’ / cA’
A’ → aA’ / ∈
Now, given grammar becomes-
A → BaA’ / cA’
A’ → aA’ / ∈
B → Bb / Ab / d
Step-02:
Substituting the productions of A in B → Ab, we get the following grammar-
A → BaA’ / cA’
A’ → aA’ / ∈
B → Bb / BaA’b / cA’b / d
Step-03:
Now, eliminating left recursion from the productions of B, we get the following grammar-
A → BaA’ / cA’
A’ → aA’ / ∈
B → cA’bB’ / dB’
B’ → bB’ / aA’bB’ / ∈
This is the final grammar after eliminating left recursion.

Problem-09:
Consider the following grammar and eliminate left recursion-
X → XSb / Sa / b
S → Sb / Xa / a
Solution-
This is a case of indirect left recursion.
Step-01:
First let us eliminate left recursion from X → XSb / Sa / b
Eliminating left recursion from here, we get-
X → SaX’ / bX’
X’ → SbX’ / ∈
Now, given grammar becomes-
X → SaX’ / bX’
X’ → SbX’ / ∈
S → Sb / Xa / a

Prepared by Sallauddin Md,Assistant Professor,SREC Page 5


Step-02:
Substituting the productions of X in S → Xa, we get the following grammar-
X → SaX’ / bX’
X’ → SbX’ / ∈
S → Sb / SaX’a / bX’a / a
Step-03:
Now, eliminating left recursion from the productions of S, we get the following grammar-
X → SaX’ / bX’
X’ → SbX’ / ∈
S → bX’aS’ / aS’
S’ → bS’ / aX’aS’ / ∈
This is the final grammar after eliminating left recursion.

Problem-10:
Consider the following grammar and eliminate left recursion-
S → Aa / b
A → Ac / Sd / ∈
Solution-
This is a case of indirect left recursion.
Step-01:
First let us eliminate left recursion from S → Aa / b
This is already free from left recursion.
Step-02:
Substituting the productions of S in A → Sd, we get the following grammar-
S → Aa / b
A → Ac / Aad / bd / ∈

Prepared by Sallauddin Md,Assistant Professor,SREC Page 6


Step-03:
Now, eliminating left recursion from the productions of A, we get the following grammar-
S → Aa / b
A → bdA’ / A’
A’ → cA’ / adA’ / ∈

Problem-11:
S→AA|0
A →S S | 1
Answer
Considering the ordering S, A, we get:
S →A A | 0
A → AAS | 0S | 1
and removing immediate left recursion, we get
S →A A | 0
A → 0SA’ | 1A’
A → ASA’ |∈
Problem-12:
S → Aa | b
A → Ac | Sd | e
A → Sd will be replaced by
A → Ac | Aad | bd | e , then eliminates immediate recursion among A productions and yields the
following
S → Aa | b
A → bdA’ | A’
A’ → cA’ | adA’ | e

Note: We can remove the left recursion in many ways .it is depends on grammar. In indirect
Left recursion, the replacement of production can take before removal of left recursion or after
removal of left recursion.

Verify the below example

Prepared by Sallauddin Md,Assistant Professor,SREC Page 7


Procedure 1

Procedure 2

The above mentioned two procedures to same problem are correct for removal of left recursion.

Prepared by Sallauddin Md,Assistant Professor,SREC Page 8


A → ABd / Aa / a A → aA’
B → Be / b A’ → BdA’ / aA’ / ∈
B → bB’
B’ → eB’ / ∈

S → (L) / a S → (L) / a
L→L,S/S L → SL’
L’ → ,SL’ / ∈

E →E+T|T E → TE`
E →T*F|F E`→ + TE` | E
F → (E) | id T → FT`
T → * FT` | E
F → (E) | id
S → S0S1S / 01 S → 01A
A → 0S1SA / ∈

S→A S→A
A → Ad / Ae / aB / ac A → aBA’ / acA’
B → bBc / f A’ → dA’ / eA’ / ∈
B → bBc / f

A → AAα / β A → βA’
A’ → AαA’ / ∈

A → Ba / Aa / c A → BaA’ / cA’
B → Bb / Ab / d A’ → aA’ / ∈
B → cA’bB’ / dB’
B’ → bB’ / aA’bB’ / ∈

X → XSb / Sa / b X → SaX’ / bX’


S → Sb / Xa / a X’ → SbX’ / ∈
S → bX’aS’ / aS’
S’ → bS’ / aX’aS’ / ∈

S → Aa / b S → Aa / b
A → Ac / Sd / ∈ A → bdA’ / A’
A’ → cA’ / adA’ / ∈

A --> B x y | x
B --> C D
C --> A | c
D --> d

Prepared by Sallauddin Md,Assistant Professor,SREC Page 9


S→Ta|b Replace T by S then S→Sa|b
T→S
S→bS’
S’→aS’|∈
T →S

S → X | Xb | SS
X→S|a
S → a | Xb | SS
X → Xb | SS | a
S → SX | SSb | XS | a S → XSS′ | aS′
S′ → XS′ | SbS′| ∈
S → SX | SSb | XS | a S → XSS′ | aS′
X → Xb | Sa | b S′ → XS′ | SbS′| ǫ
X → bX′ | aS′aX′
X′ → SS′aX′ | bX′ | ǫ
S → SX | SSb | XS | a S → XSS′ | aS′
X → Sa | Xb S′ → XS′ | SbS′ | ∈
X → SaX′
X′ → bX′ | ∈
A → AAα / β A → βA’
A’ → AαA’ / ∈

S → Ab | a
A → Sa | b.

S → Ab | a S → Ab | a
A → SAa | b. Replace A by its productions.
S → Saab|bb|a

Prepared by Sallauddin Md,Assistant Professor,SREC Page 10


remove left recursion
S → bbS’|aS’
S’→AabS’|∈
A → SAa | b.

A → Bb | a

B → Cc | b

C → Dd | c

D → Aa | d

Replace D in C’s production


C → Aad | dd|c

Replace C in B’s production

B → Aadc | ddc|cc|b

Replace B in A’s production


A → Aadcb | ddcb|ccb|bb|a

Then remove left recursion

A → ddcbA’|ccbA’|bbA’|aA’

A’→adcbA’|∈

B → Cc | b

C → Dd | c

D → Aa | d

Prepared by Sallauddin Md,Assistant Professor,SREC Page 11

You might also like