An Introduction to Pushdown Automata - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

An Introduction to Pushdown Automata

Description:

Each transition of M in (c) adds a single rule to G, while each transition in (d) ... This language cannot be recognized by empty stack because it is not prefix-free. ... – PowerPoint PPT presentation

Number of Views:212
Avg rating:3.0/5.0
Slides: 24
Provided by: csU73
Category:

less

Transcript and Presenter's Notes

Title: An Introduction to Pushdown Automata


1
An Introduction toPushdown Automata
  • COT 4210
  • Summer 2006
  • Dr. David A. Workman
  • School of EE and CS

2
Definition
  • A (nondeterministic) PDA is a 6-tuple, M (Q, S,
    G, d, q0, Z0, A).
  • Q finite set of states
  • ? input alphabet
  • ? stack alphabet
  • q0 ? Q (initial state)
  • Z0 ? ? (initial stack symbol) BOS
  • A ? Q (set of accept states)
  • ? rel Q ? (???) ? ?? Q ? ? (partial relation)

a1a2a3.. ak . an
(string over the input alphabet)
(read head)(advances one symbol on each read
operation)
Finite Control
q
Configuration of M ( q, akan, ZtopZ0)
Ztop Z0
String over the stack alphabet ?
stack
3
Languages Accepted by PDA
  • Move Relation on Configurations ?
  • (q , x, Zw) M? (q', x, g w) if and only if (q',
    g ) ? d (q, ?, Z) //stack move
  • (q , ax, Zw) M? (q', x, g w) if and only if
    (q', g ) ? d (q, a, Z) a ?? //read move
  • Lstk(M) x ? ? (q0 , x, Z0) M? (q', l ,l )
  • Lste(M) x ? ? (q0 , x, Z0) M? (q', l , ?
    ), for some q' ? A

(a,Z0) ? a
1
2
(a, a) ? aa
(b, a) ? ?
(?, Z0) ? ?
3
(b, a) ? ?
Lstk(M) anbn n ? 0
4
Lstk(M) Lste(M)
M accepts by final state
M accepts by empty stack
q0
p0
?
q1
5
Lste(M) Lstk(M)
M accepts by empty stack
M accepts by final state
q0
p0
?
q1
6
PDA Examples
L x?a, b xa xb
(a, a) ? aa (b, b) ? bb (b, a) ? ? (a, b) ? ?
(a,Z0) ? aZ0
1
2
(b,Z0) ? bZ0
Accept byfinal state
(?, Z0) ? Z0
(a, a) ? aa (b, b) ? bb (b, a) ? ? (a, b) ? ?
(a,Z0) ? aZ0
1
2
(b,Z0) ? bZ0
(?, Z'0) ? Z0 Z'0
(?, Z0) ? Z0
Accept byempty stack
1'
(?, Z0) ? ?
3
(?, Z'0) ? ?
7
PDA Examples
L a0n1n n ? 0 ? b0n12n n ? 0
(0, 0) ? 00
(0, a) ? 0
(?,a) ? ?
(1, 0 ) ? ?
2
4
(1, 0 ) ? ?
(a, Z0) ? a
1
(b, Z0) ? b
Accept byempty stack
(1, 0 ) ? ?
(1, 0 ) ? 0
6
5
3
(?,b) ? ?
(0, b) ? 0
(1, 0 ) ? 0
(0, 0) ? 00
8
PDA Examples
L a0n1n n ? 0 ? b0n12n n ? 0
(1, 0 ) ? ?
(0, 0) ? 00
Accept byfinal state
(0, a) ? 0
(?,a) ? ?
(1, 0 ) ? ?
2
4
(?, Z'0) ? ?
(a, Z0) ? a
(?, Z'0) ? ?
?
(?, Z'0) ? Z0 Z'0
1
1'
(?, Z'0) ? ?
(b, Z0) ? b
(?, Z'0) ? ?
(1, 0 ) ? ?
(1, 0 ) ? 0
6
5
3
(?,b) ? ?
(0, b) ? 0
(1, 0 ) ? 0
(0, 0) ? 00
9
Equivalence of CFG to PDAs
Example. Consider the grammar for arithmetic
expressions we introduced earlier. It is
reproduced below for convenience. G ( E, T,
F, n, v, , , ( , ), P, E), where E 1
E ? E T, 2 E ? T, 3 T ? T? F,
4 T ? F, 5 F ? n, 6 F ? v, 7 F ? ( E
), Suppose the input to our parser is
the expression, n(vnv). Since G is
unambiguous this expression has only one
leftmost derivation, p 2345712463456. We
describe the behavior of the PDA in general, and
then step through its moves using this derivation
to guide the computation.
10
Equivalence of CFG to PDAs
PDA Simulator. Step 1 Initialize the stack
with the start symbol (E in this case). The
start symbol will serve as the bottom of stack
marker (Z0). Step 2 Ignoring the input, check
the top symbol of the stack. Case (a) Top of
stack is a nonterminal, X non-deterministicall
y decide which X-rule to use as the next step of
the derivation. After selecting a rule, replace
X in the stack with the rightpart of that rule.
If the stack is non-empty, repeat step 2.
Otherwise, halt (input may or may not be
empty.) Case(b) Top of stack is a terminal, a
Read the next input. If the input matches a, then
pop the stack and repeat step 2. Otherwise,
halt (without popping a from the stack.) This
parsing algorithm by showing the sequence of
configurations the parser would assume in an
accepting computation for the input, n(vnv).
Assume q0 is the one and only state of this
PDA. p (leftmost derivation in G)
2345712463456
11
Equivalence of CFG to PDAs
p (leftmost derivation in G) 2345712463456
(q0, n(vnv), E) 2?M (q0, n(vnv), T)
3?M (q0, n(vnv), TF) 4?M (q0, n(vnv),
FF) 5?M (q0, n(vnv), nF) read?M (q0,
(vnv), F) read?M (q0, (vnv), F) 7?M (q0,
(vnv), (E) ) read?M (q0, vnv), E) ) 1?M
(q0, vnv),ET) ) 2?M (q0, vnv), TT) ) 4?M
(q0, vnv), FT) ) 6?M (q0, vnv), vT)
) read?M (q0, nv), T) ) read?M (q0, nv), T)
) 3?M (q0, nv), TF) ) 4?M (q0, nv), FF)
) 5?M (q0, nv), nF) ) read?M (q0, v), F)
) read?M (q0, v), F) ) 6?M (q0, v), v)
) read?M (q0, ), ) ) read?M (q0, l, l )
accept!
12
Equivalence of CFGs to PDAs
THEOREM. Let G (N, S, P, S) denote any reduced
Context-free grammar. Then there is a PDA, M
(q0, S, VG, d, q0, F), such that Lstk(M)
L(G). The converse is also true. If M is any
PDA accepting by empty stack, then there is a
Context-free grammar, G, such that L(G)
Lstk(M).
Proof. Given G, construct M. ? a ? ? ?(q0,
a, a ) (q0, ? ) ? X ? N ?(q0, ?, X )
(q0, ? ) X ? ? ? P Then M
non-deterministically simulates a leftmost
derivation in G. If the stack is empty precisely
when the input has been consumed, then the PDA
halts and accepts and it musthave traced a
leftmost derivation of the input in G.
13
Equivalence of CFGs to PDAs
THEOREM. Let G (N, S, P, S) denote any reduced
Context-free grammar. Then there is a PDA, M
(q0, S, VG, d, q0, F), such that Lstk(M)
L(G). The converse is also true. If M is any
PDA accepting by empty stack, then there is a
Context-free grammar, G, such that L(G)
Lstk(M).
Proof. Given M (Q, S, G, d, q0, Z0, F),
construct G (N, S, P, S). The key ideas of the
construction are two-fold(a) N (nonterminal
alphabet of G) S ? qZq' q, q' ?QM
and Z ? ? (b) For all q, q' ?QM qZq' G? x
? ? if and only if ( q, x , Z) M? (q', l,
l)From (b) it then follows that for each q' in
Q, q0Z0q' G? x ? ? if and only if ( q0, x
, Z0 ) M? (q', l, l). But the latter expression
is exactly an accepting computation in M. Thus
if we add rules to G of the form S ? q0Z0q',
for each q' in Q, then we have S G? q0Z0q'
G? x ? ? ( x ?L(G)) if and only if x
?Lstk(M)). We now show how to construct the
rules of G for each non-terminal, qZq'. The
approach is to develop rules of G recursively in
terms of transitions in M. (c) For each
transition (q, l) ? d(q, a, Z) in M, a ? ? ,
add qXq ? a to G For each transition
(q, l) ? d(q, L, Z) in M, add qXq ? l to
G. (d) For each transition (p0, Y1Y2Yk) ? d(q,
a, Z) in M, a ? ? , add the set of rules
qZpk ? a p0Y1p1p1Y2p2pk-1Ykpk
p1,p2,,pk ? Q to G For each transition
(p0, Y1Y2Yk) ? d(q, L, Z) in M, add the set of
rules qZpk ? p0Y1p1p1Y2p2pk-1Ykp
k p1,p2,,pk ? Q to G
14
Equivalence of CFGs to PDAs
  • TIPS and Insights
  • Each transition of M in (c) adds a single rule to
    G, while each transition in (d) adds a set of
    Qk rules to G. The reason for this exponential
    explosion in (d) is because without knowing
    details of M or its runtime input, we have to
    allowfor all possible states M could be in (or
    reach) from any configuration wherethe
    transition is defined. Thus p1, , pk have to
    vary over all states of M.
  • Once the grammar, G, is constructed, apply the
    standard algorithm for reducing a CFG to
    eliminate all useless rules.
  • Even for small PDAs, the size of G may be
    prohibitive in size to construct by hand.
    Fortunately there are one or two heuristics we
    can apply during construction to avoid generating
    rules we know will be useless. Principle A
    rule of the form qXpk ? p0Y1p1p1Y2p2pk-1Yk
    pk is useless if any one of the nonterminals
    qXpk, pj-1Yjpj, is unreachable qZq is
    useless in G if in M there is no way to reach
    state q from state q, or there is no way to
    reach state q from state q with Z is on top of
    the stack where the stack in state q is shorter
    by one symbol.

15
Example
We illustrate the construction of a CFG from a
PDA with a PDA, M, for which Lstk(M) 0n1n n
? 0.
The rules of G produced for this PDA are (a)
S ? 1Z01, S ? 1Z02, S ? 1Z03 ? (b) 1
Z01 ? l, 203 ? 1, 303 ? 1 ? (c) 1Z01
? 0201, 1Z02 ? 0202, 1Z03 ? 0203 ?
201 ? 0201101, 201 ? 0202201,
201 ? 0203301 ? 202 ?
0201102, 202 ? 0202202, 202 ?
0203302 ? 203 ? 0201103,
203 ? 0202203, 203 ? 0203303
Note 1 There is no way for M to go from state 2
back to state 1, or from state 3 to state 2 or
1. Consequently, all nonterminals of the form
2Z1 and 3Z2, 3Z1 are unreachable and the
corresponding rules of G are useless. These are
highlighted in red.
Note 2 202 is also unreachable because there
is no way for M to go from state 2 back to
state 2 with a shorter stack. All these
rules are highlighted in blue.
Note 3 Applying the algorithm for reducing a
CFG singles out S ? 1Z02 as the last useless
rule.
16
Deterministic PDAs and DCFLs
  • Our PDA model exhibits non-determinism in several
    forms
  • Option to read or ignore input in a given state
    and stack symbol pair.
  • Choice of several possible next states and stack
    configurations on any given transition.
  • Our next definition eliminates these options for
    DPDAs!

Definition. A Deterministic Pushdown Automaton
(DPDA) is a 7-tuple, M (Q, ?, ?, ?, q0, Z0, A),
where Q finite set of states, ? input
alphabet, ? stack alphabet, q0 ? Q the
initial state, Z0 ?? bottom of stack marker
(or initial stack symbol), and ? Q ? (? ?L) ?
? ? Q ? ? the transition function (not
necessarily total). Specifically, 1 if d(q,
a, Z) is defined for some a ?? and Z ??, then
d(q, L, Z) ? and ?d(q, a, Z)? 1. 2
Conversely, if d(q, L, Z) ? ?, for some Z, then
d(q, a, Z) ? ?, for all a ??, and ?d(q, L, Z)?
1. NOTE DPDAs can accept their input either by
final state or by empty stack just as for the
non-deterministic model. We therefore define
Dstk and Dste, respectively, as the corresponding
families of Deterministic Context-free Languages
accepted by a DPDA by empty stack and final
state.
17
DPDA Examples
Transitions Valid tworead transitions defined
fordifferent input symbols in the
sameconfiguration.
Lstk(M) 0n1n n ? 0 .
Violates determinism bothread
and stack transitionspossible in the
sameconfiguration.
18
DPDA Examples
Lste(M) 0n1n n ? 0 .
OK Accept state allowsacceptance of null
input,or when stack is emptyin state 3.
OK Read transitionand stack transition
aredefined for different stack configurations.
Empties stack to ensure M halts instate 1.
19
Properties of DCFLs
  • Theorem. If L ? DCFLstk, then L is prefix-free.
    A language is prefix-free if and only if, x ? L
    implies xy ? L for all strings y ?? ? .
  • Proof. Suppose (q0, x, Z0) M? (q', ?, ?).
    Then the computation must follow one of three
    scenarios(a) x ? and M accepts by a sequence
    of one or more stack moves (q0, ?, Z0) s?
    (p', ?, ?) s? (q', ?, ?) and ?(q0, ?, Z0) (p',
    ?) or(b) x ?? ? and M either empties the stack
    on the last read move (b.1), or M reads the
    last symbol of x without emptying the stack and
    then executes one or more stack moves that
    empty the stack (b.2)(b.1) (q0, x, Z0) M?
    (p', a, Z') r? (q', ?, ?) and ?(p', a, Z')
    (q', ?) or(b.2) (q0, x, Z0) M? (p , a, Y?) r?
    (p', ?, Z'?) s? (q', ?, ?).
  • Now consider M's behavior on (q0, xy, Z0). If x
    ?, then scenario (a) must apply because M
    cannot define both a read transition and a stack
    transition in its initial configuration. Since M
    accepts x ?, then it must make a stack
    transition on its first move and we have (q0, y,
    Z0) s? (p', y, ?) s? (q', y, ?) Halt! Thus it
    is impossible for M to ever read y before
    emptying its stack.
  • Suppose x ?? ?. If scenario (b.1) applies, then
    we have (q0, xy, Z0) M? (p', ay, Z') r? (q',
    y, ?) Halt! Once again, M cannot read any of y
    before emptying the stack.If scenario (b.2)
    applies, then we obtain (q0, xy, Z0) M? (p ,
    ay, Y?) r? (p', y, Z'?) s? (q', y, ?) Halt! and
    it is impossible for M to read y before it
    empties the stack.

20
Properties of DCFLs
  • Theorem. If L ? DCFLstk, then L ? DCFLste.
    However, the converse is not true.
  • Proof. The construction used to convert a PDA,
    M, that accepts by empty stack to some M' that
    accepts by final state preserves determinism.
    That is, if M is a DPDA, then M' will also be a
    PDA. This is because all the new transitions
    added to construct M' from M, are stack
    transitions defined only on stack symbols that
    are unknown to M.The same statement cannot be
    made about the construction converting from M' to
    M.For in this case, M must use stack transitions
    to empty the stack from any accept state of M'
    this may require M to define a stack transitions
    in some state of M' for which a read transition
    is already defined for the same stack
    configuration.Note This last statement does not
    imply that an equivalent DPDA that accepts by
    empty stack does not exist, it only states that
    the one produced by the canonical construction
    may not be a DPDA. The converse is proven by a
    simple counter example. Lste(M) 0n1n n
    ? 0 . This language cannot be recognized by
    empty stack because it is not prefix-free.
    However, the DPDA shown below clearly accepts
    this language by final state.

(0,0)?00
(1,0)??

(0,Z
)?0Z0
(1,0)??
2
3
0
start
1
(?, Z0)??
21
Properties of CFLs and DCFLs
L1 wwrev w ? 0,1 L2 0n1n n ? 0
L3 L2 ? L4 a , aa L5 ab
CFLstk CFLste
L1
DCFLste
L2
DCFLstk
Regular
L4
L3
L5
22
Properties of CFLs and DCFLs
  • Theorem (Pumping Lemma for CFLs). Let L be a
    Context-free Language. Then there exists a
    positive constant a defined for L such that, for
    all x in L with x ? a, x can be written in the
    form uvwxy where vwx ? ? and vx ? ? such that,
    for all k ? 0, uvkwxky ? L.

Example. L anbncn n ? 0 is NOT
Context-free. Proof. Suppose L is a CFL. By
the P.L. for CFLs let a be the constant defined
for L.Consider x aabaca. Any decomposition
of x in the form uvwxy, where vwx is boundedby
a implies the following choices for v and x(1)
both occur in aa,(2) both occur in ba,(3) both
occur in ca,(4) v occurs in aa while x occurs in
ba,(5) v occurs in ba while x occurs in ca.If
cases (1)(2) or (3) prevail, then pumping
increases exactly one of the symbols
a,b,cwithout changing the number of
occurrences of the others. Such strings do not
belong to L.If cases (4) or (5) hold, then
pumping increases at most two of the symbols a,
b, c withoutchanging the number of occurrences
of the third. Such strings do not belong to L.
Thusno decomposition of the desired form exists
for x where arbitrary pumping generates
stringsonly in L. This contradicts the PLCFL
and we conclude, L must not be Context-free.
23
Closure Properties of CFLs and DCFLs
Write a Comment
User Comments (0)
About PowerShow.com