You are on page 1of 23

CSC 3130: Automata theory and formal languages

Andrej Bogdanov
http://www.cse.cuhk.edu.hk/~andrejb/csc3130
Pushdown automata
Fall 2008
MELJUN P. CORTES, MBA,MPA,BSCS,ACS
Motivation
• We had two ways to describe regular languages





• How about context-free languages?
regular
expression
DFA NFA
syntactic
computational
CFG pushdown automaton
syntactic
computational
Pushdown automata versus NFA
• Since context-free is more powerful than regular,
pushdown automata must generalize NFAs
state control
0 1 0 0
input
NFA
Pushdown automata
• A pushdown automaton has access to a stack,
which is a potentially infinite supply of memory
state control
0 1 0 0
input
pushdown automaton (PDA)

stack
Pushdown automata
• As the PDA is reading the input, it can push / pop
symbols in / out of the stack
state control
0 1 0 0
input
pushdown automaton (PDA)
Z
0
0 1
stack

1
Rules for pushdown automata
• The transitions are nondeterministic
• Stack is always accessed from the top
• Each transition can pop a symbol from the stack
and / or push another symbol onto the stack
• Transitions depend on input symbol and on last
symbol popped from stack

• Automaton accepts if after reading whole input, it
can reach an accepting state
Example
L = {0
n
#1
n
: n ≥ 0}
state control
0 0 0 #
input
Z
0
a a
stack
a

1 1 1
read 0
push a
read #
read 1
pop a
pop Z
0
Shorthand notation
read 0
push a
read #
read 1
pop a
pop Z
0
0, c / a
#, c / c

c, Z
0
/ c
1, a / c

read, pop / push
Formal definition
A pushdown automaton is (Q, E, I, o, q
0
, Z
0
, F):
– Q is a finite set of states;
– E is the input alphabet;
– I is the stack alphabet, including a special symbol Z
0
;
– q
0
in Q is the initial state;
– Z
0
in I is the start symbol;
– F _ Q is a set of final states;
– o is the transition function
o: Q × (E {c}) × (I {c}) subsets of Q × (I {c})
state input symbol pop symbol state push symbol
Notes on definition
• We use slightly different definition than textbook
• Example
0, c / a
#, c / c

c, Z
0
/ c
1, a / c

o: Q × (E {c}) × (I {c}) → subsets of Q × (I {c})
q
0
q
1
q
2
o(q
0
, 0, c) = {(q
0
, a)}
o(q
0
, 1, c) = ∅
o(q
0
, #, c) = {(q
1
, c)}
o(q
0
, 0, o) = ∅
...
A convention
• Sometimes we denote “transitions” by:


• This will mean:



– Intuitively, pop b, then push c
1
, c
2
, and c
3
a, b / c
1
c
2
c
3

q
0
q
1
c, c / c
2

q
0
q
1
a, b / c
1

c, c / c
3

intermediate
states
Examples
• Describe PDAs for the following languages:
– L = {w#w
R
: w {0, 1}*}, E = {0, 1, #}
– L = {ww
R
: w E*}, E = {0, 1}
– L = {w: w has same number of 0s and 1s}, E = {0, 1}
– L = {0
i
1
j
: i ≤ j ≤ 2i}, E = {0, 1}
Main theorem
A language L is context-free if and only if it
is accepted by some pushdown automaton.
context-free grammar pushdown automaton
From CFGs to PDAs
• Idea: Use PDA to simulate (rightmost) derivations
A → 0A1
A → B
B → #
A
¬ 0A1 ¬ 00A11 ¬ 00B11 ¬ 00#11
PDA control:
CFG:
write start variable

stack:
Z
0
A
replace production in reverse
Z
0
1A0
pop terminals and match Z
0
1A
e, e / A
0, 0 / e
e, A / 1A0
input:
00#11
00#11
0#11
replace production in reverse
Z
0
11A0 e, A / 1A0 0#11
pop terminals and match Z
0
11A 0, 0 / e #11
replace production in reverse
Z
0
11B e, A / B #11
From CFGs to PDAs
• If, after reading whole input, PDA ends up with an
empty stack, derivation must be valid

• Conversely, if there is no valid derivation, PDA
will get stuck somewhere
– Either unable to match next input symbol,
– Or match whole input but stack non empty
Description of PDA for CFGs
• Repeat the following steps:
– If the top of the stack is a variable A:
Choose a rule A → o and substitute A with o
– If the top of the stack is a terminal a:
Read next input symbol and compare to a
If they don’t match, reject (die)
– If top of stack is Z
0
, go to accept state
Description of PDA for CFGs
q
0
q
1
q
2
c, c / S
a, a / c
for every terminal a
c, A / o
k
...o
1
for every production A → o
1
...o
k
c, Z
0
/ c
From PDAs to CFGs
• First, we simplify the PDA:
– It has a single accept state q
f
– Z
0
is always popped exactly before accepting
– Each transition is either a push, or a pop, but not both
context-free grammar pushdown automaton

From PDAs to CFGs
• We look at the stack in an accepting
computation:
a
Z
0
Z
0
a
Z
0
a
Z
0
a
Z
0
a
Z
0
a
Z
0
a
Z
0
a
Z
0
a
Z
0
Z
0
b a c c c
a
portions that preserve the stack
q
0
q
1
q
3
q
1
q
7
q
0
q
1
q
2
q
1
q
3
q
7
A
03
= {x: x leads from q
0
to q
3
and preserves stack}
c
1 1
c
0 1
c c
0 0 0
input
state
stack
q
f
From PDAs to CFGs
a
Z
0
Z
0
a
Z
0
a
Z
0
a
Z
0
a
Z
0
a
Z
0
a
Z
0
a
Z
0
a
Z
0
Z
0
b a c c c
a
q
0
q
1
q
3
q
1
q
7
q
1
q
2
q
1
q
7
c
1 1
c
0 1
c c
0 0 0
input
state
stack
A
11
A
03
0
c
A
11
→ 0A
03
c
q
0
q
3
q
f
From PDAs to CFGs
a
Z
0
Z
0
a
Z
0
a
Z
0
a
Z
0
a
Z
0
a
Z
0
a
Z
0
a
Z
0
a
Z
0
Z
0
b a c c c
a
q
0
q
1
q
3
q
1
q
7
q
1
q
2
q
1
q
7
c
1 1
c
0 1
c c
0 0 0
input
state
stack
A
03
A
13
A
13
→ A
10
A
03

q
0
q
3
A
10
q
f
From PDAs to CFGs
q
i
q
j
q
i’
q
j’
A
ij
→ aA
i’j’
b
q
i
q
j
q
k
A
ik
→ A
ij
A
jk

q
i
A
ii
→ c
variables:
A
ij

q
f
a, Z
0
/c
q
i
A
0f
→ A
0i
a
start variable:
A
0f

Example
0, c / a
#, c / c

c, Z
0
/ c
1, a / c

q
0
q
1
q
2
start variable: A
02
productions:

A
00
→ A
00
A
00

A
00
→ A
01
A
10

A
00
→ A
03
A
30

A
01
→ A
01
A
11

A
01
→ A
02
A
21

A
00
→ c
...
A
11
→ c
A
22
→ c
A
01
→ 0A
01
1
A
01
→ #A
33

A
33
→ c
0, c / a
#, c / $

c, Z
0
/ c
1, a / c

q
0
q
1
q
2
q
3
c, $ / c

A
02
→ A
01