Context-Free Languages (Pre Lecture) Dr. Neil T. Dantam CSCI-561, Colorado School of Mines Fall 2017 Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 1 / 34
Outline Pumping Lemma for CFL s Decision Properties Closure Properties Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 2 / 34
Pumping Lemma for CFL s Outline Pumping Lemma for CFL s Decision Properties Closure Properties Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 3 / 34
Pumping Lemma for CFL s Regular Pumping Lemma Idea σ [j]... σ [k 1] σ [0]... σ [j 1] σ [k]... σ [n 1] q [0] q [j] = q [k] q [n] Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 4 / 34
Pumping Lemma for CFL s Context-Free Pumping Lemma Idea PDA Interpretation σ [i]... σ [j 1] ɛ β [0]... β [t] σ [k]... σ [l 1] β [t]... β [0] ɛ σ [0]... σ [i 1] ɛ α [0]... α [s] q [0] q [i] = q [j] q [k] = q [l] q [n] u v σ [j]... σ [k 1] ɛ ɛ x y σ [l]... σ [n 1] α [s]... α [0] ɛ z Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 5 / 34
Pumping Lemma for CFL s Context-Free Pumping Lemma Idea Grammar Interpretation S S... S... R... R... R... R u x z R... u v x y z R u v v x y y z Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 6 / 34
Pumping Lemma for CFL s Theorem: Pumping Lemma for CFLs Theorem: Pumping Lemma for CFLs If L is a context-free language, there is a number p (the pumping length) where if: σ L σ p then we can divide σ into five pieces σ = uvxyz such that: for each i 0, uv i xy i z L vy > 0 vxy p Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 7 / 34
Pumping Lemma for CFL s Pumping Lemma for CFLs Restated For every context-free language L, There exists an integer p, such that For every string σ L where σ p There exists an u, v, x, y, and z where σ = uvxyz for alli 0, uv i xy i z L vy > 0 vxy p L C, p Z, σ L, ( σ p) = ( u, v, x, y, z) ((σ = uvxyz) ( vxy p) ( vy > 0) ( i, uv i xy i z L )) Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 8 / 34
Pumping Lemma for CFL s Example 0: Pumping Lemma A = {a n b n c n n 0} is not context-free Proof Assume A is context-free and apply the pumping lemma Choose σ = a p b p c p = uvxyz Any way to divide σ into uvxyz will violate the pumping lemma Case 1: Both v and y contain only one type of symbol. Then uv 2 xy 2 z will not contain equal numbers of a s, b s, and c s. Case 2: Either v or y contain more than one type of symbol. Then uv 2 xy 2 z will not have symbols in the correct order. Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 9 / 34
Pumping Lemma for CFL s Example 1: Pumping Lemma B = { a k b l c k d l k 1 l 1 } is not context-free Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 10 / 34
Pumping Lemma for CFL s Example 2: Pumping Lemma C = {ωω ω {0, 1} } is not context-free Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 11 / 34
Decision Properties Outline Pumping Lemma for CFL s Decision Properties Closure Properties Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 12 / 34
Decision Properties Summary of CFL Decision Properties Can Answer: Membership: ω? C Emptiness: C? = Infinite: C? = Regular Subset: C? R Cannot Answer: Equivalence: C 1? = C2 Disjoint: C 1 C 2? = Subset: C 1? C2 Need more powerful computation model (Turing Machine) to prove no algorithm exists Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 13 / 34
Decision Properties Membership Given: CFL A, string w Find: Is w in A? w? L (A) Solution (simple): Simulate PDA for A on w Solution (fast): Many specialized parsing algorithms Earley: An CFL, O(n 3 ) in length of string CYK: An CFL, O(n 3 ) in length of string LALR: Some CFL, O(n) in length of string LL(1): Some CFLs, O(n) in length of string LL(*): Some CFL, O(n 2 ) in length of string Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 14 / 34
Decision Properties Useless symbols Useful Nonterminal X is useful if: There exists a derivation S α X β ω where S is the start symbol and ω T Requirements Two requirements for usefulness: 1. A terminal string is derivable from X 2. X is in string derivable from S Useless No such derivation using X Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 15 / 34
Decision Properties Useless symbols Terminal-deriving symbols Algorithm 1: Find terminal-deriving symbols 1 V old ; 2 V new A A σ for some σ T }{{} ; 3 while V old V new do 4 V old V new ; 5 V new V old A 6 return V new ; A derives string of terminals A α for α (T V old) }{{} A derives string of terminals and terminal-deriving nonterminals ; Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 16 / 34
Decision Properties Useless symbols Reachable symbols Algorithm 2: Find terminal-deriving symbols 1 V old ; 2 V new {S}; 3 while V old V new do 4 V old V new ; 5 V new V old B A V old, A αbβ }{{} ; 6 return V new ; A derives B Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 17 / 34
Decision Properties Useless symbols Useful Set V useful = V terminal-deriving V reachable P useful are productions containing only V useful Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 18 / 34
Decision Properties Useless symbols Example Original Grammar Symbols Simplified Grammar S A B [a] A [a] Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 19 / 34
Decision Properties Emptiness Given: Grammar G Find: Is the language of G empty? L (G) =? Solution: Is start symbol S a useless symbol? Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 20 / 34
Decision Properties Infiniteness Given: Context-Free Language L Find: Is L infinite? L? = Solution: Based on pumping lemma For pumping length p Is there a string in the language between p and 2p 1? Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 21 / 34
Closure Properties Outline Pumping Lemma for CFL s Decision Properties Closure Properties Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 22 / 34
Closure Properties CFL Closure under Concatenation Given: CFGs A = (V A, T A, P A, S A ) B = (V B, T B, P B, S B ) Find: C = (V C, T C, P C, S C ) where L (C) = L (A)L (B) Solution (simple): Union on start symbols: V C = V A V B {S } T C = T A T B S C = S P C = P A P B {S S A S B } Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 23 / 34
Closure Properties Example: Concatenation on CFGs A B AB A 0 A 1 ɛ B 1 B 0 ɛ Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 24 / 34
Closure Properties CFL Closure under Union Given: CFGs A = (V A, T A, P A, S A ) B = (V B, T B, P B, S B ) Find: C = (V C, T C, P C, S C ) where L (C) = L (A) L (B) Solution (simple): Union on start symbols: V C = V A V B {S } T C = T A T B S C = S P C = P A P B {S S A, S S B } Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 25 / 34
Closure Properties Example: Union on CFGs A B A B A 0 A 1 ɛ B 1 B 0 ɛ Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 26 / 34
Closure Properties CFL Closure under Repetition Given: CFGs A = (V A, T A, P A, S A ) Find: C = (V C, T C, P C, S C ) where L (C) = L (A) Solution (simple): Union on start symbols: V C = V A {S } T C = T A S C = S P C = P A {S S A S, S ɛ} Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 27 / 34
Closure Properties Example: Repetition on CFGs A A A 0 A 1 ɛ Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 28 / 34
Closure Properties CFL Non-closure under Intersection Theorem The context-free set is not closed under intersection ( ). Proof Proof by counterexample L 1 = {a n b n c n n 1} is not context-free L 2 = { a n b n c i n 1, i 1 } is context-free S A B A a A b ab B c B c L 3 = { a i b n c n n 1, i 1 } is context-free S A B A a A a B b B c bc L 1 = L 2 L 3 Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 29 / 34
Closure Properties CFL Non-closure under Difference Theorem The context-free set is not closed under difference (\). Proof Proof by contradiction Generally: L M = L \ (L \ M) If CFLs were closed under difference, they would also be closed under intersection. But CFLs are not closed under intersection. Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 30 / 34
Closure Properties CFL-Regular Intersection Given: A context-free language and a regular language as: PDA P = (Q P, Σ, Γ P, δ P, q 0,P, F P ) DFA D = (Q D, Σ, δ D, q 0,D, F D ), where Find: C such that L (C) = L (P) L (D) Solution: Simulate P and D in parallel. Result is a PDA. Q C = Q P Q D and q 0,C = (q 0,P, q 0,D ) and F C = F P F D Γ C = Γ P predecessor input {}}{{}}{ (q P, q D ), a, δ C ( q P, ) q D, γ }{{} successor }{{} pushed popped {}}{ γ (( q P, γ ) δ P (q P, a, γ) ) ( q D }{{} = δ D(q D ) ) }{{} PDA Transition DFA Transition Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 31 / 34
Closure Properties CFL-Regular Intersection Diagram PDA q p a, γ γ q p Intersect. DFA a, γ γ (q p, q d ) (q p, q d ) q d a q d Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 32 / 34
Closure Properties CFL-Regular Intersection Example {ω {0, 1} ω has an equal number of 0 s and 1 s} 0 1 start 0, ɛ 0 0, 1 ɛ ɛ, ɛ $ ɛ, $ ɛ p 0 p 1 p 2 start 0 1 d 0 1 d 1 1, ɛ 1 1, 0 ɛ Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 33 / 34
Closure Properties CFL-Regular Intersection Example continued Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 34 / 34