Practice Problems Based On Derivations and Parse Tree
Practice Problems Based On Derivations and Parse Tree
Problem-01:
Consider the grammar-
S → bB / aA
A → b / bS / aAA
B → a / aS / bBB
For the string w = bbaababa, find-
1. Leftmost derivation
2. Rightmost derivation
3. Parse Tree
Solution-
1. Leftmost Derivation-
S → bB
→ bbBB (Using B → bBB)
→ bbaB (Using B → a)
→ bbaaS (Using B → aS)
→ bbaabB (Using S → bB)
→ bbaabaS (Using B → aS)
→ bbaababB (Using S → bB)
→ bbaababa (Using B → a)
2. Rightmost Derivation-
S → bB
→ bbBB (Using B → bBB)
→ bbBaS (Using B → aS)
→ bbBabB (Using S → bB)
→ bbBabaS (Using B → aS)
→ bbBababB (Using S → bB)
→ bbBababa (Using B → a)
→ bbaababa (Using B → a)
3. Parse Tree-
Problem-02:
Consider the grammar-
S → A1B
A → 0A / ∈
B → 0B / 1B / ∈
For the string w = 00101, find-
1. Leftmost derivation
2. Rightmost derivation
3. Parse Tree
Solution-
1. Leftmost Derivation-
S → A1B
→ 0A1B (Using A → 0A)
→ 00A1B (Using A → 0A)
→ 001B (Using A → ∈)
→ 0010B (Using B → 0B)
→ 00101B (Using B → 1B)
→ 00101 (Using B → ∈)
2. Rightmost Derivation-
S → A1B
→ A10B (Using B → 0B)
→ A101B (Using B → 1B)
→ A101 (Using B → ∈)
→ 0A101 (Using A → 0A)
→ 00A101 (Using A → 0A)
→ 00101 (Using A → ∈)
3. Parse Tree-
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-
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’ / ∈
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
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 / ∈
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’ / ∈