Transcript Slide 1

Pushdown Automata
Part I: PDAs
Chapter 12
1
Recognizing Context-Free Languages
Two notions of recognition:
(1) Say yes or no, just like with FSMs
(2) Say yes or no, AND
if yes, describe the structure
a
+
b
*
c
2
Just Recognizing
We need a device similar to an FSM except that it
needs more power.
The insight: Precisely what it needs is a stack, which
gives it an unlimited amount of memory with a
restricted structure.
Example: Bal (the balanced parentheses language)
(((()))
3
Definition of a Pushdown Automaton
M = (K, , , , s, A), where:
K is a finite set of states
 is the input alphabet
 is the stack alphabet
s  K is the initial state
A  K is the set of accepting states, and
 is the transition relation. It is a finite subset of
(K
state
 (  {}) 
*)

input or  string of
symbols
to pop
from top
of stack
(K
state

*)
string of
symbols
to push
on top
of stack
4
Definition of a Pushdown Automaton
A configuration of M is an element of K  *  *.
The initial configuration of M
is (s, w, ).
Compare to the
configuration of
FSM
K : Current state
* : Input left to read
* : Stack content
5
Manipulating the Stack
c
will be written as cab
a
b
If c1c2…cn is pushed onto the stack:
c1
c2
cn
c
a
b
c1c2…cncab
6
Yields
Let c be any element of   {},
Let 1, 2 and  be any elements of *, and
Let w be any element of *.
Then, yields-in-one-step |-M is defined as
(q1, cw, 1) |-M (q2, w, 2) iff ((q1, c, 1), (q2, 2))  .
Let |-M* be the reflexive, transitive closure of |-M.
C1 yields configuration C2 iff C1 |-M* C2
If ((q1, c, 1), (q2, 2))  ,
q1
c/1/2
q2
7
Computations
A computation by M is a finite sequence of configurations
C0, C1, …, Cn for some n  0 such that:
● C0
is an initial configuration,
is of the form (q, , ), for some state q  KM and
some string  in *, and
● Cn
● C0
|-M C1 |-M C2 |-M … |-M Cn.
8
Nondeterminism
If M is in some configuration (q1, s, ) it is possible that:
●
contains exactly one transition that matches.
●
contains more than one transition that matches.
●
contains no transition that matches.
9
Accepting
A computation C of M is an accepting computation iff:
● C = (s, w, ) |-M* (q, , ), and
● q  A.
M accepts a string w iff at least one of its computations accepts.
Other paths may:
● Read all the input and halt in a nonaccepting state,
● Read all the input and halt in an accepting state with the stack not
empty,
● Loop forever and never finish reading the input, or
● Reach a dead end where no more input can be read.
The language accepted by M, denoted L(M), is the set of all strings
accepted by M.
10
Rejecting
A computation C of M is a rejecting computation iff:
= (s, w, ) |-M* (q, w, ),
● C is not an accepting computation, and
● M has no moves that it can make from (q, w, ).
●C
M rejects a string w iff all of its computations reject.
So note that it is possible that, on input w, M neither
accepts nor rejects.
11
A PDA for Balanced Parentheses
12
A PDA for Balanced Parentheses
If input is ) and ( is on
stack top, then ( is
popped and nothing
is pushed to stack.
If input is (, what’s on
stack top doesn’t matter
and a ( is pushed to stack.
M = (K, , , , s, A), where:
K = {s}
the states
 = {(, )}
the input alphabet
 = {(}
the stack alphabet
A = {s}
 contains:
((s, (, **), (s, ( ))
((s, ), ( ), (s, ))
**Important: This does not mean that the stack is empty
13
A PDA for AnBn = {anbn: n  0}
14
A PDA for AnBn = {anbn: n  0}
M = (K, , , , s, A), where:
K = {s, f}
the states
 = {a, b}
the input alphabet
 = {a}
the stack alphabet
A = {f}
the accepting states
 contains: ((s, a, ), (s, a))
((s, b, a), (f, ))
((f, b, a), (f, ))
15
A PDA for {wcwR: w  {a, b}*}
16
A PDA for {wcwR: w  {a, b}*}
M = (K, , , , s, A), where:
K = {s, f}
the states
 = {a, b, c}
the input alphabet
 = {a, b}
the stack alphabet
A = {f}
the accepting states
 contains: ((s, a, ), (s, a))
((s, b, ), (s, b))
((s, c, ), (f, ))
((f, a, a), (f, ))
((f, b, b), (f, ))
17
A PDA for {anb2n: n  0}
18
A PDA for {anb2n: n  0}
M = (K, , , , s, A), where:
K=
the states
=
the input alphabet
=
the stack alphabet
A=
the accepting states
 contains:
19
Exploiting Nondeterminism
A PDA M is deterministic iff:
● M contains no pairs of transitions that compete with each other,
and
● Whenever M is in an accepting configuration it has no available
moves.
But many useful PDAs are not deterministic.
20
A PDA for PalEven ={wwR: w  {a, b}*}
S
S  aSa
S  bSb
A PDA:
21
A PDA for PalEven ={wwR: w  {a, b}*}
S
S  aSa
S  bSb
A PDA:
22
A PDA for {w  {a, b}* : #a(w) = #b(w)}
23
A PDA for {w  {a, b}* : #a(w) = #b(w)}
24
More on Nondeterminism
Accepting Mismatches
L = {ambn : m  n; m, n > 0}
Start with the case where n = m:
b/a/
a//a
b/a/
1
2
25
More on Nondeterminism
Accepting Mismatches
L = {ambn : m  n; m, n > 0}
Start with the case where n = m:
b/a/
a//a
b/a/
1
2
● If stack and input are empty, halt and reject.
● If input is empty but stack is not (m > n) (accept):
● If stack is empty but input is not (m < n) (accept):
26
More on Nondeterminism
Accepting Mismatches
L = {ambn : m  n; m, n > 0}
b/a/
a//a
b/a/
2
1
● If input is empty but stack is not (m > n) (accept):
b/a/
a//a
/a/
b/a/
1
/a/
2
3
27
More on Nondeterminism
Accepting Mismatches
L = {ambn : m  n; m, n > 0}
b/a/
a//a
b/a/
2
1
● If stack is empty but input is not (m < n) (accept):
b/a/
a//a
b//
b/a/
1
b//
2
4
28
Putting It Together
L = {ambn : m  n; m, n > 0}
● Jumping to the input clearing state 4:
Need to detect bottom of stack.
● Jumping to the stack clearing state 3:
Need to detect end of input.
29
The Power of Nondeterminism
Consider AnBnCn = {anbncn: n  0}.
PDA for it?
30
The Power of Nondeterminism
Consider AnBnCn = {anbncn: n  0}.
Now consider L =  AnBnCn. L is the union of two
languages:
1. {w  {a, b, c}* : the letters are out of order}, and
2. {aibjck: i, j, k  0 and (i  j or j  k)} (in other words,
unequal numbers of a’s, b’s, and c’s).
31
A PDA for L = AnBnCn
What is it?
32
Are the Context-Free Languages
Closed Under Complement?
AnBnCn is context free.
If the CF languages were closed under complement,
then
AnBnCn = AnBnCn
would also be context-free.
But we will prove that it is not.
33
L = {anbmcp: n, m, p  0 and n  m or m  p}
S  NC
S  QP
NA
NB
Aa
A  aA
A  aAb
Bb
B  Bb
B  aBb
C   | cC
P  B'
P  C'
B'  b
B'  bB'
B'  bB'c
C'  c | C'c
C'  C'c
C'  bC'c
Q   | aQ
/* n  m, then arbitrary c's
/* arbitrary a's, then p  m
/* more a's than b's
/* more b's than a's
/* add any number of c's
/* more b's than c's
/* more c's than b's
/* prefix with any number of a's
34
Reducing Nondeterminism
and
show where the
competitions are
● Jumping to the input clearing state 4:
Need to detect bottom of stack, so push # onto the
stack before we start.
● Jumping to the stack clearing state 3:
Need to detect end of input. Add to L a termination
character (e.g., $)
35
Reducing Nondeterminism
● Jumping to the input clearing state 4:
36
Reducing Nondeterminism
● Jumping to the stack clearing state 3:
37
More on PDAs
A PDA for {wwR : w  {a, b}*}:
What about a PDA to accept {ww : w  {a, b}*}?
38