COMP 3803 - Solutions Assignment 3
COMP 3803 - Solutions Assignment 3
Solution: First, we do
L1 = {02n 1n |n ≥ 0}.
Any string in L1 is either
• empty or
Next, we do
I should have made it clear whether or not the empty string is contained in L2 .
First we consider the case when is not included in L2 . Any string in L2 either
• starts with 0, is followed by an arbitrary binary string, and ends with 1, or
1
Observe that from X, we can derive all binary strings (including the empty string). Thus,
from S, we can derive all strings in L2 (and nothing else).
If we assume that is contained in L2 , we get the rules
S → |0X1|1X0
X → |0X|1X
Next, we do
L3 = {w| w is a palindrome}.
Any string in L3 either
• is empty,
• is equal to 0,
• is equal to 1,
This leads to the context-free grammar G = (V, Σ, R, S), where V = {S}, Σ = {0, 1}, and
R consists of the rules
S → |0|1|0S0|1S1
Finally, we do
L4 = {am bn cn |m ≥ 0, n ≥ 0}.
Any string in L4
• starts with 0 or more a’s, followed by a string of the form bn cn , for some n ≥ 0.
This leads to the context-free grammar G = (V, Σ, R, S), where V = {S, X}, Σ = {a, b, c},
and R consists of the rules
S → AX
A → |aA
X → |bXc
Observe that from A, we can derive all strings of the form am for some m ≥ 0. From X, we
can derive all strings of the form bn cn , for some n ≥ 0. Therefore, from S, we can derive all
strings in L4 (and nothing else).
2
Question 3: Let L and L0 be context-free languages over the same alphabet Σ.
(3.1) Prove that the union L ∪ L0 of L and L0 is also context-free.
(3.2) Prove that the concatenation LL0 of L and L0 is also context-free.
(3.3) Prove that the star L∗ of L is also context-free.
• R = R1 ∪ R2 ∪ {S → S1 |S2 }.
From the start variable S, we can derive the strings S1 and S2 . From S1 , we can derive
all strings of L, whereas from S2 , we can derive all strings of L0 . Hence, from S, we can
derive all strings of L ∪ L0 . In other words, the grammar G generates the union of L and L0 .
Therefore, this union is context-free.
Next, we show that LL0 is context-free. Let G = (V, Σ, R, S) be the context-free grammar,
where
• R = R1 ∪ R2 ∪ {S → S1 S2 }.
From the start variable S, we can derive the string S1 S2 . From S1 , we can derive all strings
of L, whereas from S2 , we can derive all strings of L0 . Hence, from S, we can derive all
strings of the form uv, where u ∈ L and v ∈ L0 . In other words, the grammar G generates
the concatenation of L and L0 . Therefore, this concatenation is context-free.
• empty or
• R = R1 ∪ {S → |S1 S}.
3
From the start variable S, we can derive all strings S1n , where n ≥ 0. From S1 , we can derive
all strings of L. Hence, from S, we can derive all strings of the form u1 u2 . . . un , where n ≥ 0,
and each string ui (1 ≤ i ≤ n) is in L. In other words, the grammar G generates the star of
L. Therefore, L∗ is context-free.
By the way, the context-free grammar G = (V1 , Σ, R, S1 ), where
R = R1 ∪ {S1 → |S1 S1 }
S1 ⇒ 0S1 0 ⇒ 00.
• While reading 0’s: For each 0, push a symbol S onto the stack.
• While reading 1’s: For each 1, pop the top two symbols from the stack. Popping two
symbols is done in two steps: Pop the top symbol and do not move on the input tape;
then again pop the (new) top symbol and make one step to the right on the input tape.
4
• q0 : This is the start state. If we are in this state, then we are reading the block of 0’s.
The stack contains $ at the bottom; the number of S-symbols on the stack is equal to
the number of 0’s read so far.
• q1 : We have already seen a 1. If the current symbol on the input tape is a 1, then we
are going to do the first pop.
• q10 : The current symbol on the input tape is a 1; we are going to do the second pop.
• q0 1$ → q0 N $
– Explanation: The input string starts with 1; loop forever and, thus, do not accept.
• q0 1S → q1 N S
• q0 $ → q0 N
– Explanation: The input string is empty. We make the stack empty and, thus
terminate and accept.
• q0 S → q0 RSS
– Explanation: The input string is non-empty and only contains 0’s; loop forever
and, thus, do not accept.
• q1 0$ → q1 N $
– Explanation: There is a 0 to the right of a 1; loop forever and, thus, do not accept.
• q1 0S → q1 N S
– Explanation: There is a 0 to the right of a 1; loop forever and, thus, do not accept.
• q1 1$ → q1 N $
– Explanation: Too many 1’s; loop forever and, thus, do not accept.
• q1 1S → q10 N
5
• q1 $ → q1 N
• q1 S → q1 N S
– Explanation: Too many 0’s; loop forever and, thus, do not accept.
• q10 1$ → q10 N $
– Explanation: Too many 1’s; loop forever and, thus, do not accept.
• q10 1S → q1 R
• All symbols in the first half of the input string are pushed onto the stack.
• After we have entered the second half, we check if the contents of the stack is the same
as the remaining part of the input string.
• q 0 : We have guessed already that we have entered the second half of the input string.
The instructions are as follows.
6
• q0$ → qR$0 (push; stay in start state)
• q 0 0$ → q 0 N $ (loop forever)
• q 0 1$ → q 0 N $ (loop forever)
• q 0 $ → q 0 N (accept)
• q 0 00 → q 0 R (pop)
• q 0 10 → q 0 N 0 (loop forever)
• q 0 0 → q 0 N 0 (loop forever)
• q 0 01 → q 0 N 1 (loop forever)
• q 0 11 → q 0 R (pop)
• q 0 1 → q 0 N 1 (loop forever)
7
Question 5: Prove that the following languages are not context-free:
(5.1) {0n 1 02n 1 03n |n ≥ 0}.
(5.2)
{ w ∈ {a, b, c}∗ | w contains more b’s than a’s and
w contains more c’s than a’s }.
8
1. vy is non-empty,
Consider the pumping length p. We choose s = ap bp+1 cp+1 . Then s is a string in B, and
the length of s is 3p + 2, which is at least p. Thus, we can write s = uvxyz such that 1., 2.,
and 3. above hold.
Case 1: v or y overlaps the block of a’s.
Since |vxy| ≤ p, the string y does not overlap the block of c’s. Thus, since |vy| ≥ 1,
the string s0 = uvvxyyz contains at least p + 1 many a’s, and exactly p + 1 many c’s.
Therefore, this string is not in B. But, by the pumping lemma, s0 is contained in B. This is
a contradiction.
Case 2: Neither v nor y overlaps the block of a’s.
Consider the string s0 = uxz. Since |vy| ≥ 1, this string contains at most p many b’s
or at most p many c’s. On the other hand, it contains exactly p many a’s. Therefore, this
string is not in B. But, by the pumping lemma, s0 is contained in B. This is a contradiction.
Question 6: In Question 1, you have shown that
L = {am bn cn |m ≥ 0, n ≥ 0}
L0 = {am bm cn |m ≥ 0, n ≥ 0}
is context-free.
(6.1) Argue that the intersection of two context-free languages is not necessarily context-free.
(You may use any result that was proven in class.)
(6.2) Argue that the complement of a context-free language is not necessarily context-free.
• L is context-free.
• L0 is context-free.
• Let L00 = L ∩ L0 .
9
The second part is proved by contradiction. Thus, we assume that for any context-free
language A, the complement A is also context-free. Under this assumption, we will show that
the intersection of any two context-free languages is also context-free. This will contradict
the first part of the question.
Let A and B be two arbitrary context-free languages.
10