0% found this document useful (0 votes)
139 views10 pages

COMP 3803 - Solutions Assignment 3

The document contains solutions to an assignment involving context-free languages and grammars. It includes: 1) Context-free grammars that generate specific languages over the alphabets {0,1} and {a,b,c}. 2) Proofs that the union, concatenation, and Kleene star of two context-free languages are also context-free. 3) Descriptions and transition functions for deterministic and nondeterministic pushdown automata that accept two example languages.

Uploaded by

Nizam CAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
139 views10 pages

COMP 3803 - Solutions Assignment 3

The document contains solutions to an assignment involving context-free languages and grammars. It includes: 1) Context-free grammars that generate specific languages over the alphabets {0,1} and {a,b,c}. 2) Proofs that the union, concatenation, and Kleene star of two context-free languages are also context-free. 3) Descriptions and transition functions for deterministic and nondeterministic pushdown automata that accept two example languages.

Uploaded by

Nizam CAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

COMP 3803 — Solutions Assignment 3

Question 1: Write your name and student number.

Solution: Erling Haaland, 9


Question 2: Give context-free grammars that generate the following languages. For each
case, justify your answer.
(2.1) {02n 1n |n ≥ 0}. The set Σ of terminals is equal to {0, 1}.
(2.2) {w|w starts and ends with different symbols}. The set Σ of terminals is equal to
{0, 1}.
(2.3) {w| w is a palindrome}. The set Σ of terminals is equal to {0, 1}.
A palindrome is a string w having the property that w = wR , i.e., reading w from left to
right gives the same result as reading w from right to left. For example, the four binary
strings , 1, 0110, and 10101 are palindromes.
(2.4) {am bn cn |m ≥ 0, n ≥ 0}. The set Σ of terminals is equal to {a, b, c}.

Solution: First, we do
L1 = {02n 1n |n ≥ 0}.
Any string in L1 is either
• empty or

• starts with 00, followed by an arbitrary string in L1 , and ends with 1.


This leads to the context-free grammar G = (V, Σ, R, S), where V = {S}, Σ = {0, 1}, and
R consists of the two rules
S → |00S1.

Next, we do

L2 = {w|w starts and ends with different symbols}.

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

• starts with 1, is followed by an arbitrary binary string, and ends with 0.


This leads to the context-free grammar G = (V, Σ, R, S), where V = {S, X}, Σ = {0, 1},
and R consists of the rules
S → 0X1|1X0
X → |0X|1X

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,

• starts with 0, followed by a palindrome, and ends with 0, or

• starts with 1, followed by a palindrome, and ends with 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.

Solution: Since L is context-free, there is a context-free grammar G1 = (V1 , Σ, R1 , S1 )


that generates L. Similarly, since L0 is context-free, there is a context-free grammar G2 =
(V2 , Σ, R2 , S2 ) that generates L0 . We assume that V1 ∩ V2 = ∅. (If this is not the case, then
we rename the variables of G2 .)

First, we show that L ∪ L0 is context-free. Let G = (V, Σ, R, S) be the context-free


grammar, where

• V = V1 ∪ V2 ∪ {S}, where S is a new variable, which is the start variable of G,

• 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

• V = V1 ∪ V2 ∪ {S}, where S is a new variable, which is the start variable of G,

• 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.

Finally, we show that L∗ is context-free. Any string in L∗ is either

• empty or

• a string in L, followed by a string in L∗ .

Let G = (V, Σ, R, S) be the context-free grammar, where

• V = V1 ∪ {S}, where S is a new variable, which is the start variable of G,

• 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 }

may not generate L∗ . Here is an example:


The context-free grammar G1 = (V1 , Σ, R1 , S1 ), where V1 = {S1 }, Σ = {0, 1}, and
R1 = {S1 → 0S1 0|1} generates the language

L = {0n 10n : n ≥ 0}.

From the grammar G above, we can obtain the string 00:

S1 ⇒ 0S1 0 ⇒ 00.

However, this string 00 is not in L∗ .


Question 4: Give (deterministic or nondeterministic) pushdown automata that accept the
following languages. For each pushdown automaton, start by explaining the algorithm in
plain English, then mention the states that you are going to use, then explain the meaning
of these states, and finally give the list of instructions.
(4.1) {02n 1n |n ≥ 0}.
(4.2) {wwR |w ∈ {0, 1}∗ }.
(If w = w1 . . . wn , then wR = wn . . . w1 .)

Solution: For the language


{02n 1n |n ≥ 0}
we can use a deterministic pushdown automaton. The approach is as follows:

• Walk along the input string from left to right.

• 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.

• Tape alphabet Σ = {0, 1}.

• Stack alphabet Γ = {$, S}.

We use three states:

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.

The instructions are as follows.

• q0 0$ → q0 R$S (push S onto the stack)

• q0 0S → q0 RSS (push S onto the stack)

• q0 1$ → q0 N $

– Explanation: The input string starts with 1; loop forever and, thus, do not accept.

• q0 1S → q1 N S

– Explanation: We have reached the first 1. We switch to state q1 , do not move on


the input tape, and do not modify the stack.

• 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 

– Explanation: The first pop for the current 1.

5
• q1 $ → q1 N 

– Explanation: Make the stack empty, terminate, and accept.

• q1 S → q1 N S

– Explanation: Too many 0’s; loop forever and, thus, do not accept.

• q10 0$ → cannot happen

• q10 0S → cannot happen

• q10 1$ → q10 N $

– Explanation: Too many 1’s; loop forever and, thus, do not accept.

• q10 1S → q1 R

– Explanation: The second pop for the current 1.

• q10 $ → cannot happen

• q10 S → cannot happen

For the language


{wwR |w ∈ {0, 1}∗ }
we use a nondeterministic pushdown automaton. The approach is as follows:
• Walk along the input string from left to right.

• Guess when we enter the second half of the input string.

• 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.

• Tape alphabet Σ = {0, 1}.

• Stack alphabet Γ = {$, 0, 1}.

We will use two states:


• q: This is the start state. If we are in this state, then we have not guessed yet that we
have entered the second half 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)

• q0$ → q 0 R$0 (push, switch to q 0 )

• q1$ → qR$1 (push; stay in start state)

• q1$ → q 0 R$1 (push, switch to q 0 )

• q$ → qN  (input empty; accept)

• q00 → qR00 (push; stay in start state)

• q00 → q 0 R00 (push, switch to q 0 )

• q10 → qR01 (push; stay in start state)

• q10 → q 0 R01 (push, switch to q 0 )

• q0 → qN 0 (loop forever)

• q01 → qR10 (push; stay in start state)

• q01 → q 0 R10 (push, switch to q 0 )

• q11 → qR11 (push; stay in start state)

• q11 → q 0 R11 (push, switch to q 0 )

• q1 → qN 1 (loop forever)

• 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 }.

Solution: First, we prove that the language


A = {0n 1 02n 1 03n |n ≥ 0}
is not context-free.
Assume that A is context-free. By the pumping lemma, there is an integer p ≥ 1, such
that for all strings s ∈ A with |s| ≥ p, the following holds: We can write s = uvxyz, where
1. vy is non-empty,
2. vxy has length at most p,
3. the string uv i xy i z is in A, for all i ≥ 0.
Consider the pumping length p. We choose s = 0p 1 02p 1 03p . Then s is a string in A,
and the length of s is 6p + 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 contains a 1.
The string s0 = uvvxyyz contains at least three 1’s; thus, this string is not in A. But, by
the pumping lemma, s0 is contained in A. This is a contradiction.
Case 2: v or y overlaps the leftmost block of 0’s.
Since |vxy| ≤ p, the string y does not overlap the rightmost block of 0’s. Thus, since
|vy| ≥ 1, the string s0 = uvvxyyz starts with more than p many 0’s, and ends with exactly
3p many 0’s. Therefore, this string is not in A. But, by the pumping lemma, s0 is contained
in A. This is a contradiction.
Case 3: Neither v nor y contains a 1, and neither v nor y overlaps the leftmost block of 0’s.
The string s0 = uvvxyyz starts with exactly p many 0’s. The middle block of 0’s has
length more than 2p or the rightmost block of 0’s has length more than 3p. Therefore, this
string is not in A. But, by the pumping lemma, s0 is contained in A. This is a contradiction.

Next we prove that the language


B = { w ∈ {a, b, c}∗ | w contains more b’s than a’s and
w contains more c’s than a’s }
is not context-free.
Assume that B is context-free. By the pumping lemma, there is an integer p ≥ 1, such
that for all strings s ∈ B with |s| ≥ p, the following holds: We can write s = uvxyz, where

8
1. vy is non-empty,

2. vxy has length at most p,

3. the string uv i xy i z is in B, for all i ≥ 0.

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}

is a context-free language. By a symmetric argument, the language

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.

Solution: For the first part:

• L is context-free.

• L0 is context-free.

• Let L00 = L ∩ L0 .

• L00 = {an bn cn |n ≥ 0}.

• We have seen in class that L00 is not context-free.

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.

• By our assumption, both A and B are context-free.

• From Question 2: A ∪ B is context-free.

• By our assumption, A ∪ B is context-free.

• By De Morgan, the latter language is equal to A ∩ B.

10

You might also like