CS-1301315 Theory of Computation
Worksheet-6 Solution:
Context Free Grammars
Dr Mohammed Khader
OBJECTIVES:
Understand how context-free grammars (CFG) generate context-free languages.
Understand ambiguity in CFG and how to remove it.
Practice how to trace the CYK algorithm to derive a string from a CFG.
Q1. Find the language generated by the grammar G = ({S}, {a, b}, S, P).
P: S → aS | λ
L = { an : n ≥ 0 }
Q2. Find the language generated by the grammar G = ({S}, {a, b}, S, P).
P: S → aS | a
L = { an : n ≥ 1 }
Q3. Find the language generated by the grammar G = ({S}, {a, b}, S, P).
P: S → aS | bS | λ
L = { w ∈ {a, b}* }; i.e. all possible strings on {a, b}
Q4. Find the language generated by the grammar G = ({S}, {a, b}, S, P).
P: S → aSb | b
L = { anbn+1 : n ≥ 0 }
Q5. Find the language generated by the grammar G = ({S}, {a, b}, S, P).
P: S → aSbb | λ
L = { anb2n : n ≥ 0 }
Q6. Find the language generated by the grammar G = ({S, A, B}, {a, b}, S, P).
P: S → AB, A → aA | a, B → bB | b
L = { anbm : n, m ≥ 1 }
Q7. Find the language generated by the grammar G = ({S}, {a, b}, S, P).
P: S → aSa | bSb | b
L = { wbwR : w ∈ {a, b}*}
Q8. Find the language generated by the grammar G = ({S, A}, {a, b}, S, P).
P: S → aAb, A → aA | bA | λ
L = { awb : w ∈ {a, b}*}
Q9. Find the language generated by the grammar G = ({S, A, B}, {a, b}, S, P).
P: S → AAB, A → a | b, B → aB | bB | λ
L = { |w| ≥ 2 : w ∈ {a, b}*}; i.e. strings of length 2 or more on {a, b}
Q10. Find the language generated by the grammar G = ({S, A}, {a, b}, S, P).
P: S → AAS | λ, A → a | b
L = { |w| = 2k, k ≥ 0 : w ∈ {a, b}*}; strings of even length on {a, b}
Q11. Find the CFG for the following language:
L= {anbmcma2n: n,m>=0}
Solution:
P: S → aSaa| B
B → bSc | λ
Q12. Find the CFG for the following language:
L= {a2nbnakcma2m: n,m>=0, k>0}
Solution:
P: S → ABC
A → aaAb | λ
B → aB | a
C → cCaa | λ
Q13. Find the CFG for the following language:
L= {a2nbk+n+2mck: n,m>0, k>=0}
Solution:
P: S → ABC
A → aaAb | aab
B → bbB | bb
C → bCc | λ
Q11. Consider the following grammar:
G = ({S, A, B}, {a, b}, S, P)
P: S → AB | aaB
A → Aa | a
B→b
A- Show that the grammar G is ambiguous by deriving 2 different derivation trees.
First, we need to find a string with two different derivation trees. Choose 'aab'.
S S
A B a a B
A a b b
a
derivation tree 1 derivation tree 2
B- Show that the grammar G is ambiguous by finding 2 different left most derivations for string ‘aab’.
S=>AB=>AaB=>aaB=>aab
S=>aaB=>aab
C- Show that the grammar G is ambiguous by finding 2 different right most derivations for
string ‘aab’.
S=>AB=>Ab=>Aab=>aab
S=>aaB=>aab
Q12. Show that the grammar G is ambiguous. (Hint: derive 1+2*3)
G = ({E}, {+, *, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, E, P)
P: E → E + E | E * E | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
AE E
E + E E * E
1 E * E E + E 3
2 3 1 2
derivation tree 1 derivation tree 2
You can show that by having 2 distinct LMD or 2 distinct RMD.
Another solution: 2 distinct LMD for 1+2*3:
E=> E+E=>1+E=>1+E*E=>1+2*E=>1+2*3
E=> E*E=>E+E*E=>1+E*E=>1+2*E=>1+2*3
Another solution: 2 distinct RMD for 1+2*3:
E=> E+E=>E+E*E=>E+E*3=>E+2*3=>1+2*3
E=> E*E=>E*3=E+E*3=>E+2*3=>1+2*3
Q13. Show that the grammar G is ambiguous. (Hint: derive 'abb')
G = ({S}, {a, b}, S, P)
P: S → aSb | Sb | λ
S S
a S b S b
S b a S b
λ λ
derivation tree 1 derivation tree 2
Q14. Which of the following grammar is a Simple grammar (S-Grammar)?
A- G1: S aAB| bA
AaA | bB | a
B- G2: S aSS | bAS| aA
AaA|b
C- G3: S aSA | bSS | cA
A aA|bA| d
Answer: C