Homework 4

DCP3122 Introduction to Formal Languages, Spring 2015
27-Apr-2015
Homework 4 - Solution
Instructor: Prof. Wen-Guey Tzeng
Due: 4-May-2015
1. Draw the derivation tree corresponding to the following context-free grammar G = ({S}, {a, b}, S, P )
with productions
S → aSa, S → bSb, S → λ.
A typical derivation in this grammar is S ⇒ aSa ⇒ aaSaa ⇒ aabSbaa ⇒ aabbaa. It is clear
that L(G) = {wwR : w ∈ {a, b}∗ }.
Ans.
2. Find context-free grammars for the following languages (n ≥ 0, m ≥ 0, k ≥ 0).
(a) L = {an bm : n 6= m − 1}.
(b) L = {w ∈ {a, b}∗ : na (v) ≥ nb (v), where v is any prefix of w}.
(c) L = {an bm ck : n = m or m 6= k}.
Ans.
(a) L = {an bm : n ≥ m > m − 1} ∪ {an bm : n ≤ m − 2 < m − 1}. A context-free grammar
for L is G = ({S, S1 , A, B, B1 }, {a, b}, S, P ) with the productions
S → AS1 |S1 B,
S1 → aS1 b|λ, A → aA|λ,
S2 → aS1 b|λ, B → bbB1 , B1 → bB1 |λ.
(b) L = {w ∈ {a, b}∗ : na (w) = nb (w), na (v) ≥ nb (v), where v is any prefix of w} ∪ {w ∈
{a, b}∗ : na (w) > nb (w), na (v) ≥ nb (v), where v is any prefix of w}. A context-free
grammar for L is G = ({S, A}, {a, b}, S, P ) with the productions
S → aSb|SS|A,
A → aA|λ.
1-1
(c) L = {an bm ck : n = m} ∪ {an bm ck : m 6= k}. A context-free grammar for L is G =
({S, A, B, C, D, D1 , D2 , E, F }, {a, b, c}, S, P ) with the productions
S → AB|CD,
A → aAb|λ, B → cB|λ,
C → aC|λ, D → ED1 |D1 F, D1 → bD1 c|λ, E → bE|b, F → cF |c.
3. Show that L = {w ∈ {a, b, c}∗ : |w| = 3na (w)} is a context-free languages.
Ans. A context-free grammar for L is G = ({S, F }, {a, b, c}, S, P ) with the productions
S → aSF SF S|F SaSF S|F SF SaS|λ,
F → b|c.
4. Show that the following language is context-free.
L = {uvwv R : u, v, w ∈ {a, b}+ , |u| = |w| = 2}.
Ans. A context-free grammar for L is G = ({S, A, B, C}, {a, b}, S, P ) with the productions
S → AB,
A → aa|ab|ba|bb,
B → aBa|bBb|aAa|bAb.
5. Show a derivation tree for the string aabbbb with the grammar S → AB|λ, A → aB, B → Sb.
Give a verbal description of the language generated by this grammar.
Ans. This grammar produces the strings of the form an b2n , n ≥ 0, i.e., it generates the
language L(G) = {an b2n : n ≥ 0}.
1-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 ([]), ([[]]),
and [()], but not ([)] or ((]]. Using your definition, give a context-free grammar for generating
all properly nested parentheses.
Ans. A context-free grammar for generating all properly nested parentheses is G = ({S}, {(, ), [, ]}, S, P )
with production
S → [S]|(S)|λ.
7. Construct an unambiguous grammar equivalent to the grammar: S → AB|aaB, A → a|Aa,
B → b.
Ans. This grammar produces the strings ab, aab, aaab, . . ., i.e., it generates G = ({S, A}, {a, b}, S, P )
with productions S → Ab, A → a|Aa.
8. Show that the grammar S → aSbS|bSaS|λ is ambiguous.
Ans. The string w = abab has the following two derivation trees:
9. Eliminate all useless productions from the grammar S → aS|AB, A → bA, B → AA. What
language does this grammar generate?
Ans. This grammar generates the language with ”nothing”, i.e., L(G) = {}. A procedure of
eliminating all useless productions is as follows.
• Substitute B: S → aS|AAA, A → bA.
• Remove useless variable A (since A has no way to get a terminal string): S → aS.
• Remove useless variable S (since S has no way to get a terminal string).
10. Eliminate all λ-productions from S → AaB|aaB, A → λ, B → bbA|λ.
Ans. A procedure of removing all λ-productions is as follows.
• The λ-production A → λ can be removed after adding new productions obtained by
substituting λ for A where it occurs on the right: S → AaB|aaB|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|aaB|aB|Aa|aa|a, B →
bbA|bb.
11. Eliminate all unit-productions, all useless productions, and all λ-productions from the grammar S → aA|aBB, A → aaA|λ, B → bB|bbC, C → B.
Ans. A removing procedure is as follows.
1-3
• Removing useless productions A → aBB, B → bB|bbC, and C → B since they cannot
be transformed into terminal strings: S → aA|aBB, A → aaA|λ
• Removing λ-productions to have S → aA|a, A → aaA|aa.
• The result in the last step is the final answer since there is no any unit-production.
12. Eliminate all unit-productions from S → a|aA|B|C, A → aB|λ, B → Aa, C → cCD,
D → ddd.
Ans. We first eliminate all λ-productions and obtain
S → a|aA|B|C,
A → aB,
B → Aa|a,
C → cCD,
D → ddd.
The we eliminate all unit-productions and obtain
S → a|aA|Aa|cCD,
A → aB,
B → Aa|a,
C → cCD,
D → ddd.
13. Transform the grammar with productions S → abAB, A → bAB|λ, B → BAa|A|λ into
Chomsky normal form.
Ans. The transform procedure is as follows.
• Removing λ-productions:
– Removing A → λ: S → abAB|abB, A → bAB|bB, B → BAa|A|λ|Ba.
– Removing B → λ: S → abAB|abB|abA|ab, A → bAB|bB|bA|b, B → BAa|A|Ba|Aa|a.
• Removing unit-production B → A: S → abAB|abB|abA|ab, A → bAB|bB|bA|b, B →
BAa|bAB|bB|bA|b|Ba|Aa|a.
• Convert the grammar into Chomsky normal form:
– Introduce new variables Sa for each a ∈ T :
S → Sa Sb AB|Sa Sb B|Sa Sb A|Sa Sb ,
A → Sb AB|Sb B|Sb A|Sb ,
B → BASa |Sb AB|Sb B|Sb A|Sb |BSa |ASa |Sa ,
Sa → a,
Sb → b.
– Introduce additional variables to get the first two productions into normal form and
1-4
we get the final result
S → Sa U |Sa X|Sa Y |Sa Sb ,
A → Sb V |Sb B|Sb A|Sb ,
B → BZ|Sb V |Sb B|Sb A|Sb |BSa |ASa |Sa ,
U → Sb V,
V → AB,
X → Sb B,
Y → Sb A,
Z → ASa ,
Sa → a,
Sb → b.
14. Use the CYK method to determine if the string w = aaabbbbab is in the language generated
by the grammar S → aSb|b.
Ans. Firstly, we convert the grammar S → aSb|b into Chomsky normal form:
• S → ASB|b, A → a, B → b.
• S → AX|b, X → SB, A → a, B → b.
Then for w = w1 w2 · · · w9 = aaabbbbab, we have
V11 = {A}, V22 = {A}, V33 = {A}, V44 = {B, S}, V55 = {B, S},
V66 = {B, S}, V77 = {B, S}, V88 = {A}, V99 = {B, S},
By using the equation
Vij =
[
{A → BC, with B ∈ Vik , C ∈ Vk+1,j },
k∈{i,i+1,...,j−1}
we have
V12 = {A : A → BC, B ∈ V11 , C ∈ V22 } = {},
V23 = {A : A → BC, B ∈ V22 , C ∈ V33 } = {},
V34 = {A : A → BC, B ∈ V33 , C ∈ V44 } = {},
V45 = {A : A → BC, B ∈ V44 , C ∈ V55 } = {X},
V56 = {A : A → BC, B ∈ V55 , C ∈ V66 } = {X},
V67 = {A : A → BC, B ∈ V66 , C ∈ V77 } = {X},
V78 = {A : A → BC, B ∈ V77 , C ∈ V88 } = {},
V89 = {A : A → BC, B ∈ V88 , C ∈ V99 } = {};
V13 = {A : A → BC, B ∈ V11 , C ∈ V23 } ∪ {A : A → BC, B ∈ V12 , C ∈ V33 } = {},
V24 = {A : A → BC, B ∈ V22 , C ∈ V34 } ∪ {A : A → BC, B ∈ V23 , C ∈ V44 } = {},
V35 = {A : A → BC, B ∈ V33 , C ∈ V45 } ∪ {A : A → BC, B ∈ V34 , C ∈ V55 } = {},
V46 = {A : A → BC, B ∈ V44 , C ∈ V56 } ∪ {A : A → BC, B ∈ V45 , C ∈ V66 } = {S},
V57 = {A : A → BC, B ∈ V55 , C ∈ V67 } ∪ {A : A → BC, B ∈ V56 , C ∈ V77 } = {},
V68 = {A : A → BC, B ∈ V66 , C ∈ V78 } ∪ {A : A → BC, B ∈ V67 , C ∈ V88 } = {},
V79 = {A : A → BC, B ∈ V77 , C ∈ V89 } ∪ {A : A → BC, B ∈ V78 , C ∈ V99 } = {};
1-5
Continue this procedure, we have
V14 = {}, V25 = {}, V36 = {X}, V47 = {}, V58 = {}, V69 = {};
V15 = {}, V26 = {S}, V37 = {}, V48 = {}, V59 = {};
V16 = {}, V27 = {X}, V38 = {}, V49 = {};
V17 = {S}, V28 = {}, V39 = {};
V18 = {}, V29 = {};
V19 = {}.
Because V19 = {}, we conclude that w is not in the language generated by the grammar
S → aSb|b by using the CYK algorithm.
1-6