PDA 1 Increasing the power of FSAs The mechanical view Input tape Finite state control device Output tape
PDA 2 Now let us enrich it : a Input tape stack memory Finite state control device x q Output tape p A B Z 0
PDA 3 What is a stack? New symbols are inserted to the top The stack is read from the top A read symbol is extracted from the stack LIFO policy Z 0
PDA 4 Example Insert in the following order the symbols a b c Z 0
PDA 5 Example Insert in the following order the symbols a b c a Z 0
PDA 6 Example Insert in the following order the symbols a b c b a Z 0
PDA 7 Example Insert in the following order the symbols a b c c b a Z 0
PDA 8 Example Insert in the following order the symbols a b c c b a Read from the stack Z 0
PDA 9 Example Insert in the following order the symbols a b c c c b a Read from the stack Z 0
PDA 10 Pushdown automata Finite state automata can be enriched with a stack Pushdown Automata (PDA) PDAs differ from finite state machines in two ways: They can use the top of the stack to decide which transivon has to be made They can manipulate the stack as part of performing a transivon
PDA 11 Moves of a PDA Depending on the symbol read from the input (but it could also not read anything) the symbol read from the top of the stack the state of the control device the PDA changes its state moves ahead the scanning head changes the symbol read from the stack with a string α (possibly empty)
PDA 12 Acceptance The input string x is recognized (accepted) if the PDA scans it completely upon reaching the end of x, it is in an accepvng state Input tape stack memory Finite state control device q p A B Z 0
PDA 13 PDA: a first example L={a n b n n>0} a,a/aa b,a ε a,z 0 / BZ 0 q b,a ε 0 q b,b ε 1 q 2 q 3 a,b/ab b,b ε
PDA 14 Example (2) a,a/aa b,a ε a,z 0 / BZ 0 q 0 q 1 b,a ε q 2 b,b ε q 3 a,b/ab b,b ε a a a b b b Z 0
PDA 15 Example (2) a,a/aa b,a ε a,z 0 / BZ 0 q 0 q 1 b,a ε q 2 b,b ε q 3 a,b/ab b,b ε a a a b b b B Z 0
PDA 16 Example (2) a,a/aa b,a ε a,z 0 / BZ 0 q 0 q 1 b,a ε q 2 b,b ε q 3 a,b/ab b,b ε A a a a b b b B Z 0
PDA 17 Example (2) a,a/aa b,a ε a,z 0 / BZ 0 q 0 q 1 b,a ε q 2 b,b ε q 3 a,b/ab b,b ε A A a a a b b b B Z 0
PDA 18 Example (2) a,a/aa b,a ε a,z 0 / BZ 0 q 0 q 1 b,a ε q 2 b,b ε q 3 a,b/ab b,b ε A a a a b b b B Z 0
PDA 19 Example (2) a,a/aa b,a ε a,z 0 / BZ 0 q 0 q 1 b,a ε q 2 b,b ε q 3 a,b/ab b,b ε a a a b b b B Z 0
PDA 20 Example (2) a,a/aa b,a ε a,z 0 / BZ 0 q 0 q 1 b,a ε q 2 b,b ε q 3 a,b/ab b,b ε a a a b b b Z 0
PDA 21 Formally A PDA is a tuple <Q, I, Γ, δ, q 0, Z 0, F> Q is a finite set of states I is the input alphabet Γ is the stack alphabet δ is the transivon funcvon q 0 Q is the inival state Z 0 Γ is inival stack symbol F Q is the set of final states
PDA 22 TransiVon funcvon δ is the transivon funcvon δ: Q (I {ε}) Γ Q Γ * δ(q, i, A)=<p, α> Graphical notavon: q i,a/α p
PDA 23 Remarks Q, I, q 0 and F are defined as in FSAs δ is a parval funcvon Z 0 is the inival symbol of the stack, but it is not essenval It is useful to simplify definivons δ(q, ε, A)=<p, α>? An ε move is a spontaneous move ε does not mean that the input is empty!
PDA 24 Example Again L={a n b n n>0} a,a/aa b,a/ε a,z 0 / AZ 0 q 0 q 1 q 2 q 3 b,a/ε ε, Z 0 / Z 0
PDA 25 Informally A configuravon is a generalizavon of the novon of state A configuravon shows the current state of the control device, the porvon of the input string that starts from the head the stack It is a snapshot of the PDA
PDA 26 Formally A configuravon c is <q, x, γ> q Q is the current state of the control device x I * is the unread porvon of the input string γ Γ * is the string of symbols in the stack Stack convenvon High-lej, low-right The other way around is possible, but it is important to be coherent!
PDA 27 Example c=<q 1, abbb, ABZ 0 > a,a/aa b,a ε a,z 0 / BZ 0 q 0 q 1 b,a ε q 2 b,b ε q 3 A B Z 0 a,b/ab a a a b b b b,b ε
PDA 28 TransiVons TransiVons between configuravons ( ) depend on the transivon funcvon The transivon funcvon shows how to commute from a PDA snapshot to another There are two cases The transivon funcvon is defined for an input symbol The transivon funcvon is defined for an ε move
PDA 29 TransiVons: case 1 δ(q, i, A) = <q,α> is defined c=<q,x,γ> c =<q, x, γ > γ=aβ x=iy then γ = αβ x =y
PDA 30 TransiVons: case 2 δ(q, ε, A) = <q,α> is defined c=<q,x,γ> c =<q, x, γ > γ=aβ then γ = αβ x =x
PDA 31 Important remark An ε move is a spontaneous move If δ(q,ε,a) and A is the top symbol on the stack, the transivon can always be performed If δ(q,ε,a), then i I δ(q,i,a)= If this property was not savsfied, both the transivons would be allowed Nondeterminism
PDA 32 Acceptance condivon Let * be the reflexive transivve closure of the relavon Acceptance condivon: x I * (x L c 0 =<q 0,x,Z 0 > * c F =<q,ε,γ> and q F) Informally, a string is accepted by a PDA if there is a path coherent with x on the PDA that goes from the inival state to the final state The input string has to be read completely
PDA 33 Example L = {wcw R w {a,b} + } We need to use a LIFO policy to memorize w PUSH b,a/ba b,b/bb a,a/aa a,b/ab c,b/b c,a/a a,a/ε b,b/ε ε,z0/z0 q0 q1 q2 POP a,z0/az0 b,z0/bz0
PDA 34 PDA vs FSA (1) We know that a n b n cannot be recognized by any FSA Pumping Lemma but it can be recognized by a PDA Every regular language can be recognized by a PDA Given an FSA A=<Q,I,δ,q 0,F> it is straighporward to build a PDA A =<Q,I,Γ,δ,q 0,Z 0,F > such that L(A)=L(A )
PDA 35 PDA vs FSA (2) PDAs are more expressive than FSAs a n b n Regular languages wcw R Languages recognized by PDAs
PDA 36 Loops in PDAs Unlike FSAs, PDAs may not necessarily stop ajer a finite number of moves Loops of ε-moves are possible Example: a,z 0 AZ 0 ε,a ΑΑ Yet, looping PDAs do not add expressive power to the class of PDAs We can get rid of such PDAs Let s see how q
Loop-free PDA <q,x,α> * d <q,y,β> means that <q,x,α> * <q,y,β> and, for β=zβ, δ(q,ε,z)= * d is a sequence of moves that needs a symbol to proceed A PDA is loop free if and only if x I * q γ such that <q 0,x,Z 0 > * d <q,ε,γ> it always reads the whole input string and then stops i.e., it is unable to loop indefinitely with ε-moves Every PDA can be transformed into an equivalent loop-free PDA PDA 37
PDA 38 Complement The class of languages recognized by PDAs is closed under complement The complement can be built by eliminavng loops adding error states taking care of ε-moves connecvng final and nonfinal states swapping final and non-final states
PDA 39 Loop-free PDAs Loop eliminavon is essenval, otherwise the end of the string might never be reached What if there are sequences of ε-moves traversing some final and some non-final states (and the input is envrely read)? We might want to force acceptance only at the end of a (necessarily finite) sequence of ε-moves Otherwise swapping final and non-final states to obtain the complement will not work With these precauvons, we are sure that either the PDA or its complement will accept the string
PDA 40 Union The class of languages recognized by PDAs is not closed under union There is no PDA that recognizes {a n b n n 1} {a n b 2n n 1}, but {a n b n n 1} is recognizable by PDA {a n b 2n n 1} is recognizable by PDA IntuiVve sketch of proof If the stack is empved with n b s, then, if there are other b s, n is forgoven If the stack is half empved with the b s, then, if there are no more b s, it is impossible to know whether the stack contains n symbols
PDA 41 Other operavons The class of languages recognized by PDAs is not closed under intersecvon A B=(A c B c ) c Since PDA languages are closed under complement, if they were closed under intersecvon, they should be closed under union as well The class of languages recognized by PDAs is not closed under difference A B=A-B c Since PDA languages are closed under complement, if they were closed under difference, they should be closed under intersecvon as well
PDA 42 DefiniVon A PD transducer (PDT) is a tuple <Q, I, Γ, δ, q 0, Z 0, F, O, η> Q is a finite set of states F Q is the set of final states O is the output alphabet η: Q (I {ε}) Γ O * δ(q,i,a)=<p,α> η(q,i,a)=w q i,a/α,w p
PDA 43 Remarks Q, I, Γ, δ, q 0, Z 0 and F are defined as in acceptor PDAs η is defined only where δ is defined The stack can be necessary for two reasons: The language to be recognized requires it The translavon requires it
PDA 44 ConfiguraVon A configuravon c is <q, x, γ, z> q Q is the current state of the control device x I * is the unread porvon of the input string γ Γ * is the string of symbols in the stack z is the string already wriven on the output tape
PDA 45 TransiVons c=<q, x, γ, z> c =<q, x, γ, z > Case 1: δ(q, i, A) = <q,α> is defined and η(q, i, A)=w γ=aβ x=iy then γ = αβ x =y z =zw Case 2: δ(q, ε, A) = <q,α> is defined and η(q, ε, A)=w γ=aβ x=y then γ = αβ x =y z =zw
PDA 46 Acceptance condivon x I * (x L z= τ(x) c 0 =<q 0,x,Z 0, ε> * c F =<q,ε,γ,z> and q F) NoVce that the translavon of x is defined only if the string x is accepted
PDA 47 Example L={wc w {a,b} + } and τ(wc)=w R a,z 0 / AZ 0,ε a,a/ AA,ε a,b/ AB,ε b,z 0 / BZ 0,ε b,b/ BB,ε b,a/ BA,ε ε,a/ ε, a ε,b/ ε, b c,a/ ε, a ε,z 0 / Z 0,ε q 0 c,b/ ε, b q q 2 1
PDA 48 PDAs and compilers PDAs are at the heart of compilers Stack memory has a LIFO policy LIFO is suitable to analyze nested syntacvc structures ArithmeVcal expressions Compound instrucvons AcVvaVon records
PDA 49 Example PDA to recognize well parenthesized strings (()(()())) OK )())())) NO (,P/ PP ),P/ ε (,Z 0 / PZ 0 ε,z 0 / Z q 0 0 q q 2 1 Homework: try to build a PDA that recognizes the complement of this language