Formal Languages and Automata Lecture 6 2017-18 LFAC (2017-18) Lecture 6 1 / 31
Lecture 6 1 The recognition problem: the Cocke Younger Kasami algorithm 2 Pushdown Automata 3 Pushdown Automata and Context-free Languages 4 Deterministic pushdown automata LFAC (2017-18) Lecture 6 2 / 31
The recognition problem: the Cocke Younger Kasami algorithm Lecture 6 1 The recognition problem: the Cocke Younger Kasami algorithm 2 Pushdown Automata 3 Pushdown Automata and Context-free Languages 4 Deterministic pushdown automata LFAC (2017-18) Lecture 6 3 / 31
The recognition problem: the Cocke Younger Kasami algorithm The Cocke Younger Kasami (CYK) Agorithm The recognition problem in grammars that are in Chomsky Normal Form can be solved using the CYK algorithm in O(n 3 ). If w = a 1 a 2...a n then we build the sets V ij = {A A + a i a i+1...a i+j 1 } inductively, for j = 1,...,n w L(G) S V 1n LFAC (2017-18) Lecture 6 4 / 31
The recognition problem: the Cocke Younger Kasami algorithm The Cocke Younger Kasami Algorithm For j = 1: V i1 = {A A + a i } = {A A a i P} For j > 1, V ij : If A + a i a i+1...a i+j 1 : A BC + a i a i+1...a i+j 1 and B + a i a i+1...a i+k 1 (B V ik ) C + a i+k a i+k+1...a i+j 1 (C V i+k j k ) where 1 i n+1 j, 1 k j 1 V ij = j 1 k=1 {A A BC P, B V ik, C V i+k j k } LFAC (2017-18) Lecture 6 5 / 31
The recognition problem: the Cocke Younger Kasami algorithm The Cocke Younger Kasami Algorithm Notation: {A A BC P, B V ik, C V i+k j k } = V ik V i+k j k Then: for 2 j n, 1 i n+1 j : V ij = j 1 (V ik V i+k j k ) k=1 LFAC (2017-18) Lecture 6 6 / 31
The recognition problem: the Cocke Younger Kasami algorithm The Cocke Younger Kasami Algorithm Input: G = (N, T, S, P) in Chomsky Normal Form, w = a 1 a 2...a n Output: w L(G)? for(i=1; i<=n; i++) V i1 = {A A a i P}; for(j=2; j<=n; j++) for (i=1; i<=n+1-j; i++){ V ij = ; for(k=1; k<=j-1; k++) V ij = V ij (V ik V i+k j k ); } if(s V 1n ) w L(G) else w L(G) LFAC (2017-18) Lecture 6 7 / 31
The recognition problem: the Cocke Younger Kasami algorithm Example G = ({S, X, Y, Z},{a, b, c}, S, P), where P: S XY X XY a Y YZ a b Z c w = abc LFAC (2017-18) Lecture 6 8 / 31
The recognition problem: the Cocke Younger Kasami algorithm Example G = ({S, X, Y, Z},{a, b, c}, S, P), where P: S XY X XY a Y YZ a b Z c w = abc V 11 = {X, Y} V 12 = {S, X} V 13 = {S, X} V 21 = {Y} V 22 = {Y} V 31 = {Z} S V 13 abc L(G) LFAC (2017-18) Lecture 6 8 / 31
Lecture 6 1 The recognition problem: the Cocke Younger Kasami algorithm 2 Pushdown Automata 3 Pushdown Automata and Context-free Languages 4 Deterministic pushdown automata LFAC (2017-18) Lecture 6 9 / 31
Pushdown Automata Finite automata + pushdown memory (stack) LFAC (2017-18) Lecture 6 10 / 31
Pushdown automata - definition Definition 1 A pushdown automaton: M = (Q,Σ,Γ,δ, q 0, z 0, F): Q is a finite set of states Σ is the input alphabet Γ is the stack alphabet q 0 Q is the initial state z 0 Γ is the initial symbol in the stack F Q is the set of final states δ : Q (Σ {ǫ}) Γ 2 Q Γ The model is non-deterministic LFAC (2017-18) Lecture 6 11 / 31
Configuration of a Pushdown Automata Configuration: (q, w,γ) Q Σ Γ 1 : γ (first symbol in γ) represents the top of the stack LFAC (2017-18) Lecture 6 12 / 31
Pushdown automata Pushdown Automata Initial configuration: (q 0, w, z 0 ) Q Σ Γ LFAC (2017-18) Lecture 6 13 / 31
The transition relation between configurations Configuration (q, aw, zβ) and (q,α) δ(q, a, z) (q, q Q, a Σ {ǫ}, z Γ, α,β Γ ) LFAC (2017-18) Lecture 6 14 / 31
The transition relation between configurations (q, aw, zβ) (q, w,αβ) LFAC (2017-18) Lecture 6 15 / 31
The transition relation between configurations Let M = (Q,Σ,Γ,δ, q 0, z 0, F) be a pushdown automaton. The transition relation between configurations: (q, aw, zβ) (q, w,αβ) if (q,α) δ(q, a, z) (q, q Q, a Σ {ǫ}, z Γ, α,β Γ ) Computation: the transitive and reflexive closure of the transition relation: C 1,...,C n configurations such that: C 1 C 2... C n written: C 1 + C n if n 2, C 1 C n, if n 1 LFAC (2017-18) Lecture 6 16 / 31
Recognized language By final states (if F ) L(M) = {w Σ (q 0, w, z 0 ) (q,ǫ,γ), q F, γ Γ } By empty stack (if F = ) L ǫ (M) = {w Σ (q 0, w, z 0 ) (q,ǫ,ǫ), q Q} LFAC (2017-18) Lecture 6 17 / 31
Example Automaton which recognizes the language {a n b n n 1}: M = ({q 0, q 1, q 2 },{a, b},{a, z},δ, q 0, z,{q 2 }) 1 δ(q 0, a, z) = {(q 0, az)} 2 δ(q 0, a, a) = {(q 0, aa)} 3 δ(q 0, b, a) = {(q 1,ǫ)} 4 δ(q 1, b, a) = {(q 1,ǫ)} 5 δ(q 1,ǫ, z) = {(q 2,ǫ)} LFAC (2017-18) Lecture 6 18 / 31
Examples Pushdown automaton which recognizes the language {waw R w {0, 1} } LFAC (2017-18) Lecture 6 19 / 31
Examples Pushdown automaton which recognizes the language {waw R w {0, 1} } Every 0 or 1 read is added to the stack a in the input changes the state LFAC (2017-18) Lecture 6 19 / 31
Examples Pushdown automaton which recognizes the language {waw R w {0, 1} } Every 0 or 1 read is added to the stack a in the input changes the state after reading a, for every symbol read from the input, the same symbol must be removed from the stack M = ({q 0, q 1, q 2 },{0, 1, a},{0, 1, z},δ, q 0, z,{q 2 }) 1 δ(q 0, i, z) = {(q 0, iz)}, (i {0, 1}) 2 δ(q 0, i, j) = {(q 0, ij)}, (i, j {0, 1}) 3 δ(q 0, a, i) = {(q 1, i)} 4 δ(q 1, i, i) = {(q 1,ǫ)} 5 δ(q 1,ǫ, z) = {(q 2,ǫ)} LFAC (2017-18) Lecture 6 19 / 31
Examples Pushdown automaton which recognizes the language {waw R w {0, 1} } Every 0 or 1 read is added to the stack a in the input changes the state after reading a, for every symbol read from the input, the same symbol must be removed from the stack M = ({q 0, q 1, q 2 },{0, 1, a},{0, 1, z},δ, q 0, z,{q 2 }) 1 δ(q 0, i, z) = {(q 0, iz)}, (i {0, 1}) 2 δ(q 0, i, j) = {(q 0, ij)}, (i, j {0, 1}) 3 δ(q 0, a, i) = {(q 1, i)} 4 δ(q 1, i, i) = {(q 1,ǫ)} 5 δ(q 1,ǫ, z) = {(q 2,ǫ)} Pushdown automaton for {ww R w {0, 1} }? LFAC (2017-18) Lecture 6 19 / 31
Examples Pushdown automaton which recognizes the language {waw R w {0, 1} } Every 0 or 1 read is added to the stack a in the input changes the state after reading a, for every symbol read from the input, the same symbol must be removed from the stack M = ({q 0, q 1, q 2 },{0, 1, a},{0, 1, z},δ, q 0, z,{q 2 }) 1 δ(q 0, i, z) = {(q 0, iz)}, (i {0, 1}) 2 δ(q 0, i, j) = {(q 0, ij)}, (i, j {0, 1}) 3 δ(q 0, a, i) = {(q 1, i)} 4 δ(q 1, i, i) = {(q 1,ǫ)} 5 δ(q 1,ǫ, z) = {(q 2,ǫ)} Pushdown automaton for {ww R w {0, 1} }? Pushdown automaton for {ww w {0, 1} }? LFAC (2017-18) Lecture 6 19 / 31
Pushdwon automata with final states and without final states Theorem 1 For any pushdown automaton M with F =, there exists a pushdown automaton M with final states such that L(M ) = L ǫ (M). LFAC (2017-18) Lecture 6 20 / 31
Pushdwon automata with final states and without final states Theorem 1 For any pushdown automaton M with F =, there exists a pushdown automaton M with final states such that L(M ) = L ǫ (M). If M = (Q,Σ,Γ,δ, q 0, z 0, ), let M = (Q {q f, q 0 },Σ,Γ {z 0 },δ, q 0, z 0,{q f}) with δ : LFAC (2017-18) Lecture 6 20 / 31
Pushdwon automata with final states and without final states Theorem 1 For any pushdown automaton M with F =, there exists a pushdown automaton M with final states such that L(M ) = L ǫ (M). If M = (Q,Σ,Γ,δ, q 0, z 0, ), let M = (Q {q f, q 0 },Σ,Γ {z 0 },δ, q 0, z 0,{q f}) with δ : 1 δ (q 0,ǫ, z 0 ) = {(q 0, z 0 z 0 )} (without reading any symbol, M goes to the initial configuration of M) LFAC (2017-18) Lecture 6 20 / 31
Pushdwon automata with final states and without final states Theorem 1 For any pushdown automaton M with F =, there exists a pushdown automaton M with final states such that L(M ) = L ǫ (M). If M = (Q,Σ,Γ,δ, q 0, z 0, ), let M = (Q {q f, q 0 },Σ,Γ {z 0 },δ, q 0, z 0,{q f}) with δ : 1 δ (q 0,ǫ, z 0 ) = {(q 0, z 0 z 0 )} (without reading any symbol, M goes to the initial configuration of M) 2 δ (q, a, z) = δ(q, a, z), q Q, a Σ {ǫ}, z Γ (M makes the same transitions as M) LFAC (2017-18) Lecture 6 20 / 31
Pushdwon automata with final states and without final states Theorem 1 For any pushdown automaton M with F =, there exists a pushdown automaton M with final states such that L(M ) = L ǫ (M). If M = (Q,Σ,Γ,δ, q 0, z 0, ), let M = (Q {q f, q 0 },Σ,Γ {z 0 },δ, q 0, z 0,{q f}) with δ : 1 δ (q 0,ǫ, z 0 ) = {(q 0, z 0 z 0 )} (without reading any symbol, M goes to the initial configuration of M) 2 δ (q, a, z) = δ(q, a, z), q Q, a Σ {ǫ}, z Γ (M makes the same transitions as M) 3 δ (q,ǫ, z 0 ) = {(q f,ǫ)}, q Q (M goes into a final state only if the stack of M is empty) LFAC (2017-18) Lecture 6 20 / 31
Pushdwon automata with final states and without final states Theorem 2 For every pushdown automaton M with F, there exists a pushdown automaton M with F = such that L ǫ (M ) = L(M). LFAC (2017-18) Lecture 6 21 / 31
Pushdwon automata with final states and without final states Theorem 2 For every pushdown automaton M with F, there exists a pushdown automaton M with F = such that L ǫ (M ) = L(M). If M = (Q,Σ,Γ,δ, q 0, z 0, F), let M = (Q {q ǫ, q 0 },Σ,Γ {z 0 },δ, q 0, z 0, ) LFAC (2017-18) Lecture 6 21 / 31
Proof M = (Q {q ǫ, q 0 },Σ,Γ {z 0 },δ, q 0, z 0, ), with δ : LFAC (2017-18) Lecture 6 22 / 31
Proof M = (Q {q ǫ, q 0 },Σ,Γ {z 0 },δ, q 0, z 0, ), with δ : 1 δ (q 0,ǫ, z 0 ) = {(q 0, z 0 z 0 )} (without reading any symbol, M goes into the initial configuration of M) LFAC (2017-18) Lecture 6 22 / 31
Proof M = (Q {q ǫ, q 0 },Σ,Γ {z 0 },δ, q 0, z 0, ), with δ : 1 δ (q 0,ǫ, z 0 ) = {(q 0, z 0 z 0 )} (without reading any symbol, M goes into the initial configuration of M) 2 a) δ (q, a, z) = δ(q, a, z), q Q, a Σ, z Γ (M makes the same transitions as f M, for any symbol read from the input) b) δ (q,ǫ, z) = δ(q,ǫ, z), if q Q \ F, z Γ (M makes the same ǫ-transitions as M, if the state is non-final ) c) δ (q,ǫ, z) = δ(q,ǫ, z) {(q ǫ,ǫ)}, q F, z Γ (if M goes into a final state, M can go into a special state ) LFAC (2017-18) Lecture 6 22 / 31
Proof M = (Q {q ǫ, q 0 },Σ,Γ {z 0 },δ, q 0, z 0, ), with δ : 1 δ (q 0,ǫ, z 0 ) = {(q 0, z 0 z 0 )} (without reading any symbol, M goes into the initial configuration of M) 2 a) δ (q, a, z) = δ(q, a, z), q Q, a Σ, z Γ (M makes the same transitions as f M, for any symbol read from the input) b) δ (q,ǫ, z) = δ(q,ǫ, z), if q Q \ F, z Γ (M makes the same ǫ-transitions as M, if the state is non-final ) c) δ (q,ǫ, z) = δ(q,ǫ, z) {(q ǫ,ǫ)}, q F, z Γ (if M goes into a final state, M can go into a special state ) 3 δ (q,ǫ, z 0 ) = {(q ǫ,ǫ)}, if q F ( case 2(c), if the top of the stack is z 0 ) LFAC (2017-18) Lecture 6 22 / 31
Proof M = (Q {q ǫ, q 0 },Σ,Γ {z 0 },δ, q 0, z 0, ), with δ : 1 δ (q 0,ǫ, z 0 ) = {(q 0, z 0 z 0 )} (without reading any symbol, M goes into the initial configuration of M) 2 a) δ (q, a, z) = δ(q, a, z), q Q, a Σ, z Γ (M makes the same transitions as f M, for any symbol read from the input) b) δ (q,ǫ, z) = δ(q,ǫ, z), if q Q \ F, z Γ (M makes the same ǫ-transitions as M, if the state is non-final ) c) δ (q,ǫ, z) = δ(q,ǫ, z) {(q ǫ,ǫ)}, q F, z Γ (if M goes into a final state, M can go into a special state ) 3 δ (q,ǫ, z 0 ) = {(q ǫ,ǫ)}, if q F ( case 2(c), if the top of the stack is z 0 ) 4 δ (q ǫ,ǫ, z) = {(q ǫ,ǫ)}, if z Γ {z 0 } ( M remains in state q ǫ and pops the top of the stack) LFAC (2017-18) Lecture 6 22 / 31
Lecture 6 Pushdown Automata and Context-free Languages 1 The recognition problem: the Cocke Younger Kasami algorithm 2 Pushdown Automata 3 Pushdown Automata and Context-free Languages 4 Deterministic pushdown automata LFAC (2017-18) Lecture 6 23 / 31
and Context-free Languages The pushdown automaton equivalent to a type 2 grammar Theorem 3 For any type 2 grammar G there exists a pushdown automaton M without final states such that L ǫ (M) = L(G) LFAC (2017-18) Lecture 6 24 / 31
and Context-free Languages The pushdown automaton equivalent to a type 2 grammar Theorem 3 For any type 2 grammar G there exists a pushdown automaton M without final states such that L ǫ (M) = L(G) Let G = (N, T, S, P) The pushdown automaton: M = ({q}, T, N T,δ, q, S, ) where: 1 δ(q,ǫ, A) = {(q,β) A β P}, A N 2 δ(q, a, a) = {(q,ǫ)}, a T 3 δ(q, x, y) =, otherwise w L(G) S + w (q, w, S) + (q,ǫ,ǫ) w L ǫ (M) M simulates the leftmost derivations of G LFAC (2017-18) Lecture 6 24 / 31
and Context-free Languages Example G = ({x},{a, b}, x,{x axb, x ab}) The equivalent pushdown automaton: M = ({q},{a, b},{a, b, x},δ, q, x, ) 1 δ(q,ǫ, x) = {(q, axb),(q, ab)} 2 δ(q, a, a) = {(q,ǫ)} 3 δ(q, b, b) = {(q,ǫ)} LFAC (2017-18) Lecture 6 25 / 31
and Context-free Languages The grammar equivalent to a pushdown automaton Theorem 4 For any pushdown automaton M there exists a grammar G such that L(G) = L ǫ (M) LFAC (2017-18) Lecture 6 26 / 31