DCP3122 Introduction to Formal Languages, Spring 2015 5-May-2015 Homework 5 - Solution Instructor: Prof. Wen-Guey Tzeng Due: 18-May-2015 1. Given Σ = {a, b, c}, find an NPDA that accepts L = {an bn+m cm : n ≥ 0, m ≥ 1}. Ans. An NPDA that accepts L is M = (Q, Σ, Γ, δ, q0 , z, F ), where Q = {q0 , q1 , . . . , q5 }, Σ = {a, b, c}, Γ = {0, 1, z}, F = {q5 }, and the transition function δ is represented as the following graph 2. Given Σ = {a, b, c}, find an NPDA that accepts L = {w1 cw2 : w1 , w2 ∈ {a, b}∗ , w1 6= w2R }. Ans. 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 3. What language is accepted by the PDA M = ({q0 , q1 , . . . , q5 }, {a, b}, {0, 1, a, z}, δ, z, q0 , {q5 }), 1-1 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)}? Ans. L = ∅, because the transition function would never reach the final state. 4. Construct an NPDA that accepts the language generated by the grammar S → aSbb|aab. Ans. The Greibach normal form of the grammar is S → aSBB|aAB, B → b, A → a. An NPDA that accepts L is M = (Q, Σ, Γ, δ, q0 , z, F ), where Q = {q0 , q1 , q2 }, Σ = {a, b}, Γ = {S, A, B, z}, F = {q2 }, and the transition function δ is represented as the following graph 5. Find a context-free grammar that generates the language accepted by the NPDA M = ({q0 , q1 }, {a, b}, {A, z}, δ, q0 , z, {q1 }), with transitions δ(q0 , a, z) = {(q0 , Az)}, δ(q0 , b, A) = {(q0 , AA)}, δ(q0 , a, A) = {(q1 , λ)}. Ans. We first transform the NPDA into a new one that satisfies the following two requirements: (1) It has a single final state qf that is entered if and only if the stack is empty; (2) While a ∈ Σ ∪ {λ}, all transitions must have the form δ(qi , a, A) = {c1 , c2 , . . . , cn }, where ci = (qj , λ) or ci = (qj , BC). The NPDA satisfies requirement (2), but not (1). To satisfy the latter, we introduce a new final state q2 so that Qnew = {q0 , q1 , q2 }, Fnew = {q2 }, and δnew is δ(q0 , a, z) = {(q0 , Az)}, δ(q0 , b, A) = {(q0 , AA)}, δ(q0 , a, A) = {(q1 , λ)}, δ(q1 , λ, A) = {(q1 , λ)}, δ(q1 , λ, z) = {(q2 , λ)}. 1-2 The last three transitions yield the corresponding productions (q0 Aq1 ) → a, (q1 Aq1 ) → λ, (q1 zq2 ) → λ. From the first two transitions we get the set of productions (q0 zq0 ) → a(q0 Aq0 )(q0 zq0 )|a(q0 Aq1 )(q1 zq0 )|a(q0 Aq2 )(q2 zq0 ), (q0 zq1 ) → a(q0 Aq0 )(q0 zq1 )|a(q0 Aq1 )(q1 zq1 )|a(q0 Aq2 )(q2 zq1 ), (q0 zq2 ) → a(q0 Aq0 )(q0 zq2 )|a(q0 Aq1 )(q1 zq2 )|a(q0 Aq2 )(q2 zq2 ), (q0 Aq0 ) → a(q0 Aq0 )(q0 Aq0 )|a(q0 Aq1 )(q1 Aq0 )|a(q0 Aq2 )(q2 Aq0 ), (q0 Aq1 ) → a(q0 Aq0 )(q0 Aq1 )|a(q0 Aq1 )(q1 Aq1 )|a(q0 Aq2 )(q2 Aq1 ), (q0 Aq2 ) → a(q0 Aq0 )(q0 Aq2 )|a(q0 Aq1 )(q1 Aq2 )|a(q0 Aq2 )(q2 Aq2 ). To simplify the transitions, we remove the useless variables: • A variable that does not occur on the left side of any production: (q1 zq0 ), (q1 zq1 ), (q1 zq2 ), (q2 zq0 ), (q2 zq1 ), (q2 zq2 ), (q1 Aq0 ), (q1 Aq1 ), (q1 Aq2 ), (q2 Aq0 ), (q2 Aq1 ), and (q2 Aq2 ). • Non-reachable path: None. The result grammar is (q0 Aq1 ) → a, (q1 Aq1 ) → λ, (q1 zq2 ) → λ, (q0 zq0 ) → a(q0 Aq0 )(q0 zq0 ), (q0 zq1 ) → a(q0 Aq0 )(q0 zq1 ), (q0 zq2 ) → a(q0 Aq0 )(q0 zq2 ), (q0 Aq0 ) → a(q0 Aq0 )(q0 Aq0 ), (q0 Aq1 ) → a(q0 Aq0 )(q0 Aq1 ), (q0 Aq2 ) → a(q0 Aq0 )(q0 Aq2 ), with the start variable is (q0 zq2 ). 6. Show that the language L = {w ∈ {a, b, c}∗ : na (w) = nb (w) ≤ nc (w)} is not context-free. Ans. 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 = ak , y = ak : uv 0 xy 0 z = am−2k bm cm ∈ / L. • v = ak , y = ak bk : uv 0 xy 0 z = am−2k bm−k cm ∈ / L. • v = ak , y = b` : uv 0 xy 0 z = am−k bm−` cm ∈ / L. • v = ak bk , y = bk : uv 0 xy 0 z = am−k bm−2k cm ∈ / L. • v = bk , y = bk : uv 0 xy 0 z = am bm−2k cm ∈ / L. • v = bk , y = bk ck : uv 0 xy 0 z = am bm−2k cm−k ∈ / L. 1-3 • v = bk , y = ck uv 0 xy 0 z = am bm−k cm−k ∈ / L. • v = bk ck , y = ck : uv 0 xy 0 z = am bm−2k cm−k ∈ / L. • v = ck , y = ck : uv 0 xy 0 z = am bm cm−2k ∈ / L. Therefore, by the pumping lemma for context-free languages, L is not context-free. 7. Show that the language L = {an bm : n and m are both prime} is not context-free. Ans. We pick a string w = am bm ∈ L, m is an odd prime. 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 = a` with k + ` is odd: uv 0 xy 0 z = am−k−` bm ∈ / L since m − k − ` is even. • v = ak1 , y = ak2 b` with one of k1 and k2 is odd, or ` is odd: uv 0 xy 0 z = am−k1 −k2 bm−` ∈ /L since m − k1 − k2 is even or m − ` is even. • v = ak , y = b` with one of k and ` is odd: uv 0 xy 0 z = am−k bm−` ∈ / L since m − k is even or m − ` is even. • v = a` bk1 , y = bk2 with one of k1 and k2 is odd, or ` is odd: uv 0 xy 0 z = am−` bm−k1 −k2 ∈ /L since m − ` is even or m − k1 − k2 is even. • v = bk , y = b` with k + ` is odd: uv 0 xy 0 z = am bm−k−` ∈ / L since m − k − ` is even. Therefore, by the pumping lemma for context-free languages, L is not context-free. 8. Determine whether or not L = {an bj ak bl : n ≤ k, j ≤ l} is context-free. You have to prove your answer. Ans. We pick a string w = am bm am bm ∈ 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: • [1st a] v = ak , y = ak with k ∈ N: uv i xy i z = am+2(i−1)k bm am bm ∈ / L for i = 2, 3, . . .. • [1st a, 1st b] v = ak , y = ak bk : uv i xy i z = am+2(i−1)k bm+(i−1)k am bm ∈ / L for i = 2, 3, . . .. • [1st a, 1st b] v = ak , y = bk : uv i xy i z = am+(i−1)k bm+(i−1)k am bm ∈ / L for i = 2, 3, . . .. • [1st a, 1st b] v = ak bk , y = bk : uv i xy i z = am+(i−1)k bm+2(i−1)k am bm ∈ / L for i = 2, 3, . . .. • [1st b] v = bk , y = bk : uv i xy i z = am bm+2(i−1)k am bm ∈ / L for i = 2, 3, . . .. • [1st b, 2nd a] v = bk , y = bk ak : uv i xy i z = am bm+2(i−1)k am+(i−1)k bm ∈ / L for i = 2, 3, . . .. • [1st b, 2nd a] v = bk , y = ak : uv i xy i z = am bm+(i−1)k am+(i−1)k bm ∈ / L for i = 2, 3, . . .. • [1st b, 2nd a] v = bk ak , y = ak : uv i xy i z = am bm+(i−1)k am+2(i−1)k bm ∈ / L for i = 2, 3, . . .. • [2nd a] v = ak , y = ak : uv 0 xy 0 z = am bm am−2k bm ∈ / L. • [2nd a, 2nd b] v = ak , y = ak bk : uv 0 xy 0 z = am bm am−2k bm−k ∈ / L. • [2nd a, 2nd b] v = ak , y = bk : uv 0 xy 0 z = am bm am−k bm−k ∈ / L. • [2nd a, 2nd b] v = ak bk , y = bk : uv 0 xy 0 z = am bm am−k bm−2k ∈ / L. • [2nd b] v = bk , y = bk : uv 0 xy 0 z = am bm am bm−2k ∈ / L. Therefore, by the pumping lemma for context-free languages, L is not context-free. 1-4 9. 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. Ans. The answer contains the following two parts: • Let L1 and L2 be context-free languages. By theorem, we have that the family of contextfree languages is not close under intersection and complement. Thus, L1 − L2 = L1 ∩ L2 is not close under difference in general. • Let L1 be a context-free language and L2 a regular language. By the closure property of regular languages, we know that L2 is also regular. By theorem, we know that L1 ∩ L2 is context-free by the closure property under regular intersection. Therefore, L1 − L2 = L1 ∩ L2 is also context-free by the closure property under regular intersection. 10. Show that L = {w ∈ {a, b}∗ : na (w) = nb (w); w does not contain a substring aab} is contextfree. Ans. Let L1 = {w ∈ {a, b}∗ : na (w) = nb (w)} and L2 = {w ∈ {a, b}∗ : w contains ’aab’ as a string}. The following shows that L1 is context-free and L2 is regular. • An NPDA that accepts L1 is M1 = ({q0 , q1 }, {a, b}, {a, b, z}, δ, q0 , z, {q1 }), where the transition function δ is defined as follows: δ(q0 , λ, z) = (q1 , z), δ(q0 , a, z) = (q0 , az), δ(q0 , a, a) = (q0 , aa), δ(q0 , a, b) = (q0 , λ), δ(q0 , b, z) = (q0 , bz), δ(q0 , b, a) = (q0 , λ), δ(q0 , b, b) = (q0 , bb). • An NFA that accepts L2 is M2 = ({q0 , q1 , q2 , q3 }, {a, b}, δ, q0 , {q3 }) accepts L00 , where the transition function δ is defined as follows: δ(q0 , a) = q1 , δ(q0 , b) = q0 , δ(q1 , a) = q2 , δ(q1 , b) = q0 , δ(q2 , a) = q0 , δ(q2 , b) = q3 , δ(q3 , a) = q3 , δ(q3 , b) = q3 . Therefore, L = L1 ∩ L2 is context-free by the closure property under regular intersection. 1-5
© Copyright 2025 Paperzz