Professional Documents
Culture Documents
CH 12 PDAs
CH 12 PDAs
Chapter 12
Recognizing Context-Free Languages
We need a device similar to an FSM except that it
needs more power.
(((()))
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
Then:
(q1, cw, 1) |-M (q2, w, 2) iff ((q1, c, 1), (q2, 2)) .
● C0 is an initial configuration,
● Cn is of the form (q, , ), for some state q KM and
some string in *, and
● C0 |-M C1 |-M C2 |-M … |-M Cn.
Nondeterminism
If M is in some configuration (q1, s, ) it is possible that:
¶
This does not mean that the stack is empty
A PDA for AnBn = {anbn: n 0}
A PDA for {wcwR: w {a, b}*}
A PDA:
A PDA for {w {a, b}* : #a(w) = #b(w)}
Accepting Mismatches
L = {ambn : m n; m, n > 0}
b/a/
a//a
b/a/
1 2
b/a/ /a/
a//a
b/a/ /a/
1 2 3
Accepting Mismatches
L = {ambn : m n; m, n > 0}
b/a/
a//a
b/a/
1 2
b/a/ b//
a//a
b/a/ b//
1 2 4
Putting It Together
L = {ambn : m n; m, n > 0}
Restate theorem:
Two approaches:
• Top down
• Bottom up
Top Down
The idea: Let the stack keep track of expectations.
EE+T
ET
TTF
TF
F (E)
F id
(1) (q, , E), (q, E+T) (7) (q, id, id), (q, )
(2) (q, , E), (q, T) (8) (q, (, ( ), (q, )
(3) (q, , T), (q, T*F) (9) (q, ), ) ), (q, )
(4) (q, , T), (q, F) (10) (q, +, +), (q, )
(5) (q, , F), (q, (E) ) (11) (q, , ), (q, )
(6) (q, , F), (q, id)
A Top-Down Parser
The construction in general:
input = a a b b a a