Additional NPDA examples (From Lin 7.1 p. 183 Exercise 4.)
Formal Definition Non-Deterministic Pushdown Automaton NPDA M = ( Q, Σ, Γ, δ, q,, 0 F ) Final Set of states states Input alphabet Stack alphabet Transition function Initial state Stack start symbol 2
Instantaneous Description ( q, u, s) ) Current state Remaining input Current stack contents 3
1. Construct NPDA that accepts the language L = {wcw R : w {a, b}} with Σ = {a, b, c} The idea of machine construction*: We construct M that accepts L. It reads w and after seeing c, it reads w R. We will need three states, for reading w, switching to the new state after reading c, and the final accepting state. tt First for each a or b seen we push an a or b respectively onto the stack and stay in state q0. When we encounter a c, we do not push anything on to the stack, but go to the next state, q1. While being in state q1, if we encounter an a or b, we match it with the a or b respectively on the stack and pop one a for each a or one b for each b seen, so that it matches w R against the contents of the stack and the matching starts after it encounters a c. Finally it accepts if the input is done and there are no more a s or b s on the stack. *This is a variation on the example 7.5 on the p. 181 of Lin book, for the language L = {ww R : w {a, b}}
L = NPDA { { } * } R wcw w a,b a, λ b, λ a b b,b b λ a, a λ c, λ λ λ, q 0 q 1 q 2 5
Instantaneous description of the automaton accepting L = {wcw R : w {a, b}} Σ = {a, b, c} Here is the machine definition. M = ({q0, q1, q2}, Σ, {a, b, }, δ, q0,, {q2}) Instantaneous description (ID): (current state, remaining input, stack) δ(q0, a, a) = {(q0, aa)}, δ(q0, b, a) = {(q0, ba)}, δ(q0, a, b) = {(q0, ab)}, δ(q0 0, b, b) = {(q0, 0 bb)}, δ(q0, a, ) = {(q0, a)}, δ(q0, b, ) = {(q0, b)}, δ(q0, c, ) ) = {(q1, )}, ) δ(q0, c, a) = {(q1, a)}, δ(q0, c, b) = {(q1, b)}, δ(q1, a, a) = {(q1, λ)}, δ(q1, b, b) = {(q1, λ)}, δ(q1, λ, ) = {(q2, )}. a, λ a a, a aa b, λ b a, b ab b,b λ b, b bb a,a λ c, λ λ λ, q 0 q 1 q 2
2. Construct NPDA that accepts the language L = {a n b m c n+m : n 0, m 0} with Σ = {a, b, c} The idea of machine construction: We construct M that accepts L. Firstitaccepts it the empty string (n=m=0). Further, for each a seen (if any) it pushes an a onto the stack. Then it pushes an a onto the stack for each b seen (if any). Thus after reading all as and bsin the input, the stack has na (w) + nb (w) as on it. It then pops one a for each c seen guaranteeing that na (w) + nb (w) = nc(w) and they are in the right order. Finally it accepts if the input is done and there are no more as on the stack. M = ({q0, q1, q2, q3, q4}, Σ, {a, }, δ, q0,, {q0, q4 })
NPDA L = { a n b m b n + m : n 0, m 0} a, λ a b, λ a c, a λ λ, λ λ λ, λ λ λ,λ q 0 q 1 q 3 q 2 8
Instantaneous description of the automaton accepting L = {a n b m c n+m : n 0, m 0} Σ = {a, b, c} Machine definition: M = ({q0, q1, q2, q3, q4}, Σ, {a, }, δ, q0,, {q0, q4 }) Instantaneous description (ID): (current state, remaining input, stack) δ(q0, λ, ) = {(q1, )}, δ(q1, a, ) = {(q1, a)}, δ(q1, a, a) = {(q1, aa)}, δ(q1, λ, a) = {(q2, a)}, δ(q1, λ, ) = {(q2, )}, δ(q2, 2 b, ) ) = {(q2, 2 a)}, δ(q2, b, a) = {(q2, aa)}, δ(q2, c, a) = {(q3, λ)}, δ(q3, c, a) ) = {(q3, λ)}, ) δ(q3, λ, ) = {(q4, λ)}. a, λ a b, λ a c, a λ λ, λ λ λ, λ λ λ, q 0 q 1 q 2 q 3 q 2
3. Construct NPDA that accepts the language L = {a n b m : n m 3n, n 0} with Σ = {a, b, c}. The idea of machine construction: We will construct M that accepts L. It accepts empty string. The state q0 keeps track of the n as encountered in the first part of the string and provides a chance for state q1 to match the bs to anywhere between n and 3n. It proceeds to state q2 from state q1 if the number of bs encountered is between n and 3n.
NPDA L { n m a b : n m 3n, 0} = n a, λ a, λ a aa b,a λ a, λ aaa b,a λ λ, q 0 q 1 q 2 11
Instantaneous description of the automaton accepting L = {a n b m : n m 3n, n 0} with Σ = {a, b, c}. Machine definition: M = ({q0, q1, q2 }, Σ, {a, b, }, δ, q0,, {q2 }) Instantaneousdescription (ID): (currentstate state, remaininginput input, stack) δ(q0, a, ) = {(q0, a)}, δ(q0, a, ) = {(q0, aa)}, δ(q0, a, ) = {(q0, aaa)}, δ(q0, a, a) = {(q0, aa)}, δ(q0, a, a) = {(q0, aaa)}, δ(q0, a, a) = {(q0, aaaa)}, δ(q0, b, a) = {(q1, λ)}, δ(q1, b, a) = {(q1, λ)}, δ(q1, λ, ) = {(q2, )} a, λ a a, λ aa a, λ aaa a,a a a,a aa a,a aaa b,a λ b,a λ λ, q 0 q 1 q 2
4. Construct NPDA that accepts the language L = {w : n a (w) = n b (w) + 1} with Σ = {a, b, c}. L { w : n ( w ) = n ( w ) +, w { a,b} * } = 1 a b The idea of machine construction: Machine M accepts L and it starts with one extra a on the stack and then uses the machine given in Example 7.4 in the Lin book to tell if the number of as and bsare the same. If they are and we started with one more a on the stack then the condition of L is met. Additionally, if it sees a c, it just reads it and does not modify the stack.
We start with a simpler machine that accepts the language: L w a,b : n ( w n w * = { { } ) = ( )} a( b Here we use Lin solution. He uses 0 to count a s which will be popped when b s bsare found. Assometimes sometimes in some prefix there may be more b s bs than a s we will find no 0 on the stack. In that case 1 is used that later on can be matched with a s.
NPDA for a simpler machine L * = { w { a,b} : n ( w ) = n ( w )} a b a, a,0 0 00 b, 1 b, 0 λ b, 1 11 a, 1 λ q 0 λ, q f
NPDA L { { } = } * w a,b : n ( w ) n ( w )+1 = ) a b λ,λ, q 0 a, a,0 a 0 00 b, 1 b, 0 λ b, 1 11 a, 1 λ λ, q 1 q2
Instantaneous description of the automaton accepting L = {w : n a (w) = n b (w) + 1} with Σ = {a, b, c}. M = ({q0, q1, q2}, Σ, {a, b, }, δ, q0,, {q2}) Instantaneous description (ID): (current state, remaining input, stack) δ(q0,, λ,, ) ) = {(q1, a)}, δ(q1, a, ) = {(q1, a)}, δ(q1, a, a) = {(q1, aa)}, b, 1 δ(q1, a, b) = {(q1, λ)}, b, 0 λ b, 1 11 a, 0 a, 1 λ δ(q1, b, ) ) = {(q1, b)}, a,0 00 δ(q1, b, a) = {(q1, λ)}, δ(q1, b, b) = {(q1, bb)}, λ,λ a λ, δ(q1, λ, ) = {(q2, λ)}, q 0 q 1 q2 δ(q1, c, ) = {(q1, c)}, δ(q1, c, a) = {(qa, a)}, δ(q1, c, b) = {(q1, b)}. In this solution a and b are used as counters instead of 0 and 1.