1 Pushdown Automata (PDA) Informally: –A PDA is an NFA-µ with a stack....

download 1 Pushdown Automata (PDA) Informally: –A PDA is an NFA-µ with a stack. –Transitions are modified to accommodate stack operations. Questions: –What is a

of 39

  • date post

    14-Dec-2015
  • Category

    Documents

  • view

    216
  • download

    3

Embed Size (px)

Transcript of 1 Pushdown Automata (PDA) Informally: –A PDA is an NFA-µ with a stack....

  • Slide 1

1 Pushdown Automata (PDA) Informally: A PDA is an NFA- with a stack. Transitions are modified to accommodate stack operations. Questions: What is a stack? How does a stack help? A DFA can remember only a finite amount of information, whereas a PDA can remember an infinite amount of (certain types of) information, in one memory-stack Slide 2 2 Example: {0 n 1 n | 0= 4 Formal Definition of a PDA A pushdown automaton (PDA) is a seven-tuple: M = (Q, , , , q 0, z 0, F) QA finite set of states A finite input alphabet A finite stack alphabet q 0 The initial/starting state, q 0 is in Q z 0 A starting stack symbol, is in // need not always remain at the bottom of stack FA set of final/accepting states, which is a subset of Q A transition function, where : Q x ( U {}) x > finite subsets of Q x * Slide 5 5 Consider the various parts of : Q x ( U {}) x > finite subsets of Q x * Q on the LHS means that at each step in a computation, a PDA must consider its current state. on the LHS means that at each step in a computation, a PDA must consider the symbol on top of its stack. U { } on the LHS means that at each step in a computation, a PDA may or may not consider the current input symbol, i.e., it may have epsilon transitions. Finite subsets on the RHS means that at each step in a computation, a PDA may have several options. Q on the RHS means that each option specifies a new state. * on the RHS means that each option specifies zero or more stack symbols that will replace the top stack symbol, but in a specific sequence. Slide 6 6 Two types of PDA transitions: (q, a, z) = {(p 1, 1 ), (p 2, 2 ),, (p m, m )} Current state is q Current input symbol is a Symbol currently on top of the stack z Move to state p i from q Replace z with i on the stack (leftmost symbol on top) Move the input head to the next input symbol : q p1p1 p2p2 pmpm a/z/ 1 a/z/ 2 a/z/ m Slide 7 7 Two types of PDA transitions: (q, , z) = {(p 1, 1 ), (p 2, 2 ),, (p m, m )} Current state is q Current input symbol is not considered Symbol currently on top of the stack z Move to state p i from q Replace z with i on the stack (leftmost symbol on top) No input symbol is read : q p1p1 p2p2 pmpm /z/ 1 /z/ 2 /z/ m Slide 8 8 Example: 0 n 1 n M = ({q 1 }, {0, 1}, {L, #}, , q 1, #, ) : (1)(q 1, 0, #) = {(q 1, L#)} // stack order: L-on top-then- # lower (2)(q 1, 1, #) = // illegal, string rejected (3)(q 1, 0, L) = {(q 1, LL)} (4) (q 1, 1, L) = {(q 2, )}// change state (5)(q 2, , #) = {(q 2, )}//if read & stack hits bottom, accept (6)(q 2, , L) = // illegal, string rejected (7)(q 1, , #) = {(q 2, )}// n=0, accept // What does it mean? When will it happen? Goal: (acceptance) Terminate in a non-null state Read the entire input string Terminate with an empty stack Informally, a string is accepted if there exists a computation that uses up all the input and leaves the stack empty. How many rules should be in delta? Slide 9 9 Example: balanced parentheses, e.g. in-language: ((())()), not-in-language: ((()) M = ({q 1 }, {(, )}, {L, #}, , q 1, #, ) : (1)(q 1, (, #) = {(q 1, L#)} // stack order: L-on top-then- # lower (2)(q 1, ), #) = // illegal, string rejected (3)(q 1, (, L) = {(q 1, LL)} (4) (q 1, ), L) = {(q 1, )} (5)(q 1, , #) = {(q 1, )}//if read & stack hits bottom, accept (6)(q 1, , L) = // illegal, string rejected // What does it mean? When will it happen? Goal: (acceptance) Terminate in a non-null state Read the entire input string Terminate with an empty stack Informally, a string is accepted if there exists a computation that uses up all the input and leaves the stack empty. How many rules should be in delta? Slide 10 10 Transition Diagram: Example Computation: Current InputStackTransition (())#-- initial status ())L#(1)- Could have applied rule (5), but ))LL#(3) it would have done no good )L#(4) #(4) -(5) q0q0 (, # | L# , # | (, L | LL ), L | Slide 11 How about language like: ((())())() 11 M = ({q 1,q 2 }, {(, )}, {L, #}, , q 1, #, ) : (1)(q 1, (, #) = {(q 1, L#)} (2)(q 1, ), #) = // illegal, string rejected (3)(q 1, (, L) = {(q 1, LL)} (4) (q 1, ), L) = {(q 2, )} (5)(q 2, ), L) = {(q 2, )} (6)(q 2, (, L) = {(q 1, LL)} (7)(q 2, (, #) = {(q 1, L#)} // start fresh again (8)(q 2, , #) = {(q 2, )}// end of string & stack hits bottom, accept (9)(q 1, , #) = {(q 1, )}// special rule for empty string (10)(q 1, , L) = // illegal, end of string but more L in stack Total number of transitions? Verify all carefully. Slide 12 12 Example PDA #1: For the language {x | x = wcw r and w in {0,1}*, but sigma={0,1,c}} Is this a regular language? Note: length |x| is odd M = ({q 1, q 2 }, {0, 1, c}, {#, B, G}, , q 1, #, ) : (1)(q 1, 0, #) = {(q 1, B#)}(9)(q 1, 1, #) = {(q 1, G#)} (2)(q 1, 0, B) = {(q 1, BB)}(10)(q 1, 1, B) = {(q 1, GB)} (3)(q 1, 0, G) = {(q 1, BG)} (11)(q 1, 1, G) = {(q 1, GG)} (4)(q 1, c, #) = {(q 2, #)} (5)(q 1, c, B) = {(q 2, B)} (6)(q 1, c, G) = {(q 2, G)} (7)(q 2, 0, B) = {(q 2, )} (12)(q 2, 1, G) = {(q 2, )} (8)(q 2, , #) = {(q 2, )} Notes: Stack grows leftwards Only rule #8 is non-deterministic. Rule #8 is used to pop the final stack symbol off at the end of a computation. Slide 24 24 Lemma 1: Let L = L E (M 1 ) for some PDA M 1. Then there exits a PDA M 2 such that L = L F (M 2 ). Lemma 2: Let L = L F (M 1 ) for some PDA M 1. Then there exits a PDA M 2 such that L = L E (M 2 ). Theorem: Let L be a language. Then there exits a PDA M 1 such that L = L F (M 1 ) if and only if there exists a PDA M 2 such that L = L E (M 2 ). Corollary: The PDAs that accept by empty stack and the PDAs that accept by final state define the same class of languages. Note: Similar lemmas and theorems could be stated for PDAs that accept by both final state and empty stack. Slide 25 Back to CFG again: PDA equivalent to CFG 25 Slide 26 26 Definition: Let G = (V, T, P, S) be a CFL. If every production in P is of the form A > a Where A is in V, a is in T, and is in V*, then G is said to be in Greibach Normal Form (GNF). Example: S > aAB | bB A > aA | a B > bB | cLanguage: (aa + +b)b + c Theorem: Let L be a CFL. Then L {} is a CFL. Theorem: Let L be a CFL not containing {}. Then there exists a GNF grammar G such that L = L(G). Slide 27 27 Lemma 1: Let L be a CFL. Then there exists a PDA M such that L = L E (M). Proof: Assume without loss of generality that is not in L. The construction can be modified to include later. Let G = (V, T, P, S) be a CFG, and assume without loss of generality that G is in GNF. Construct M = (Q, , , , q, z, ) where: Q = {q} = T = V z = S : for all a in and A in , (q, a, A) contains (q, ) if A > a is in P or rather: (q, a, A) = {(q, ) | A > a is in P and is in *}, for all a in and A in WRONG: Note stack grows rightwards here with For a given string x in *, M will attempt to simulate a leftmost derivation of x with G. Slide 28 28 Example #1: Consider the following CFG in GNF. S > aSG is in GNF S > aL(G) = a + Construct M as: Q = {q} = T = {a} = V = {S} z = S (q, a, S) = {(q, S), (q, )} (q, , S) = Is complete? Slide 29 29 Example #2: Consider the following CFG in GNF. (1)S > aA (2)S > aB (3)A > aA G is in GNF (4)A > aBL(G) = a + b + (5)B > bB (6)B > b[Can you write a simpler CFG?] Construct M as: Q = {q} = T = {a, b} = V = {S, A, B} z = S (1)(q, a, S) = {(q, A), (q, B)}From productions #1 and 2, S->aA, S->aB (2)(q, a, A) = {(q, A), (q, B)}From productions #3 and 4, A->aA, A->aB (3)(q, a, B) = (4)(q, b, S) = (5)(q, b, A) = (6)(q, b, B) = {(q, B), (q, )}From productions #5 and 6, B->bB, B->b (7)(q, , S) = (8)(q, , A) = (9)(q, , B) = Is complete? Slide 30 30 For a string w in L(G) the PDA M will simulate a leftmost derivation of w. If w is in L(G) then (q, w, z 0 ) |* (q, , ) If (q, w, z 0 ) |* (q, , ) then w is in L(G) Consider generating a string using G. Since G is in GNF, each sentential form in a leftmost derivation has form: => t 1 t 2 t i A 1 A 2 A m terminalsnon-terminals And each step in the derivation (i.e., each application of a production) adds a terminal and some non- terminals. A 1 > t i+1 => t 1 t 2 t i t i+1 A 1 A 2 A m Each transition of the PDA simulates one derivation step. Thus, the i th step of the PDAs computation corresponds to the i th step in a corresponding leftmost derivation. After the i th step of the computation of the PDA, t 1 t 2 t i+1 are the symbols that have already been read by the PDA and A 1 A 2 A m are the stack contents. Slide 31 31 For each leftmost derivation of a string generated by the grammar, there is an equivalent accepting computation of that string by the PDA. Each sentential form in the leftmost derivation corresponds to an instantaneous description in the PDAs corresponding computation. For example, the PDA instantaneous description corresponding to the sentential form: => t 1 t 2 t i A 1 A 2 A m would be: (q, t i+1 t i+2 t n, A 1 A 2 A m ) Slide 32 32 Example: Using the grammar from example #2: S=> aA(1) => aaA(3) => aaaA(3) => aaaaB(4) => aaaabB(5) => aaaabb(6) The corresponding computation of the PDA: (rule#)/right-side# (q, aaaabb, S) | (q, aaabb, A)(1)/1 | (q, aabb, A)(2)/1 | (q, abb, A)(2)/1 | (q, bb, B)(2)/2 | (q, b, B)(6)/1 | (q, , )(6)/2 String is read Stack is emptied Therefore the string is accepted by the PDA Slide 33 33 Another Example: Using the PDA from example #2: (q, aabb, S) | (q, abb, A)(1)/1 | (q, bb, B)(2)/2 | (q, b, B)(6)/1 | (q, , )(6)/2 The corresponding derivation using the grammar: S=> aA(1) => aaB(4) => aabB(5) => aabb(6) Slide 34 34 Example #3: Consider the following CFG in GNF. (1)S > aABC (2)A > a G is in GNF (3)B > b (4)C > cAB (5)C > cCLanguage? Construct M as: Q = {q} = T = {a, b, c} = V = {S, A, B, C} z = S (1)(q, a, S) = {(q, ABC)}S->aABC(9)(q, c, S) = (2)(q, a, A) = {(q, )}A->a(10)(q, c, A) = (3)(q, a, B) = (11)(q, c, B) = (4)(q, a, C) = (12)(q,