Professional Documents
Culture Documents
Homework 4 Solutions
Instructor: Prof. Wen-Guey Tzeng Scribe: Yi-Ruei Chen
1
4. Let L = {an bn : n ≥ 0}. Show that L and L∗ are context-free.
Answer.
(1) L = {am bk : m 6= k} ∪ {(a + b)∗ ba(a + b)∗ }. A context-free grammar for L is
G = ({S, S1 , S2 , A, B, T }, {a, b}, S, P ) with the productions
S → S1 |S2 ,
S1 → aS1 b|A|B, A → a|aA, B → b|bB,
S2 → T baT, T → aT |bT |λ.
S → SS1 |λ,
S1 → aS1 b|λ.
5. Show a derivation tree for the string aabbbb with the grammar
S → AB|λ,
A → aB,
B → Sb.
Answer.
2
6. Define what one might mean by properly nested parenthesis structures involving two
kinds of parentheses, say ( ) and [ ]. Intuitively, properly nested strings in this situation
are ([ ]), ([[ ]])[( )], but not ([ )] or (( ]]. Using your definition, give a context-free
grammar for generating all properly nested parentheses.
Answer.
A context-free grammar for generating all properly nested parentheses is G = ({S}, {(, ), [, ]}, S, P )
with production
S → [S]|(S)|λ.
2
S → aS1 B,
S1 → aS2 B,
S2 → aS2 B|b,
B → b.
S → AB|aaB,
A → a|Aa,
B → b.
Answer.
This grammar generates the strings a+ b. A desirable grammar is G = ({S, A}, {a, b}, S, P )
with the productions
S → aS|b,
A → aA|a.
S → aSbS|bSaS|λ.
Answer.
The string w = abab has the following two derivation trees: 2
3
10. Eliminate useless productions from
S → a|aA|B|C,
A → aB|λ,
B → Aa,
C → cCD,
D → ddd.
Answer.
There are two cases for useless variables
S → a|aA|B,
A → aB|λ,
B → Aa,
D → ddd.
4
– D is unreachable from S;
Thus, after removing the related useless productions, we have:
S → a|aA|B,
A → aB|λ,
B → Aa.
S → AaB|aBB,
A → λ,
B → bbA|λ.
Answer.
A procedure of removing all λ-productions is as follows.
S → AaB|aBB|aB,
B → bbA|λ|bb;
S → AaB|Aa|aBB|aB|a,
B → bbA|bb;
5
12. Eliminate all unit-productions in Question 10.
Answer.
From the dependency graph of the grammar, we add the new rules S → Aa|cCD to
the non-unit productions
S → a|aA|Aa|cCD,
A → aB|λ,
B → Aa,
C → cCD,
D → ddd.
S → abAB,
A → bAa|λ,
B → BAa|A|λ
• Removing λ-productions:
– Removing A → λ: S → abAB|abB, A → bAa|ba, B → BAa|A|λ|Ba.
– Removing B → λ: S → abAB|abB|abA|ab, A → bAa|ba, B → BAa|A|Ba|Aa|a.
• Removing unit-production B → A: S → abAB|abB|abA|ab, A → bAa|ba, B →
BAa|bAa|ba|Ba|Aa|a.
• Removing useless productions: No useless productions.
• Convert the grammar into Chomsky normal form:
– Introduce new variables Sx for each x ∈ T :
– Introduce additional variables to get the first two productions into normal
6
form and we get the final result
S → ABb|a,
A → aaA|B,
B → bAb|λ
S → ABX|Y,
A → Y Y A|B,
B → XAX|λ,
X → b,
Y →a
S → aY ABX|bAXBX|bAXX|b|a,
A → aY A|B,
B → bAX|λ,
X → b,
Y →a
7
15. Use the CYK algorithm to determine whether the strings aabb, aabba, and abbbb are
in the language generated by the grammar in Example 6.11.
Answer.
(1) For the string aabb:
Firstly, we have
8
Then, by using the equation
[
Vij = {A → BC, with B ∈ Vik , C ∈ Vk+1,j },
k∈{i,i+1,...,j−1}
we have
Thus, we have
9
V1,4 = {A}
V1,3 = {S, B} V2,4 = {A}
V1,2 = {} V2,3 = {S, B} V3,4 = {A}
V1,1 = {A} V2,2 = {A} V3,3 = {B} V4,4 = {B}
Because V1,4 = {A}, S 6∈ V1,4 , we conclude that aabb is not in the language generated
by the given grammar, i.e., aabb 6∈ L(G), by using the CYK algorithm.
(2) For the string aabba: Similarly, we have
10
V1,5 = {}
V1,4 = {A} V2,5 = {}
V1,3 = {S, B} V2,4 = {A} V3,5 = {}
V1,2 = {} V2,3 = {S, B} V3,4 = {A} V4,5 = {}
V1,1 = {A} V2,2 = {A} V3,3 = {B} V4,4 = {B} V5,5 = {A}
Because V1,5 = {}, we conclude that aabba is not in the language generated by the
given grammar, i.e., aabba ∈
/ L(G), by using the CYK algorithm.
(3) For the string abbbb: Similarly, we have
V1,5 = {A}
V1,4 = {S, B} V2,5 = {A}
V1,3 = {A} V2,4 = {S, B} V3,5 = {S, B}
V1,2 = {S, B} V2,3 = {A} V3,4 = {A} V4,5 = {A}
V1,1 = {A} V2,2 = {B} V3,3 = {B} V4,4 = {B} V5,5 = {B}
Because V1,5 = {A}, S 6∈ V1,5 , we conclude that abbbb is not in the language generated
by the given grammar, i.e., abbbb ∈ / L(G), by using the CYK algorithm. 2
11