0% found this document useful (0 votes)
14 views

HW 2

Uploaded by

Breeha Qasim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

HW 2

Uploaded by

Breeha Qasim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Homework 2: Pumping Lemma, Context Free Languages

CS 212 Nature of Computation


Habib University
Homework 02 22
Fall 2023

1. Consider the grammar containing the following productions.

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

(c) 5 points Argue whether this grammar is ambiguous.

2. 5 points Given an alphabet, Σ, consider the operation, f : Σ∗ → Σ∗ , defined as follows.

f (w) = wn ◦ wn−1 ◦ wn−2 ◦ . . . w1 , where w = w1 ◦ w2 ◦ w3 ◦ . . . ◦ wn .

f is extended to apply to a given language, L, as follows.

f (L) = {f (w) | w ∈ L}.

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}

And the language can be defined as


X
L(G) = {a∗ (ba)∗ | a, b ∈ }

(b) The parse tree for the string aaaba is as follows:


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

Problem 2: We are given the operation, f : Σ∗ → Σ∗ , defined as follows:

f (w) = wn ◦ wn−1 ◦ wn−2 ◦ . . . w1 , where w = w1 ◦ w2 ◦ w3 ◦ . . . ◦ wn .

Extending f to a language L, we have: f (L) = {f (w) | w ∈ L}


Essentially, f reverses the order of the characters in a string. Extending f to L means
that we apply f to every string in L, then f (L) consists of all the strings that we get by
reversing each string in L.
P
Given that L is a context-free language over an alphabet and its context-free grammar
G, we can assume, without loss of generality that L is in Chomsky Normal Form (CNF) -
if not, then we can convert it to CNF. Then all production rules are of the form A → BC,
or A → a, where A, B, and C are non-terminal symbols and a is a terminal symbol.
Then we construct a new grammar G′ that generates the language f (L) which consists of
the reverse of every string in L. The non-terminal symbols of G′ are the same as those of
G, and the start symbol of G′ is the same as that of G. The production rules of G′ are as
follows:

• For each rule of the form A → BC in G, we add the rule A → CB to G′

• For each rule of the form A → a in G, we add the rule A → a to G′

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.

Problem 4: We are given the language, L = {0i 1j 2k | i, j, k ≥ 0, k ̸= 1 or i = j}.


(a) We can prove that L is not regular without the use of pumping lemma, by the inter-
section operation on L and any other language that is regular.
Regular languages are closed under intersection, that is,

L1 is regular ∧ L2 is regular =⇒ L1 ∩ L2 is regular (1)

. Then by the contrapositive,

L1 ∩ L2 is not regular =⇒ L1 is not regular ∨ L2 is not regular (2)

Let us assume that the languages L = {0i 1j 2k | i, j, k ≥ 0, k ̸= 1 or i = j} and


L1 = {0∗ 1∗ 2} are regular. For L1 , the following finite automata can be constructed:
0 1

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

Then xy = 0p , y = 0m , and x = 0p−m . Pumping y into the string:

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

You might also like