Left Recursion
Left Recursion
Recursive Grammar
2. Non-Recursive Grammar
1. Recursive Grammar-
A grammar is said to be recursive if it contains at least one production that has the same
variable at both its LHS and RHS.
OR
A grammar is said to be recursive if and only if it generates infinite number of strings.
A recursive grammar is said to be left recursive if the leftmost variable of RHS is same as
variable of LHS.
OR
A recursive grammar is said to be left recursive if it has Left Recursion.
Example-
S → Sa / b
(Left Recursive Grammar)
A recursive grammar is said to be right recursive if the rightmost variable of RHS is same
as variable of LHS.
OR
A recursive grammar is said to be right recursive if it has right recursion.
Example-
S → aS / b
(Right Recursive Grammar)
2. Non-Recursive Grammar-
NOTE
A non-recursive grammar has neither left recursion nor right recursion.
Example-
S → aA / bB
A→a/b
B→c/d
(Non-Recursive Grammar)
Important Notes-
Note-01:
The grammar which is either left recursive or right recursive is always unambiguous.
Examples-
S → aS / b (Unambiguous Grammar)
S → Sa / b (Unambiguous Grammar)
Note-02:
The grammar which is both left recursive and right recursive is always ambiguous.
Example-
E → E + E / E – E / E x E / id
(Ambiguous Grammar)
Note-03:
Note-04:
The conversion of left recursive grammar into right recursive grammar and vice-versa is
decidable.
For ambiguous grammar, length of parse tree For unambiguous grammar, length of parse
is less. tree is large.
To check whether a given grammar is ambiguous or not, we follow the following steps-
Step-01:
We try finding a string from the Language of Grammar such that for the string there exists
more than one-
parse tree
or derivation tree
or syntax tree
or leftmost derivation
or rightmost derivation
Step-02:
If there exists at least one such string, then the grammar is ambiguous otherwise
unambiguous.
Problem-01:
Solution-
Since two different parse trees exist for string w, therefore the given grammar is ambiguous.
Problem-02:
Solution-
Since two different parse trees exist for string w, therefore the given grammar is ambiguous.
Problem-03:
Solution-
Since two different parse trees exist for string w, therefore the given grammar is ambiguous.
Problem-04:
Check whether the given grammar is ambiguous or not-
S → AB / aaB
A → a / Aa
B→b
Solution-
Since two different parse trees exist for string w, therefore the given grammar is ambiguous.
Problem-05:
Solution-
Since two different parse trees exist for string w, therefore the given grammar is ambiguous.
Problem-06:
Solution-
There exists no string belonging to the language of grammar which has more than one
parse tree.
Since a unique parse tree exists for all the strings, therefore the given grammar is
unambiguous.
Problem-07:
Solution-
Problem-08:
Solution-
1. Left Recursion
2. Right Recursion
3. General Recursion
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.
Example-
S → Sa / ∈
(Left Recursive Grammar)
Left recursion is eliminated by converting the grammar into a right recursive grammar.
Then, we can eliminate left recursion by replacing the pair of productions with-
A → βA’
A’ → αA’ / ∈
(Right Recursive Grammar)
2. 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-
S → aS / ∈
(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-
S → aSb / ∈
Problem-01:
A’ → BdA’ / aA’ / ∈
B → bB’
B’ → eB’ / ∈
Problem-02:
Solution-
A → +EA / xEA / ∈
Problem-03:
Solution-
The grammar after eliminating left recursion is-
E → TE’
E’ → +TE’ / ∈
T → FT’
T’ → xFT’ / ∈
F → id
Problem-04:
Solution-
L’ → ,SL’ / ∈
Problem-05:
Solution-
A → 0S1SA / ∈
Problem-06:
Solution-
A’ → dA’ / eA’ / ∈
B → bBc / f
Problem-07:
Solution-
A’ → AαA’ / ∈
Problem-08:
Solution-
Step-01:
A’ → aA’ / ∈
A’ → aA’ / ∈
B → Bb / Ab / d
Step-02:
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’ / ∈
Problem-09:
Solution-
Step-01:
X’ → SbX’ / ∈
X’ → SbX’ / ∈
S → Sb / Xa / a
Step-02:
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’ / ∈
Problem-10:
A → Ac / Sd / ∈
Solution-
Step-01:
First let us eliminate left recursion from S → Aa / b
This is already free from left recursion.
Step-02:
A → Ac / Aad / bd / ∈
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’ / ∈
Left Factoring-
Left factoring is a process by which the grammar with common prefixes is transformed
to make it useful for Top down parsers.
How?
In left factoring,
We make one production for each common prefixes.
The common prefix may be a terminal or a non-terminal or a combination of both.
Rest of the derivation is added by new productions.
The grammar obtained after the process of left factoring is called as Left Factored
Grammar.
Example-