ppt

advertisement
Pushdown Stack Automata
Zeph Grunschlag
1
Agenda
Pushdown Automata


Stacks and recursiveness
Formal Definition
2
From CFG’s to Stack Machines
CFG’s naturally define recursive procedure:
boolean derives(strings x, y)
1. if (x==y) return true
2. for(all uy)
if derives(x,u) return true
3. return false //no successful branch
EG:
S  # | aSa | bSb
17
From CFG’s to Stack Machines
By general principles, can carry out any
recursive computation on a stack. Can
do it on a restricted version of an
activation record stack, called a
“Pushdown (Stack) Automaton” or PDA
for short.
Q: What is the language generated by
S  # | aSa | bSb ?
18
From CFG’s to Stack Machines
A: Palindromes in {a,b,#}* containing
exactly one #-symbol.
Q: Using a stack, how can we recognize
such strings?
19
From CFG’s to Stack Machines
A: Use a three phase process:
1. Push mode: Before reading “#”, push
everything on the stack.
2. Reading “#” switches modes.
3. Pop mode: Read remaining symbols
making sure that each new read symbol is
identical to symbol popped from stack.
Accept if able to empty stack completely.
Otherwise reject, and reject if could not pop
somewhere.
20
From CFG’s to Stack Machines
read a or b ?
Push it
(1)
PUSH
read == peek ?
Pop
Else: CRASH!
(2)
read # ?
(ignore stack)
(3)
POP
empty
stack?
ACCEPT
21
From CFG’s to Stack Machines
read == peek ?
Pop
Else: CRASH!
read a or b ?
Push it
(1)
PUSH
(2)
read # ?
(ignore stack)
(3)
POP
empty
stack?
ACCEPT
Input:
aaab#baa
22
From CFG’s to Stack Machines
read == peek ?
Pop
Else: CRASH!
read a or b ?
Push it
(1)
PUSH
(2)
read # ?
(ignore stack)
Input:
aaab#baa
(3)
POP
empty
stack?
ACCEPT
a
23
From CFG’s to Stack Machines
read == peek ?
Pop
Else: CRASH!
read a or b ?
Push it
(1)
PUSH
(2)
read # ?
(ignore stack)
Input:
aaab#baa
(3)
POP
empty
stack?
ACCEPT
a a
24
From CFG’s to Stack Machines
read == peek ?
Pop
Else: CRASH!
read a or b ?
Push it
(1)
PUSH
(2)
read # ?
(ignore stack)
Input:
aaab#baa
(3)
POP
empty
stack?
ACCEPT
a a a
25
From CFG’s to Stack Machines
read == peek ?
Pop
Else: CRASH!
read a or b ?
Push it
(1)
PUSH
(2)
read # ?
(ignore stack)
Input:
aaab#baa
(3)
POP
empty
stack?
ACCEPT
b a a a
26
From CFG’s to Stack Machines
read == peek ?
Pop
Else: CRASH!
read a or b ?
Push it
(1)
PUSH
(2)
read # ?
(ignore stack)
Input:
aaab#baa
(3)
POP
empty
stack?
ACCEPT
b a a a
27
From CFG’s to Stack Machines
read == peek ?
Pop
Else: CRASH!
read a or b ?
Push it
(1)
PUSH
(2)
read # ?
(ignore stack)
Input:
aaab#baa
(3)
POP
empty
stack?
ACCEPT
a a a
28
From CFG’s to Stack Machines
read == peek ?
Pop
Else: CRASH!
read a or b ?
Push it
(1)
PUSH
(2)
read # ?
(ignore stack)
Input:
aaab#baa
(3)
POP
empty
stack?
ACCEPT
a a
29
From CFG’s to Stack Machines
read == peek ?
Pop
Else: CRASH!
read a or b ?
Push it
(1)
PUSH
(2)
read # ?
(ignore stack)
Input:
aaab#baa
(3)
POP
empty
stack?
ACCEPT
a
REJECT (nonempty stack)
30
From CFG’s to Stack Machines
read == peek ?
Pop
Else: CRASH!
read a or b ?
Push it
(1)
PUSH
(2)
read # ?
(ignore stack)
Input:
aaab#baaa
(3)
POP
empty
stack?
ACCEPT
a a
31
From CFG’s to Stack Machines
read == peek ?
Pop
Else: CRASH!
read a or b ?
Push it
(1)
PUSH
(2)
read # ?
(ignore stack)
Input:
aaab#baaa
(3)
POP
empty
stack?
ACCEPT
a
32
From CFG’s to Stack Machines
read == peek ?
Pop
Else: CRASH!
read a or b ?
Push it
(1)
PUSH
(2)
read # ?
(ignore stack)
Input:
aaab#baaa
(3)
POP
Pause input
empty
stack?
ACCEPT
33
From CFG’s to Stack Machines
read == peek ?
Pop
Else: CRASH!
read a or b ?
Push it
(1)
PUSH
(2)
read # ?
(ignore stack)
Input:
aaab#baaa
(3)
POP
ACCEPT
empty
stack?
ACCEPT
34
From CFG’s to Stack Machines
read == peek ?
Pop
Else: CRASH!
read a or b ?
Push it
(1)
PUSH
(2)
read # ?
(ignore stack)
Input:
aaab#baaaa
(3)
POP
empty
stack?
ACCEPT
a a
35
From CFG’s to Stack Machines
read == peek ?
Pop
Else: CRASH!
read a or b ?
Push it
(1)
PUSH
(2)
read # ?
(ignore stack)
Input:
aaab#baaaa
(3)
POP
empty
stack?
ACCEPT
a
36
From CFG’s to Stack Machines
read == peek ?
Pop
Else: CRASH!
read a or b ?
Push it
(1)
PUSH
(2)
read # ?
(ignore stack)
Input:
aaab#baaaa
(3)
POP
Pause input
empty
stack?
ACCEPT
37
From CFG’s to Stack Machines
read == peek ?
Pop
Else: CRASH!
read a or b ?
Push it
(1)
PUSH
(2)
read # ?
(ignore stack)
Input:
aaab#baaaa
CRASH
(3)
POP
empty
stack?
ACCEPT
38
PDA’s à la Sipser
To aid analysis, theoretical stack machines
restrict the allowable operations. Each textbook author has his own version. Sipser’s
machines are especially simple:
Push/Pop rolled into a single operation:
replace top stack symbol
No intrinsic way to test for empty stack
Epsilon’s used to increase functionality, result
in default nondeterministic machines.
39
Sipser’s Version
read a or b ?
Push it
(1)
PUSH
Becomes:
e , e$
(2)
read # ?
(ignore stack)
a , ea
b , eb
#, ee
read == peek ?
Pop
Else: CRASH!
(3)
POP
empty
stack?
ACCEPT
a , ae
b , be
e , $e
40
Sipser’s Version
p
x, y z
q
Meaning of labeling convention:
If at p and next input x and top stack y,
then go to q and replace y by z on stack.
x = e: ignore input, don’t read
y = e: ignore top of stack and push z
z = e: pop y
41
Sipser’s Version
a , ea
b , eb
e , e$
#, ee
a , ae
b , be
e , $e
push $ to
detect
empty
stack
42
Sipser’s Version
a , ea
b , eb
e , e$
#, ee
a , ae
b , be
e , $e
Input:
aaab#baaa
43
Sipser’s Version
a , ea
b , eb
e , e$
Input:
aaab#baaa
#, ee
a , ae
b , be
e , $e
$
44
Sipser’s Version
a , ea
b , eb
e , e$
Input:
aaab#baaa
#, ee
a , ae
b , be
e , $e
a $
45
Sipser’s Version
a , ea
b , eb
e , e$
Input:
aaab#baaa
#, ee
a , ae
b , be
e , $e
a a $
46
Sipser’s Version
a , ea
b , eb
e , e$
Input:
aaab#baaa
a , ae
b , be
#, ee
e , $e
a a a $
47
Sipser’s Version
a , ea
b , eb
e , e$
Input:
aaab#baaa
#, ee
a , ae
b , be
e , $e
b a a a $
48
Sipser’s Version
a , ea
b , eb
e , e$
Input:
aaab#baaa
#, ee
a , ae
b , be
e , $e
b a a a $
49
Sipser’s Version
a , ea
b , eb
e , e$
Input:
aaab#baaa
a , ae
b , be
#, ee
e , $e
a a a $
50
Sipser’s Version
a , ea
b , eb
e , e$
Input:
aaab#baaa
#, ee
a , ae
b , be
e , $e
a a $
51
Sipser’s Version
a , ea
b , eb
e , e$
Input:
aaab#baaa
#, ee
a , ae
b , be
e , $e
a $
52
Sipser’s Version
a , ea
b , eb
e , e$
Input:
aaab#baaa
#, ee
a , ae
b , be
e , $e
$
53
Sipser’s Version
a , ea
b , eb
e , e$
Input:
aaab#baaa
#, ee
a , ae
b , be
e , $e
ACCEPT!
54
PDA
Formal Definition
DEF: A pushdown automaton (PDA) is
a 6-tuple M = (Q, S, G, d, q0, F ). Q, S, and
q0, are the same as for an FA. G is the tape
alphabet. d is as follows:
δ : Q  Σ ε  Gε  P (Q  Gε )
So given a state p, an input letter x and a tape
letter y, d(p,x,y) gives all (q,z) where q is a
target state and z a stack replacement for y.
55
PDA
Formal Definition
a , e e
0
Q:
1.
2.
3.
4.
a , ea
b , eb
1
a , ae
b , be
2
e , e$
b, e$
e , $e
What is d(p,x,y) in each case?
d(0,a,b)
d(0,e,e)
d(1,a,e)
d(3,e,e)
3
56
PDA
Formal Definition
a , e e
0
e , e$
a , ea
b , eb
1
b, e$
a , ae
b , be
2
e , $e
3
A:
1.
2.
3.
4.
d(0,a,b) = 
d(0,e,e) = {(1,$)}
d(1,a,e) = {(0,e),(1,a)}
d(3,e,e) = 
57
PDA Exercise
(Sipser 2.6.a) Draw the PDA acceptor for
L = { x  {a,b}* | na(x) = 2nb(x) }
NOTE: The empty string is in L.
58
PDA Example.
No-Bubbles
e, eA
a , ae
a, $ $
a, AA
e, Ae
e , e$
b, $ $
b, a a
e, ea
b, Ae
e, $$
e , $e
e, ea
61
Download