δ - IDt

Additional NPDA examples
Additional NPDA examples
(From Linz 7.1 p. 183 Exercise 4.)
Formal Definition
Non-Deterministic Pushdown Automaton NPDA
M = ( Q , Σ , Γ , δ , q0 , z , F )
Final
states
Set of 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 R : w {a, b}}
LL = {wcw
{
{ b}} with Σ = {a, b, c}
ith Σ { b }
The idea of machine construction*:
We construct M that accepts L. It reads w and after seeing c, it reads wR.
We will need three states, for reading w, switching to the new state after reading c, and the final accepting state.
di
d th fi l
ti
t t
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 wR
against the contents of the stack and the matching starts after it encounters
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 Linz book, for the language L = {wwR : w ‫{ א‬a, b}}
NPDA
{
L = wcw w ∈ {a ,b}
*
R
a,λ / a
b,b / λ
b,λ / b
a, a / λ
q0
c, λ / λ
q1
}
λ,z / z
q2
5
Instantaneous description of the automaton
of the automaton
accepting L = {wcwR : w {a, b}} Σ = {a, b, c} Here is the machine definition.
M = ({q0 , q1, q2}, Σ, {a, b, z}, δ, q0, z, {q2}) Instantaneous description (ID): (current state, remaining input, stack)
δ(q0 , a, a) = {(q0 , aa)}, δ(q0, b, a) = {(q0, ba)}, δ(q0, a, b) = {(q0 , ab)}, δ( 0 b b) {( 0 bb)}
δ(q0 , b, b) = {(q0, bb)}, δ(q0, a, z) = {(q0 , az)}, δ(q0, b, z) = {(q0, bz)}, δ(q0, c, z) = {(q1, z)}, (q
) (q )
δ(q0, c, a) = {(q1, a)}, δ(q0, c, b) = {(q1 , b)}, δ(q1, a, a) = {(q1, λ)},
δ(q1 b b) = {(q1 λ)}
δ(q1 , b, b) = {(q1, λ)}, δ(q1, λ, z) = {(q2 , z)}.
a,λ / a
a , a / aa
b,λ / b
a , b / ab
b, b / λ
b , b / bb
a, a / λ
q0
c, λ / λ
q1
λ,z / z
q2
2. Construct NPDA that accepts the language 2
Construct NPDA that accepts the language
L = {an bm cn+m : n ≥ 0, m ≥ 0} with Σ = {a, b, c}
The idea of machine construction:
We construct M that accepts L. First it accepts the empty string (n=m=0). We
construct M that accepts L First it accepts 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 bs in 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.
stack
M = ({q0, q1, q2, q3 , q4}, Σ, {a, z}, δ, q0, z, {q0, q4 })
NPDA
{
L = a n b m b n + m : n ≥ 0, m ≥ 0
a λ/a
a,
q0
c a/λ
c,
b λ/a
b,
λ, λ / λ
q1
}
λ, λ / λ
q2
λ,z / z
q3
8
Instantaneous description of the automaton
accepting L = {an bm cn+m : n ≥ 0, m ≥ 0} Σ = {a, b, c} Machine definition:
M = ({q0, q1, q2, q3 , q4}, Σ, {a, z}, δ, q0, z, {q0, q4 }) Instantaneous description (ID): (current state, remaining input, stack)
δ(q0 , λ, z) = {(q1, z)}, δ(q1 , a, z) = {(q1 , az)}, δ(q1 , a, z) {(q1 , az)},
δ(q1 , a, a) = {(q1 , aa)}, δ(q1, λ, a) = {(q2, a)}, δ(q1, λ, z) = {(q2 , z)}, δ( 2 b ) {( 2 )}
δ(q2, b, z) = {(q2, az)},
δ(q2 , b, a) = {(q2, aa)}, δ(q2, c, a) = {(q3, λ)}, (q
) (q )
δ(q3, c, a) = {(q3, λ)}, δ(q3, λ, z) = {(q4, λ)}.
a, λ / a
q0
c, a / λ
b, λ / a
λ, λ / λ
q1
λ, λ / λ
q2
λ,z / z
q3
3. Construct NPDA that accepts the language L = {an bm : n ≤ m ≤ 3n, n≥0} with Σ = {a, b, c}.
The idea of machine
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 = {a b
n m
}
: n ≤ m ≤ 3n , n ≥ 0
a, λ / a
b, a / λ
a , λ / aa
a , λ / aaa
q0
b, a / λ
q1
λ,z / z
q2
11
Instantaneous description of the automaton
accepting L = {an bm : n ≤ m ≤ 3n, n≥0} with Σ = {a, b, c}.
Machine definition:
Machine
definition:
M = ({q0, q1, q2 }, Σ, {a, b, z}, δ, q0 , z, {q2 })
Instantaneous description (ID): (current state remaining input stack)
Instantaneous description (ID): (current state, remaining input, stack)
δ(q0, a, z) = {(q0, az)}, δ(q0 , a, z) = {(q0, aaz)}, δ(q0 a z) = {(q0 aaaz)}
δ(q0, a, z) = {(q0, aaaz)}, δ(q0 , a, a) = {(q0, aa)}, δ(q0, a, a) = {(q0 , aaa)}, δ(q0, a, a) = {(q0, aaaa)}, δ(q0 , b, a) = {(q1, λ)}, δ(q1 , b, a) = {(q1, λ)}, δ(q1, λ, z) = {(q2 , z)}
a, λ / a
a, a / a
a , a / aa
a , λ / aa
a , λ / aaa
b, a / λ
a , a / aaa
q0
b, a / λ
q1
λ,z / z
q2
4. Construct NPDA that accepts the language L = {w : na (w) = nb (w) + 1} with Σ = {a, b, c}.
L = { w : na ( w ) = nb ( w ) + 1, w∈ {a ,b} }
*
The idea
h d of machine
f
h construction:
Machine M accepts L and it starts with one extra a on the stack and then uses g
in Example 7.4 in the Linz book to tell if the number of as p
the machine given
and bs are 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} : na ( w ) = nb ( w )}
*
Here we use Linz solution. He uses 0 to count a’s which will be popped
when b
b’ss are found. As sometimes
are found As sometimes in some
in some prefix there
prefix there may be more
be more b
b’ss
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} : na ( w ) = nb ( w )}
*
b , z / 1z
b, 0 / λ
b ,1 / 11
a ,1 / λ
a , z / 0z
a , 0 / 00
q0
λ,z / z
qf
NPDA
L = { w∈ {a ,b} : na ( w ) = nb ( w ) + 1}
*
b , z / 1z
b, 0 / λ
a ,1 / λ
a , z / 0z
a , 0 / 00
q0
λ ,λ / a
b ,1 / 11
q1
λ,z / z
q2
Instantaneous description of the automaton
accepting L = {w : na (w) = nb (w) + 1} with Σ = {a, b, c}.
M = ({q0, q1, q2}, Σ, {a, b, z}, δ, q0, z, {q2})
Instantaneous description (ID): (current state, remaining input, stack)
δ(q0, λ, z) = {(q1 , az)}, (q , , ) {(q , )},
δ(q1 , a, z) = {(q1, az)}, δ(q1 , a, a) = {(q1 , aa)}, δ(q1 , a, b) = {(q1, λ)}, δ(q1 b ) {(q1 b )}
δ(q1, b, z) = {(q1, bz)}, δ(q1, b, a) = {(q1 , λ)}, δ(q1, b, b) = {(q1, bb)}, δ(q1, λ, z) = {(q2, λ)}, δ(q1, c, z) = {(q1 , c)}, δ(q1, c, a) = {(qa , a)},
δ(q1, c, b) = {(q1, b)}.
b , z / 1z
b, 0 / λ
a , z / 0z
a , 0 / 00
q0
λ ,λ / a
In this solution a and b are used as counters instead of 0 and 1.
q1
b ,1 / 11
a ,1 / λ
λ,z / z
q2