CFG 2
CFG 2
CFG 2
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.
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.
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)|.
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.
11. Eliminate all unit-productions, all useless productions, and all -productions from the gram-
mar 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.
S a|aA|B|C,
A aB,
B Aa|a,
C cCD,
D ddd.
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 :
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,...,j1}
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 } = {};
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