reference solutions

Introduction to Formal Language, Fall 2016
Due: 05-May-2016 (Thursday)
Homework 5 Solutions
Instructor: Prof. Wen-Guey Tzeng
Scribe: Amir Rezapour
1. Construct npda that accepts the following language on Σ = {a, b, c}.
L = {w : na (w) < nb (w)}.
Answer.
An NPDA that accepts L is M = (Q, Σ, Γ, δ, q0 , z, F ), where Q = {q0 , q1 }, Σ =
{a, b, c}, Γ = {0, 1, z}, F = {q1 }, and the transition function δ is represented as the
following graph
The idea is the same as an example in pushdown automata slide (L = {w : na (w) =
nb (w)}) which accepts the strings with equal number of as and bs, but now it only
accepts if there is a b on top of the stack.
2
2. Find an npda on Σ = {a, b, c} that accepts the language
L = {w1 cw2 : w1 , w2 ∈ {a, b}∗ , w1 6= w2R }.
Answer.
An NPDA that accepts L is M = (Q, Σ, Γ, δ, q0 , z, F ), where Q = {q0 , q1 , q2 }, Σ =
{a, b, c}, Γ = {0, 1, z}, F = {q2 }, and the transition function δ is represented as the
following graph
2
1
3. What language is accepted by the pda
M = ({q0 , q1 , q2 , q3 , q4 , q5 }, {a, b}, {0, 1, a, z}, δ, z, q0 , {q5 }),
with
δ(q0 , b, z) = {(q1 , 1z)},
δ(q1 , b, 1) = {(q1 , 11)},
δ(q2 , a, 1) = {(q3 , λ)},
δ(q3 , a, 1) = {(q4 , λ)},
δ(q4 , a, z) = {(q4 , z), (q5 , z)}?
Answer.
L = ∅, because the transition function would never reach the final state.
4. Construct an npda corresponding to the grammar
S → aABB|aAA,
A → aBB|a,
B → bBB|A.
2
2
Answer.
First, we convert the cdf to the corresponding GNF
S → aABB|aAA,
A → aBB|a,
B → bBB|aBB|a.
We construct npda M = ({q0 , q1 , q2 }, {a, b}, {S, A, B, Z}, δ, q0 , Z, {q2 }) by the following steps:
(a) Initial: δ(q0 , λ, z) = {(q1 , Sz)}
(b) Process the input:
• For S → aABB|aAA : δ(q1 , a, S) = {(q1 , ABB), (q1 , AA)}
• For A → aBB|a : δ(q1 , a, A) = {(q1 , BB), (q1 , λ)}
• For B → bBB|aBB|a : δ(q1 , b, B) = {(q1 , BB)}, δ(q1 , a, B) = {(q1 , BB), (q1 , λ)}
(c) In the end: δ(q1 , λ, z) = {(q2 , λ)}
2
5. Show that the following language is not context-free.
L = {w ∈ {a, b, c}∗ : na (w) = nb (w) ≤ nc (w)}.
Answer. Let L0 = L ∩ L(a∗ b∗ c∗ ) = {ai bi ck |i ≤ k}. Since context-free languages are
closed under interaction with regular languages. We only show L0 to be not contextfree. We pick a string w = am bm cm ∈ L0 , m ∈ N . There are many ways to decompose
w as w = uvxyz with |vxy| ≤ m and |vy| ≥ 1. However, for all of them have a winning
countermove such that uv i xy i z ∈
/ L:
• v = ak , y = as : uv 0 xy 0 z = am−k−s bm cm ∈
/ L.
• v = ak , y = ar bs : uv 2 xy 2 z = am+2k+r bm+s ar bs cm ∈
/ L.
• v = ak , y = bl : uv 2 xy 2 z = am+k bm+l cm ∈
/ L.
3
• v = ar bs , y = bt : uv 0 xy 0 z = am−r bm−s−t cm ∈
/ L.
• v = br , y = bs : uv 0 xy 0 z = am bm−r−s cm ∈
/ L.
• v = bs , y = br ct : uv 0 xy 0 z = am bm−s−r cm−t ∈
/ L.
• v = bs , y = cr : uv 0 xy 0 z = am bm−s cm−r ∈
/ L.
• v = bs cr , y = ct : uv 0 xy 0 z = am bm−s cm−r−t ∈
/ L.
• v = cr , y = cs : uv 0 xy 0 z = am bm cm−r−s ∈
/ L.
Therefore, by the pumping lemma for context-free languages, L is not context-free. 2
6. Show that the following language on Σ = {a, b, c} is not context-free.
L = {an bj ck : k = jn}.
Answer.
2
We pick a string w = am bm cm ∈ L, m ∈ N . There are many ways to decompose w
as w = uvxyz with |vxy| ≤ m and |vy| ≥ 1. However, for all of them have a winning
countermove such that uv i xy i z ∈
/ L:
2
• v = as , y = ar : uv 0 xy 0 z = am−r−s bm cm ∈
/ L.
2
• v = as , y = ar bt : uv 0 xy 0 z = am−s−r bm−t cm ∈
/ L.
2
• v = as , y = br : uv 0 xy 0 z = am−s bm−r cm ∈
/ L.
2
• v = as br , y = bt : uv 0 xy 0 z = am−s bm−r−t cm ∈
/ L.
2
• v = bs , y = br : uv 0 xy 0 z = am bm−r−s cm ∈
/ L.
• v = bs , y = br ct : uv 0 xy 0 z = am bm−s−r cm
m2 −r
• v = bs , y = cr : uv 0 xy 0 z = am bm−s c
• v=
bs cr , y
=
• v=
cr , y
cs
=
ct
uv 0 xy 0 z
:
:
uv 0 xy 0 z
=
=
2 −t
∈
/ L.
2
am bm−s cm −r−t
2
am bm cm −r−t
∈
/ L.
∈
/ L.
∈
/ L.
Therefore, by the pumping lemma for context-free languages, L is not context-free. 2
7. Determine whether or not the following languages is context free, and prove your
answer.
L = {an bn cj : n ≤ j}.
Answer.
We pick a string w = am bm cm ∈ L, m ∈ N . There are many ways to decompose w
as w = uvxyz with |vxy| ≤ m and |vy| ≥ 1. However, for all of them have a winning
countermove such that uv i xy i z ∈
/ L:
• v = ar , y = as : uv 0 xy 0 z = am−r−s bm cm ∈
/ L.
• v = ar , y = as bt : uv 2 xy 2 z = am+2r+s bm+t as bt cm ∈
/ L.
• v = ar , y = bs : uv 2 xy 2 z = am+r bm+s cm ∈
/ L.
• v = ar bs , y = bt : uv 2 xy 2 z = am+r bm+s+t cm ∈
/ L.
4
• v = br , y = bs : uv 0 xy 0 z = am bm−r−s cm ∈
/ L.
• v = br , y = bs ct : uv 0 xy 0 z = am bm−s−r cm−t ∈
/ L.
• v = br , y = cs : uv 0 xy 0 z = am bm−r cm−s ∈
/ L.
• v = br cs , y = ct : uv 0 xy 0 z = am bm−r cm−s−t ∈
/ L.
• v = cr , y = cs : uv 0 xy 0 z = am bm cm−r−t ∈
/ L.
Therefore, by the pumping lemma for context-free languages, L is not context-free. 2
8. Show that the family of context-free languages is not closed under difference in general,
but is closed under regular difference, that is, if L1 is context-free and L2 is regular,
then L1 − L2 is context-free.
Answer.
The answer contains the following two parts:
• If L3 = L1 − L2 is context-free for context-free L1 and L2 , then L1 ∩ L2 =
L1 − (L1 − L2 ) = L1 − L3 is also context-free. This is a contradiction. Thus,
L1 − L2 is not necessarily context-free for context-free L1 and L2 .
• Let L1 be a context-free language and L2 a regular language. By the closure
property of regular languages, we know that L¯2 is also regular. By theorem,
we know that L1 ∩ L2 is context-free by the closure property under regular
intersection. Therefore, L1 − L2 = L1 ∩ L¯2 is also context-free by the closure
property under regular intersection.
2
9. Show that the following language is context-free.
L = {w ∈ {a, b}∗ : na (w) = nb (w); w does not contain a substring aab}.
Answer.
Let L1 = {w ∈ {a, b}∗ : na (w) = nb (w)} and L2 = {w ∈ {a, b}∗ : w contains ’aab’ as
a string}. Since L1 is context-free and L2 is regular, L = L1 − L2 is context-free by
the result in the previous problem.
2
5