FL 4
FL 4
FL 4
Pushdown Automata
pushdown automata(PDA) is a way to implement
a context-free grammar in a similar way we design
DFA for a regular grammar.
A DFA can remember a finite amount of
information, but a PDA can remember an infinite
amount of information.
Basically a pushdown automaton is −
"Finite state machine" + "a stack“
A pushdown automaton has three components −
1. input tape,
2. control unit, and
3. stack with infinite size.
A stack does two operations −
Push − a new symbol is added at the top.
Pop − the top symbol is read and removed.
A PDA may or may not read an input symbol,
but it has to read the top of the stack in every
transition.
A Pushdown Automata is defined by a 7 tuple
(Q,,,,q0, Z0, F), with
• Q finite set of states
• finite input alphabet
• finite stack alphabet(symbols)
• q0 start state
• Z0: Initial stack
• F Final/accepting states
• transition function
: Q × (Σ ∪ {λ}) × Γ → set of finite subsets of Q × Γ*
Initial Stack Symbol
Stack Stack
$ Z0
top
bottom
special symbol
The following diagram shows a transition in a PDA
from a state q1 to state q2, labeled as a,b → c −
a a
stack
b top c
Replac
h e h
e e
Z0 Z0
a, c
q1 q2
input
a a
stack
c
b top b
Pus
h h h
e e
Z0 Z0
a, b
q1 q2
input
a a
stack
b top
h Pop
h
e e
Z0 Z0
a,
q1 q2
input
a a
stack
b top b
No
h Change h
e e
Z0 Z0
A Possible Transition
a,Z0 →
q1 q2
a a
stack empty
top Pop
Z0
A Bad Transition
a, b c
q1 q2
input
a
Empty stack
HALT
a
Empty stack
HALT
x, y z
q1 q2
Empty stack
A Good Transition
a, Z0 → b
q1 q2
a a
stack
Z0
top Pop
b
Accepting Criteria of PDA
There are two criteria for string acceptance by PDA.
First Criteria (final state and empty stack)
An input string x is accepted by the PDA if the PDA stops at a
final state and the stack is empty. Otherwise, the input string
is
rejected.
Second Criteria (final state only)
An input string x is accepted by the PDA if the PDA stops at a
final state no matter what symbols in the stacks.
Example 1
stack symbols A and B represent the input a and b
Input = abcba
b /B b B/
a /A a A/
Q = {q0 , q1 }
å = {a, b ,c } c /
> q0 q1
q0= { q0 }
G = {A ,B}
F = { q1 }
stack
B
(q0, a, ) = { [q0 , A] }
A
(q0, b, ) = { [q0 , B] }
(q0, c, ) = { [q1 , ] } Z0
(q1, b, B) = { [q1 , ] }
(q , a, A) = { [q , ] } 18
Example 2 aabb
a /A b A/
b A/
> q0 q1
Q = {q0 , q1 }
å = {a, b }
å q0 = { q0 }
G = {A}
stack
F = {q0 , q1 }
A
(q0, a, ) = { [q0 , A] }
(q0, a, ) = { [q0 , A] } A
(q0, b, A) = { [q1 , ] }
Z0
(q1, b, A) = { [q1 , ] }
19
Construct a PDA that accepts L={0n1n |n>0}
Is 0011 accepted?
Step 3 Step 4
Step 2 PUSH 0 POP 0
PUSH 0
Step 1 0 0
Initial stack
0 0 0
Z0 Z0 Z0 Z0
Step 5
POP 0
Step 6
Empty
0 POP Z0
stack
Z0 Z0
0011 is accepted
Nondeterministic Push-Down
Automata(NPDA)
NPDA = NFA + stack for memory.
You can only access the top element of the stack.
A stack does two operations −
Push − a new symbol is added at the top.
Pop − the top symbol is read and removed.
Symbols from the input string must be read one symbol at
a time. You cannot back up.
The current configuration (state, string, stack) of the NPDA
includes:
The current state
the remaining symbols left in the input string, and
the entire contents of the stack
NPDA consists of
Input file, Control unit, Stack
Output
output is yes/no
Each move
reads a symbol from the input
-moves are legal
pops a symbol from the stack
no move is possible if stack is empty
pushes a string, right-to-left, onto the stack
move to the target state
A deterministic pushdown accepter (which we have
not yet considered) must have only one transition for
any given input symbol and stack symbol.
a, a b, a
q0 , q b, a q , Z0→ Z0 q
1 2 3
Execution Example: Time 0
Input
a a a b b b
Z0
Stack
current
state
a, a b, a
, q b, a q , Z0→ Z0 q
q0 1 2 3
Time 1
Input
a a a b b b
Z0
Stack
a, a b, a
q0 , q1 b, a q2
, Z0→ Z0
q3
Time 2
Input
a a a b b b a
Z0
Stack
a, a b, a
q0 , q1 b, a q2
, Z0→ Z0
q3
Time 3
Input
a
a a a b b b a
Z0
Stack
a, a b, a
q0 , q1 b, a q2
, Z0→ Z0
q3
Time 4
Input
a
a
a a a b b b a
Z0
Stack
a, a b, a
q0 , q1 b, a q2
, Z0→ Z0
q3
Time 5
Input
a
a
a a a b b b a
Z0
Stack
a, a b, a
q0 , q1 b, a q2
, Z0→ Z0
q3
Time 6
Input
a
a a a b b b a
Z0
Stack
a, a b, a
q0 , q1 b, a q2
, Z0→ Z0
q3
Time 7
Input
a a a b b b a
Z0
Stack
a, a b, a
q0 , q1 b, a q2
, Z0→ Z0
q3
Time 8
Input
a a a b b b
Z0
Stack
a, a b, a
accept
q0 , q1 b, a q2
, Z0→ Z0
q3
NPDA -- Another example
NPDA
M
R
L( M ) {ww }
a, a a, a
b, b b, b
q0 , q1
, Z0→ Z0 q2
Execution Example: Time 0
Input
a b b a
Z0
Stack
a, a a, a
b, b b, b
q0 , q1
, Z0→ Z0 q2
Time 1
Input
a b b a
a
Z0
Stack
a, a a, a
b, b b, b
q0 , q1
, Z0→ Z0 q2
Time 2
Input
b
a b b a
a
Z0
Stack
a, a a, a
b, b b, b
q0 , q1
, Z0→ Z0 q2
Time 3
Input
b
a b b a
Guess the middle a
of string
Z0
Stack
a, a a, a
b, b b, b
q0 , q1
, Z0→ Z0 q2
Time 4
Input
b
a b b a
a
Z0
Stack
a, a a, a
b, b b, b
q0 , q1
, Z0→ Z0 q2
Fall 2004 COMP 335 41
Time 5
Input
a b b a
a
Z0
Stack
a, a a, a
b, b b, b
, , Z0→ Z0 q2
q0 q1
Time 6
Input
a b b a
Z0
Stack
a, a a, a
b, b b, b
accept
, , Z0→ Z0
q0 q1 q2