Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Exercise 1.

(e is epsilon)*

a) L = { w ∈ {a, b}* | w contains 1 mod 3 b’s }


S→ aS | bE
E→ aE | bR |e
R→ aR | bL
L→ aL | bE

b) L = { w ∈ {a, b, c}* | w is of odd length, and its middle character is c }


S→ ESE|c
E→ a|b|c

c) L = { aj bm ck | j + k = m }
S → L R
L→ aLb | e
R→ bRc |e

d) L = { w ∈ {a, b}* | w contains the same number of a’s and b’s }


S→ aSb | bSa |e

Exercise 2.
e, S / S a
1. e, R / R a
e, S / E b
e, R / L b
e, E / E a
e, L / L a
e, E / R b
e, L / E b
e, E / E e

e, e / $S e, $ / e
q0 q1 q2

a, a / e
b, b / e

However, this language is regular, so it does not require stack.


a
a q2
b

q0
b

a
b
DFA q1

e, S / E S E
2. e, S / c
e, E / a
e, E / b
e, E / c

e, e / $S e, $ / e
q0 q1 q2

a, a / e
b, b / e
e, S / R L c, c / e
3. e, R / c R b
e, L / b R a
e, R / e , c/e
e, L / e

e, e / $S e, $ / e
q0 q1 q2

a, a / e
b, b / e
c, c / e

, c/e
4.
e, S / b S a
e, S / a S b
e, S / e

e, e / $S e, $ / e
q0 q1 q2
, c/e
a, a / e
b, b / e

Exercise 3.
, c/e
a) { an b m | 0 < m < n }
Context-free
S → aa R b
R→ aR | aRb | e
Assume L = { an bm | 0 < m < n }is regular. Thus, it is pumpable by the pumping
lemma and has the pumping length p. Suppose w = ap+1bp and we know that w is in
L and |w| = 2p+1 >= p. Suppose we divide up w into xyz such that: the length of xy
is at most p and xy contains only a's, but maybe not all of the a's, y will contain at
least one a and z will contain the remaining a's, and all of the b’s. If we pump down
the string xy^0, then it also should be in L by the pumping lemma. However, when
we pump down, we see that we get ap+1-ybp which contains more a’s than b’s or the
same number of a’s as b’s in the string. Thus, the string is not in L. Thus, L is not
pumpable and not regular.

b) { 𝑤 ∈ {a, b, c} ∗ | 𝑤 has the same number of a’s, b’s, and c’s }


Not context-free.
Assume L = {𝑤 ∈ {a, b, c} ∗ | 𝑤 has the same number of a’s, b’s, and c’s}is
context-free. Thus, it is pumpable by the pumping lemma and has the pumping
length p. Suppose w = apbpcp and we know that w is in L and |w| = 3p >= p.
Suppose we divide up w into uvxyz such that: the length of vxy is at most p and v
and y are non-empty. Thus, vxy cannot contain all of the three characters. If we
pump down to the string uv0xy0z, then it also should be in L by the pumping
lemma. However, when we pump down, we see that the resulting string will contain
a smaller number of one or two characters than the others and at least the number of
one character will remain the same. Thus, the string is not in L. Thus, L is not
pumpable and not context-free.

c) { a2^n | 𝑛 ≥ 0 }
Not context-free.
Assume L = {a^𝑘 | 𝑘 = 2^𝑛 , 𝑛 ∈ 𝑁} is context-free. Thus, it is pumpable by the
pumping lemma and has the pumping length p. Suppose w = a^(2^p) and we know
that w is in L and |w| = 2^p >= p. Suppose we divide up w into uvxyz such that: the
length of vxy is at most p and where v and y are non-empty. If we pump up to the
string uv^2xy^2z , we will get a string with greater number of a’s. Therefore, the
length of this string should be >= 2^(p+1) so that it is in L. Suppose v and y contains
k number of a’s. Then, we have that:
2^p < 2^p + k <= 2^p + p
2^p + p < 2^(p+1)
Therefore, 2^p < 2^p + k < 2^(p+1), which means that the length is now between
two consecutive powers of 2. Thus, the string is not in L. Thus, L is not pumpable
and not context-free.
.

d) { 𝑤𝑤reverse𝑤 | 𝑤 ∈ {0,1} ∗ }
Not context-free.
Assume L = { 𝑤𝑤reverse𝑤 | 𝑤 ∈ {0,1} ∗ }is context-free. Thus, it is pumpable by the
pumping lemma and has the pumping length p. Suppose w = 01p001p001p0 and we
know that w is in L and |w| = 3p+3 >= p. Suppose we divide up w into uvxyz such
that: the length of vxy is at most p and v and y are non-empty. Thus, vxy can
contain at most two of the 0’s. If we pump down to the string uv0xy0z, then it also
should be in L by the pumping lemma. However, when we pump down, we see that
the resulting string will contain a smaller number of 0’s and we cannot divide this
string into three parts with same number of 1’s and 0’s. In the second case, we can
divide w into uvxyz such that v and y will contain only a’s and at least one a. If we
pump down this string, we can get the resulting strings 01(p-v)001(p-y)001p0 or
01p-v-y001p001p0 or 01p001p-v-y001p0. These strings cannot be in the language,
since they cannot be divided into three parts with same number of 1’s and 0’s. Thus,
L is not pumpable and not context-free.

e) language of palindromes over {a, b} that have the same number of a’s and b’s.
Not context-free
Assume this language is context-free. Thus, it is pumpable by the pumping lemma
and has the pumping length p. Suppose w = apb2pap and we know that w is in L and
|w| = 4p >= p. Suppose we divide up w into uvxyz such that: the length of vxy is at
most p and v and y are non-empty. Here, in the first case, vxy may contain only a’s.
If we pump down the string uv0xy0z, the resulting string will contain a smaller
number of a’s and the same number of b’s. Therefore, this string is not in L. In the
second case, vxy may contain some k number of a’s and some r number of b’s,
again v and y are non-empty and |vxy| <= p. If we pump down this string, then we
might get strings ap-kb2p-rap or apb2p-rap-k. We can see that these strings cannot be
palindromes, since number of a’s on the left-side is not equal to the a’s on the right-
side. Therefore, these strings are also not in the language. Finally, in the third case,
vxy may contain only b’s, but not all b’s. If we pump down this string, then we can
get the string apb2p-v-yap. Now the number of b’s is less than before, so we cannot
divide the string into two parts that will contain the same number of a’s and b’s, the
number of b’s will be less. Thus, this string is also not in L. Therefore, the language
is not pumpable and context-free.

f) { wv | w,v ∈ {a, b, c }* such that w and v have the same length.


Regular language.
RegExp: (aa | ab | ac | bb | ba | bc | cc | ca | cb)*

g) { w$x | 𝑤, 𝑥 ∈ {0,1} ∗ , and w is a substring of x } // here, $ is a symbol in the


alphabet.
Not context-free.
Assume L = { w$x | 𝑤, 𝑥 ∈ {0,1} ∗ , and w is a substring of x } is context-free.
Thus, it is pumpable by the pumping lemma and has the pumping length p. Suppose
w = 0p1p$0p1p and we know that w is in L and |w| = 4p >= p. Suppose we divide
up w into uvxyz such that: the length of vxy is at most p and v and y are non-empty.
Let’s consider cases, when vxy contains characters before $, vxy contains characters
after $, and vxy contains 1’s before $ and 0’s after $, v contains $ or y contains $.
1. vxy contains 0’s before $. If we pump it down, we’ll decrease the number of
0’s in the left of the symbol $ without changing the number of 0’s in the right of the
symbol $. Therefore, the left string will not be a substring of the right string, and the
resulting word will not be in the language.
2. vx is contains of 0’s, but y includes some 0’s and some 1’s from the left string.
If we pump it up, we will obtain a word uv42xy42z where the left string may contain
some symbols, a substring of the form 10 which cannot be a substring of the right
string. Therefore, the string is not in the language.
3. v contains of 0’s, x contains some 0’s and 1’s, and y contains only 1’s to the
left of $. If we pump it up, we obtain a string where the left string is of the form
0p+421p+42 for k > 0. This string cannot be a substring of the string of the rightmost
string which was not changed. Therefore, the word is not in the language.
4. v contains only 1’s from the left string, x contains the symbol $ and y contains
0’s from the right string. If we pump up, we obtain a string where the left string
contains more a’s than in the right. The resulting string cannot be in the language.
5. v or y contains the symbol #. If we pump it up, we obtain a word with more
than one symbol #, which cannot be in the language.

Same operation can be applied to vyx with characters chosen from the right string.
Therefore, since none of the strings are not in the language, the language is not
pumpable and thus not context-free.

You might also like