Sie sind auf Seite 1von 27

SSK5204

Chapter 6: Pushdown Automata


Dr. Nor Fazlida Mohd Sani, Dept. of Computer Science,
Fac. of Computer Science and Information Technology, UPM.

Introduction
New type of computational model called pushdown
automata (PDA).
This automata are like nondeterministic finite
automata but have an extra component called stack.
The stack allows pushdown automata to recognize
some nonregular languages.
PDA are equivalent in power to CFG, in terms of
recognizing the same language.

Pushdown automata

Motivation
regular
expression

NFA

syntactic

DFA
computational

is more powerful than

CFG

pushdown automaton

syntactic

computational

Pushdown automata versus NFA


state control

NFA

input
0 1 0 0

Pushdown automata

state control

input
0 1 0 0

stack

pushdown automaton (PDA)


A PDA is like an NFA with but with an infinite stack
6

Pushdown automata
state control

input
0 1 0 0
0 1
1
stack
$

pushdown automaton (PDA)


As the PDA is reading the input, it can
push / pop symbols from the top of the stack
7

Building a PDA
state control
push $
read 0
push x
read 1
pop x
read 1
pop x
pop $

L = {0n1n: n 1}
We remember each 0
by pushing x onto the stack
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
state control
push $
read 0
push x
read 1
pop x
read 1
pop x
pop $

L = {0n1n: n 1}
input
0 0 0 1 1 1
x x x
stack
$

Notation for PDAs


q0

push $

e, e / $

read 0
push x
read 1
pop x
read 1
pop x
pop $

q1

1, x / e
q2

10

1, x / e

e, $ / e
q3

read, pop / push

0, e / x

Definition of a PDA
A pushdown automaton is (Q, , , , q0, F) where:

Q is a finite set of states;


is the input alphabet;
is the stack alphabet
q0 in Q is the initial state;
F Q is a set of final states;
is the transition function

: Q ( {e}) ( {e}) subsets of Q ( {e})


state input symbol

11

pop symbol

state push symbol

Example
q0

= {0, 1}
= {$, x}

e, e / $
q1

0, e / x

(q0, e, e) = {(q1, $)}


(q0, e, $) =
(q0, e, x) =
(q0, 0, e) =
...

1, x / e
q2

1, x / e
e, $ / e

q3

: Q ( {e}) ( {e}) subsets of Q ( {e})


state input symbol
12

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 * that can lead the PDA to an
accepting state

13

Example 1
L = {w#wR: w {0, 1}*}

= {0, 1, #}
= {0, 1}

#, 0#0, 01#10 L
e, 01#1, 0##0 L

q0

e, e / $

q1

#, e / e

0, e / 0
1, e / 1

q2

e, $ / e

q3

0, 0 / e
1, 1 / e

write w on stack
read wR from stack
14

Example 2
L = {wwR: w *}

= {0, 1}

e, 00, 0110 L
1, 011, 010 L

q0

e, e / $

q1

e, e / e

0, e / 0
1, e / 1

q2

e, $ / e

0, 0 / e
1, 1 / e

guess middle of string


15

q3

Example 3
L = {w: w = wR, w *}
e, 1, 00, 010, 0110 L

0110110110 or 011010110
x
xR
x
xR

011 L

q0

e, e / $

= {0, 1}

q1

0, e / 0
1, e / 1

e, e / e
0, e / e
1, e / e

q2

e, $ / e

0, 0 / e
1, 1 / e

middle symbol can be e, 0, or 1


16

q3

Example 4
L = {0n1m0m1n | n 0, m 0}

0, e / 0
q0

e, e / $

q1

1, e / 1

e, e / e

q2

input: 0n 1m 0m 1n
e, e / e

q5

e, $ / e

q4

1, 0/ e
17

= {0, 1}

e, e / e

q3

0, 1 / e

stack: 0n 1m

Example 5
L = {w: w has same number 0s and 1s}

= {0, 1}

Strategy: Stack keeps track of excess of 0s or 1s


If at the end, stack is empty, number is equal

q0

e, e / $
0, e / 0
0, 1 / e

18

q1

e, $ / e
1, e / 1
1, 0 / e

q3

Example 5
L = {w: w has same number 0s and 1s}

= {0, 1}

Invariant: In every execution of the PDA:


#1 - #0 on stack = #1 - #0 in input so far

q0

e, e / $
0, e / 0
0, 1 / e

q1

e, $ / e

q3

1, e / 1
1, 0 / e

If w is not in L, it must be rejected


19

Example 5
L = {w: w has same number 0s and 1s}

= {0, 1}

Property: In some execution of the PDA:


stack consists only of 0s or only of 1s (or e)

q0

e, e / $
0, e / 0
0, 1 / e

q1

e, $ / e

q3

1, e / 1
1, 0 / e

If w is in L, some execution will accept


20

Example 5
L = {w: w has same number 0s and 1s}
q0

e, e / $

q1

21

q3

1, e / 1
1, 0 / e

0, e / 0
0, 1 / e

w = 001110

e, $ / e

read

stack

0
0
1
1
1
0

$0
$00
$0
$
$1
$

= {0, 1}

CFG PDA conversions

22

CFGs and PDAs


L has a context-free grammar if and only if it
is accepted by some pushdown automaton.

context-free grammar

23

pushdown automaton

A convention

When we have a sequence of transitions like:


q0

e, e / c

x, a / b

e, e / d

pop a, then push b, c, and d

We will abbreviate it like this:


q0

x, a / bcd

q1

replace a by bcd on stack


24

q1

Converting a CFG to a PDA

Idea: Use PDA to simulate derivations

A 0A1
AB
B#

A 0A1 00A11 00B11 00#11

PDA control:

write start variable

e, e / A

replace production in reverse e, A / 1A0


pop terminal and match

0, 0 / e

replace production in reverse e, A / 1A0


pop terminal and match

0, 0 / e

replace production in reverse e, A / B

25

stack:

input:

$A

00#11

$1A0

00#11

$1A

0#11

$11A0

0#11

$11A

#11

$11B

#11

Converting a CFG to a PDA


A 0A1
AB
B#

q0

e, A / 1A0
e, A / B
e, B / #

CFG
input
stack

e, e / $A

q1

e, $ / e

q2

0, 0 / e
1, 1 / e
#, # / e

00#11
$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
$

A 0A1 00A11 00B11 00#11


26

General PDA to CFG conversion


a, a / e

e, A / ak...a1

for every production A a1...ak

for every terminal a

q0

27

e, e / $S

q1

e, $ / e

q2

Das könnte Ihnen auch gefallen