Homework 4 Solutions

Homework 4 Solutions
Professor Xin Li
Head CA: Ryan Marren
EN.600.271 - Automata and Computation Theory
March 31, 2017
1
(20 points) Show that the class of context-free languages is closed under the star operation
(recall that in class we have shown that it is closed under union and concatenation).
Suppose that A is a context free language, then there exists a context free grammar G which
recognizes this language. Let S be the start symbol of G and construct a new grammar G0
with start symbol S 0 as follows:
S 0 → S 0S | To the grammar G0 we will then also add all of the rules in the grammar G, so that the
nonterminal symbol S in the new grammar G0 can generate any string that can be generated
by the original grammar G.
Theorem . G’ is a context free grammar which recognizes A∗ .
Proof. Suppose w is a string in L(G0 ). Then w was generated by taking the transition
S 0 → S 0 S n times and then taking the transition S 0 → for some n ∈ Z≥0 (since the only
way to produce a non-terminal symbol from S 0 is the rule S 0 → ). This would result in a
string of n copies of the non-terminal character S, each of which will produce a string in A
since L(G) = A. Thus, w ∈ A∗ by the definition of kleene star (w is the concatenation of 0
or more strings in A).
Suppose w is a string in A∗ . Then by the definition of the kleene star, w is the concatenation
of n ∈ Z≥0 strings in A, call them w1 ◦ w2 ◦ . . . ◦ wn , each of which can be produced by
the grammar G which has start symbol S. Since we can obtain a string of n copies of the
non-terminal character S from the start symbol S 0 by taking the rule S 0 → S 0 S n times and
then taking the sule S 0 → , we can then label the non-terminal symbols S1 S2 . . . Sn and
then generate wi from Si for each i ∈ {1, . . . , n}. Thus w is a string which can be generated
by G0 and thus w ∈ L(G0 )
This proves that A∗ is recognized by G0 (L(G0 ) = A∗ ), so A∗ is context free. Since A was
originally an arbitrary context free language, this further proves that context free languages
are closed under the kleene star operation.
1
2
(20 points) In class we showed that the language C = {an bn cn |n ≥ 0} is not context free.
Consider the languages A = {am bn cn |m, n ≥ 0} and B = {an bn cm |m, n ≥ 0}.
a (10 points) Give a context free grammar for each of A and B. Then, use A and B to
show that the class of context free languages is not closed under intersection.
CFG GA for A:
S → AP
A → Aa | P → bP c | CFG GB for B:
S → PC
C → Cc | P → aP b | Lemma . A ∩ B = C
Proof. If w ∈ A ∩ B then w ∈ A and w ∈ B so w is a string of ana bnb cnc for na = nb
and nb = nc . Thus, na = nb = nc and w is also in C.
If w ∈ C we have trivially that w ∈ A and w ∈ B since m = n is still valid in
each since n ≥ 0.
Since A∩B = C, we know A and B are context free languages since they are generated
by GA and GB respectively, and C is not context free as proved in class, we have a
counter example to the closure of ∩ for context free languages.
b (10 points) Use (b) and DeMorgan’s Law (Textbook Theorem 0.20) to show that the
class of context free languages is not closed under complementation.
Suppose A and B are context free languages and suppose for sake of contradiction
the class of context free languages is closed under complementation. Then A and B
are context free languages, and further A ∪ B is a context free language by the closure
of ∪ proven in class. This implies by DeMorgan’s law that A ∪ B = A ∩ B = A ∩ B
is context free, which would imply that the class of context free languages are closed
under ∩. We just proved this false, so we have a contradiction and it must be that the
class of context free languages is not closed under complementation.
2
3
(20 points) Give a context free grammar for the following language, where the alphabet is
{0, 1}: the complement of the language A = {0n 1n |n ≥ 0}.
If w ∈ {0, 1}∗ , then w can be in A for the following reasons:
1. w has a 1 symbol which appears to the left of a 0 symbol.
2. w does not satisfy condition 1, but w has n0 00 s and n1 10 s where n0 6= n1 .
This gives intuition for the following grammar:
S→L|R|W
L → Z0P
R → P 1O
W → M 10M
P → 0P 1 | Z → Z0 | O → O1 | M → M0 | M1 | Note that P generates all even length 0n 1n , n ∈ Z≥0 palindromes, Z generates 0∗ , O generates
1∗ , and M generates {0, 1}∗ . Thus, R generates strings where no 1 appears before a 0 but
there are more 10 s than 00 s and L generates strings where no 1 appears before a 0 but there
are more 00 s than 10 s, so L and R generate all strings with condition 2. W generates all
strings which have a 1 appear before a 0, which covers strings in condition 1.
4
n
(20 points). Prove that the language L = {02 |n ≥ 0} is not context free.
Suppose for sake of contradiction that L is a context free language, then L is pumpable.
This implies that there exists a pumping length p such that for all s ∈ L where |s| ≥ p,
we can write s as s = uvxyz where |vxy| ≤ p, |vy| > 0, and uv i xy i z is in L for all i ≥ 0.
p
Consider the string 02 = uvxyz. Then f (i) = i|vy| + |uxz| is a power of 2 for all i ≥ 0.
Since f is a linear function, this would imply that 2k − 2k−1 ≤ |vy| ≤ p ∀ k ∈ Z≥0 , which is
definitely not true since this difference is not bounded. Thus we have a contradiction and it
must be that L is not a context free language.
3
5
(20 points) Let B be the language of all palindromes over {0, 1} containing an equal number
of 0s and 1s. Show that B is not context-free.
Suppose for sake of contradiction that B is context free, then B is pumpable. This implies that there exists a pumping length p such that for all s ∈ L where |s| ≥ p, we can write
s as s = uvxyz where |vxy| ≤ p, |vy| > 0, and uv i xy i z is in B for all i ≥ 0. Consider the
string s = 0p 12p 0p . We have the following possibilities for the contents of |vxy|:
1. |vxy| contains only 00 s or only 10 s. Then by pumping up v and y by one, we will have
an imbalance of 00 s and 10 s since |vy| > 0. Thus, the pumped string is no longer in B.
2. |vxy| contains a mix of 00 s and 10 s. Then, by pumping up by one v and y, we have two
subcases – (a) v or y contains a 0. Then, since |vxy| ≤ p, vxy cannot contain 00 s on
both sides of the middle portion 12p . Thus, by pumping down v and y by one we still
have a string of form 00 s followed by 10 s followed by 00 s, but the side which overlapped
with vxy will have less 00 s. Thus, the string is no longer a palindrome, and is therefore
no longer in B. (b) Neither v or y contains a 0 (e.g. there are 00 s in vxy, but they are
in the x portion). Then v or y must contain a 1 since |vy| > 0. Pumping up by one
will create an unequal total number of 00 s and 10 s in the string, so the pumped string
is no longer in B.
In either case we cannot pump the string s, so B is not pumpable. Thus, B is not a context
free language.
4