8/13/2019 7. Pushdown Automata
1/26
Pushdown Automata
Wen-Guey Tzeng
Department of Computer ScienceNational Chiao Tung University
1
8/13/2019 7. Pushdown Automata
2/26
Nondeterministic Pushdown Automata
2
8/13/2019 7. Pushdown Automata
3/26
Definition of NPDA
A nondeterministic pushdown acceptor (npda)is defined by M=(Q, , , , q0, z, F), where Q: a finite set of internal states
: the input alphabet: the stack alphabet
: Q({})a subset of Q*
q0Q: the initial state z: the stack start symbol
FQ: the set of final states
3
8/13/2019 7. Pushdown Automata
4/26
Example
M=(Q, , , , q0, z, F) Q={q0, q1, q2, q3}
={a, b}
={0, 1}
z=0
F={q3}
(q0, a, 0)={(q1, 10), (q3, )}(q0, , 0)={(q3, )}
(q1, a, 1)={(q1, 11)}(q1, b, 1)={(q2, )}(q2, b, 0)={(q2, )}(q2, , 0)={(q3, )}
4
8/13/2019 7. Pushdown Automata
5/26
Transition graph
5
8/13/2019 7. Pushdown Automata
6/26
Instantaneous description (q, w, u)
q: the current state
w: the current un-read input
u: the symbols in the current stack
Initial id (q0, w, z)
Transition: (q, aw, bx)(p, w, yx)
Example
(q0, ab, 0)(q1, b, 10)(q2, , 0)(q3, , )
6
8/13/2019 7. Pushdown Automata
7/26
Language accepted by npda
The language accepted by M is
L(M)={w *: (q0, w, z)* (p, , u), pF, u *}.
7
8/13/2019 7. Pushdown Automata
8/26
Example
Design an npda to accept
L={w{a,b}* : na(w)=nb(w)}
Idea: the stack holds extra as (or bs) up to
now
8
8/13/2019 7. Pushdown Automata
9/26
9
8/13/2019 7. Pushdown Automata
10/26
Example
Design an npda to accept
L={w{a,b}* : na(w) nb(w)}
10
8/13/2019 7. Pushdown Automata
11/26
Example
Design an npda to accept
L={wcwR: w{a,b}+}
Idea
The stack holds w first by pushing them in.
After seeing c, M switches to the matching
phase.
The stack matches wRwith the w in the stack.
11
8/13/2019 7. Pushdown Automata
12/26
Q={q0, q1, q2},
={a, b},
={a, b, z}, F={q2} Pushing w into the stack:
(q0, a, a)={ (q0, aa)}
(q0, b, a)={ (q0, ba)}
(q0, a, b)={ (q0, ab)}
(q0, b, b)={ (q0, bb)}
(q0
, a, z)={ (q0
, az)}
(q0, b, z)={ (q0, bz)}
12
8/13/2019 7. Pushdown Automata
13/26
After seeing c, switch to q1for matching
(q0, c, a)={ (q1, a)}
(q0, c, b)={ (q1, b)}
Matching w
R
with w in the stack(q1, a, a)={ (q1, )}
(q1, b, b)={ (q1, )}
In the end(q1, , z)={ (q2, z)}
13
8/13/2019 7. Pushdown Automata
14/26
Example
Design an npda to accept
L={wwR: w{a,b}+}
Idea
The stack holds w first by pushing them in.
M switches to the matching phase.
The stack matches wRwith the w in the stack.
Difficulty: How does M know the end of w?
14
8/13/2019 7. Pushdown Automata
15/26
Q={q0, q
1, q
2}, ={a, b}, ={a, b, z}, F={q
2}
Pushing w into the stack:
(q0, a, a)={ (q0, aa)}
(q0, b, a)={ (q0, ba)}
(q0, a, b)={ (q0, ab)}
(q0, b, b)={ (q0, bb)}
(q0
, a, z)={ (q0
, az)}
(q0, b, z)={ (q0, bz)}
15
8/13/2019 7. Pushdown Automata
16/26
Switching from q0to q
1for matching
(nondeterministic)
(q0, , a)={ (q1, a)}
(q0, , b)={ (q1, b)}
Matching wRwith w in the stack
(q1, a, a)={ (q1, )}
(q1, b, b)={ (q1, )}
In the end
(q1, , z)={ (q2, z)}
16
8/13/2019 7. Pushdown Automata
17/26
Run M on the input abba:
17
8/13/2019 7. Pushdown Automata
18/26
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
8/13/2019 7. Pushdown Automata
19/26
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: (q0, , z)={ (q1, Sz) }
Process the input: (q1, a, S)={ (q1, SA), (q1, )}
(q1, b, A)={ (q1, B) } (q1, b, B)={ (q1, )}
In the end: (q1, , z)={ (q2, )}
19
8/13/2019 7. Pushdown Automata
20/26
Run M on the input aaabbbb
See the relation between *(q0, aaabbbb, z) and
S* aaabbbb
20
8/13/2019 7. Pushdown Automata
21/26
Convert pda to cfg
Idea: the grammar simulates the move of pda
Assumptions for an npda Only one final state qf
All transitions must have form, a{},
(qi, a, A)={ c1, c2, , cn}, where ci=(qj, ) or ci=(qi, BC)
Simulation each variable is of form (qiAqj) -- generating string w
Starting at state qi
Top stack symbol is A Read in string w
Ending at state qj
Stack symbol is popped out.
21
8/13/2019 7. Pushdown Automata
22/26
The construction
Start variable: S=(q0zqf)
For transitions a{},
(qi, a, A)={ c1, c2, , cn}, where ci=(qj, ) or ci=(qi, BC)
If ci=(qj, ), add production (qiAqj)a If ci=(qj, BC), add production (qiAqk)a(qjBql)(qlCqk),
for all qk, qlQ
22
8/13/2019 7. Pushdown Automata
23/26
Example
(q0, a, z)={ (q0, Az) }
(q0, a, A)={ (q0, A) }
(q0, b, A)={ (q1, ) }
(q1, , z)={ (q2, ) } Converted to satisfy the requirements
(q0, a, z)={ (q0, Az) }
(q3, , z)={ (q0, Az) }
(q0, a, A)={ (q3, ) }
(q0, b, A)={ (q1, ) }
(q1, , z)={ (q2, ) }
23
8/13/2019 7. Pushdown Automata
24/26
Work
24
8/13/2019 7. Pushdown Automata
25/26
The final result
25
8/13/2019 7. Pushdown Automata
26/26
Comments on dpda and dcfl
For efficient parsing, we need deterministic
cfl.
The corresponding deterministic pda
(q, a, b) contains one element
If (q, , b) is not empty, (q0, c, b) is empty for
c
26