Homework 5 Solutionswgtzeng/courses/FL2016...Introduction to Formal Language, Fall 2016 Due:...

5

Click here to load reader

Transcript of Homework 5 Solutionswgtzeng/courses/FL2016...Introduction to Formal Language, Fall 2016 Due:...

Page 1: Homework 5 Solutionswgtzeng/courses/FL2016...Introduction to Formal Language, Fall 2016 Due: 05-May-2016 (Thursday) Homework 5 Solutions Instructor: Prof. Wen-Guey Tzeng Scribe: Amir

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 thefollowing 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 onlyaccepts if there is a b on top of the stack. 2

2. Find an npda on Σ = {a, b, c} that accepts the language

L = {w1cw2 : w1, w2 ∈ {a, b}∗, w1 6= wR2 }.

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 thefollowing graph

2

1

Page 2: Homework 5 Solutionswgtzeng/courses/FL2016...Introduction to Formal Language, Fall 2016 Due: 05-May-2016 (Thursday) Homework 5 Solutions Instructor: Prof. Wen-Guey Tzeng Scribe: Amir

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

4. Construct an npda corresponding to the grammar

S → aABB|aAA,A→ aBB|a,B → bBB|A.

2

Page 3: Homework 5 Solutionswgtzeng/courses/FL2016...Introduction to Formal Language, Fall 2016 Due: 05-May-2016 (Thursday) Homework 5 Solutions Instructor: Prof. Wen-Guey Tzeng Scribe: Amir

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 follow-ing 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, λ)}• ForB → 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 L′ = L ∩ L(a∗b∗c∗) = {aibick|i ≤ k}. Since context-free languages areclosed under interaction with regular languages. We only show L′ to be not context-free. We pick a string w = ambmcm ∈ L′, m ∈ N . There are many ways to decomposew as w = uvxyz with |vxy| ≤ m and |vy| ≥ 1. However, for all of them have a winningcountermove such that uvixyiz /∈ L:

• v = ak, y = as : uv0xy0z = am−k−sbmcm /∈ L.

• v = ak, y = arbs : uv2xy2z = am+2k+rbm+sarbscm /∈ L.

• v = ak, y = bl : uv2xy2z = am+kbm+lcm /∈ L.

3

Page 4: Homework 5 Solutionswgtzeng/courses/FL2016...Introduction to Formal Language, Fall 2016 Due: 05-May-2016 (Thursday) Homework 5 Solutions Instructor: Prof. Wen-Guey Tzeng Scribe: Amir

• v = arbs, y = bt : uv0xy0z = am−rbm−s−tcm /∈ L.

• v = br, y = bs : uv0xy0z = ambm−r−scm /∈ L.

• v = bs, y = brct : uv0xy0z = ambm−s−rcm−t /∈ L.

• v = bs, y = cr : uv0xy0z = ambm−scm−r /∈ L.

• v = bscr, y = ct : uv0xy0z = ambm−scm−r−t /∈ L.

• v = cr, y = cs : uv0xy0z = ambmcm−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 = {anbjck : k = jn}.

Answer.We pick a string w = ambmcm

2 ∈ L, m ∈ N . There are many ways to decompose was w = uvxyz with |vxy| ≤ m and |vy| ≥ 1. However, for all of them have a winningcountermove such that uvixyiz /∈ L:

• v = as, y = ar : uv0xy0z = am−r−sbmcm2/∈ L.

• v = as, y = arbt : uv0xy0z = am−s−rbm−tcm2/∈ L.

• v = as, y = br : uv0xy0z = am−sbm−rcm2/∈ L.

• v = asbr, y = bt : uv0xy0z = am−sbm−r−tcm2/∈ L.

• v = bs, y = br : uv0xy0z = ambm−r−scm2/∈ L.

• v = bs, y = brct : uv0xy0z = ambm−s−rcm2−t /∈ L.

• v = bs, y = cr : uv0xy0z = ambm−scm2−r /∈ L.

• v = bscr, y = ct : uv0xy0z = ambm−scm2−r−t /∈ L.

• v = cr, y = cs : uv0xy0z = ambmcm2−r−t /∈ 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 youranswer.

L = {anbncj : n ≤ j}.

Answer.We pick a string w = ambmcm ∈ L, m ∈ N . There are many ways to decompose was w = uvxyz with |vxy| ≤ m and |vy| ≥ 1. However, for all of them have a winningcountermove such that uvixyiz /∈ L:

• v = ar, y = as : uv0xy0z = am−r−sbmcm /∈ L.

• v = ar, y = asbt : uv2xy2z = am+2r+sbm+tasbtcm /∈ L.

• v = ar, y = bs : uv2xy2z = am+rbm+scm /∈ L.

• v = arbs, y = bt : uv2xy2z = am+rbm+s+tcm /∈ L.

4

Page 5: Homework 5 Solutionswgtzeng/courses/FL2016...Introduction to Formal Language, Fall 2016 Due: 05-May-2016 (Thursday) Homework 5 Solutions Instructor: Prof. Wen-Guey Tzeng Scribe: Amir

• v = br, y = bs : uv0xy0z = ambm−r−scm /∈ L.

• v = br, y = bsct : uv0xy0z = ambm−s−rcm−t /∈ L.

• v = br, y = cs : uv0xy0z = ambm−rcm−s /∈ L.

• v = brcs, y = ct : uv0xy0z = ambm−rcm−s−t /∈ L.

• v = cr, y = cs : uv0xy0z = ambmcm−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 closureproperty 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 regularintersection. Therefore, L1 − L2 = L1 ∩ L̄2 is also context-free by the closureproperty 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’ asa string}. Since L1 is context-free and L2 is regular, L = L1 − L2 is context-free bythe result in the previous problem. 2

5