HW 2
HW 2
S → XaY
X → aX | ϵ
Y → aY | bY | ϵ
(a) 5 points Give a formal definition of the language generated by this grammar.
(b) 5 points Draw the parse tree under this grammar for the string: aaaba.1
Prove or disprove the claim that the class of context-free languages is closed under f .
3. 5 points Prove or disprove the claim that the language, L = {x#y | x, y ∈ {0, 1}∗ }, is
context-free.
4. We are given the language, L = {0i 1j 2k | i, j, k ≥ 0, k ̸= 1 or i = j}.
(a) 5 points Without using the pumping lemma, argue that L is not regular.
(b) 5 points Show that the pumping lemma for regular languages applies to L.
(c) 5 points What conclusion can you draw about the pumping lemma from the above ob-
servations?
1 See this post on using the forest package to draw a parse tree.
1
CS 212, Fall 2023 HW 2: Pumping Lemma, Context Free Languages Fall 2023
Solution:
Problem 1:
(a) For the language L, our components for its grammar becomes:
• N is a non empty set of non terminal symbols; N = {S, X, Y }
•
P P
is a finite set of terminal symbols; = {ε, a}
• P is the set of grammar rules; P = {S → XaY, X → aX | ε, Y → aY | bY | ε}
• S ∈ N is the start symbol; S = S
Formally the grammar for this language is,
G = {{S, X, Y }, {ε, a}, {S → XaY, X → aX | ε, Y → aY | bY | ε}, S}
X Y
X Y
X Y
a a ε a b a ε
(c) This grammar is ambiguous because the string aaaba can be parsed in different ways.
One is as shown in the previous part and the other is as follows:
S
X Y
X Y
a ε a a b a ε
Therefore, the grammar is ambiguous since we have found two different parse trees
for the same string.
Page 2 of 5
CS 212, Fall 2023 HW 2: Pumping Lemma, Context Free Languages Fall 2023
The reversal of order in the production rules is applied consistently throughout the gram-
mar, therefore, this construction ensures that every string generated by G will be generated
in reverse by G′ .
Since we have constructed a context-free grammar for the language f (L), we can conclude
that f (L) is context-free.
Hence proved that the class of context-free languages is closed under f .
Page 3 of 5
CS 212, Fall 2023 HW 2: Pumping Lemma, Context Free Languages Fall 2023
Problem 3: We are given the language, L = {x#y | x, y ∈ {0, 1}∗ }. This language
consists of all the strings over the alphabet {0, 1, #} where # appears exactly once, and
separates any sequence of 0’s and 1’s from any other sequence of 0’s and 1’s.
We can prove this language is context-free by providing a context-free grammar (CFG)
that generates this language:
S −→ X#Y
X −→ 0X | 1X | ε
Y −→ 0Y | 1Y | ε
Here the grammar can be formally defined as
G = {{S, X, Y }, {ε, 0, 1, #}, {S → X#Y, X → 0X | 1X | ε, Y → 0Y | 1Y | ε}, S}
Since we can construct a CFG for this language, we can conclude that this language is
context-free.
q0 q1 q3
1 0
2
2 0, 1, 2
q2
Then by the closure of regular languages under intersection, we know that L ∩ L1 will
also be regular. Then L ∩ L1 = L2 = {0n 1n 2 | n ≥ 0}(we take 0 and 1 both to the
power of n, since i = j, therefore, we have equal number of 0s and 1s). Suppose that
Page 4 of 5
CS 212, Fall 2023 HW 2: Pumping Lemma, Context Free Languages Fall 2023
L2 is also regular, then the pumping lemma should hold. Consider a string s = 0p 1p 2.
Since s ∈ L2 , and |s| ≥ p, the pumping lemma guarantees that s can be split into
three pieces; s = xyz such that:
1. for each i ≥ 0, xy i z ∈ L2 .
2. |y| > 0
3. |xy| ≤ p
Condition 3 of the pumping lemma guarantees that y can only consist of 0s;
- y cannot be a 1, as that would imply x = 0p , then |xy| > p, furthermore, then
the first part of the string would more 1’s than the second part, which is a
contradiction
- By the same argument as above, y cannot be a combination of a 1’s followed by
a 2 either as that would imply x = 0p , then |xy| > p
xyyz = 0p 0m 1p 2
xy 2 z = 0p+m 1p 2
This shows that there will inevitably be more 0s than 1s since p + m > p, then
xy 2 z ∈
/ L2 . Hence we arrive at a contradiction as the pumping lemme does not hold.
Then L2 is not regular. We know that L1 is regular, therefore, by (2), L is not regular.
Hence proved.
■
Page 5 of 5