Chapter 5 Pushdown Automata 1 Copyright © 2011 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Definitions and Examples • A language can be generated by a CFG if and only if it can be accepted by a pushdown automaton • A pushdown automaton is similar to a FA but has an auxiliary memory in the form of a stack • Pushdown automata are, by default, nondeterministic. Unlike FA’s, the nondeterminism cannot always be removed Introduction to Computation 2 Definitions and Examples (cont’d.) • We’ll start with a simple example, the language AnBn = {anbn | n 0} – This is not a regular language, but it is context-free • In processing the first part of an input string that might be in AnBn, all we need to remember is the number of a’s – Saving the actual a’s is a simple way to do this – So, the PDA will start by reading a’s and pushing them onto the stack Introduction to Computation 3 Definitions and Examples (cont’d.) • As soon as the PDA reads a b, two things should happen – It enters a new state in which only b’s are legal inputs – It pops one a off the stack to cancel this b • In the new state, the correct move on the input symbol b is to pop an a off the stack to cancel it. Once enough b’s have been read to cancel the a’s on the stack, the string read so far is accepted • The stack has no limit to its size, so the PDA can handle anything in AnBn Introduction to Computation 4 Definitions and Examples (cont’d.) • A single move of a PDA will depend on the current state, the next input, and the symbol currently on top of the stack (the only one the PDA can see) • In the move, the PDA is allowed to change states and to modify the top of the stack. In many stack applications, the only legal stack moves are to push a symbol on and to pop one off; here, to make things a little simpler, we will allow the PDA to replace the top symbol X by a string of stack symbols • Special cases are pushing a symbol Y (replacing X by YX) and popping X (replacing X by ) Introduction to Computation 5 Definitions and Examples (cont’d.) • Definition 5.1: A pushdown automaton is a 7-tuple M = (Q, , , q0, Z0, A, ), where: – – – – – – Q is a finite set of states The input and stack alphabets and are finite sets q0 Q is the initial state Z0 is the initial stack symbol A Q is the set of accepting states The transition function is : Q ( {}) the set of finite subsets of Q * • Because values of are sets, M may be nondeterministic • A move requires that there be at least one symbol on the stack. Z0 is the one on the stack initially Introduction to Computation 6 Definitions and Examples (cont’d.) • A configuration of a PDA is a triple (q, x, ) – q Q is the current state – x * is the portion of the input string that has not yet been read – The contents of the stack is * (the convention will be that the top corresponds to the leftmost symbol) • We write (p, x, ) ⊢M (q, y, ) to mean that one of the possible moves in the first configuration takes M to the second – ⊢Mn and ⊢M* refer to n moves and zero or more moves Introduction to Computation 7 Definitions and Examples (cont’d.) • Definition 5.2: If M = (Q, , , q0, Z0, A, ) and x *, the string x is accepted by M if (q0, x, Z0) ⊢M* (q, , ) for some * and some q A – A language L is said to be accepted by M if L is precisely the set of strings accepted by M – Sometimes a string accepted by M is said to be accepted by final state, because acceptance does not depend on the final stack contents at all Introduction to Computation 8 Definitions and Examples (cont’d.) • A PDA for AnBn is M = (Q, , , q0, Z0, A, ) where Q = {q0, q1, q2, q3}, A = {q0, q3}, and the transitions are shown in this table: Move # State Input Stack top Move(s) 1 q0 a Z0 (q1, aZ0) 2 q1 a a (q1, aa) 3 q1 b a (q2, ) 4 q2 b a (q2, ) 5 q2 Z0 (q3, Z0) all other combinations Introduction to Computation none 9 Definitions and Examples (cont’d.) • The moves that M makes as it processes the string aabb are shown below: (q0, aabb, Z0) ⊢ (q1, abb, aZ0) ⊢ (q1, bb, aaZ0) ⊢ (q2, b, aZ0) ⊢ (q2, , Z0) ⊢ (q3, , Z0) • M is deterministic, because it never has a choice of moves 10 Deterministic Pushdown Automata • Definition 5.10: A pushdown automaton M = (Q, , , q0, Z0, A, ) is deterministic if it satisfies both of the following conditions: – For every q Q, every in {}, and every X , the set (q, , X) has at most one element – For every q Q, every , and every X , the two sets (q, , X) and (q, , X) cannot both be nonempty • A language L is a deterministic context-free language (DCFL) if there is a deterministic PDA (DPDA) accepting L Introduction to Computation 11 Deterministic Pushdown Automata (cont’d.) • One example is the previous PDA accepting AnBn • Another example: the language of balanced strings of brackets – Two states q0 and q1, where q0 is the accepting state – Input symbols are ‘[‘ and ‘]’ – Stack symbols are the input symbols plus Z0 Introduction to Computation 12 Deterministic Pushdown Automata (cont’d.) • The transition table for a PDA accepting the language of balanced strings of brackets Move # State Input Stack top Move 1 q0 [ Z0 (q1, [Z0) 2 q1 [ [ (q1, [[) 3 q1 ] [ (q1, ) 4 q1 Z0 (q0, Z0) (all other combinations) Introduction to Computation none 13 Deterministic Pushdown Automata (cont’d.) • The language Pal of palindromes over {a,b} can be accepted by a PDA M that saves symbols on the stack until it “guesses” that it has reached the middle of the string, then cancels stack symbols with input symbols. Guessing implies nondeterminism • The initial state of M, in which it stays while it is processing the first half of the string, is q0 • The state it enters when it is ready to begin the second half is q1, and the accepting state is q2 Introduction to Computation 14 Deterministic Pushdown Automata (cont’d.) • Two typical lines from the transition table are – (q0, a, Z0) = {(q0, aZ0), (q1, Z0)} – (q0, a, b) = {(q0, ab), (q1, b)} • In both lines, the first move is the right one if the input symbol a is still in the first half of the string. • The second move is the right one if the a is the middle symbol in an (odd-length) palindrome • M decides which move to make by guessing. The other guess it can make is that it has reached the middle of an even-length palindrome. In this case, M enters q1 by a -transition; for example, (q0, , b) = {(q1, b)} Introduction to Computation 15 Deterministic Pushdown Automata (cont’d.) • Here are the moves by which the palindromes aba and aa are accepted: (q0, aba, Z0) ⊢ (q0, ba, aZ0) ⊢ (q1, a, aZ0) ⊢ (q1, , Z0) ⊢ (q2, , Z0) (q0, aa, Z0) ⊢ (q0, a, aZ0) ⊢ (q1, a, aZ0) ⊢ (q1, , Z0) ⊢ (q2, , Z0) • The only nondeterminism is in the transition from q0 to q1; once M makes the transition, only two strings can be accepted: the even-length string whose second half is the reverse of the string read so far, and the odd-length string whose second half is the reverse of the string read before the previous symbol Introduction to Computation 16 Deterministic Pushdown Automata (cont’d.) • Theorem 5.16: The language Pal cannot be accepted by a DPDA (i.e., cannot be accepted without guessing) • Proof sketch: suppose, for the sake of contradiction, that M is a DPDA that accepts Pal – We can modify M if necessary so that every move is either of the form (p, , X) = {(q, )} or (p, , X) = {(q, X)} – Observe that M must read every string in its entirety, because every string x is a prefix of the palindrome xxr Introduction to Computation 17 Deterministic Pushdown Automata (cont’d.) • The idea of the proof is to find two different strings r and s such that for every suffix z, M treats both rz and sz the same way – This will produce a contradiction, because for every r and s with r ≠ s, r and s are distinguishable w.r.t. Pal • Definition: For every string x, there is a string yx such that of all strings xy, xyx is one resulting in a configuration with minimum stack height – This means that if x represents the stack contents when xyx has been processed, no symbols of x are ever removed in further processing Introduction to Computation 18 Deterministic Pushdown Automata (cont’d.) • Now choose the strings r and s – There are infinitely many different strings xyx – Therefore, because there are only a finite number of combinations of state and stack symbol, there must be two different strings r = uyu and s = vyv so that the configurations resulting from M’s processing r and s have both the same state and the same top stack symbol – M won’t be able to tell the difference between these – Therefore, for every z, the results of processing rz and sz must be the same Introduction to Computation 19 A PDA from a Given CFG • We’ll examine two ways of constructing a PDA from an arbitrary CFG – In both cases the PDA will be nondeterministic • In both approaches, the PDA attempts to simulate a derivation in the grammar, using the stack to hold portions of the current string – The PDA terminates the computation if it finds that the derivation-in-progress is not consistent with the input string – It attempts to construct a derivation tree from the input string Introduction to Computation 20 A PDA from a Given CFG (cont’d.) • The two approaches are top-down and bottom-up, which refer to the way the tree is constructed – We’ll describe for each one the PDA obtained from the grammar and indicate why it accepts the language • The top-down PDA: – Begins by placing the start symbol of the grammar on the stack – From this point, each step in the construction of the derivation tree consists of replacing a variable A that is currently on top of the stack by the right side of a grammar production A Introduction to Computation 21 A PDA from a Given CFG (cont’d.) • The top-down PDA: (cont’d.) – This step corresponds to building the portion of the tree containing that variable-node and its children – The intermediate moves of the PDA are to remove terminal symbols from the stack as they are produced and match them with input symbols – To the extent that they continue to match, the derivation being simulated is consistent with the input string – Because the variable replaced in each step is the leftmost one, we are simulating a leftmost derivation Introduction to Computation 22 A PDA from a Given CFG (cont’d.) • Definition 5.17: Let G = (V, , S, P) be a CFG – The nondeterministic top-down PDA corresponding to G is NT(G)=(Q, , , q0, Z0, A, ), defined as follows: • Q = {q0, q1, q2}, A = {q2}, = V ∪ ∪ {Z0} • The initial move is (q0, , Z0) = {(q1, SZ0)} • The only move to the accepting state is (q1, , Z0) = {(q2, Z0)} • The moves from q1 are the following: – For every A V, (q1, , A) = {(q1, ) | A P} – For every , (q1, , ) = {(q1, )} Introduction to Computation 23 A PDA from a Given CFG (cont’d.) • After the initial move, before the final move, the PDA remains in state q1 – The only moves are to replace a variable with the right side of a production and to match a terminal symbol on the stack with an input symbol and discard both • Theorem 5.18: If G is a CFG, then the nondeterministic top-down PDA NT(G) accepts the language L(G) – Proof: see book Introduction to Computation 24 A PDA from a Given CFG (cont’d.) • The bottom-up PDA: – The tree is constructed from the bottom up – The PDA simulates the reverse of a derivation starting with the last move and ending with the first – Another thing that’s reversed is the order in which we read the symbols on the stack when doing a “reduction”: the reduction corresponding to A is performed when the string r appears on the top of the stack; it is replaced by A. – The simulated derivation is a rightmost derivation Introduction to Computation 25 A PDA from a Given CFG (cont’d.) • Definition 5.22: Let G=(V, , S, P) be a CFG – The nondeterministic bottom-up PDA corresponding to G is NB(G)=(Q, , , q0, Z0, A, ), defined as follows: • For every and every X , (q0, , X)={(q0, X)}, the shift moves • For every production B , and every nonnull string *, (q0, , r ) ⊢* (q0, B ), where this reduction is a sequence of one or more moves in which, if there is more than one, the intermediate configurations involve other states that are specific to this sequence and appear in no other moves of NB(G) • (q1, ) (q0, , S) and (q1, , Z0)={(q2, Z0)} Introduction to Computation 26 A PDA from a Given CFG (cont’d.) • Theorem 5.23: If G is a CFG, then the nondeterministic bottom-up PDA NB(G) accepts L(G) – Proof: see book Introduction to Computation 27 A CFG from a Given PDA • Definition 5.27: If M is a PDA with input alphabet , initial state q1, and initial stack symbol Z1, then M accepts a language L by empty stack if L = Le(M), where Le(M) = {x * | (q1, x, Z1) ⊢ * (q, , )} • Theorem 5.28: If M = (Q, , , q0, Z0, A, ) is a PDA, then there is another PDA M1 such that Le(M1) = L(M) – Proof: see book Introduction to Computation 28 A CFG from a Given PDA (cont’d.) • Theorem 5.29: If M=(Q, , , q0, Z0, A, ) is a PDA accepting L by empty stack, then there is a CFG G such that L = L(G) Introduction to Computation 29 A CFG from a Given PDA (cont’d.) • Proof: define G=(V, , S, P) as follows: – V contains S and all possible variables of the form [p, A, q], where A and p, q Q. P contains: • For every q Q, the production S [q0, Z0, q] • For every q, q1 Q, every {}, and every A , if (q, , A) contains (q1, ) then [q, A, q1] is in P • For every q, q1 Q, every {}, every A , and every m 1, if (q, , A) contains (q1, B1B2…Bm) for some B1, B2, … , Bm in , then for every choice of q2, q3, .. , qm+1 Q, the production [q, A, qm+1] [q1, B1, q2][q2, B2, 3]…[qm, Bm, qm+1] is in P – See book for details Introduction to Computation 30 Parsing • To parse a string relative to a CFG G means to determine an appropriate derivation for the string, or to determine that there is none • Parsing a sentence is necessary to understand it • We can sometimes eliminate the nondeterminism from our top-down or bottom-up PDAs, yielding a rudimentary parser • This isn’t always possible, and even if it is, the process is not always easy Introduction to Computation 31 Parsing (cont’d.) • Consider the CFG with rules S [S] | SS | – The way we have formulated the top-down PDA involves inherent nondeterminism – Each time a variable appears on top of the stack, the machine replaces it by the right side of a production – These moves depend only on the variable; they are -transitions and are chosen nondeterministically – The natural approach to eliminating the nondeterminism is to consider whether consulting the next input symbol would allow the PDA to choose correctly Introduction to Computation 32 Parsing (cont’d.) • The first few moves made for the input string [[][]] are enough to show that this is not enough: 1. 2. 3. 4. 5. (q0, [[][]], Z0) ⊢ (q1, [[][]], SZ0) ⊢ (q1, [[][]], [S]Z0) ⊢ (q1, [][]], S]Z0) ⊢ (q1, [][]], SS]Z0) S [S] [SS] • In lines 2 and 4, S is on top of the stack and the next symbol is a left parenthesis, but S is replaced by different strings in these two places Introduction to Computation 33 Parsing (cont’d.) • We might have predicted this; not only is the grammar ambiguous, but there are more productions than there are terminal symbols for the right sides to start with. • Let’s try a different grammar for the language: S [S]S | – This is unambiguous and generates the same language – In the non-deterministic top-down PDA NT(G), with the input string [], there is still a problem, but it is not as serious Introduction to Computation 34 Parsing (cont’d.) • The derivation goes as follows: 1. (q0, [], Z0) 2. ⊢ (q1, [], SZ0) 3. ⊢ (q1, [], [S]SZ0) 4. ⊢ (q1, ], S]SZ0) 5. ⊢ (q1, ], ]SZ0) 6. ⊢ (q1, , SZ0) 7. ⊢ (q1, , Z0) S [S]S []S [] • During these moves, there are three times when S is the top stack symbol Introduction to Computation 35 Parsing (cont’d.) • The first two are fine; if the input symbol is [, then replace S by [S]S, and if it is ], then replace S by • Unfortunately, S is also replaced by at the end of the computation – The PDA must make a -transition in this case, and if it can choose to do this, then it’s not deterministic • A similar problem arises for many CFGs and there is a common solution – We introduce an end-marker $ into the alphabet, use it only to mark the end of the input, and change the language (and the CFG) slightly Introduction to Computation 36 Parsing (cont’d.) • Our new CFG G has the productions S S1$ and S1 [S1]S1 | • The corresponding transition table for NT(G) is: Move # State Input Stack top Move(s) 1 q0 Z0 (q1, SZ0) 2 q1 S (q1, S1$) 3 q1 S1 2 choices 4 q1 [ [ (q1, ) 5 q1 ] ] (q1, ) 6 q1 $ $ (q1, ) 7 q1 Z0 (q2, ) Introduction to Computation 37 Parsing (cont’d.) • The only non-determinism is in line 3, which allows both the moves (q1, [S1]S1) and (q1, ) • We can fix this by replacing line 3 with these 6 lines: State Input Stack top Move q1 [ S1 (q1,[, [S1]S1) q1,[ [ (q1, ) q1 ] S1 (q1,], ) q1,] ] (q1, ) q1 $ S1 (q1,$, ) q1,$ $ (q1, ) Introduction to Computation 38 Parsing (cont’d.) • The resulting PDA is clearly deterministic • To check that it is equivalent to the original it suffices to verify that our assumptions about the correct -transition made by NT(G1) in each case where S1 is on the stack are correct • See book for details • In some simple grammars, we can do something similar to eliminate nondeterminism from the bottom-up PDA NB(G) Introduction to Computation 39