Pushdown Automata (Pre Lecture) Dr. Neil T. Dantam CSCI-561, Colorado School of Mines Fall 2017 Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 1 / 41
Outline Pushdown Automata Pushdown Automata and Context-Free Grammar Equivalence Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 2 / 41
Pushdown Automata Outline Pushdown Automata Pushdown Automata and Context-Free Grammar Equivalence Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 3 / 41
Pushdown Automata Conceptual Finite Automaton Operation M : Σ {accept, reject} input tape σ = σ [0] σ [1]... σ [k 1] }{{} history σ [k] σ [k+1] }... {{ σ [n] } future Finite Control Q {accept, reject} Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 4 / 41
Pushdown Automata Conceptual Pushdown Automaton Operation input tape σ = σ [0] σ [1]... σ [k 1] }{{} history σ [k] σ [k+1] }... {{ σ [n] } future stack γ [m] γ [m 1]. γ [0] $ Finite Control Q {accept, reject} Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 5 / 41
Pushdown Automata Pushdown Automaton Definition (Pushdown Automaton) A pushdown automaton is a 6-tuple M = (Q, Σ, Γ, δ, q 0, F ), where: Q is the finite set of states Σ is the input alphabet Γ is the stack alphabet δ : Q Σ Γ P (Q Γ) is transition function q 0 is the start state F Q is the set of accept states Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 6 / 41
Pushdown Automata PDA State Diagrams Transitions Stack Symbols q i input {}}{ σ, pop {}}{ γ k push {}}{ γ l q j ɛ: push/pop nothing $: marks bottom of stack γ Γ: any other symbol Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 7 / 41
Pushdown Automata Example: PDA for 0 n 1 n start 0, ɛ m 1, m ɛ ɛ, ɛ $ 1, m ɛ ɛ, $ ɛ q 0 q 1 q 2 q 3 Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 8 / 41
Pushdown Automata Example: Recognize 0011 start 0, ɛ m 1, m ɛ ɛ, ɛ $ 1, m ɛ ɛ, $ ɛ 0 0 1 1 q 0 q 1 q 2 q 3 stack start 0, ɛ m 1, m ɛ ɛ, ɛ $ 1, m ɛ ɛ, $ ɛ 0 0 1 1 q 0 q 1 q 2 q 3 $ stack Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 9 / 41
Pushdown Automata Example: Recognize 0011 continued (0) start 0, ɛ m 1, m ɛ ɛ, ɛ $ 1, m ɛ ɛ, $ ɛ m 0 0 1 1 q 0 q 1 q 2 q 3 $ stack start 0, ɛ m 1, m ɛ m ɛ, ɛ $ 1, m ɛ ɛ, $ ɛ m 0 0 1 1 q 0 q 1 q 2 q 3 $ stack Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 10 / 41
Pushdown Automata Example: Recognize 0011 continued (1) start 0, ɛ m 1, m ɛ ɛ, ɛ $ 1, m ɛ ɛ, $ ɛ m 0 0 1 1 q 0 q 1 q 2 q 3 $ stack start 0, ɛ m 1, m ɛ ɛ, ɛ $ 1, m ɛ ɛ, $ ɛ 0 0 1 1 q 0 q 1 q 2 q 3 $ stack Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 11 / 41
Pushdown Automata Example: Recognize 0011 continued (2) start 0, ɛ m 1, m ɛ ɛ, ɛ $ 1, m ɛ ɛ, $ ɛ 0 0 1 1 q 0 q 1 q 2 q 3 stack Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 12 / 41
Pushdown Automata Example: PDA for ωω R { ωω R ω {0, 1} } Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 13 / 41
Pushdown Automata Example: Recognize 0110 Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 14 / 41
Pushdown Automata Example: Recognize 0110 continued (0) Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 15 / 41
Pushdown Automata Example: Recognize 0110 continued (1) Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 16 / 41
Pushdown Automata Example: Recognize 0110 continued (2) Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 17 / 41
Outline Pushdown Automata Pushdown Automata and Context-Free Grammar Equivalence Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 18 / 41
Definition: Context-Free Grammar Definition Context-Free Grammar A context-free grammar G is the tuple G = (V, T, P, S), where: V is a finite set of variables or nonterminals T is a finite set of terminals P is a finite set of productions in the form V (V T ) S V is the start symbol Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 19 / 41
Extended PDA Diagrams p a, s xyz q p a, s z ɛ, ɛ y ɛ, ɛ x (pq) 0 (pq) 1 q Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 20 / 41
CFGs to PDAs nondeterministically expand productions on stack { }} { ɛ, }{{} A }{{} ω for every (A ω) P pop A push RHS start push start symbol and end mark {}}{ pop end mark {}}{ q start ɛ, ɛ S$ q loop ɛ, $ ɛ q accept a, a ɛ for every a Σ }{{} nondeterministically pop terminals off stack Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 21 / 41
Example: CFG to PDA S [a] T [b] [b] T T [a] ɛ Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 22 / 41
Example: Parse tree for aab Grammar Parse Tree S [a] T [b] [b] T T [a] ɛ Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 23 / 41
Example: Recognize aab Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 24 / 41
Example: Recognize aab continued (0) Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 25 / 41
Example: Recognize aab continued (1) Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 26 / 41
Example: Recognize aab continued (2) Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 27 / 41
Example: Recognize aab continued (3) Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 28 / 41
PDA to CFG PDA M = (Q, Σ, Γ, δ, q 0, F ) Assume single start state pushes $ Assume single accept state with empty stack Can always do this with new symbols/states push/empty stack CFG G = (V, T, P, S) S Nonterminals q, A, p, q, p Q and A Γ Idea: q, A, p derives ω going from state q to p and erasing A Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 29 / 41
PDA to CFG nonterminals Nonterminal Elements p Q: initial PDA state p, A, q q Q: ending PDA state A Γ: stack symbol popped p β, A ɛ q β Σ : derived string Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 30 / 41
PDA to CFG Productions S q 0, $, f for f F p p p a, X ɛ q p, X, q a a, X Y r p, X, q a r, Y, q a, ɛ Y r p, X, q a r, Y, s s, X, q for all s Q Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 31 / 41
PDA to CFG Productions, case 0 X... $... $ p, X, q p a, X ɛ q p, X, q a Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 32 / 41
PDA to CFG Productions, case 1 X... $ Y... $ p, X, q... $ φ, Y ɛ a, X Y p r q p, X, q a r, Y, q r, Y, q Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 33 / 41
PDA to CFG Productions, case 2 Y X... $ X... $ X... $... $ p, X, q φ, Y ɛ θ, X ɛ a, ɛ Y p r s q p, X, q a r, Y, s s, X, q r, Y, s s, X, q Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 34 / 41
Example: PDA to CFG {0 m 1 n (m n) (n 1)} Edges start 0, ɛ x 1, x ɛ ɛ, x ɛ ɛ, ɛ $ 1, x ɛ ɛ, $ ɛ q s q 0 q 1 q a q 0 q 0 q 1 q 1 0, ɛ x q 0 1, x ɛ q 1 1, x ɛ q 1 ɛ, x ɛ q 1 Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 35 / 41
Example: PDA to CFG ɛ, ɛ $ 1, x ɛ ɛ, $ ɛ continued: nonterminals q s q 0 q 1 q a start Nonterminals 0, ɛ x 1, x ɛ ɛ, x ɛ Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 36 / 41
Example: PDA to CFG continued: productions Edges Productions q 0 q 0 q 1 q 1 0, ɛ x q 0 1, x ɛ q 1 1, x ɛ q 1 ɛ, x ɛ q 1 Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 37 / 41
Example: Simulation/Derivation on 001 Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 38 / 41
Example: Simulation/Derivation on 001 continued (0) Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 39 / 41
Example: Simulation/Derivation on 001 continued (1) Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 40 / 41
Example: Simulation/Derivation on 001 continued (2) Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 41 / 41