Sei sulla pagina 1di 27

Section: Pushdown Automata

Ch. 7 - Pushdown Automata


A DFA=(Q,,,q
0
,F)
head moves
input tape
tape head
current state
a a b b a b
0 1
1
Modify DFA by adding a stack. New
machine is called Pushdown
Automata (PDA).
a
a
b a a a
stack
head
tape
input tape
current state
b a
1 0
Z
head moves
2
Denition: Nondeterministic PDA
(NPDA) is dened by
M=(Q,, , , q
0
, z, F)
where
Q is nite set of states
is tape (input) alphabet
is stack alphabet
q
0
is initial state
z is start stack symbol (bottom of stack )
F Q is set of nal states.
:Q( {}) nite subsets of Q

3
Example of transitions
(q
1
,a,b) = {(q
3
,b),(q
4
,ab),(q
6
,)}
The diagram for the above transitions
is:
4
Instantaneous Description:
(q,w,u)
Description of a Move:
(q
1
,aw,bx) (q
2
,w,yx)
i
Denition Let M=(Q,,,,q
0
,z,F) be
a NPDA. L(M)={w

| (q
0
,w,z)

(p,,u), pF, u

}. The NPDA
accepts all strings that start in q
0
and
end in a nal state.
5
Example: L={a
n
b
n
|n 0}, = {a, b},
= {z, a}
6
Another Denition for Language
Acceptance
NPDA M accepts L(M) by empty
stack:
L(M)={w

|(q
0
, w, z)

(p, , )}
7
Example: L={ww
R
|w
+
}, = {a, b},
= {z, a, b}
8
Example: L={ww|w

}, = {a, b}
9
Examples for you to try on your own:
(solutions are at the end of the
handout).
L={a
n
b
m
|m > n, m, n > 0}, = {a, b},
= {z, a}
L={a
n
b
n+m
c
m
|n, m > 0}, = {a, b, c},
L={a
n
b
2n
|n > 0}, = {a, b}
10
Theorem Given NPDA M that
accepts by nal state, NPDA M
that accepts by empty stack s.t.
L(M)=L(M).
Proof (sketch)
M=(Q,,,,q
0
,z,F)
Construct M=(Q,,

,q
s
,z,F)
11
Theorem Given NPDA M that
accepts by empty stack, NPDA M
that accepts by nal state.
Proof: (sketch)
M=(Q,,,,q
0
,z,F)
Construct M=(Q,,

,q
s
,z,F)
12
Theorem For any CFL L not
containing , an NPDA M s.t.
L=L(M).
Proof (sketch)
Given (-free) CFL L.
CFG G such that L=L(G).
G in GNF, s.t. L(G)=L(G).
G=(V,T,S,P). All productions in P
are of the form:
13
Example: Let G=(V,T,S,P), P=
S aSA | aAA | b
A bBBB
B b
14
Theorem Given a NPDA M, a
NPDA M s.t. all transitions have the
form (q
i
,a,A)={c
1
, c
2
, . . . c
n
} where
c
i
=(q
j
, )
or c
i
=(q
j
,BC)
Each move either increases or
decreases stack contents by a single
symbol.
Proof (sketch)
15
Theorem If L=L(M) for some NPDA
M, then L is a CFL.
Proof: Given NPDA M.
First, construct an equivalent
NPDA M that will be easier to
work with. Construct M such that
1. accepts if stack is empty
2. each move increases or decreases
stack content by a single symbol.
(can only push 2 variables or no
variables with each transition)
M=(Q,,,,q
0
,z,F)
Construct G=(V,,S,P) where
V={(q
i
cq
j
)|q
i
, q
j
Q, c }
Goal: (q
0
zq
f
) which will be the
start symbol in the grammar.
16
Example:
L(M)={aa

b}, M=(Q,,,,q
0
,z,F),
Q={q
0
, q
1
, q
2
, q
3
},
={a, b},={A, z},F={}.
4
5
3
2 1
q
0 q
1
q
2
a,z;Az
b,A;
,z;
a,A;
,z;Az

q
3
17
Construct the grammar G=(V,T,S,P),
V={(q
0
Aq
0
), (q
0
zq
0
), (q
0
Aq
1
), (q
0
zq
1
), . . .}
T=
S=(q
0
zq
2
)
P=
18
From transition 1 (q
0
Aq
1
) b
From transition 2 (q
1
zq
2
)
From transition 3 (q
0
Aq
3
) a
From transition 4 (q
0
zq
0
) a(q
0
Aq
0
)(q
0
zq
0
)|
a(q
0
Aq
1
)(q
1
zq
0
)|
a(q
0
Aq
2
)(q
2
zq
0
)|
a(q
0
Aq
3
)(q
3
zq
0
)
(q
0
zq
1
) a(q
0
Aq
0
)(q
0
zq
1
)|
a(q
0
Aq
1
)(q
1
zq
1
)|
a(q
0
Aq
2
)(q
2
zq
1
)|
a(q
0
Aq
3
)(q
3
zq
1
)
(q
0
zq
2
) a(q
0
Aq
0
)(q
0
zq
2
)|
a(q
0
Aq
1
)(q
1
zq
2
)|
a(q
0
Aq
2
)(q
2
zq
2
)|
a(q
0
Aq
3
)(q
3
zq
2
)
(q
0
zq
3
) a(q
0
Aq
0
)(q
0
zq
3
)|
a(q
0
Aq
1
)(q
1
zq
3
)|
a(q
0
Aq
2
)(q
2
zq
3
)|
a(q
0
Aq
3
)(q
3
zq
3
)
19
From transition 5 (q
3
zq
0
) (q
0
Aq
0
)(q
0
zq
0
)|
(q
0
Aq
1
)(q
1
zq
0
)|
(q
0
Aq
2
)(q
2
zq
0
)|
(q
0
Aq
3
)(q
3
zq
0
)
(q
3
zq
1
) (q
0
Aq
0
)(q
0
zq
1
)|
(q
0
Aq
1
)(q
1
zq
1
)|
(q
0
Aq
2
)(q
2
zq
1
)|
(q
0
Aq
3
)(q
3
zq
1
)
(q
3
zq
2
) (q
0
Aq
0
)(q
0
zq
2
)|
(q
0
Aq
1
)(q
1
zq
2
)|
(q
0
Aq
2
)(q
2
zq
2
)|
(q
0
Aq
3
)(q
3
zq
2
)
(q
3
zq
3
) (q
0
Aq
0
)(q
0
zq
3
)|
(q
0
Aq
1
)(q
1
zq
3
)|
(q
0
Aq
2
)(q
2
zq
3
)|
(q
0
Aq
3
)(q
3
zq
3
)
20
Recognizing aaab in M:
(q
0
, aaab, z) (q
0
, aab, Az)
(q
3
, ab, z)
(q
0
, ab, Az)
(q
3
, b, z)
(q
0
, b, Az)
(q
1
, , z)
(q
2
, , )
Derivation of string aaab in G:
(q
0
zq
2
) a(q
0
Aq
3
)(q
3
zq
2
)
aa(q
3
zq
2
)
aa(q
0
Aq
3
)(q
3
zq
2
)
aaa(q
3
zq
2
)
aaa(q
0
Aq
1
)(q
1
zq
2
)
aaab(q
1
zq
2
)
aaab
21
Denition: A PDA
M=(Q,,,,q
0
,z,F) is deterministic if
for every q Q, a {}, b
1. (q, a, b) contains at most 1 element
2. if (q, , b) = then (q, c, b)= for all
c
Denition: L is DCFL i DPDA M
s.t. L=L(M).
Examples:
1. Previous pda for {a
n
b
n
|n 0} is
deterministic.
2. Previous pda for
{a
n
b
m
c
n+m
|n, m > 0} is deterministic.
3. Previous pda for
{ww
R
|w
+
}, = {a, b} is
nondeterministic.
Note: There are CFLs that are not
deterministic.
22
L={a
n
b
n
|n 1} {a
n
b
2n
|n 1} is a CFL
and not a DCFL.
Proof:
L = {a
n
b
n
: n 1} {a
n
b
2n
: n 1}
It is easy to construct a NPDA for
{a
n
b
n
: n 1} and a NPDA for
{a
n
b
2n
: n 1}. These two can be
joined together by a new start state
and -transitions to create a NPDA
for L. Thus, L is CFL.
Now show L is not a DCFL.
Assume that there is a
deterministic PDA M such that
L = L(M). We will construct a PDA
that recognizes a language that is
not a CFL and derive a
contradiction.
Construct a PDA M

as follows:
1. Create two copies of M: M
1
and
M
2
. The same state in M
1
and M
2
23
are called cousins.
2. Remove accept status from
accept states in M
1
, remove
initial status from initial state in
M
2
. In our new PDA, we will
start in M
1
and accept in M
2
.
3. Outgoing arcs from old accept
states in M
1
, change to end up in
the cousin of its destination in
M
2
. This joins M
1
and M
2
into
one PDA. There must be an
outgoing arc since you must
recognize both a
n
b
n
and a
n
b
2n
.
After reading n bs, must accept if
no more bs and continue if there
are more bs.
4. Modify all transitions that read a
b and have their destinations in
M
2
to read a c.
This is the construction of our new
PDA.
24
When we read a
n
b
n
and end up in
an old accept state in M
1
, then we
will transfer to M
2
and read the
rest of a
n
b
2n
. Only the bs in M
2
have been replaced by cs, so the
new machine accepts a
n
b
n
c
n
.
The language accepted by our new
PDA is a
n
b
n
c
n
. But this is not a
CFL. Contradiction! Thus there is
no deterministic PDA M such that
L(M) = L. Q.E.D.
25
Example: L={a
n
b
m
|m > n, m, n > 0},
= {a, b}, = {z, a}
q
0
q
1
q
2
q
3
a,z;az
a,a;aa
b,a;
b,a;
b,z;z
b,z;z

Example: L={a
n
b
n+m
c
m
|n, m > 0},
= {a, b, c},
a,z;az
a,a;aa
b,z;bz
b,b;bb
q0 q1 q2
q3
b,a;
b,a;
c,b;
q4
q5
,z;z c,b;
Example: L={a
n
b
2n
|n > 0}, = {a, b}
q
0
q
1
q
2
q
3
a,z;aaz
a,a;aaa
b,a;
b,a;
,z;


26
27

Potrebbero piacerti anche