Pushdown Automata
PDAs
Pushdown Automaton -- PDA
Input String
Stack
States
Initial Stack Symbol
Stack
$
Stack
z
bottom special symbol stack
head top
Appears at time 0
The States
q
1a, b c q
2Input symbol
Pop
symbol Push
symbol
q
1a, b c q
2 a
b
topinput
stack
a
Replace
e h
e h
c
q
1a, c q
2 a a
b b
c
top input
stack
q
1a, b q
2 a a
b
Pope h
e h
top input
stack
q
1a, q
2 a a
b
topb
input
stack
q
1a , $ q
2 a a
$
top Pop inputstack
Empty Stack
empty
The automaton HALTS No possible transition after
q
q
1a , $ b q
2 a a
$
top Pop inputstack
A Possible Transition
b
Non-Determinism
q
1q
2a, b c
q
3a, b c
q
1 , b c q
2transition
PDAs are non-deterministic
Allowed non-deterministic transitions
Example PDA
a, a b , a
PDA
M
} 0 :
{ )
( M a b n
L
n n ,
a, a
b, a
q
0q
1q
2q
3b , a
, $ $
} 0 :
{ )
( M a b n
L
n nBasic Idea:
1. Push the a’s
on the stack 2. Match the b’s on input with a’s on stack
3. Match found
a, a
Execution Example:
Input
a a a b b b
current state
b , a
Time 0
Stack
$
a, a
b , a
q
0q
1q
2q
3Input
a a a b b b
b , a
Time 1
, , $ $
Stack
$
a, a
Input
Stack
a a a b b b
$ a
b , a
Time 2
a, a
b , a
q
0q
1q
2q
3Input
Stack
a a a b b b
$ a a
b , a
Time 3
, , $ $
a, a
Input
Stack
a a a b b b
$ a a a
b , a
Time 4
a, a
b , a
q
0q
1q
2q
3Input
a a a b b b
Stack
$ a a a
b , a
Time 5
, , $ $
a, a
Input
a a a b b b
$ a
Stack
b , a
Time 6
a
a, a
b , a
q
0q
1q
2q
3Input
a a a b b b
$
Stack
b , a
Time 7
, , $ $
a
a, a
Input
a a a b b b
b , a
Time 8
accept
$
Stack
A string is accepted if there is a computation such that:
All the input is consumed AND
The last state is an accepting state
At the end of the computation,
we do not care about the stack contents (the stack can be empty at the last state)
The input string
is accepted by the PDA:
aaabbb
a, a b , a
} 0 :
{
a b n L
n nis the language accepted by the PDA:
a, a
b , a
q
0q
1q
2q
3b , a
In general,
, , $ $
a, a
Rejection Example:
Input
a a b
current state
b , a
Time 0
Stack
$
q
0a, a
b, a
q
1q
2q
3Rejection Example:
Input
a a b
current state
b , a
Time 1
, , $ $
Stack
$
a, a
Rejection Example:
Input
a a b
current state
b , a
Time 2
Stack
$
a
q
0a, a
b, a
q
1q
2q
3Rejection Example:
Input
a a b
current state
b , a
Time 3
, , $ $
Stack
$
a
a
a, a
Rejection Example:
Input
a a b
current state
b , a
Time 4
Stack
$
a
a
q
0a, a
b, a
q
1q
2q
3Rejection Example:
Input
a a b
current state
b , a
Time 4
, , $ $
Stack
$ a a
reject
The input string
is rejected by the PDA:
aab
a, a b , a
A string is rejected if there is
no
computation such that:All the input is consumed AND
The last state is an accept state
At the end of the computation,
we do not care about the stack contents
Another PDA example
b b
a a
,
,
b
b
a a
, ,
PDA
M
} } ,
{ :
{ )
( M vv v a b
L
R , $ $
q q
b b
a a
,
,
, q
b
b
a a
, ,
} } ,
{ :
{ )
( M vv v a b
L
RBasic Idea:
1. Push v
on stack 2. Guess middle of input
3. Match on input with v on stack
v
R4. Match found
Execution Example:
Input
Time 0
Stack
$
b b
a a
,
,
b
b
a a
,
,
a b b a
Input
a b a
Time 1
Stack
$
, $ $
q q
b b
a a
,
,
, q
b
b
a a
,
,
b a
Input
Time 2
Stack
$
b b
a a
,
,
b
b
a a
, ,
a b b a a b
Input
Time 3
Stack
$
, $ $
q q
b b
a a
,
,
, q
b
b
a a
, ,
a b b a a b
Guess the middle of string
Input
Time 4
Stack
$
b b
a a
,
,
b
b
a a
, ,
a b b a a b
Input
Time 5
Stack
$
, $ $
q q
b b
a a
,
,
, q
b
b
a a
, ,
a b b a a
Input
Time 6
Stack
$
b b
a a
,
,
b
b
a a
,
,
a b b a
Rejection Example:
Input
Time 0
Stack
$
, $ $
q q
b b
a a
,
,
, q
b
b
a a
,
,
a b b b
Input
Time 1
Stack
$
b b
a a
,
,
b
b
a a
,
,
a b b b a
Input
Time 2
Stack
$
, $ $
q q
b b
a a
,
,
, q
b
b
a a
, ,
a
a b b b b
Input
Time 3
Stack
$
b b
a a
,
,
b
b
a a
, ,
a b
Guess the middle of string
a b b b
Input
Time 4
Stack
$
, $ $
q q
b b
a a
,
,
, q
b
b
a a
, ,
a
a b b b b
Input
Time 5
Stack
$
b b
a a
,
,
b
b
a a
, , a b b b a
There is no possible transition.
Input is not consumed
Another computation on same string:
Input Time 0
Stack
$
, $ $
q q
b b
a a
,
,
, q
b
b
a a
,
,
a b b b
Input
Time 1
Stack
$
b b
a a
,
,
b
b
a a
,
,
a b b b a
Input
Time 2
Stack
$
, $ $
q q
b b
a a
,
,
, q
b
b
a a
, ,
a
a b b b b
Input
Time 3
Stack
$ a a b b b b
b b
a a
,
,
b
b
a a
, ,
b
Input
Time 4
Stack
a b b b
, $ $
q q
b b
a a
,
,
, q
b
b
a a
, ,
$
a
b
b
b
Input
Time 5
Stack
a b b b
$ a b b b
b b
a a
,
,
b
b
a a
, ,
No final state is reached
, $ $
q q
b b
a a
,
,
, q
b
b
a a
, ,
There is no computation that accepts string
abbb
) ( M L
abbb
Another PDA example
a
b
a a
, ,
PDA
M
)}
( )
(
, prefix every
in
: } ,
{ {
)
( *
v n
v n
v b
a w
M L
b
a
Execution Example:
Input
Time 0
Stack
a a b
$ , , , b
a b
a a
q
0$
Input
Time 1
Stack
a a b a
$
$ , , , b
a b
a a
Input
Time 2
Stack
a a b
q
0a a
$
$ , , , b
a b
a a
Input
Time 3
Stack
a a b a
$
$ , , , b
a b
a a
Rejection example:
Input
Time 0
Stack
a b b
q
0b
$
Input
Time 1
Stack
a b b b a
$
$ , , , b
a b
a a
Input
Time 2
Stack
q
0a b b b
$
$ , , , b
a b
a a
Input
Time 3
Stack
a b b b
$ , , , b
a b
a a
Input
Time 4
Stack
q
0 Halt and Rejecta b b b
$ , , , b
a b
a a
Pushing Strings
q
1a, b w q
2Input symbol
Pop
symbol Push string
q
1a, b cdf q
2 a
b
topinput
stack
a
Push
e h h
e d c
f
pushed string Example:
Another PDA example
a, $ 0$
a, 0 00 a, 1
b , $ 1$
b , 1 11 b , 0
PDA
M
)}
( )
( :
} ,
{ {
)
( M w a b
*n w n w
L
a
bTime 0 Input
a b b b a
current state
a
$
Stack
$
$
,
q q
a, $ 0$
a, 0 00 a, 1
b , $ 1$
b , 1 11 b , 0
Execution Example:
Time 1 Input
a b b b a a
$
Stack
0 a, $ 0$
a, 0 00 a, 1
b , $ 1$
b , 1 11
b , 0
Time 3 Input
a b b b a
$
Stack
a
$
$
$
,
q q
a, $ 0$
a, 0 00 a, 1
b , $ 1$
b , 1 11 b , 0
0
Time 4 Input
a b b b a
$
Stack
a
1 a, $ 0$
a, 0 00 a, 1
b , $ 1$
b , 1 11
b , 0
Time 5 Input
a b b b a
$
Stack
a
1 1
$
$
,
q q
a, $ 0$
a, 0 00 a, 1
b , $ 1$
b , 1 11
b , 0
Time 6 Input
a b b b a
$
Stack
a
1 a, $ 0$
a, 0 00 a, 1
b , $ 1$
b , 1 11 b , 0
1
Time 7 Input
a b b b a
$
Stack
a
1
$
$
,
q q
a, $ 0$
a, 0 00 a, 1
b , $ 1$
b , 1 11
b , 0
Time 8 Input
a b b b a a
$
Stack
a, $ 0$
a, 0 00 a, 1
b , $ 1$
b , 1 11
b , 0
Formalities for PDAs
q
1a, b w q
2)}
, {(
) ,
,
( q a b q w
Transition function:
q
1q
2w
b
a ,
q
3w
b
a ,
)}
, (
), ,
{(
) ,
,
( q
1a b q
2w q
3w
Transition function:
Formal Definition
Pushdown Automaton (PDA)
) ,
, ,
δ , Γ , Σ ,
( Q q
0z F
M
States Input
Final states
Stack
Instantaneous Description
) ,
,
( q u s
Current
state Remaining input
Current stack
contents
a, a
Input
Stack
a a a b b b
$ a a
b, a
Time 4:
Example: Instantaneous Description
$) ,
,
( q
1bbb aaa
a
a, a
b , a
q q q q
Input
Stack
a a a b b b
$ a a
b, a
Time 5:
, , $ $
Example: Instantaneous Description
$) ,
,
( q
2bb aa
a
We write:
$) ,
, (
$) ,
,
( q
1bbb aaa q
2bb aa
Time 4 Time 5
a, a
b , a
q
0q
1q
2q
3b, a
, , $ $ ,$) ,
( ,$)
, (
$) ,
, (
$) ,
, (
$) ,
, (
$) ,
, (
$) ,
, (
,$) ,
( ,$)
, (
3 2
2 2
1 1
1
1 0
q
q a
b q
aa bb
q
aaa bbb
q aa
abbb q
a aabbb q
aaabbb q
aaabbb q
A computation:
,$) ,
( ,$)
, (
$) ,
, (
$) ,
, (
$) ,
, (
$) ,
, (
$) ,
, (
,$) ,
( ,$)
, (
3 2
2 2
1 1
1
1 0
q
q a
b q
aa bb
q
aaa bbb
q aa
abbb q
a aabbb q
aaabbb q
aaabbb q
For convenience we write:
Formal Definition
Language of PDA :
M
)}
' , ,
( )
, ,
( :
{ )
( M w q
0w s q s
L
f
Initial state Final state
)
( M
L
Example:
,$) ,
( ,$)
,
( q
0aaabbb
q
3
a, a b , a
PDA :
M
) ( M L
aaabbb
,$) ,
( ,$)
,
( q
0a
nb
n
q
3
a, a
b, a
q
0q
1q
2q
3b , a
, , $ $
) ( M L
b
a
n n
PDA :
M
a, a b , a
PDA :
M
} 0 :
{ )
( M a b n
L
n nTherefore: