You are on page 1of 30

CMSC 214 Principles of Programming Language

Pushdown automata
2
nd
Semester 2013-2014
Presented by EOA
Motivation
regular
expression
DFA NFA
syntactic
computational
CFG pushdown automaton
syntactic
computational
is more powerful than
Pushdown automata versus NFA
state control
0 1 0 0
input
NFA
Pushdown automata
state control
0 1 0 0
input
pushdown automaton (PDA)

stack
A PDA is like an NFA with but with an infinite stack
Pushdown automata
state control
0 1 0 0
input
pushdown automaton (PDA)
$ 0 1
stack

1
As the PDA is reading the input, it can
push / pop symbols from the top of the stack
Building a PDA
L = {0
n
1
n
: n 1}
state control
We remember each 0
by pushing x onto the stack
read 1
pop x
read 0
push x
pop $
push $
read 1
pop x
When we see a 1, we
pop an x from the stack
We want to accept when
we hit the stack bottom
Well use t$ mark bottom
A PDA in action
L = {0
n
1
n
: n 1}
state control
0 0 0 1
input
$ x x
stack
x

1 1
read 0
push x
read 1
read 1
pop x
pop $
push $
pop x
Notation for PDAs
read, pop / push
read 1
pop x
read 0
push x
pop $
push $
read 1
pop x
1, x / c
0, c / x
c, $ / c
c, c / $
1, x / c
q
0
q
1
q
2
q
3
Definition of a PDA
A pushdown automaton is (Q, E, I, o, q
0
, F) where:
Q is a finite set of states;
E is the input alphabet;
I is the stack alphabet
q
0
in Q is the initial state;
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
Example
o: Q (E {c}) (I {c}) subsets of Q (I {c})
o(q
0
, c, c) = {(q
1
, $)}
o(q
0
, c, $) =
o(q
0
, c, x) =
o(q
0
, 0, c) =
...
E = {0, 1}
I = {$, x}
1, x / c
0, c / x
c, $ / c
c, c / $
1, x / c
q
0
q
1
q
2
q
3
state input symbol
pop symbol
state push symbol
The language of a PDA
A PDA is nondeterministic
Multiple transitions on same pop/input allowed

Transitions may but do not have to push or pop
The language of a PDA is the set of all
strings in E* that can lead the PDA to an
accepting state
Example 1
L = {w#w
R
: w e {0, 1}*} E = {0, 1, #}
#, 0#0, 01#10 e L
c, 01#1, 0##0 e L
0, c / 0
q
1
1, c / 1
0, 0 / c
1, 1 / c
c, c / $
q
0
q
3
c, $ / c
q
2
#, c / c
write w on stack read w
R
from stack
I = {0, 1}
Example 2
0, c / 0
q
1
1, c / 1
c, c / $
q
0
q
3
c, $ / c
0, 0 / c
1, 1 / c
q
2
c, c / c
L = {ww
R
: w e E*} E = {0, 1}
c, 00, 0110 e L
1, 011, 010 e L
guess middle of string
Example 3
L = {w: w = w
R
, w e E*} E = {0, 1}
c, 1, 00, 010, 0110 e L
011 e L
0110110110 011010110
or
x x x
R
x
R
0, c / 0
q
1
1, c / 1
c, c / $
q
0
q
3
c, $ / c
c, c / c
0, c / c
1, c / c
0, 0 / c
1, 1 / c
q
2
middle symbol can be c, 0, or 1
Example 4
L = {0
n
1
m
0
m
1
n
| n > 0, m > 0} E = {0, 1}
0, c / 0
q
1
c, c / c
1, c / 1
q
2
c, c / $
q
0
c, $ / c
q
5
c, c / c
q
3
0, 1 / c
c, c / c
q
4
1, 0/ c
0
n
1
m
0
m
1
n
0
n
1
m
input:
stack:
Example 5
L = {w: w has same number 0s and 1s} E = {0, 1}
Strategy: Stack keeps track of excess of 0s or 1s
If at the end, stack is empty, number is equal
0, c / 0
q
1
1, c / 1
c, c / $
q
0
q
3
c, $ / c
0, 1 / c 1, 0 / c
Example 5
L = {w: w has same number 0s and 1s} E = {0, 1}
Invariant: In every execution of the PDA:
0, c / 0
c, c / $
q
0
q
1
1, c / 1
q
3
c, $ / c
0, 1 / c 1, 0 / c
#1 #0 on stack = #1 #0 in input so far
If w is not in L, it must be rejected
Example 5
L = {w: w has same number 0s and 1s} E = {0, 1}
Property: In some execution of the PDA:
0, c / 0
c, c / $
q
0
q
1
1, c / 1
q
3
c, $ / c
0, 1 / c 1, 0 / c
stack consists only of 0s or only of 1s (or c)
If w is in L, some execution will accept
Example 5
L = {w: w has same number 0s and 1s} E = {0, 1}
0, c / 0
c, c / $
q
0
q
1
1, c / 1
q
3
c, $ / c
0, 1 / c 1, 0 / c
w = 001110
read stack
0 $0
0 $00
1 $0
1 $
1 $1
0 $
Example 6
L = {w: w has two 0-blocks with same number of 0s
Strategy:
Detect start of first 0-block
Push 0s on stack
01011, 001011001, 10010101001 01001000, 01111
allowed not allowed
Detect start of second 0-block
Pop 0s from stack
Example 6
1 Detect start of first 0-block 2 Push 0s on stack
3 Detect start of second 0-block 4 Pop 0s from stack
1
1
1
0, 1
0, 0 / c
0, c / 0
0, c / 0
q
0
q
1
0, 1
1
c
c
1
2
3
4
Example 6
L = {w: w has two 0-blocks with same number of 0s
q
2
0, c / 0
q
3
0, c / 0
1, c / c
q
4
1, c / c
1, c / c
0, c / c
1, c / c
q
5
0, 0 / c
0, c / c
c, c / $
q
0
q
1
1, c / c
1, c / c
c, c / $
1, c / c
q
6
0, c / c
1, c / c
q
7
c, $ / c
c, $ / c
CFG PDA conversions
CFGs and PDAs
L has a context-free grammar if and only if it
is accepted by some pushdown automaton.
context-free grammar pushdown automaton
A convention
When we have a sequence of transitions like:



We will abbreviate it like this:
x, a / bcd
q
0
q
1
c, c / c
q
0
q
1
x, a / b c, c / d
pop a, then push b, c, and d
replace a by bcd on stack
Converting a CFG to a PDA
Idea: Use PDA to simulate derivations
A 0A1
A B
B #
A 0A1 00A11 00B11 00#11
write start variable

$A
replace production in reverse
$1A0
pop terminal and match
$1A
c, c / A
0, 0 / c
c, A / 1A0
00#11
00#11
0#11
replace production in reverse
$11A0 c, A / 1A0 0#11
pop terminal and match
$11A 0, 0 / c #11
replace production in reverse
$11B c, A / B #11
PDA control: stack: input:
Converting a CFG to a PDA
A 0A1
A B
B #
CFG
0, 0 / c
1, 1 / c
#, # / c
c, A / 1A0
c, A / B
c, B / #
q
0
c, c / $A
c, $ / c
q
1
q
2
$A
00#11
$1A0
00#11
$1A
00#11
$11A0
00#11
$11A
00#11
$11B
00#11
$11#
00#11
$11
00#11
$1
00#11
$
00#11
input
stack
A 0A1 00A11 00B11 00#11
General PDA to CFG conversion
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, $ / c
Andrej Bogdanov
http://www.cse.cuhk.edu.hk/~andrejb/csc3130
The Chinese University of Hong Kong
REFERENCES
F. D. Lewis
http://www.cs.uky.edu/~lewis/texts/theory/automata/pd-auto.pdfDepartment
of Computer Science -University of Kentucky

You might also like