DCP3122 Introduction to Formal Languages, Spring 2015 Instructor: Prof. Wen-Guey Tzeng Homework 5 - Solution 5-May-2015 Due: 18-May-2015 1. Given Σ = {a, b, c}, find an NPDA that accepts L = {a n b n+m c m : n 0, m 1}. Ans. An NPDA that accepts L is M = (Q, Σ, Γ, δ, q 0, z, F ), where Q = {q 0, q 1,..., q 5 }, Σ = {a, b, c}, Γ = {0, 1, z}, F = {q 5 }, and the transition function δ is represented as the following graph 2. Given Σ = {a, b, c}, find an NPDA that accepts L = {w 1 cw 2 : w 1, w 2 {a, b}, w 1 w R 2 }. Ans. An NPDA that accepts L is M = (Q, Σ, Γ, δ, q 0, z, F ), where Q = {q 0, q 1, q 2 }, Σ = {a, b, c}, Γ = {0, 1, z}, F = {q 2 }, and the transition function δ is represented as the following graph 3. What language is accepted by the PDA M = ({q 0, q 1,..., q 5 }, {a, b}, {0, 1, a, z}, δ, z, q 0, {q 5 }), 1-1
with δ(q 0, b, z) = {(q 1, 1z)}, δ(q 1, b, 1) = {(q 1, 11)}, δ(q 2, a, 1) = {(q 3, λ)}, δ(q 3, a, 1) = {(q 4, λ)}, δ(q 4, a, z) = {(q 4, z), (q 5, 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, Σ, Γ, δ, q 0, z, F ), where Q = {q 0, q 1, q 2 }, Σ = {a, b}, Γ = {S, A, B, z}, F = {q 2 }, 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 = ({q 0, q 1 }, {a, b}, {A, z}, δ, q 0, z, {q 1 }), with transitions δ(q 0, a, z) = {(q 0, Az)}, δ(q 0, b, A) = {(q 0, AA)}, δ(q 0, a, A) = {(q 1, λ)}. Ans. We first transform the NPDA into a new one that satisfies the following two requirements: (1) It has a single final state q f that is entered if and only if the stack is empty; (2) While a Σ {λ}, all transitions must have the form δ(q i, a, A) = {c 1, c 2,..., c n }, where c i = (q j, λ) or c i = (q j, BC). The NPDA satisfies requirement (2), but not (1). To satisfy the latter, we introduce a new final state q 2 so that Q new = {q 0, q 1, q 2 }, F new = {q 2 }, and δ new is δ(q 0, a, z) = {(q 0, Az)}, δ(q 0, b, A) = {(q 0, AA)}, δ(q 0, a, A) = {(q 1, λ)}, δ(q 1, λ, A) = {(q 1, λ)}, δ(q 1, λ, z) = {(q 2, λ)}. 1-2
The last three transitions yield the corresponding productions (q 0 Aq 1 ) a, (q 1 Aq 1 ) λ, (q 1 zq 2 ) λ. From the first two transitions we get the set of productions (q 0 zq 0 ) a(q 0 Aq 0 )(q 0 zq 0 ) a(q 0 Aq 1 )(q 1 zq 0 ) a(q 0 Aq 2 )(q 2 zq 0 ), (q 0 zq 1 ) a(q 0 Aq 0 )(q 0 zq 1 ) a(q 0 Aq 1 )(q 1 zq 1 ) a(q 0 Aq 2 )(q 2 zq 1 ), (q 0 zq 2 ) a(q 0 Aq 0 )(q 0 zq 2 ) a(q 0 Aq 1 )(q 1 zq 2 ) a(q 0 Aq 2 )(q 2 zq 2 ), (q 0 Aq 0 ) a(q 0 Aq 0 )(q 0 Aq 0 ) a(q 0 Aq 1 )(q 1 Aq 0 ) a(q 0 Aq 2 )(q 2 Aq 0 ), (q 0 Aq 1 ) a(q 0 Aq 0 )(q 0 Aq 1 ) a(q 0 Aq 1 )(q 1 Aq 1 ) a(q 0 Aq 2 )(q 2 Aq 1 ), (q 0 Aq 2 ) a(q 0 Aq 0 )(q 0 Aq 2 ) a(q 0 Aq 1 )(q 1 Aq 2 ) a(q 0 Aq 2 )(q 2 Aq 2 ). To simplify the transitions, we remove the useless variables: A variable that does not occur on the left side of any production: (q 1 zq 0 ), (q 1 zq 1 ), (q 1 zq 2 ), (q 2 zq 0 ), (q 2 zq 1 ), (q 2 zq 2 ), (q 1 Aq 0 ), (q 1 Aq 1 ), (q 1 Aq 2 ), (q 2 Aq 0 ), (q 2 Aq 1 ), and (q 2 Aq 2 ). Non-reachable path: None. The result grammar is with the start variable is (q 0 zq 2 ). (q 0 Aq 1 ) a, (q 1 Aq 1 ) λ, (q 1 zq 2 ) λ, (q 0 zq 0 ) a(q 0 Aq 0 )(q 0 zq 0 ), (q 0 zq 1 ) a(q 0 Aq 0 )(q 0 zq 1 ), (q 0 zq 2 ) a(q 0 Aq 0 )(q 0 zq 2 ), (q 0 Aq 0 ) a(q 0 Aq 0 )(q 0 Aq 0 ), (q 0 Aq 1 ) a(q 0 Aq 0 )(q 0 Aq 1 ), (q 0 Aq 2 ) a(q 0 Aq 0 )(q 0 Aq 2 ), 6. Show that the language L = {w {a, b, c} : n a (w) = n b (w) n c (w)} is not context-free. Ans. We pick a string w = a m b m c m 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 = a k, y = a k : uv 0 xy 0 z = a m 2k b m c m / L. v = a k, y = a k b k : uv 0 xy 0 z = a m 2k b m k c m / L. v = a k, y = b l : uv 0 xy 0 z = a m k b m l c m / L. v = a k b k, y = b k : uv 0 xy 0 z = a m k b m 2k c m / L. v = b k, y = b k : uv 0 xy 0 z = a m b m 2k c m / L. v = b k, y = b k c k : uv 0 xy 0 z = a m b m 2k c m k / L. 1-3
v = b k, y = c k uv 0 xy 0 z = a m b m k c m k / L. v = b k c k, y = c k : uv 0 xy 0 z = a m b m 2k c m k / L. v = c k, y = c k : uv 0 xy 0 z = a m b m c m 2k / L. 7. Show that the language L = {a n b m : n and m are both prime} is not context-free. Ans. We pick a string w = a m b m 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 = a k, y = a l with k + l is odd: uv 0 xy 0 z = a m k l b m / L since m k l is even. v = a k 1, y = a k 2 b l with one of k 1 and k 2 is odd, or l is odd: uv 0 xy 0 z = a m k 1 k 2 b m l / L since m k 1 k 2 is even or m l is even. v = a k, y = b l with one of k and l is odd: uv 0 xy 0 z = a m k b m l / L since m k is even or m l is even. v = a l b k 1, y = b k 2 with one of k 1 and k 2 is odd, or l is odd: uv 0 xy 0 z = a m l b m k 1 k 2 / L since m l is even or m k 1 k 2 is even. v = b k, y = b l with k + l is odd: uv 0 xy 0 z = a m b m k l / L since m k l is even. 8. Determine whether or not L = {a n b j a k b l : n k, j l} is context-free. You have to prove your answer. Ans. We pick a string w = a m b m a m b m 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 = a k, y = a k with k N: uv i xy i z = a m+2(i 1)k b m a m b m / L for i = 2, 3,.... [1st a, 1st b] v = a k, y = a k b k : uv i xy i z = a m+2(i 1)k b m+(i 1)k a m b m / L for i = 2, 3,.... [1st a, 1st b] v = a k, y = b k : uv i xy i z = a m+(i 1)k b m+(i 1)k a m b m / L for i = 2, 3,.... [1st a, 1st b] v = a k b k, y = b k : uv i xy i z = a m+(i 1)k b m+2(i 1)k a m b m / L for i = 2, 3,.... [1st b] v = b k, y = b k : uv i xy i z = a m b m+2(i 1)k a m b m / L for i = 2, 3,.... [1st b, 2nd a] v = b k, y = b k a k : uv i xy i z = a m b m+2(i 1)k a m+(i 1)k b m / L for i = 2, 3,.... [1st b, 2nd a] v = b k, y = a k : uv i xy i z = a m b m+(i 1)k a m+(i 1)k b m / L for i = 2, 3,.... [1st b, 2nd a] v = b k a k, y = a k : uv i xy i z = a m b m+(i 1)k a m+2(i 1)k b m / L for i = 2, 3,.... [2nd a] v = a k, y = a k : uv 0 xy 0 z = a m b m a m 2k b m / L. [2nd a, 2nd b] v = a k, y = a k b k : uv 0 xy 0 z = a m b m a m 2k b m k / L. [2nd a, 2nd b] v = a k, y = b k : uv 0 xy 0 z = a m b m a m k b m k / L. [2nd a, 2nd b] v = a k b k, y = b k : uv 0 xy 0 z = a m b m a m k b m 2k / L. [2nd b] v = b k, y = b k : uv 0 xy 0 z = a m b m a m b m 2k / L. 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 L 1 is context-free and L 2 is regular, then L 1 L 2 is context-free. Ans. The answer contains the following two parts: Let L 1 and L 2 be context-free languages. By theorem, we have that the family of contextfree languages is not close under intersection and complement. Thus, L 1 L 2 = L 1 L 2 is not close under difference in general. Let L 1 be a context-free language and L 2 a regular language. By the closure property of regular languages, we know that L 2 is also regular. By theorem, we know that L 1 L 2 is context-free by the closure property under regular intersection. Therefore, L 1 L 2 = L 1 L 2 is also context-free by the closure property under regular intersection. 10. Show that L = {w {a, b} : n a (w) = n b (w); w does not contain a substring aab} is contextfree. Ans. Let L 1 = {w {a, b} : n a (w) = n b (w)} and L 2 = {w {a, b} : w contains aab as a string}. The following shows that L 1 is context-free and L 2 is regular. An NPDA that accepts L 1 is M 1 = ({q 0, q 1 }, {a, b}, {a, b, z}, δ, q 0, z, {q 1 }), where the transition function δ is defined as follows: δ(q 0, λ, z) = (q 1, z), δ(q 0, a, z) = (q 0, az), δ(q 0, a, a) = (q 0, aa), δ(q 0, a, b) = (q 0, λ), δ(q 0, b, z) = (q 0, bz), δ(q 0, b, a) = (q 0, λ), δ(q 0, b, b) = (q 0, bb). An NFA that accepts L 2 is M 2 = ({q 0, q 1, q 2, q 3 }, {a, b}, δ, q 0, {q 3 }) accepts L, where the transition function δ is defined as follows: δ(q 0, a) = q 1, δ(q 0, b) = q 0, δ(q 1, a) = q 2, δ(q 1, b) = q 0, δ(q 2, a) = q 0, δ(q 2, b) = q 3, δ(q 3, a) = q 3, δ(q 3, b) = q 3. Therefore, L = L 1 L 2 is context-free by the closure property under regular intersection. 1-5