pushdown automata
play

Pushdown Automata 7-0 Pushdown Automata The automata we - PDF document

Griffith University 3130CIT Theory of Computation (Based on slides by Harald Sndergaard of The University of Melbourne) Pushdown Automata 7-0 Pushdown Automata The automata we saw so far were limited by their


  1. ✬ ✩ Griffith University 3130CIT Theory of Computation (Based on slides by Harald Søndergaard of The University of Melbourne) Pushdown Automata ✫ ✪ 7-0

  2. ✬ ✩ Pushdown Automata The automata we saw so far were limited by their lack of memory . A pushdown automaton (PDA) is a finite-state automaton, equipped with a stack . state control y y x b a a x input stack Language { a i b i | i ≥ 0 } is not recognised by any DFA as it requires the ability of a recogniser to remember how many a ’s were in the input. ✫ ✪ 7-1

  3. ✬ ✩ Fine but Important Points We shall consider the non-deterministic version of a PDA. It may, in one transition step, read a symbol from input and pop the top stack symbol. Based on these, and the current state, it will decide on which state to go to next, as well as what sequence of symbols to push on the stack. It may choose to leave out the popping, or pushing, or both. It may also ignore the input. ✫ ✪ 7-2

  4. ✬ ✩ Pushdown Automata Formally A pushdown automaton is a 6-tuple ( Q, Σ , Γ , δ, q 0 , F ) where • Q is a finite set of states , • Σ is the finite input alphabet , • Γ is the finite stack alphabet , • δ : Q × Σ ǫ × Γ ǫ → P ( Q × Γ ∗ ) is the transition function , • q 0 ∈ Q is the start state , and • F ⊆ Q are the accept states . ✫ ✪ 7-3

  5. � � � � ✬ ✩ PDA Example 1 This PDA recognises { 0 n 1 n | n ≥ 0 } : � ���� ���� ���� ���� � ���� ���� ǫ, ǫ → $ q 0 q 1 0 , ǫ → 0 1 , 0 → ǫ ���� ���� ���� ���� ���� ���� q 3 q 2 1 , 0 → ǫ ǫ, $ → ǫ ✫ ✪ 7-4

  6. ✬ ✩ Acceptance Precisely The PDA ( Q, Σ , Γ , δ, q 0 , F ) accepts input w iff w = w 1 w 2 · · · w n with each w i ∈ Σ ǫ , and there are states r 0 , r 1 , . . . , r n ∈ Q and strings s 0 , s 1 , . . . , s n ∈ Γ ∗ such that 1. r 0 = q 0 and s 0 = ǫ . 2. ( r i +1 , b ) ∈ δ ( r i , w i +1 , a ), s i = at , s i +1 = b 1 . . . b k t , with a ∈ Γ ǫ , b 1 . . . b k ∈ Γ ∗ and t ∈ Γ ∗ . 3. r n ∈ F . Note 1: There is no requirement that s n = ǫ , so the stack may be non-empty when the machine stops (even when it accepts). Note 2: Trying to pop an empty stack leads to rejection of input, rather than “runtime error”. ✫ ✪ 7-5

  7. � � � � ✬ ✩ PDA Example 2 This PDA recognises { ww R | w ∈ { 0 , 1 } ∗ } : � ���� ���� ���� ���� ǫ, ǫ → $ � ���� ���� 0 , ǫ → 0 q 0 q 1 1 , ǫ → 1 ǫ, ǫ → ǫ ���� ���� ���� ���� ���� ���� 0 , 0 → ǫ q 3 q 2 1 , 1 → ǫ ǫ, $ → ǫ ✫ ✪ 7-6

  8. ✬ ✩ CFLs Have PDAs Lemma: Some PDA recognises CFL L . Proof: Given a CFG G , we show how to construct a PDA P such that L ( P ) = L ( G ). The idea is to let the PDA use its stack to store a list of “pending” recogniser tasks. For example, if S → xAy is a rule in G , and the PDA finds the symbol S on top of its stack, it may pop S and push y , A , and x , in that order. state state control control x A S y x y y x y y input stack input stack If it finds the terminal x on top of the stack, and x is the next input symbol, it may consume the ✫ input and pop x . ✪ 7-7

  9. � � ✬ ✩ CFLs Have PDAs (cont.) Construct the PDA with an initial state, an intermediate state q , and a final state. Add a self-loop from q for each terminal a . a,a → ǫ � ���� ���� ǫ,ǫ → S $ � ���� ���� ���� ���� ���� ���� ǫ, $ → ǫ � q Also add, for each rule A → w 1 . . . w n , another self-loop from q . ǫ,S → w 1 ...w n ���� ���� q ✫ ✪ 7-8

  10. � � � � ✬ ✩ Example Recogniser For the grammar S → a S b | b | ǫ we get ���� ���� ǫ, ǫ → S $ a, a → ǫ b, b → ǫ ���� ���� ǫ, S → aSb q ǫ, S → b ǫ, S → ǫ ǫ, $ → ǫ ���� ���� ���� ���� ✫ ✪ 7-9

  11. ✬ ✩ PDAs Recognise CFLs Lemma: If some PDA recognises L then L is context-free. Proof sketch: We show how to construct a CFG G which “simulates” the given PDA P . The variables will be A pq where p and q are states in P . The idea is that A pq will generate a string w iff w takes P from state p to state q leaving the stack unchanged. Without loss of generality we can assume that P has only one accept state, q f , that P empties its stack, and that each transition either pops or pushes a symbol. The start variable will be A q 0 q f where q 0 is P ’s start state. ✫ ✪ 7-10

  12. � � � � � � � ✬ ✩ PDAs Recognise CFLs (cont.) ���� � ���� ���� ���� ǫ, ǫ → $ 0 , ǫ → 0 1 2 ǫ, ǫ → $ ���� ���� 1 , 0 → ǫ 5 ǫ, $ → ǫ ���� ���� ���� ���� ���� ���� 1 , 0 → ǫ 4 3 ǫ, $ → ǫ Identify all pairs of transitions where the same symbol is pushed then popped. For $: A 14 → ǫ A 23 ǫ | ǫ A 55 ǫ | ǫ A 25 ǫ | ǫ A 53 ǫ . For 0: A 23 → 0 A 22 1 | 0 A 23 1. Add the five rules A ii → ǫ . Add the 125 rules A ik → A ij A jk . However, most of these variables will be ✫ ✪ unreachable. 7-11

  13. � � � � � � � ✬ ✩ PDAs Recognise CFLs (cont.) Cleaning up, the PDA ���� � ���� ���� ���� ǫ, ǫ → $ 0 , ǫ → 0 1 2 ǫ, ǫ → $ ���� ���� 1 , 0 → ǫ 5 ǫ, $ → ǫ ���� ���� ���� ���� ���� ���� 1 , 0 → ǫ 4 3 ǫ, $ → ǫ is simulated by the CFG A 14 → A 23 | ǫ A 23 → 0 1 | 0 A 23 1 ✫ ✪ 7-12

  14. ✬ ✩ PDAs Recognise CFLs (cont.) The construction precisely: Let P = ( Q, Σ , Γ , δ, q 0 , { q f } ). The variables of G are { A pq | p, q ∈ Q } and the start variable is A q 0 q f . • Add rule A pq → a A rs b whenever δ ( p, a, ǫ ) contains ( r, t ) and δ ( s, b, t ) contains ( q, ǫ ). • Add rule A pq → A pr A rq for all p, q, r ∈ Q . • Add rule A pp → ǫ for all p ∈ Q . We then have: A pq generates x iff x can bring P from p to q with unchanged stack. The detailed proof is by induction on the length ∗ of the derivation A pq ⇒ x . ✫ ✪ 7-13

  15. ✬ ✩ PDAs Recognise Exactly the CFLs We have established: Theorem: L is context-free iff some PDA recognises L . Since an NFA is also a PDA (which ignores its stack), we have: Corollary: Every regular language is context-free. ✫ ✪ 7-14

Recommend


More recommend