You are on page 1of 11

Introduction to Formal Language, Fall 2016 Due: 21-Apr-2016 (Thursday)

Homework 4 Solutions
Instructor: Prof. Wen-Guey Tzeng Scribe: Yi-Ruei Chen

1. Find context-free grammars for the language L = {an bm : n 6= 2m} with n ≥ 0,


m ≥ 0.
Answer.
[Solution 1] Parse L as L = L1 ∪ L2 , where L1 = {an bm : n > 2m} and L2 = {an bm :
n < 2m}. Then construct productions for L1 and L2 , respectively. A context-free
grammar for L is G = ({S, S1 , S2 , A, B}, {a, b}, S, P ) with the productions
S → S1 |S2 ,
S1 → aaS1 b|A, A → a|aA,
S2 → aaS2 b|B, B → b|Bb|ab.
[Solution 2] Produce L0 = {an bm : n = 2m} then add extra a’s or b’s. A context-free
grammar for L is G = ({S, A, B}, {a, b}, S, P ) with the productions
S → aaSb|A|B,
A → a|aA,
B → b|Bb|ab.
2

2. Find context-free grammars for the language L = {an bm ck : k 6= n + m}. (with n ≥ 0,


m ≥ 0, k ≥ 0)
Answer.
Parse L as L = L1 ∪ L2 , where L1 = {an bm ck : k > n + m} and L2 = {an bm ck :
k < n + m}. Then construct productions for L1 and L2 , respectively. A context-free
grammar for L is G = ({S, S1 , S2 , T1 , T2 , A, B, C}, {a, b, c}, S, P ) with the productions
S → S1 |S2 ,
S1 → aS1 c|T1 , T1 → bT1 c|C, C → cC|c,
S2 → aS2 c|T2 |AB|A|B, T2 → bT2 c|B, A → aA|a, B → bB|b.
2

3. Show that L = {w ∈ {a, b, c}∗ : |w| = 3na (w)} is a context-free language.


Answer.
A context-free grammar for L is G = ({S, T }, {a, b, c}, S, P ) with the productions
S → SaST ST S|ST SaST S|ST ST SaS|λ,
T → b|c.
2

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 |λ.

(2) L∗ = {(an bn )m : n, m ≥ 0}. A context-free grammar for L is G = ({S, S1 , S2 , A, B, T }, {a, b}, S, P )


with the productions

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

7. Find an s-grammar for L = {an bn+1 : n ≥ 2}.


Answer.
An s-grammar for L is G = ({S, S1 , S2 , B}, {a, b}, S, P ) with the productions

S → aS1 B,
S1 → aS2 B,
S2 → aS2 B|b,
B → b.

8. Construct an unambiguous grammar equivalent to the following grammar.

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.

9. Show that the following grammar is ambiguous.

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

• Case 1: Variables that cannot generate strings in T ∗ .


– V1 = {}, (T ∪ V1 )∗ = {a, b, c, d}∗ ;
– Since S → a, A → λ, and D → ddd, add S, A, and D to V1 ;
– V1 = {S, A, D}, (T ∪ V1 )∗ = ({a, b, c, d, S, A, D})∗ ;
– Since S → aA and B → Aa, add S and B to V1 ;
– V1 = {S, A, B, D}, (T ∪ V1 )∗ = ({a, b, c, d, S, A, B, D})∗ ;
– Since S → B and A → aB, B → Aa, the algorithm stops since there is no
new rules can be added to V1 ;
Thus, we have V1 = {S, A, B, D}. After removing the related useless productions,
we have:

S → a|aA|B,
A → aB|λ,
B → Aa,
D → ddd.

• Case 2: Variables that cannot be reached from S.


– The dependency graph of the result grammar in Case 1 is as follows.

4
– D is unreachable from S;
Thus, after removing the related useless productions, we have:

S → a|aA|B,
A → aB|λ,
B → Aa.

11. Eliminate all λ-productions from

S → AaB|aBB,
A → λ,
B → bbA|λ.

Answer.
A procedure of removing all λ-productions is as follows.

• Find the nullable variable set VN = {A, B};


• The λ-production A → λ can be removed after adding new productions obtained
by substituting λ for A where it occurs on the right:

S → AaB|aBB|aB,
B → bbA|λ|bb;

• The λ-production B → λ can be removed after adding new productions obtained


by substituting λ for B where it occurs on the right:

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.

13. Transform the grammar with production

S → abAB,
A → bAa|λ,
B → BAa|A|λ

into Chomsky normal form.


Answer.
The transform procedure is as follows.

• 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 :

S → Sa Sb AB|Sa Sb B|Sa Sb A|Sa Sb ,


A → Sb ASa |Sb Sa ,
B → BASa |Sb ASa |BSa |ASa |a,
Sa → a,
Sb → b.

– Introduce additional variables to get the first two productions into normal

6
form and we get the final result

S → Sa U |Sa X|Sa Y |Sa Sb ,


A → Sb W |Sb Sa ,
B → BZ|Sb V |Sb B|Sb A|Sb |BSa |ASa |a,
U → Sb V,
V → AB,
X → Sb B,
Y → Sb A,
W → ASa ,
Z → ASa ,
Sa → a,
Sb → b.

14. Convert the grammar with production

S → ABb|a,
A → aaA|B,
B → bAb|λ

into Greibach normal form.


Answer.
We introduce new variables X and Y :

S → ABX|Y,
A → Y Y A|B,
B → XAX|λ,
X → b,
Y →a

Then, by using the substitution, we immediately get the equivalent grammar

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

V1,1 = {A}, V2,2 = {A}, V3,3 = {B}, V4,4 = {B}.

8
Then, by using the equation
[
Vij = {A → BC, with B ∈ Vik , C ∈ Vk+1,j },
k∈{i,i+1,...,j−1}

we have

V1,2 = {A → BC, B ∈ V1,1 , C ∈ V2,2 } = {},


V2,3 = {A → BC, B ∈ V2,2 , C ∈ V3,3 } = {S, B},
V3,4 = {A → BC, B ∈ V3,3 , C ∈ V4,4 } = {A},

V1,3 = {A → BC, B ∈ V1,1 , C ∈ V2,3 } ∪ {A → BC, B ∈ V1,2 , C ∈ V3,3 } = {S, B},


V2,4 = {A → BC, B ∈ V2,2 , C ∈ V3,4 } ∪ {A → BC, B ∈ V2,3 , C ∈ V4,4 } = {A},

V1,4 = {A → BC, B ∈ V1,1 , C ∈ V2,4 } ∪ {A → BC, B ∈ V1,2 , C ∈ V3,4 }∪


{A → BC, B ∈ V1,3 , C ∈ V4,4 } = {A}.

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

You might also like