CPS 220 – Theory of Computation
Total Page:16
File Type:pdf, Size:1020Kb
CPS 220 – Theory of Computation Pushdown Automata (PDA) Nondeterministic Finite Automaton with some extra memory Memory is called the stack, accessed in a very restricted way: in a First-In First-Out fashion (FIFO) FIFO That is, elements can be “pushed” in the memory. At any moment, only the last element pushed in memory can be accessed. To see elements below that, the top element has to be “popped” out of the stack and lost. Still this memory results in the PDA having much more power as a computational model, than the NFA without the memory. NFA Required for the PDA to be able to recognize the same languages as the CFGs. Figure of a PDA: State Control Input Stack . Example: A = { 0n 1n | n ≥ 0 } -not regular - not recognized by any FA PDA construction: Push 0s in the stack. Then, once 1s start to be read, pop 0s to match the 1s. If the number of 1s is exactly the same as the number of 0s pushed, accept. In all other situations, reject. Formal Definition of Pushdown Automata A Pushdown Automaton (PDA) is an 8-tuple P = (Q, Σ, Γ, δ, q0, Z0, F), where: 1. Q is a finite set of states. 2. Σ is a finite set of input symbols. 3. Γ is a finite set of symbols that can be pushed on the stack. (Usually all of Σ plus a few special symbols.) Q × Γε 4. δ : Q × Σε × Γε → 2 is the transition function. δ (state, sybmol, stack symbol)→(new state, new stack symbol to push) 5. q0 is the start state. 6. F is a set of final states (accept states). Accept condition: reads all input symbols, has an empty stack, and reaches a final state. δ: The transition function. δ ( state an input symbol (or ε) pops the top symbol of the stack(or ε) ) → new state, pushes a new symbol on the stack (ε) δ changes the stack by popping 0 or 1 symbol and then pushing 0 or 1 symbol. Note: The power of PDAs usually does not change if we changed what δ can do to the stack. For instance, we could require δ to either pop a symbol, or push a new symbol, but not both. Or, we could allow δ to pop and push a constant number of symbols. Example: L = { 0n 1n | n ≥ 0 } Here is the transition diagram of a PDA accepting this language: Notational conventions: A transition label “x, y→z” means “with input symbol x, and upon popping the top stack symbol y, push the stack symbol z”. Whenever x, y, or z are ε, no symbol is read/popped/pushed. Note the special symbol $: By pushing $ in the beginning of the computation to the empty stack, we can always query whether the stack is empty: whenever $ is on top, the stack is empty! (To “preserve” $ inside the stack and still perform that query, we could label the transition “x, $→$”.) Consider this PDA M = ({q,p},{a,b,c},{a,b},δ,q,{p}), where δ is defined as follows: δ(q,a,ε)={(q,a)}, δ(p,a,a)={(p,ε)}, δ(p,b,ε)={(q,b)}, δ(p,b,b)={(p,ε)}, δ(q,c,ε)={(p,ε)} Draw the state transistion diagram for the PDA above. What does the language do? a e/a a a/e b e/b b b/e q c e/e p L(M) = { wcwR | w ∈ {a,b}* } CFL: L = { w wR | w ∈ Σ* } A grammar for L: A → 0 A 0 | 1 A 1 | ε Example: Describe a PDA for L = { w wR | w ∈ { 0, 1 }* } Informal description: A PDA P: 1. If the input is empty, P accepts. 2. P reads the input 1 symbol at a time, and pushes that symbol in the stack. 3. At some point P nondeterministically switches to a state that pops symbols 1 at a time, and matches the popped symbols with the ones in the input. If any of the input symbols does not match a stack symbol, P rejects. 4. If the input and stack run empty at the exact same time, P accepts. Equivalence of CFGs and PDAs A language is context free if and only if some pushdown automaton recognizes it. That is to say, 1. Given a CFG G, one can build a PDA P that accepts the same language that G generates. (covered in class) 2. Given a PDA P, one can construct a CFG G that generates the same language that P accepts. (in book - optional) Proof of 1: Starting with a CFG G, we build a PDA P accepting the language A: 1. Initialization: Push the marker symbol $ and the start variable S of G on the stack. 2. Repeat: Pop symbol x from the top of the stack. a. If x is a nonterminal: Nondeterministically choose a production x→u, and push u to the stack. (That will require |u| push operations.) b. If x is a terminal: Read the next input symbol and if it is not the same as X, reject. c. If x is $ (empty stack): If there is no more input, accept. Else, reject. Basic steps in constructing a PDA from a CFG: 1. push on $ and start symbol S 2. burn off any symbols of Σ 3. push rules of G on in reverse order Justification: Starting from the nonterminals S, this PDA builds all the possible parse trees of G. If one of these parse trees derives the input, the input will be matched with the derivation, and accepted! Example: Construct a PDA to simulate grammar G. S → aS | aSbS | ε 2. Burn off symbols of 1. Push on $ and alphabet start symbol S e,$/e e,S/e b,b/e a,a/e e,e/$ e,e/a s e,e/S q e,e/S e,S/S e,S/S e,e/a e,e/b 3. Push rules of G on in reverse order --------------------------------------------------------------- Nondeterminism is the key here...to see what is generated then imagine a signal sent off by q every time a symbol a or b is processed for the accepting nondeterministic path. Read Pop Push Stack e e $ $ e e S S$ e S S S$ e e a aS$ a a e <signal a> S$ e S e $ e $ e <accept> e String: a ALL OTHER NONDETEMINISTIC PATHS WILL FAIL --------------------------------------------------------------- Read Pop Push Stack e e $ $ e e S S$ e S S S$ e e b bS$ e e S SbS$ e e a aSbS$ a a e <signal a> SbS$ e S S SbS$ e e a aSbS$ a a e <signal a> SbS$ e S e bS$ b b e <signal b> S$ e S e $ e $ e <accept> e String: aab ALL OTHER NONDETEMINISTIC PATHS WILL FAIL .