Title: Key Answers for Homework
1Key Answers for Homework 7
1. This problem is to clean up context-free
grammar G shown below. Using the two algorithms
presented in the class, we are going to eliminate
all useless symbols from the following
context-free grammar G. Answer the following two
questions. (a) Using the algorithm in Lemma 1 for
grammar G construct a context-free grammar G
such that L(G) L(G) and every nonterminal
symbol in G derives a string x ?(VT), where VT
is set of terminal symbols of G. (b) Using the
algorithm in Lemma 2 for grammar G, the result
of part (a), and construct a context-free grammar
G such that L(G) L(G), and for each symbol
X (which is either terminal or nonterminal
symbol) in G, starting with the start symbol,
it is possible to derive a string containing X.
Your answer should include detailed procedure
(steps of the algorithm) that you took to collect
the information needed to get your answer. No
grade will be given to an answer that only shows
the final result.
G S ?BA CDg B ?aEA A ?
DEd C ? Ce D ? Df ?
E ? Eg g F ?
hF h G ?cGCc I ? cDEFab
Answer (a) OLDV NEWV D, E, F OLDV
NEWV NEWV OLDV ? A, I A, D, E, F,
I OLDV NEWV NEWV OLDV ? B A, B, D,
E, F, I OLDV NEWV NEWV OLDV ? S A,
B, D, E, F, I, S OLDV NEWV NEWV OLDV ?
A, B, D, E, F, I, S VN NEWV A, B,
D, E, F, I, S // Set of nonterminal symbols
which derive a string x ?(VT)
G S ?BA B ?aEA
A ? DEd D ? Df ?
E ? Eg g F ? hF h
I ? cDEFab
2G S ?BA B ?aEA
A ? DEd D ? Df ?
E ? Eg g F ? hF h
I ? cDEFab
- Answer (b) Constructing a context-free grammar
G such that L(G) L(G), and for each symbol
X (which is either - terminal or nonterminal symbol) in G, starting
with the start symbol, it is possible to derive a
string containing X. - VT VN // initialize with empty set
- VN VN ?S VT VT ?
- VN VN ?A, B S, A, B VT VT ?
- VN VN ?D, E S, A, B, D, E VT
VT ? a, d a, d - VN VN ? S, A, B, D, E VT VT ?
f, g, ? a, d, f, g, ? - VN VN ? S, A, B, D, E VT VT ?
a, d, f, g, ?
G S ?BA B ?aEA
A ? DEd D ? Df ? E ?
Eg g
32. Covert the following context-free grammar into
a Chomsky Normal Form. S ? abABCde
A ? aAa B ? ba C ? cab
Answer Given rule
Converted rules -----------------------
---------------------------------------------
--------------------------------------------------
-------------- S ? abABCde
S ? S1S2 S1 ? a S2
? S3S4 S3 ? b S4 ? AS5
S5 ? BS6
S6 ? CS7
S7 ? S8S9 S8 ? d
S9 ? e A ? aAa
A ? A1A2 A1? a
A2 ? AA3 A3 ? a
B ? ba B ?
B1B2 B1 ? b
B2 ? a C ?
cab C ? C1C2 C1
? c C2 ? C3C4 C2 ?
a C2 ? b
43. (a) Show that the following grammar is
ambiguous. S ? S S S S
T T ? a b
Answer (a) For string a b a, we have the
following two parse trees.
(b) Let be the addition and be the
multiplication. Then the above grammar generates
mathematical expressions with the two operators
on two variable a and b. Convert the above
grammar to an unambiguous context-free grammar
such that it generates the same language and
has higher precedence than .
Answer S ? S A A A ? A
T T T ? a b Notice that this
grammar restricts such that multiplications are
done as a term of an addition, if any.
(c) Convert the above grammar to an unambiguous
context-free grammar such that - it
generates the same language, - the two
operators and have the same precedence, and
- the evaluation is done right to left.
Answer S ? T S T S T
T ? a b Notice that all the operators in
a string are generated left to right according to
the order of appearance.
54. The following syntax flow graph is a
simplified definition of the Pascals syntax flow
graph for . Assume that all the
strings in a circle are terminal symbols.
(a) Convert this syntax flow graph for
to a context-free grammar that
generate the same language, and show that the
grammar is ambiguous.
Answer In the following grammar, denotes
nonterminals, and others are terminals.
is starting nonterminal.
? IF()then
IF()thenelse
? a b a
b ? c d This grammar is
ambiguous because there are two parse trees for
string w IF(c) then IF (d) then a b else a -
b. (Ignore blanks in the string. They are
inserted for readability.)
6 ? IF()then
IF()thenelse t ? a b a b
? c d
For simplicity, lets transform the above grammar
as shown below with the nonterminals and
terminals substituted as follows
S B A
IF i then t else e
a b p a b m
S ? i(B)tA i(B)tAeA A ? m S p
B ? c d
Then, for string i(c)ti(d)tpem (which is IF (c)
then IF (d) then a b else a b according to
the original grammar) has two parse trees as
follows.
7(b) Convert your grammar to an unambiguous
grammar that generates the same if statements.
You should briefly describe your idea.
Answer We change the grammar such that else
part, which is e, is always generated together
with the nearest if part ( i in the grammar)
located to the left. (Notice that S has recursion
at the right end of its production.)
S ? i(B)tS i(B)tAeS i(B)tA i(B)tAeA A ?
p m B ? c d
String i(c)ti(d)tpem can only be generated as
shown to the right.
85. Using the pumping lemma we want to prove
that language L xxR x ?a, b, c is not
regular as follows. Complete the proof by
answering the questions.
Proof. Suppose that L is regular. Then L should
satisfy the pumping lemma. Let n be the constant
of the lemma. Clearly, all the following strings
belong to L. (a) abccba (b)
a100b100b100a100 (c ) an/2bn/2b n/2an/2
(d) anbnbnan Question 1 Which string will you
choose for z of the lemma? Why the other strings
cannot be a candidate for z? If none of the
above strings can be a candidate for z, what is
your choice from L? Answer Strings in (a) and
(b) cannot be a candidate for z because their
length is fixed. For n greater than the length of
these strings, the lemma is not applicable.
Strings in (c ) and (d) are fine, because their
length is not less than n. I will choose the
string in (d), since there will be smaller number
of cases to be analyzed than the string in (c
). Question 2 Let u, v, w ?a, b, c be the
strings of the lemma such that z uvw. What will
be in v? Shortly describe all possible contents
of string v. Answer Since uv ? n, and v ?
1, string v contains some as in the prefix an of
the string such that 1 ? v ? n. Question 3 The
pumping lemma says For all i ? 0, string uviw ?
L. How do you deal with this statement for your
proof? Justify what you do. Answer Since this
statement uses universal quantifier for i, it is
enough to show one value of i for which the
statement is wrong. Thus, we choose i 2.
Clearly, uv2w ?L, since uv2w ? xxR for any x ?a,
b, c
96. Which of the following language are regular.
Prove your answer. Your answer will be graded
according to the logical completeness.
L1 aibjak i, j, k 0 L2
aibjai i, j 0
Answer Since aabbaa is a regular expression
which denotes L1, it is regular. (Also you can
show that L1 is regular by showing a regular
grammar or FA whose language is L1.
Language L2 is not regular. We use the pumping
lemma for the proof. Suppose that L2 is regular.
Then it should satisfy the pumping lemma for
regular languages. Let n be the constant of the
lemma, and let z anban, which is in the
language L2. Since z ? n, according to the
pumping lemma there exist u, v, w such that (i)
z uvw, (ii) uv ? n, (iii) v ? 1, and (iv)
for all i ? 0, z? L2 By conditions (i) (iii)
above, v contains only as which belong to the
prefix an of z, and 1 ? v ? n. It follows that
uv2w ?L2 . Thus language L2 does not satisfy the
pumping lemma. We are in contradiction. L2 is not
regular.
10 7. For a string x, let a(x), b(x), and c(x)
denote, respectively, the number of as, number
of bs and number of cs in string x. Which of
the following languages are context-free? Prove
your answer. Your answer will be graded according
to the logical completeness. L3
x x ?a,b,c, and a(x) c(x)
L4 x x ?a,b,c, and a(x) b(x)
c(x) L5 x x ?a,b,c, and
a(x) b(x) c(x)
Answer L3 is context-free language because we
can construct a PDA which recognizes this
language as follows, where X ? a, c, A, Z0.
(c, Z0/cZ0)
11We prove that language L4 x x ?a,b,c,
and a(x) b(x) c(x) is not context-free.
Suppose it is. Then the language should satisfy
the pumping lemma for context-free languages. Let
p be the constant of the lemma. Choose z
apbpcp. Then we can apply the same argument that
we used for proving that aibici i ? 0 is not
context-free, and show that z ? L4. This
language does not satisfy the pumping lemma. We
are in contradiction. It is not context-free.
- We prove that language L5 x x ?a,b,c,
and a(x) b(x) c(x) is not context-free
using the pumping lemma for context-free
languages. Suppose it is, and let z ap2bp1cp.
Since vwx ? p and vx ? 1, v and x together
contain at most two different symbols among a, b
and c (see the illustration below). We have
following two cases. - When v and x contain as or bs clearly z
uv0wx0y ? L5 - When v and x contains bs or cs obviously z
uv2wx2y ? L5 - L5 does not satisfy the pumping lemma. We are in
contradiction. The language is not context-free.