1 pushdown stack automata zeph grunschlag. 2 agenda pushdown automata stacks and recursiveness...
Post on 21-Dec-2015
225 views
TRANSCRIPT
17
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 uy) if derives(x,u) return true
3. return false //no successful branch
EG: S # | aSa | bSb
18
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 ?
19
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?
20
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.
21
From CFG’s to Stack Machines
(1)PUSH
(3)POP
read a or b ?Push it
ACCEPT
(2)read # ?(ignore stack)
read == peek ?
PopElse: CRASH!
empty stack?
22
From CFG’s to Stack Machines
(1)PUSH
(3)POP
read a or b ?Push it
ACCEPT
(2)read # ?(ignore stack)
read == peek ?
PopElse: CRASH!
empty stack?
Input:aaab#baa
23
From CFG’s to Stack Machines
(1)PUSH
(3)POP
read a or b ?Push it
ACCEPT
(2)read # ?(ignore stack)
read == peek ?
PopElse: CRASH!
empty stack?
aInput:aaab#baa
24
From CFG’s to Stack Machines
(1)PUSH
(3)POP
read a or b ?Push it
ACCEPT
(2)read # ?(ignore stack)
read == peek ?
PopElse: CRASH!
empty stack?
a aInput:aaab#baa
25
From CFG’s to Stack Machines
(1)PUSH
(3)POP
read a or b ?Push it
ACCEPT
(2)read # ?(ignore stack)
read == peek ?
PopElse: CRASH!
empty stack?
a a aInput:aaab#baa
26
From CFG’s to Stack Machines
(1)PUSH
(3)POP
read a or b ?Push it
ACCEPT
(2)read # ?(ignore stack)
read == peek ?
PopElse: CRASH!
empty stack?
b a a aInput:aaab#baa
27
From CFG’s to Stack Machines
(1)PUSH
(3)POP
read a or b ?Push it
ACCEPT
(2)read # ?(ignore stack)
read == peek ?
PopElse: CRASH!
empty stack?
b a a aInput:aaab#baa
28
From CFG’s to Stack Machines
(1)PUSH
(3)POP
read a or b ?Push it
ACCEPT
(2)read # ?(ignore stack)
read == peek ?
PopElse: CRASH!
empty stack?
a a aInput:aaab#baa
29
From CFG’s to Stack Machines
(1)PUSH
(3)POP
read a or b ?Push it
ACCEPT
(2)read # ?(ignore stack)
read == peek ?
PopElse: CRASH!
empty stack?
a aInput:aaab#baa
30
From CFG’s to Stack Machines
(1)PUSH
(3)POP
read a or b ?Push it
ACCEPT
(2)read # ?(ignore stack)
read == peek ?
PopElse: CRASH!
empty stack?
aInput:aaab#baa
REJECT (nonempty stack)
31
From CFG’s to Stack Machines
(1)PUSH
(3)POP
read a or b ?Push it
ACCEPT
(2)read # ?(ignore stack)
read == peek ?
PopElse: CRASH!
empty stack?
a aInput:aaab#baaa
32
From CFG’s to Stack Machines
(1)PUSH
(3)POP
read a or b ?Push it
ACCEPT
(2)read # ?(ignore stack)
read == peek ?
PopElse: CRASH!
empty stack?
aInput:aaab#baaa
33
From CFG’s to Stack Machines
(1)PUSH
(3)POP
read a or b ?Push it
ACCEPT
(2)read # ?(ignore stack)
read == peek ?
PopElse: CRASH!
empty stack?
Input:aaab#baaa
Pause input
34
From CFG’s to Stack Machines
(1)PUSH
(3)POP
read a or b ?Push it
ACCEPT
(2)read # ?(ignore stack)
read == peek ?
PopElse: CRASH!
empty stack?
Input:aaab#baaa
ACCEPT
35
From CFG’s to Stack Machines
(1)PUSH
(3)POP
read a or b ?Push it
ACCEPT
(2)read # ?(ignore stack)
read == peek ?
PopElse: CRASH!
empty stack?
a aInput:aaab#baaaa
36
From CFG’s to Stack Machines
(1)PUSH
(3)POP
read a or b ?Push it
ACCEPT
(2)read # ?(ignore stack)
read == peek ?
PopElse: CRASH!
empty stack?
aInput:aaab#baaaa
37
From CFG’s to Stack Machines
(1)PUSH
(3)POP
read a or b ?Push it
ACCEPT
(2)read # ?(ignore stack)
read == peek ?
PopElse: CRASH!
empty stack?
Input:aaab#baaaa
Pause input
38
From CFG’s to Stack Machines
(1)PUSH
(3)POP
read a or b ?Push it
ACCEPT
(2)read # ?(ignore stack)
read == peek ?
PopElse: CRASH!
empty stack?
Input:aaab#baaaa
CRASH
39
PDA’s à la Sipser
To aid analysis, theoretical stack machines restrict the allowable operations. Each text-book 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.
40
Sipser’s Version
Becomes:
(1)PUSH
(3)POP
read a or b ?Push it
ACCEPT
(2)read # ?
(ignore stack)
read == peek ?Pop
Else: CRASH!
empty stack?
$
aabb
#
$
aabb
41
Sipser’s Version
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 = : ignore input, don’t read
y = : ignore top of stack and push z
z = : pop y
p qx, y z
55
PDAFormal Definition
DEF: A pushdown automaton (PDA) is a 6-tuple M = (Q, , , , q0, F ). Q, , and q0, are the same as for an FA. is the tape alphabet. is as follows:
So given a state p, an input letter x and a tape letter y, p,x,ygives all (q,z) where q is a target state and z a stack replacement for y.
)(Σ:δ εεε QPQ
56
PDAFormal Definition
Q: What is p,x,yin each case?1. 0,a,b2. 0,,3. 1,a,4. 3,,
0 1 2$
3
aabb
b$
$
aabb
a
57
PDAFormal Definition
A: 1. 0,a,b2. 0,,= {(1,$)}3. 1,a,= {(0,),(1,a)}4. 3,,=
0 1 2$
3
aabb
b$
$
aabb
a
58
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.