Anda di halaman 1dari 26

Pushdown Automata

Wen-Guey Tzeng
Department of Computer Science
National Chiao Tung University
1
Nondeterministic Pushdown Automata
2
Definition of NPDA
A nondeterministic pushdown acceptor (npda)
is defined by M=(Q, E, I, o, q
0
, z, F), where
Q: a finite set of internal states
E: the input alphabet
I: the stack alphabet
o: Q(E{})Ia subset of QI
*
q
0
eQ: the initial state
zeI: the stack start symbol
F_Q: the set of final states
3
Example
M=(Q, E, I, o, q
0
, z, F)
Q={q
0
, q
1
, q
2
, q
3
}
E={a, b}
I={0, 1}
z=0
F={q
3
}
o(q
0
, a, 0)={(q
1
, 10), (q
3
, )}
o(q
0
, , 0)={(q
3
, )}
o(q
1
, a, 1)={(q
1
, 11)}
o(q
1
, b, 1)={(q
2
, )}
o(q
2
, b, 0)={(q
2
, )}
o(q
2
, , 0)={(q
3
, )}



4
Transition graph
5
Instantaneous description (q, w, u)
q: the current state
w: the current un-read input
u: the symbols in the current stack
Initial id (q
0
, w, z)
Transition: (q, aw, bx) (p, w, yx)
Example
(q
0
, ab, 0) (q
1
, b, 10) (q
2
, , 0) (q
3
, , )
6
Language accepted by npda
The language accepted by M is

L(M)={weE*: (q
0
, w, z)* (p, , u), peF, ueI*}.


7
Example
Design an npda to accept
L={we{a,b}* : n
a
(w)=n
b
(w)}
Idea: the stack holds extra as (or bs) up to
now
8

9
Example
Design an npda to accept
L={we{a,b}* : n
a
(w) = n
b
(w)}
10
Example
Design an npda to accept
L={wcw
R
: we{a,b}
+
}

Idea
The stack holds w first by pushing them in.
After seeing c, M switches to the matching
phase.
The stack matches w
R
with the w in the stack.

11
Q={q
0
, q
1
, q
2
}, E={a, b}, I={a, b, z}, F={q
2
}
Pushing w into the stack:
o(q
0
, a, a)={ (q
0
, aa)}
o(q
0
, b, a)={ (q
0
, ba)}
o(q
0
, a, b)={ (q
0
, ab)}
o(q
0
, b, b)={ (q
0
, bb)}
o(q
0
, a, z)={ (q
0
, az)}
o(q
0
, b, z)={ (q
0
, bz)}




12
After seeing c, switch to q
1
for matching
o(q
0
, c, a)={ (q
1
, a)}
o(q
0
, c, b)={ (q
1
, b)}
Matching w
R
with w in the stack
o(q
1
, a, a)={ (q
1
, )}
o(q
1
, b, b)={ (q
1
, )}
In the end
o(q
1
, , z)={ (q
2
, z)}






13
Example
Design an npda to accept
L={ww
R
: we{a,b}
+
}

Idea
The stack holds w first by pushing them in.
M switches to the matching phase.
The stack matches w
R
with the w in the stack.
Difficulty: How does M know the end of w?

14
Q={q
0
, q
1
, q
2
}, E={a, b}, I={a, b, z}, F={q
2
}
Pushing w into the stack:
o(q
0
, a, a)={ (q
0
, aa)}
o(q
0
, b, a)={ (q
0
, ba)}
o(q
0
, a, b)={ (q
0
, ab)}
o(q
0
, b, b)={ (q
0
, bb)}
o(q
0
, a, z)={ (q
0
, az)}
o(q
0
, b, z)={ (q
0
, bz)}




15
Switching from q
0
to q
1
for matching
(nondeterministic)
o(q
0
, , a)={ (q
1
, a)}
o(q
0
, , b)={ (q
1
, b)}
Matching w
R
with w in the stack
o(q
1
, a, a)={ (q
1
, )}
o(q
1
, b, b)={ (q
1
, )}
In the end
o(q
1
, , z)={ (q
2
, z)}






16
Run M on the input abba:






17
Equivalence of ndpa and cfg
For any npda M, there is a cfg G such that L(G)=L(M)
For any cfg G, there is an npda M such that L(M)=L(G)


18
Convert cfg to pda
Example
SaSbb|a
CFG in Greibach normal form
Productions: SaSA|a, AbB, Bb
Construct M
Idea: use the stack to hold the derivation process, and match the input with
the derivation
Initial: o(q
0
, , z)={ (q
1
, Sz) }
Process the input:
o(q
1
, a, S)={ (q
1
, SA), (q
1
, )}
o(q
1
, b, A)={ (q
1
, B) }
o(q
1
, b, B)={ (q
1
, )}
In the end: o(q
1
, , z)={ (q
2
, )}








19
Run M on the input aaabbbb
See the relation between o*(q
0
, aaabbbb, z) and
S* aaabbbb









20
Convert pda to cfg
Idea: the grammar simulates the move of pda
Assumptions for an npda
Only one final state q
f

All transitions must have form, aeE{},
o(q
i
, a, A)={ c
1
, c
2
, , c
n
}, where c
i
=(q
j
, ) or c
i
=(q
i
, BC)
Simulation
each variable is of form (q
i
Aq
j
) -- generating string w
Starting at state q
i

Top stack symbol is A
Read in string w
Ending at state q
j

Stack symbol is popped out.









21
The construction
Start variable: S=(q
0
zq
f
)
For transitions aeE{},
o(q
i
, a, A)={ c
1
, c
2
, , c
n
}, where c
i
=(q
j
, ) or c
i
=(q
i
, BC)
If c
i
=(q
j
, ), add production (q
i
Aq
j
)a
If c
i
=(q
j
, BC), add production (q
i
Aq
k
)a(q
j
Bq
l
)(q
l
Cq
k
),
for all q
k
, q
l
eQ











22
Example
o(q
0
, a, z)={ (q
0
, Az) }
o(q
0
, a, A)={ (q
0
, A) }
o(q
0
, b, A)={ (q
1
, ) }
o(q
1
, , z)={ (q
2
, ) }
Converted to satisfy the requirements
o(q
0
, a, z)={ (q
0
, Az) }
o(q
3
, , z)={ (q
0
, Az) }
o(q
0
, a, A)={ (q
3
, ) }
o(q
0
, b, A)={ (q
1
, ) }
o(q
1
, , z)={ (q
2
, ) }
















23
Work






24
The final result















25
Comments on dpda and dcfl
For efficient parsing, we need deterministic
cfl.
The corresponding deterministic pda
o(q, a, b) contains one element
If o(q, , b) is not empty, o(q
0
, c, b) is empty for
ceE


26

Anda mungkin juga menyukai