Chpter 5 Push-down Automt By Dr Zlmiyh Zkri
Push-Down Automt Regulr expressions re string genertors they tell us how to generte ll strings in lnguge L. Finite Automt (DFA, NFA) re string cceptors they tell us if specific string w is in L. CFGs re string genertors Are there string cceptors for Context-Free lnguges? YES! Push-down utomt Sept2011 Theory of Computer Science 2
Push-Down Automt DFAs ccept regulr lnguges. Now, we wnt to design mchines similr to DFAs tht will ccept context-free lnguges. These mchines will need to e more powerful. To hndle lnguge like { n n n 0}, the mchine needs to rememer the numer of s. To do this, we use stck. A push-down utomton (PDA) is essentilly n NFA with stck.
Push-Down Automt A PDA hs some memory, ut not generl purpose, rndom-ccess memory. It uses stck, which hs two opertions Push nd Pop. Using the stck, it llows us to: Count Mtch strings Sept2011 Theory of Computer Science 4
Building PDA stte control push $ red 0 push x red 1 pop x red 1 pop x pop $ L = {0 n 1 n : n 1} Rememer ech 0 y pushing x onto the stck When we see 1, we pop n x from the stck We wnt to ccept when we hit the stck ottom $ = specil mrker for ottom
A PDA in ction stte control push $ red 0 push x red 1 pop x red 1 pop x pop $ input L = {0 n 1 n : n 1} 0 0 0 1 1 1 $ stck x x x
Nottion for PDAs push $ red 0 push x red 1 pop x red 1 pop x pop $ q 0 q 1 q 2 q 3, / $ 1, x /, $ / 0, / x 1, x / red, pop / push
Definition of PDA A pushdown utomton (PDA) is sextuple (Q,,,, q 0, F) where: Q is finite set of sttes; is the input lphet; is the stck lphet q 0 in Q is the strt stte; F Q is set of finl sttes; is the trnsition function : Q ( {}) ( {}) susets of Q ( {}) PDA ccepts w if we end up in finl stte with n empty stck
Exmple q 0 q 1 q 2 q 3, / $ 0, / x 1, x /, $ / 1, x / = {0, 1} = {$, x} (q 0,, ) = {(q 1, $)} (q 0,, $) = (q 0,, x) = (q 0, 0, ) =... : Q ( {}) ( {}) susets of Q ( {}) stte input symol pop symol stte push symol
The lnguge of PDA A PDA is nondeterministic Multiple trnsitions on sme pop/input llowed Trnsitions my ut do not hve to push or pop The lnguge of PDA is the set of ll strings in S* tht cn led the PDA to n ccepting stte
Trnsitions Let ((p,, β), (q, γ)) Δ e trnsition. It mens tht we Move from stte p. Red from the tpe, Pop the string β from the stck, Move to stte q, Push string γ onto the stck. The first three (p,, β), re input. The lst two (q, γ) re output.
Trnsitions We will drw it s or If t q 0 (p), with next input symol () nd top of stck x (), then cn consume (), pop x (), push y () onto stck nd move to q 1 (q)
Pushing nd Popping When we push β, we push the symols of β s we red them right to left. When we push the string c, we push c, then push, then push. When we pop γ, we pop the symols of γ s we red them from left to right (reverse order). When we pop the string c, we pop, then pop, then pop c.
Pushing nd Popping Thus, if we push the string c nd then pop it, we will get ck c, not c. If we wnted to reverse the order, we would use three seprte trnsitions: Push Push Push c
Configurtions A configurtion fully descries the current stte of the PDA. The current stte p. The remining input w. The current stck contents. Thus, configurtion is triple (p, w, ) (K, *, * ).
Computtions A configurtion (p, w, ) yields configurtion (p', w', ') in one step, denoted (p, w, ) (p', w', '), if there is trnsition ((p,, ), (p', )) Δ such tht w = w', =, nd ' = for some *. The reflexive, trnsitive closure of is denoted *.
Accepting Strings After processing the string on the tpe, The PDA is in either finl or nonfinl stte, nd The stck is either empty or not empty. The input string is ccepted if The ending stte is finl stte, nd The stck is empty. Tht is, the string w * is ccepted if (s, w, e) * (f, e, e) for some f F.
Accepting Strings One my define cceptnce y finl stte only. The input is ccepted if nd only if the lst stte is finl stte, regrdless of whether the stck is empty. One my define cceptnce y empty stck only. The input is ccepted if nd only if the stck is empty once the input is processed, regrdless of which stte the PDA is in.
Exmple of PDA Run the following PDA on the input string.
Exmple of PDA The steps in the processing re (s,, e) (p,, A) (p,, AA) (p,, AAA) (p,, AA) (p,, A) (q, e, e).
The Lnguge of PDA The lnguge of PDA A is L(A) = {w * A ccepts w}. Wht is the lnguge of the PDA in the previous exmple?
Exmple of PDA Wht is the lnguge of the following PDA?
Exmples of PDAs Let = {, }. Design PDA tht ccepts the lnguge {wcw R w * }. Design PDA tht ccepts the lnguge {ww R w * }.
Exmples of PDAs Design PDA whose lnguge is { m n 0 m < n}. Design PDA whose lnguge is { m n 0 n < m}.
Exmples of PDAs Design PDA whose lnguge is { m n c n d m m 0, n 0}. Design PDA whose lnguge is { m m c n d n m 0, n 0}. Design PDA whose lnguge is { m n c p d q m + n = p + q}. Design PDA whose lnguge is { m n c k m = n or m = k}.
Pushdown Automt PDAs 27
Pushdown Automton -- PDA Input String Stck Sttes 28
Initil Stck Symol Stck Stck stck hed $ z top ottom specil symol Appers t time 0 29
Input symol The Sttes Pop symol Push symol 30
, c q 1 q 2 input stck e h $ top Replce c e h $ 31
, c q 1 q 2 input stck c top Push e h $ e h $ 32
, q 1 q 2 input stck top Pop e h $ e h $ 33
, q 1 q 2 input stck e h $ top No Chnge e h $ 34
Empty Stck input, $ q 1 q 2 stck $ top Pop empty The utomton HALTS No possile trnsition fter q 2 35
A Possile Trnsition input, $ q 1 q 2 stck $ top Pop 36
Non-Determinism PDAs re non-deterministic Allowed non-deterministic trnsitions, c q 2 q 1, c q 1 q 2, c q 3 -trnsition 37
Exmple PDA PDA M, L(M) = { n n : n 0},,,,, $ $ q 0 q 1 q 2 q 3 38
Bsic Ide: L(M) = { n n : n 0} 1. Push the s on the stck 2. Mtch the s on input with s on stck,, 3. Mtch found,,, $ $ q 0 q 1 q 2 q 3 39
Execution Exmple: Input Time 0 $ Stck current stte,,,,, $ $ q q 1 q 2 q 3 0 40
Input Time 1 $ Stck,,,,, $ $ q 0 q 1 q 2 q 3 41
Input Time 2 $ Stck,,,,, $ $ q 0 q 1 q 2 q 3 42
Input Time 3 $ Stck,,,,, $ $ q 0 q 1 q 2 q 3 43
Input Time 4 $ Stck,,,,, $ $ q 0 q 1 q 2 q 3 44
Input Time 5 $ Stck,,,,, $ $ q 0 q 1 q 2 q 3 45
Input Time 6 $ Stck,,,,, $ $ q 0 q 1 q 2 q 3 46
Input Time 7 $ Stck,,,,, $ $ q 0 q 1 q 2 q 3 47
Input Time 1 $ Stck,, ccept,,, $ $ q 0 q 1 q 2 q 3 48
A string is ccepted if there is computtion such tht: All the input is consumed AND The lst stte is n ccepting stte At the end of the computtion, we do not cre out the stck contents (the stck cn e empty t the lst stte) 49
The input string is ccepted y the PDA:,,,,, $ $ q 0 q 1 q 2 q 3 50
In generl, L = { n n : n 0} is the lnguge ccepted y the PDA:,,,,, $ $ q 0 q 1 q 2 q 3 51
Rejection Exmple: Input Time 0 $ Stck current stte,, q 0,, $ $, q 1 q 2 q 3 52
Rejection Exmple: Input Time 1 $ Stck current stte,, q 0,, $ $, q 1 q 2 q 3 53
Rejection Exmple: Input Time 2 $ Stck current stte,, q 0,, $ $, q 1 q 2 q 3 54
Rejection Exmple: Input Time 3 $ Stck current stte,, q 0,, $ $, q 1 q 2 q 3 55
Rejection Exmple: Input Time 4 $ Stck current stte,, q 0,, $ $, q 1 q 2 q 3 56
Rejection Exmple: Input Time 4 reject $ Stck current stte,, q 0,, $ $, q 1 q 2 q 3 57
The input string is rejected y the PDA:,,,,, $ $ q 0 q 1 q 2 q 3 58
A string is rejected if there is no computtion such tht: All the input is consumed AND The lst stte is n ccept stte At the end of the computtion, we do not cre out the stck contents 59
Another PDA exmple L (M) = {vv R : v {,}*} PDA M,,,, q 0,, $ $ q 1 q 2 60
Bsic Ide: L (M) = {vv R : v {,}*} 1. Push v on stck 2. Guess middle 3. Mtch v R on input with v on stck of input,, 4. Mtch,, found q 0,, $ $ q 1 q 2 61
Execution Exmple: Input Time 0 $,, Stck,, q 0,, $ $ q 1 q 2 62
Input Time 1 $,, Stck,, q 0,, $ $ q 1 q 2 63
Time 2 Input $,, Stck,, q 0,, $ $ q 1 q 2 64
Time 3 Input Guess the middle of string $,, Stck,, q 0,, $ $ q 1 q 2 65
Time 4 Input $,, Stck,, q 0,, $ $ q 1 q 2 66
Input Time 5 $,, Stck,, q 0,, $ $ q q 2 1 67
Input Time 6 $,, Stck,, Accept q 0, q 1, $ $ q 2 68
Rejection Exmple: Input Time 0 $,, Stck,, q 0,, $ $ q 1 q 2 69
Input Time 1 $,, Stck,, q 0,, $ $ q 1 q 2 70
Input Time 2 $,, Stck,, q 0,, $ $ q 1 q 2 71
Input Time 3 Guess the middle of string $,, Stck,, q 0,, $ $ q 1 q 2 72
Input Time 4 $,, Stck,, q 0,, $ $ q 1 q 2 73
Input Time 5 There is no possile trnsition. Input is not consumed $,, Stck,, q 0,, $ $ q q 2 1 74
Another computtion on sme string: Input Time 0 $,, Stck,, q 0,, $ $ q 1 q 2 75
Input Time 1 $,, Stck,, q 0,, $ $ q 1 q 2 76
Input Time 2 $,, Stck,, q 0,, $ $ q 1 q 2 77
Input Time 3 $,, Stck,, q 0,, $ $ q 1 q 2 78
Input Time 4 $,, Stck,, q 0,, $ $ q 1 q 2 79
Input Time 5 No finl stte is reched $,, Stck,, q 0,, $ $ q 1 q 2 80
There is no computtion tht ccepts string L(M),,,, q 0,, $ $ q 1 q 2 81
Another PDA exmple L (M) = {w {,}*: in every prefix v, n (v) n (v)} PDA M,, q 0 82
Execution Exmple: Time 0 Input,,, $ $ Stck q 0 83
Time 1 Input,,, $ $ Stck q 0 84
Time 2 Input,,, $ $ Stck q 0 85
Time 3 Input,,, $ $ Stck Accept q 0 86
Rejection Exmple: Time 0 Input,,, $ $ Stck q 0 87
Time 1 Input,,, $ $ Stck q 0 88
Time 2 Input,,, $ $ Stck q 0 89
Time 3 Input,,, $ Stck q 0 90
Time 4 Input,,, $ Stck q 0 Hlt nd Reject 91
Pushing Strings Input symol Pop symol Push string, w q 1 q 2 92
Exmple:, cdf q 1 q 2 input stck d top f h Push h e e $ $ c pushed string 93
Another PDA exmple L (M) = {w {,}*: n (w) = n (w)} PDA M, $ 0$, 0 00,1, $ 1$, 1 11, 0, $ $ q 1 q 2 94
Execution Exmple: Input Time 0, $ 0$, 0 00,1, $ 1$, 1 11, 0 $ Stck current stte, $ $ q 1 q 2 95
Input Time 1, $ 0$, $ 1$, 0 00, 1 11,1, 0 0 $ Stck, $ $ q 1 q 2 96
Input Time 3, $ 0$, 0 00,1, $ 1$, 1 11, 0 0 $ Stck, $ $ q 1 q 2 97
Input Time 4, $ 0$, 0 00,1, $ 1$, 1 11, 0 1 $ Stck, $ $ q 1 q 2 98
Input Time 5, $ 0$, 0 00,1, $ 1$, 1 11, 0 1 1 $ Stck, $ $ q 1 q 2 99
Input Time 6, $ 0$, 0 00,1, $ 1$, 1 11, 0 1 1 $ Stck, $ $ q 1 q 2 100
Input Time 7, $ 0$, 0 00,1, $ 1$, 1 11, 0 1 $ Stck, $ $ q 1 q 2 101
Input Time 8, $ 0$, 0 00,1, $ 1$, 1 11, 0 Accept $ Stck, $ $ q 1 q 2 102
Formlities for PDAs 103
, w q 1 q 2 Trnsition function: ( 2 q1,, ) {( q, w)} 104
, w q 2 q 1, w q 3 Trnsition function: ( 3 q1,, ) {( q2, w), ( q, w)} 105
Forml Definition Pushdown Automton (PDA) M ( Q, Σ, Γ, δ, q0, z, F) Finl Sttes sttes Input lphet Stck lphet Trnsition function Initil stte Stck strt symol 106
Instntneous Description ( q, u, s) Current stte Remining input Current stck contents 107
Exmple: Time 4: Instntneous Description ( q 1,, $) Input $,, Stck,,, $ $ q 0 q 1 q 2 q 3 108