Transcript Module 32

Module 32
• Pushdown Automata (PDA’s)
– definition
– Example
• We define configurations and computations
of PDA’s
• We define L(M) for PDA’s
1
Pushdown Automata
Definition and Motivating Example
2
Pushdown Automata (PDA)
• In this presentation we introduce the PDA
model of computation (programming
language).
– The key addition to a PDA (from an NFA-/\) is
the addition of external memory in the form of
an infinite capacity stack
• The word “pushdown” comes from the stacks of
trays in cafeterias where you have to pushdown on
the stack to add a tray to it.
3
NFA for
a
b
/\
I
m
n
{a b
/\
B
C
• What strings end up in
each state of the above
NFA?
– I:
– B:
– C:
| m,n ≥ 0}
• Consider the language
{anbn | n ≥ 0}.
• This NFA can recognize
strings which have the
correct form,
– a’s followed by b’s.
• However, the NFA cannot
remember the relative
number of a’s and b’s seen
at any point in time.
4
PDA for
a
b
/\
I
/\
B
C
NFA
a;push a
I
Initialize stack to empty
|n≥0} *
Imagine we now have memory in the form of a stack which
we can use to help remember how many a’s we have seen by
pushing onto and popping from the stack
When we see an a in state I, we do the following two actions:
1) We push an a on the stack.
2) We stay in state I.
b;pop
/\
n
n
{a b
When we see a b in state B, we do the following two actions:
1) We pop an a from the stack.
/\;only if stack is empty
2) We stay in state B.
PDA
B
C
From state B, we allow a /\-transition to state C only if
1) The stack is empty.
Finally, when we begin, the stack should be empty.
5
Formal PDA definition
• PDA M = (Q, S, G, q0, Z, A, d)
• Modified elements
– G is the stack alphabet
• Z is a special character that is initially on the stack
• Often used to represent an empty stack
– d is modified as follows
• Pop to read the top character on the stack
• Stack update action
– What to push back on the stack
– If we push /\, then the net result of the action is a pop
6
Example PDA
a;a; aa
a;Z; aZ
I
Initialize stack to
only contain Z
/\;Z;Z
/\;a;a
b;a;/\
B
/\;Z;Z
C
Q = {I, B, C}
S = {a,b}
G = {Z, a}
q0 = I
Z is the initial stack character
A = {C}
d:
Example PDA
S
I
I
I
I
B
B
a
a
a
/\
/\
b
/\
TopSt
a
Z
a
Z
a
Z
NS
I
I
B
B
B
C
stack update
push aa
push aZ
push a
push Z
push /\
push Z
7
Computing with PDA’s *
• Configurations change compared with NFA-/\’s
– Configuration components:
• current state
• remaining input to be processed
• stack contents
• Computations are essentially the same as with
NFA-/\’s given the modified configurations
– Determining which transitions of a PDA can be
applied to a given configuration is more
complicated though
8
Computation Graph of PDA
Q = {I, B, C}
S = {a,b}
G = {Z, a}
q0 = I
Z is the initial stack character
A = {C}
d:
S
I
I
I
I
B
B
a
a
a
/\
/\
b
/\
TopSt
a
Z
a
Z
a
Z
NS
I
I
B
B
B
C
Computation graph for this PDA on the input string aabb
(I,aabb,Z)
(B,aabb,Z)
(I,abb,aZ)
(I,bb,aaZ)
stack update
push aa
push aZ
push a
push Z
push /\
push Z
(B,abb,aZ)
(C,aabb,Z)
(B,bb,aaZ)
(B,b,aZ)
(B,/\,Z)
(C,/\,Z)
9
a;a; aa
a;Z; aZ
Definition of ├
I
/\;Z;Z
/\;a;a
b;a;/\
B
/\;Z;Z
C
Input string aabb
(I, aabb, Z) ├ (I,abb,aZ)
(I,aabb,Z)
(I, aabb, Z) ├ (B, aabb, Z)
(B,aabb,Z)
(I,abb,aZ)
(I,bb,aaZ)
(B,bb,aaZ)
(B,b,aZ)
(B,/\,Z)
(B,abb,aZ)
(C,aabb,Z)
(I, aabb, Z) ├ 2 (C, aabb, Z)
(I, aabb, Z) ├ 3 (B, bb, aaZ)
(I, aabb, Z) ├ * (B, abb, aZ)
(I, aabb, Z) ├ * (B, /\, Z)
(I, aabb, Z) ├ * (C, /\, Z)
(C,/\,Z)
10
a;a; aa
a;Z; aZ
Acceptance and
Rejection
(I,aabb,Z)
(B,aabb,Z)
(I,abb,aZ)
(I,bb,aaZ)
(B,abb,aZ)
(C,aabb,Z)
I
/\;Z;Z
/\;a;a
b;a;/\
B
/\;Z;Z
C
Input string aabb
M accepts string x if one of the
configurations reached is an accepting
configuration
(q0, x, Z) ├* (f, /\, a),f in A, a in G*
Stack contents can be anything
(B,bb,aaZ)
(B,b,aZ)
Not an accepting
configuration since input
not completely processed
(B,/\,Z)
(C,/\,Z)
An accepting
configuration
Not an accepting
configuration since state
is not accepting
M rejects string x if all configurations
reached are either not halting
configurations or are rejecting
configurations
11
Defining L(M) and LPDA
M accepts string x if one of the
configurations reached is an accepting
configuration
(q0, x, Z) ├* (f, /\, a),f in A, a in G*
Stack contents can be anything
M rejects string x if all configurations
reached are either not halting
configurations or are rejecting
configurations
• L(M) (or Y(M))
– The set of strings ?
• N(M)
– The set of strings ?
• LPDA
– Language L is in language
class LPDA iff ?
12
Deterministic PDA’s
• A PDA is deterministic if its transition function
satisfies both of the following properties
– For all q in Q, a in S union {/\}, and X in G,
• the set d(q,a,X) has at most one element
– For all q in Q and X in G,
• if d(q, /\, X) ≠ { }, then d(q,a,X) = { } for all a in S
• A computation graph is now just a path again
• Our default assumption is that PDA’s are
nondeterministic
13
Two forms of nondeterminism
Trans Current Input Top of Next Stack
#
State Char. Stack State Update
------------------------------------------------------1
q0
a
Z
q0
aZ
2
q0
a
Z
q0
aa
3
4
q0
q0
/\
a
Z
Z
q0
q0
aZ
aa
14
LPDA and DCFL
• A language L is in language class LPDA if and
only if there exists a PDA M such that L(M) = L
• A language L is in language class DCFL
(Deterministic Context-Free Languages) if and
only if there exists a deterministic PDA M such
that L(M) = L
• To be proven
– LPDA = CFL
– CFL is a proper superset of DCFL
15
PDA Comments
• Note, we can use the stack for much more
than just a counter
• See examples in chapter 7 for some details
16