Transcript Lecture 30

Lecture 30
• Pushdown Automata (PDA’s)
– definition
– example
1
Pushdown Automata
Definition and Motivating Example
2
PDA’s
• In this presentation we introduce the PDA model of
computation (programming language).
– We first begin with an example NFA
– We augment this NFA with external memory in the form of a
counter
– We then transform the counter into a stack to produce a PDA
• We define configurations and computations of these new
computational models
• We define L(M) for these new computational models
3
NFA for
a
b
/\
A
m
n
{a b
/\
B
C
• What strings end up in
each state of the above
NFA?
– A: The set of strings in a*.
– B: The set of strings in
a*b*.
– C: The set of strings in
a*b*.
| 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
+
NFA
a
for
b
/\
/\
A
B
C
NFA
a;add 1
Initialize count 0
A
B
| n >=0 }
Imagine we now have memory in the form of a counter which
we can increment or decrement every time we see an input
character.
When we see an a in state A, we do the following two actions:
1) We increment the counter value by 1.
2) We stay in state A.
b;substract 1
/\
n
n
{a b
C
/\;only if count 0
When we see a b in state B, we do the following two actions:
1) We decrement the counter value by 1.
2) We stay in state B.
NFA+
From state B, we allow a /\-transition to state C only if
1) The counter has value 0.
Finally, when we begin, the counter should have value 0.
5
Defining Configurations of
a;add 1
Initialize count 0
A
+
NFA
b;substract 1
/\
B
C
/\;only if count 0
NFA+
The following information needs to be recorded in a
configuration of a computation of an NFA+ M on an input
string x.
1) Current state
2) Remaining input to be processed
3) Current counter value
• Note, the counter is NOT part of the input.
• It is part of the computational model.
• Its current value needs to be recorded in a configuration.
6
Computation Tree of
a;add 1
Initialize count 0
A
b;substract 1
/\
B
+
NFA
Computation Tree for this NFA+ on the input string aabb
C
(A,aabb,0)
/\;only if count 0
NFA+
(B,aabb,0)
(A,abb,1)
(A,bb,2)
(B,abb,1)
(C,aabb,0)
(B,bb,2)
(B,b,1)
(B,/\,0)
(C,/\,0)
7
Questions about
a;add 1
Initialize count 0
A
1) Why can this NFA+ accept {anbn | n >= 0}?
That is, how do we get around the Myhill-Nerode Theorem?
b;substract 1
/\
B
C
/\;only if count 0
NFA+
+
NFA
2) What happens if the counter has a finite capacity?
For example, it can only go up to 10.
Can some NFA+ still accept {anbn | n >= 0}?
3) Suppose that we have a stack data structure instead of a
counter.
Can we do anything we could with the counter?
Can we do something we could not do with only a counter?
8
PDA for
a;add 1
n
n
{a b
| n >= 0}
b;substract 1
Here is a pushdown automaton (PDA) for this language.
Initialize count 0
/\
A
B
C
/\;only if count 0
A PDA is essentially an NFA augmented with an infinite
capacity stack.
NFA+
a;push a
A
b;pop
/\
B
Initialize stack to empty
C
/\;only if stack is empty
PDA
The “pushdown” part of the name supposedly
comes from the stacks trays in cafeterias where
you have to pushdown on the stack to add a
tray to it.
In this example, only one character a is ever pushed onto
the stack.
9
Defining Configurations of PDA
a;push a
A
b;pop
/\
B
Initialize stack to empty
C
The following information needs to be recorded in a configuration
of a computation of an PDA M on an input string x.
/\;only if stack is empty
PDA
1) Current state
2) Remaining input to be processed
3) Current stack contents.
Note again, the stack is NOT part of the input.
It is part of the computational model.
However, its current value needs to be recorded in a configuration.
We will represent the stack contents by a string of characters.
The leftmost character is on top of the stack.
The rightmost character is at the bottom of the stack.
We will represent an empty stack with the string /\.
10
Computation Tree of PDA
a;push a
A
Computation Tree for this PDA on the input string aabb
b;pop
/\
B
Initialize stack to empty
(A,aabb,/\)
C
/\;only if stack is empty
(B,aabb,/\)
(A,abb,a)
PDA
(A,bb,aa)
(B,abb,a)
(C,aabb,/\)
(B,bb,aa)
(B,b,a)
(B,/\,/\)
(C,/\,/\)
11
PDA for
a;push a
| n >= 0}
1) The first black character is the current input symbol.
We allow this to be l which is different from the book.
2) The second red character is the current top stack symbol.
B
C
3) The third string of characters is the stack update action.
/\;only if stack is empty 4) The book also includes a tapehead movement action
We omit this as weinclude l in field 1.
PDA
b;pop
/\
A
n
n
{a b
Initialize stack to empty
The stack character Z is a special character which we will
always keep on the bottom of the stack. Note it is part of G, though,
so it may be used other places as well.
a;a;push a
a;Z; push a b;a;pop
A
Initialize stack to empty
(only contains Z)
/\;Z;/\;a;-
B
/\;Z;-
C
Our actual PDA
12
Computation Tree of PDA
Computation Tree for this PDA on the input string aabb
a;a;push a
a;Z; push a b;a;pop
A
Initialize stack to empty
(only contains Z)
/\;Z;/\;a;-
B
/\;Z;-
Our actual PDA
(A,aabb,Z)
C
(B,aabb,Z)
(A,abb,aZ)
(A,bb,aaZ)
(B,abb,aZ)
(C,aabb,Z)
(B,bb,aaZ)
(B,b,aZ)
(B,/\,Z)
(C,/\,Z)
13
Formal Definition of PDA
A PDA M = (Q, S, G, q0, Z, A, d)
a;a; aa
a;Z; aZ
I
/\;Z;Z
/\;a;a
Initialize stack to empty
(only contains Z)
b;a;/\
B
/\;Z;Z
input
a
a
/\
/\
b
/\
C
Our actual PDA
Q = {I, B, C}
S = {a,b}
G = {Z, a}
q0 = I
Z is the initial stack character
A = {C}
d:
state
I
I
I
I
B
B
Items common to NFAs
Q is the set of states
S is the input alphabet
q0 is the initial state
A is the set of accepting states
top stack
a
Z
a
Z
a
Z
Modified items
G is the stack alphabet
- Z is a special character in G
- The stack always begins containing 1 Z
d: modified as follows:
pops/reads top stack symbol as well as
current input character
describes how to update stack
note push /\ results in pop!
next state
stack update
I
I
B
B
B
C
push aa
push aZ
push a
push Z
push /\
push Z
14
L(M) for a PDA
a;a;aa
a;Z;aZ
A
Initialize stack to empty
(only contains Z)
Computation Tree for this PDA on the input string aabb
b;a;-
/\;Z;Z
/\;a;a
B
/\;Z;Z
(A,aabb,l)
C
(A,bb,aaZ)
A string x is in L(M) if and only if it there exists an accepting
configuration (q, /\, anything) in the computation tree of M
on x .
An accepting configuration satisfies the following two conditions :
1) The PDA must be in an accepting state
2) The input string must be completely processed
(B,aabb,l)
(A,abb,a)
Our actual PDA
(B,abb,a)
(B,bb,aa)
(B,b,a)
Not an accepting configuration
because the remaining input
string is not /\.
(B,l,l)
(C,l,l)
Note, the stack contents is unimportant.
This is known as acceptance by final state.
(C,aabb,l)
An accepting configuration.
Not an accepting configuration
because the state B is not an
accepting state.
15
PDA Comments
• Note, we can use the stack for much more
than just a counter
• See examples in section 6.1 for some details
16