Pushdown Automata PDAs Fall 2006 Costas Busch - RPI Pushdown Automaton -- PDA Input String Stack States Fall 2006 Costas Busch - RPI.
Download
Report
Transcript Pushdown Automata PDAs Fall 2006 Costas Busch - RPI Pushdown Automaton -- PDA Input String Stack States Fall 2006 Costas Busch - RPI.
Pushdown Automata
PDAs
Fall 2006
Costas Busch - RPI
1
Pushdown Automaton -- PDA
Input String
Stack
States
Fall 2006
Costas Busch - RPI
2
Initial Stack Symbol
stack
head
Stack
Stack
$
z
top
bottom special symbol
Appears at time 0
Fall 2006
Costas Busch - RPI
3
The States
Input
symbol
Pop
symbol
Push
symbol
a,
b
c
q1
q2
Fall 2006
Costas Busch - RPI
4
q1
a, b c
q2
input
a
a
stack
b
h
e
$
Fall 2006
top
Replace
Costas Busch - RPI
c
h
e
$
5
q1
a, c
q2
input
a
stack
b
h
e
$
Fall 2006
top
Push
Costas Busch - RPI
a
c
b
h
e
$
6
q1
a, b
q2
input
a
a
stack
b
h
e
$
Fall 2006
top
Pop
Costas Busch - RPI
h
e
$
7
q1
a,
q2
input
a
a
stack
b
h
e
$
Fall 2006
top
No Change
Costas Busch - RPI
b
h
e
$
8
Pop from Empty Stack
q1
input
a
a, b c
q2
Pop
stack
Automaton halts!
top
If the automaton attempts to pop from
empty stack then it halts and rejects input
Fall 2006
Costas Busch - RPI
9
Non-Determinism
PDAs are non-deterministic
Allowed non-deterministic transitions
a, b c
q2
q1
a, b c
Fall 2006
q1
, b c
q2
transition
q3
Costas Busch - RPI
10
Example PDA
PDA
M:
L(M ) {a b : n 0}
n n
a, a
b, a
,
b,
a
,
$
$
q3
q2
q0
q1
Fall 2006
Costas Busch - RPI
11
L(M ) {a b : n 0}
n n
Basic Idea:
1. Push the a’s
on the stack
2. Match the b’s on input
with a’s on stack
a, a
b, a
3. Match
found
,
b,
a
,
$
$
q3
q2
q0
q1
Fall 2006
Costas Busch - RPI
12
Execution Example: Time 0
Input
a a a b b b
$
Stack
current
state
a, a
b, a
, q b, a q , $ $ q
q0
3
2
1
Fall 2006
Costas Busch - RPI
13
Time 1
Input
a a a b b b
$
Stack
a, a
b, a
,
b,
a
,
$
$
q3
q2
q0
q1
Fall 2006
Costas Busch - RPI
14
Time 2
Input
a a a b b b
a
$
Stack
a, a
b, a
,
b,
a
,
$
$
q3
q2
q0
q1
Fall 2006
Costas Busch - RPI
15
Time 3
Input
a
a
$
a a a b b b
Stack
a, a
b, a
,
b,
a
,
$
$
q3
q2
q0
q1
Fall 2006
Costas Busch - RPI
16
Time 4
Input
a a a b b b
a
a
a
$
Stack
a, a
b, a
,
b,
a
,
$
$
q3
q2
q0
q1
Fall 2006
Costas Busch - RPI
17
Time 5
Input
a a a b b b
a
a
a
$
Stack
a, a
b, a
,
b,
a
,
$
$
q3
q2
q0
q1
Fall 2006
Costas Busch - RPI
18
Time 6
Input
a
a
$
a a a b b b
Stack
a, a
b, a
,
b,
a
,
$
$
q3
q2
q0
q1
Fall 2006
Costas Busch - RPI
19
Time 7
Input
a
$
a a a b b b
Stack
a, a
b, a
,
b,
a
,
$
$
q3
q2
q0
q1
Fall 2006
Costas Busch - RPI
20
Time 8
Input
a a a b b b
$
Stack
a, a
b, a
accept
,
b,
a
,
$
$
q3
q2
q0
q1
Fall 2006
Costas Busch - RPI
21
A string is accepted if there is
a computation such that:
All the input is consumed
AND
The last state is an accepting state
we do not care about the stack contents
at the end of the accepting computation
Fall 2006
Costas Busch - RPI
22
Rejection Example:
Time 0
Input
a a b
$
Stack
current
state
a, a
b, a
, q b, a q , $ $ q
q0
3
2
1
Fall 2006
Costas Busch - RPI
23
Rejection Example:
Time 1
Input
a a b
$
Stack
current
state
a, a
b, a
,
b,
a
,
$
$
q3
q2
q1
q0
Fall 2006
Costas Busch - RPI
24
Rejection Example:
Time 2
Input
a
a a b
$
Stack
current
state
a, a
b, a
,
b,
a
,
$
$
q3
q2
q1
q0
Fall 2006
Costas Busch - RPI
25
Rejection Example:
Time 3
Input
a
a
a a b
$
Stack
current
state
a, a
b, a
,
b,
a
,
$
$
q3
q2
q1
q0
Fall 2006
Costas Busch - RPI
26
Rejection Example:
Time 4
Input
a
a
a a b
$
Stack
current
state
a, a
b, a
,
b,
a
,
$
$
q3
q2
q1
q0
Fall 2006
Costas Busch - RPI
27
Rejection Example:
Time 4
Input
a
a
a a b
$
reject
current
state
a, a
Stack
b, a
,
b,
a
,
$
$
q3
q2
q1
q0
Fall 2006
Costas Busch - RPI
28
There is no accepting computation for
The string
aab
aab
is rejected by the PDA
a, a
b, a
,
b,
a
,
$
$
q3
q2
q0
q1
Fall 2006
Costas Busch - RPI
29
Another PDA example
PDA
M:
a, a
b, b
q0
Fall 2006
L(M ) {vv : v {a, b} }
R
,
a, a
b, b
q1
Costas Busch - RPI
, $ $
q2
30
Basic Idea:
1. Push v
on stack
a, a
b, b
q0
Fall 2006
L(M ) {vv : v {a, b} }
R
3. Match v R on input
with v on stack
2. Guess
middle
of input
,
a, a
b, b
q1
Costas Busch - RPI
4. Match
found
, $ $
q2
31
Execution Example:
Time 0
Input
a b b a
$
a, a
b, b
q0
Fall 2006
,
a, a
b, b
q1
Costas Busch - RPI
, $ $
Stack
q2
32
Time 1
Input
a b b a
a, a
b, b
q0
Fall 2006
,
a
$
a, a
b, b
q1
Costas Busch - RPI
, $ $
Stack
q2
33
Time 2
Input
b
a
$
a b b a
a, a
b, b
q0
Fall 2006
,
a, a
b, b
q1
Costas Busch - RPI
, $ $
Stack
q2
34
Time 3
Input
a b b a
Guess the middle
of string
a, a
b, b
q0
Fall 2006
,
a, a
b, b
q1
Costas Busch - RPI
, $ $
b
a
$
Stack
q2
35
Time 4
Input
b
a
$
a b b a
a, a
b, b
q0
Fall 2006
,
a, a
b, b
q1
Costas Busch - RPI
, $ $
Stack
q2
36
Time 5
Input
a b b a
a, a
b, b
q0
Fall 2006
,
a
$
a, a
b, b
q1
Costas Busch - RPI
, $ $
Stack
q2
37
Time 6
Input
a b b a
$
a, a
b, b
a, a
b, b
Stack
accept
q0
Fall 2006
,
q1
Costas Busch - RPI
, $ $
q2
38
Rejection Example:
Time 0
Input
a b b b
$
a, a
b, b
q0
Fall 2006
,
a, a
b, b
q1
Costas Busch - RPI
, $ $
Stack
q2
39
Time 1
Input
a b b b
a, a
b, b
q0
Fall 2006
,
a
$
a, a
b, b
q1
Costas Busch - RPI
, $ $
Stack
q2
40
Time 2
Input
b
a
$
a b b b
a, a
b, b
q0
Fall 2006
,
a, a
b, b
q1
Costas Busch - RPI
, $ $
Stack
q2
41
Time 3
Input
a b b b
Guess the middle
of string
a, a
b, b
q0
Fall 2006
,
a, a
b, b
q1
Costas Busch - RPI
, $ $
b
a
$
Stack
q2
42
Time 4
Input
b
a
$
a b b b
a, a
b, b
q0
Fall 2006
,
a, a
b, b
q1
Costas Busch - RPI
, $ $
Stack
q2
43
Time 5
There is no possible transition.
Input
a b b b
a, a
b, b
q0
Fall 2006
,
Input is not
consumed
a, a
b, b
q1
Costas Busch - RPI
, $ $
a
$
Stack
q2
44
Another computation on same string:
Input
Time 0
a b b b
$
a, a
b, b
q0
Fall 2006
,
a, a
b, b
q1
Costas Busch - RPI
, $ $
Stack
q2
45
Time 1
Input
a b b b
a, a
b, b
q0
Fall 2006
,
a
$
a, a
b, b
q1
Costas Busch - RPI
, $ $
Stack
q2
46
Time 2
Input
b
a
$
a b b b
a, a
b, b
q0
Fall 2006
,
a, a
b, b
q1
Costas Busch - RPI
, $ $
Stack
q2
47
Time 3
b
b
a
$
Input
a b b b
a, a
b, b
q0
Fall 2006
,
a, a
b, b
q1
Costas Busch - RPI
, $ $
Stack
q2
48
Time 4
b
b
b
a
$
Input
a b b b
a, a
b, b
q0
Fall 2006
,
a, a
b, b
q1
Costas Busch - RPI
, $ $
Stack
q2
49
Time 5
Input
a b b b
a, a
b, b
q0
Fall 2006
,
No accept state
is reached
a, a
b, b
q1
Costas Busch - RPI
, $ $
b
b
b
a
$
Stack
q2
50
There is no computation
that accepts string abbb
abbb L(M )
a, a
b, b
q0
Fall 2006
,
a, a
b, b
q1
Costas Busch - RPI
, $ $
q2
51
Pushing & Popping Strings
Pop
string
Input
symbol
q1
Fall 2006
a , w1 w 2
Costas Busch - RPI
Push
string
q2
52
Example:
q1
a , eb cdf
q2
input
a
a
stack
pop
string
Fall 2006
e
b
h
e
$
top
top
Replace
Costas Busch - RPI
c
d
f
h
e
$
push
string
53
q1
a , eb cdf
Equivalent
transitions
pop
a, e
q1
a, b
,
a, f
Fall 2006
q2
a, d
Costas Busch - RPI
push
a, c q
2
54
Another PDA example
L(M ) {w {a, b} : na (w) nb (w)}
*
PDA
a, $ 0$
a, 0 00
a, 1
b, $ 1$
b, 1 11
b, 0
q1
Fall 2006
M
, $ $
Costas Busch - RPI
q2
55
Execution Example:
Time 0
Input
a b
b b a a
a, $ 0$
a, 0 00
a, 1
current
state
Fall 2006
b, $ 1$
b, 1 11
b, 0
q1
, $ $
Costas Busch - RPI
$
Stack
q2
56
Time 1
Input
a b
b b
a, $ 0$
a, 0 00
a, 1
b, $ 1$
b, 1 11
b, 0
q1
Fall 2006
a a
, $ $
Costas Busch - RPI
0
$
Stack
q2
57
Time 3
Input
a b
b b a a
a, $ 0$
a, 0 00
a, 1
b, $ 1$
b, 1 11
b, 0
q1
Fall 2006
, $ $
Costas Busch - RPI
0
$
Stack
q2
58
Time 4
Input
a b
b b a a
a, $ 0$
a, 0 00
a, 1
b, $ 1$
b, 1 11
b, 0
q1
Fall 2006
, $ $
Costas Busch - RPI
1
$
Stack
q2
59
Time 5
Input
a b
b b a a
a, $ 0$
a, 0 00
a, 1
b, $ 1$
b, 1 11
b, 0
q1
Fall 2006
, $ $
Costas Busch - RPI
1
1
$
Stack
q2
60
Time 6
Input
a b
b b a a
a, $ 0$
a, 0 00
a, 1
b, $ 1$
b, 1 11
b, 0
q1
Fall 2006
, $ $
Costas Busch - RPI
1
1
$
Stack
q2
61
Time 7
Input
a b
b b a a
a, $ 0$
a, 0 00
a, 1
b, $ 1$
b, 1 11
b, 0
q1
Fall 2006
, $ $
Costas Busch - RPI
1
$
Stack
q2
62
Time 8
Input
a b
b b a a
a, $ 0$
a, 0 00
a, 1
b, $ 1$
b, 1 11
b, 0
q1
Fall 2006
, $ $
Costas Busch - RPI
$
Stack
accept
q2
63
Formalities for PDAs
Fall 2006
Costas Busch - RPI
64
q1
a , w1 w 2
q2
Transition function:
(q1, a ,w1 ) {(q2,w2 )}
Fall 2006
Costas Busch - RPI
65
a , w1 w2
q2
q1
a , w1 w3
q3
Transition function:
(q1, a ,w1 ) {(q2,w2 ), (q3,w3 )}
Fall 2006
Costas Busch - RPI
66
Formal Definition
Pushdown Automaton (PDA)
M (Q, Σ, Γ, δ, q0 , z, F )
Accept
states
Input
alphabet
Transition Initial
Stack
function
state
alphabet
Stack
start
symbol
States
Fall 2006
Costas Busch - RPI
67
Instantaneous Description
( q, u , s )
Current
state
Fall 2006
Remaining
input
Costas Busch - RPI
Current
stack
contents
68
Example:
Instantaneous Description
(q1, bbb, aaa$)
Time 4:
Input
a a a b b b
a, a
b, a
a
a
a
$
Stack
,
b,
a
,
$
$
q3
q2
q0
q1
Fall 2006
Costas Busch - RPI
69
Example:
Instantaneous Description
(q2 , bb, aa$)
Time 5:
Input
a a a b b b
a, a
b, a
a
a
a
$
Stack
,
b,
a
,
$
$
q3
q2
q0
q1
Fall 2006
Costas Busch - RPI
70
We write:
(q1, bbb, aaa$) (q2 , bb, aa$)
Time 4
Fall 2006
Time 5
Costas Busch - RPI
71
A computation:
(q0 , aaabbb,$) (q1, aaabbb,$)
(q1, aabbb, a$) (q1, abbb, aa$) (q1, bbb, aaa$)
(q2 , bb, aa$) (q2 , b, a$) (q2 , ,$) (q3 , ,$)
a, a
b, a
,
b,
a
,
$
$
q3
q2
q0
q1
Fall 2006
Costas Busch - RPI
72
(q0 , aaabbb,$) (q1, aaabbb,$)
(q1, aabbb, a$) (q1, abbb, aa$) (q1, bbb, aaa$)
(q2 , bb, aa$) (q2 , b, a$) (q2 , ,$) (q3 , ,$)
For convenience we write:
(q0 , aaabbb,$) (q3 , ,$)
Fall 2006
Costas Busch - RPI
73
Language of PDA
Language
L(M )
accepted by PDA M :
L(M ) {w : (q0 ,w , z ) (qf , , s )}
Initial state
Fall 2006
Costas Busch - RPI
Accept state
74
Example:
(q0 , aaabbb,$) (q3 , ,$)
aaabbb L(M )
PDA M :
a, a
b, a
,
b,
a
,
$
$
q3
q2
q0
q1
Fall 2006
Costas Busch - RPI
75
(q0 , a b ,$) (q3 , ,$)
n n
a b L(M )
n n
PDA M :
a, a
b, a
,
b,
a
,
$
$
q3
q2
q0
q1
Fall 2006
Costas Busch - RPI
76
Therefore:
L( M ) {a b : n 0}
n n
PDA M :
a, a
b, a
,
b,
a
,
$
$
q3
q2
q0
q1
Fall 2006
Costas Busch - RPI
77