theory of computation
play

Theory of Computation Course note based on Computability, Complexity, - PowerPoint PPT Presentation

Context-Free Languages (10) Theory of Computation Course note based on Computability, Complexity, and Languages: Fundamentals of Theoretical Computer Science , 2nd edition, authored by Martin Davis, Ron Sigal, and Elaine J. Weyuker. course note


  1. Context-Free Languages (10) Theory of Computation Course note based on Computability, Complexity, and Languages: Fundamentals of Theoretical Computer Science , 2nd edition, authored by Martin Davis, Ron Sigal, and Elaine J. Weyuker. course note prepared by Tyng–Ruey Chuang Institute of Information Science, Academia Sinica Department of Information Management, National Taiwan University Week 13, Spring 2008 1 / 21

  2. Context-Free Languages (10) About This Course Note ◮ It is prepared for the course Theory of Computation taught at the National Taiwan University in Spring 2008. ◮ It follows very closely the book Computability, Complexity, and Languages: Fundamentals of Theoretical Computer Science , 2nd edition, by Martin Davis, Ron Sigal, and Elaine J. Weyuker. Morgan Kaufmann Publishers. ISBN: 0-12-206382-1. ◮ It is available from Tyng-Ruey Chuang’s web site: http://www.iis.sinica.edu.tw/~trc/ and released under a Creative Commons “Attribution-ShareAlike 2.5 Taiwan” license: http://creativecommons.org/licenses/by-sa/2.5/tw/ 2 / 21

  3. Regular Grammars (10.2) Chomsky Normal Form (10.3) Context-Free Languages (10) Bar-Hillel’s Pumping Lemma (10.4) Closure Properties (10.5) Regular Grammars Definition. A context-free grammar is called regular if each of its productions has one of the two forms U → aV or U → a where U , V are variables and a is a terminal. � Theorem 2.1. If L is a regular language, then there is a regular grammar Γ such that either L = L (Γ) or L = L (Γ) ∪ { 0 } . � 3 / 21

  4. Regular Grammars (10.2) Chomsky Normal Form (10.3) Context-Free Languages (10) Bar-Hillel’s Pumping Lemma (10.4) Closure Properties (10.5) A Regular Grammar for Every Regular Language Proof of Theorem 2.1. Let L = ( M ), where M is a dfa with states q 1 , . . . q m , alphabet { s 1 , . . . , s n } , transition function δ , and the set of accepting states F . We construct a grammar Γ with variables q 1 , . . . q m , terminals s 1 , . . . , s n , and start symbol q 1 . The productions are 1. q i → s r q j whenever δ ( q i , s r ) = q j , and 2. q i → s r whenever δ ( q i , s r ) ∈ F . Clearly the grammar Γ is regular. To show that L (Γ) = L − { 0 } we suppose u ∈ L , u = s i 1 s i 2 . . . s i l s i l +1 � = 0. Thus, δ ∗ ( q 1 , u ) ∈ F , so that we have δ ( q 1 , s i 1 ) = q j 1 , δ ( q j 1 , s i 2 ) = q j 2 , . . . , δ ( q j l , s i l +1 ) = q j l +1 ∈ F . 4 / 21

  5. Regular Grammars (10.2) Chomsky Normal Form (10.3) Context-Free Languages (10) Bar-Hillel’s Pumping Lemma (10.4) Closure Properties (10.5) A Regular Grammar for Every Regular Language, Continued Proof of Theorem 2.1. (Continued) By construction, grammar Γ contains the productions q 1 → s i 1 q j 1 , q j 1 → s i 2 q j 2 , . . . , q j l − 1 → s i l q j l , q j l → s i l +1 . Thus, we have in Γ q 1 ⇒ s i 1 q j 1 ⇒ s i 1 s i 2 q j 2 ⇒ . . . ⇒ s i 1 s i 2 . . . s i l q j l ⇒ s i 1 s i 2 . . . s i l s i l +1 = u so that u ∈ L (Γ). Conversely, suppose that u ∈ L (Γ) , u = s i 1 s i 2 . . . s i l s i l +1 . Then there is a derivation of u from q 1 in Γ. By construction, Γ has all the necessary productions to simulate the transition δ ∗ ( q 1 , u ) ∈ F in the dfa M . � 5 / 21

  6. Regular Grammars (10.2) Chomsky Normal Form (10.3) Context-Free Languages (10) Bar-Hillel’s Pumping Lemma (10.4) Closure Properties (10.5) A Regular Language for Every Regular Grammar Theorem 2.2. Let Γ be a regular grammar. Then L (Γ) is a regular language. Proof. Let Γ have the variables V 1 , V 2 , . . . , V K , where S = V 1 is the start symbol, and terminals s 1 , s 2 , . . . , s n . Since Γ is regular, its productions are of the form V i → s r V j and V i → s r . We now construct the following ndfa M which accepts precisely L (Γ). ◮ The states are V 1 , V 2 , . . . V K and an additional state W . V 1 is the initial state and W is the only accepting state. ◮ For transition functions, let δ 1 ( V i , s r ) = { V j | V i → s r V j is a production of Γ } , � { W } if V i → s r is a production of Γ δ 2 ( V i , s r ) = ∅ otherwise. Then define the transition function δ as δ ( V i , s r ) = δ 1 ( V i , s r ) ∪ δ 2 ( V i , s r ). 6 / 21

  7. Regular Grammars (10.2) Chomsky Normal Form (10.3) Context-Free Languages (10) Bar-Hillel’s Pumping Lemma (10.4) Closure Properties (10.5) A Regular Language for Every Regular Grammar Proof of Theorem 2.2. (Continued) Now let u = s i 1 s i 2 . . . s i l s i l +1 ∈ L (Γ). Thus we have V 1 ⇒ s i 1 V j 1 ⇒ s i 1 s i 2 V j 2 ⇒ ∗ s i 1 s i 2 . . . s i l V i l ⇒ s i 1 s i 2 . . . s i l s i l +1 where Γ contains the productions V 1 → s i 1 V j 1 , V j 1 → s i 2 V j 2 , . . . , V j l − 1 → s i l V j l , V j l → s i l +1 Thus, V j 1 ∈ δ ( V 1 , s i 1 ) , V j 2 ∈ δ ( V j 1 , s i 2 ) , W ∈ δ ( V j l , s i l +1 ) . . . . , Thus W ∈ δ ∗ ( V 1 , u ) and u ∈ L ( M ). Conversely, if u = s i 1 s i 2 . . . s i l s i l +1 is accepted by M , then there must be a sequence of transitions of the form above. Hence, the productions listed above must all belong to Γ, so that there is a derivation of u from V 1 . � 7 / 21

  8. Regular Grammars (10.2) Chomsky Normal Form (10.3) Context-Free Languages (10) Bar-Hillel’s Pumping Lemma (10.4) Closure Properties (10.5) Every Regular Language Is Context-free Theorem 2.3. A language L is regular if and only if there is a regular grammar Γ such that either L = L (Γ) or L = L (Γ) ∪ { 0 } . � 8 / 21

  9. Regular Grammars (10.2) Chomsky Normal Form (10.3) Context-Free Languages (10) Bar-Hillel’s Pumping Lemma (10.4) Closure Properties (10.5) Every Regular Language Is Context-free Theorem 2.3. A language L is regular if and only if there is a regular grammar Γ such that either L = L (Γ) or L = L (Γ) ∪ { 0 } . � Corollary 2.4. Every regular language is context-free. � 8 / 21

  10. Regular Grammars (10.2) Chomsky Normal Form (10.3) Context-Free Languages (10) Bar-Hillel’s Pumping Lemma (10.4) Closure Properties (10.5) Right-linear Grammars Definition. A context-free grammar is called right-linear if each of its productions has one of the two forms U → xV or U → x , where U , V are variables and x � = 0 is a word consisting entirely of terminals. � 9 / 21

  11. Regular Grammars (10.2) Chomsky Normal Form (10.3) Context-Free Languages (10) Bar-Hillel’s Pumping Lemma (10.4) Closure Properties (10.5) Right-linear Grammars Definition. A context-free grammar is called right-linear if each of its productions has one of the two forms U → xV or U → x , where U , V are variables and x � = 0 is a word consisting entirely of terminals. � Thus, a regular grammar is just a right-linear grammar in which | x | = 1. 9 / 21

  12. Regular Grammars (10.2) Chomsky Normal Form (10.3) Context-Free Languages (10) Bar-Hillel’s Pumping Lemma (10.4) Closure Properties (10.5) Right-linear Grammars, Continued Theorem 2.5. Let Γ be a right-linear grammar. Then L (Γ) is regular. Proof. We replace each production of Γ of the form U → a 1 a 2 . . . a n V , n > 1 by the productions U → a 1 Z 1 , Z 1 → a 2 Z 2 , Z n − 2 → a n − 1 Z n − 1 , Z n − 1 → a n V , where Z 1 , . . . , Z n − 1 are new variables. Do similar replacement for production U → a 1 a 2 . . . a n , n > 1 � 10 / 21

  13. Regular Grammars (10.2) Chomsky Normal Form (10.3) Context-Free Languages (10) Bar-Hillel’s Pumping Lemma (10.4) Closure Properties (10.5) Chomsky Normal Form Definition. A context-free grammar Γ with variables V and terminals T is in Chomsky normal form if each of its productions has one of the forms X → YZ or X → a , where X , Y , Z ∈ V and a ∈ T . � Theorem 3.1. There is an algorithm that transforms a given positive context-free grammar Γ into a Chomsky normal form grammar ∆ such that L (Γ) = L (∆). � 11 / 21

  14. Regular Grammars (10.2) Chomsky Normal Form (10.3) Context-Free Languages (10) Bar-Hillel’s Pumping Lemma (10.4) Closure Properties (10.5) Chomsky Normal Form, Continued Proof of Theorem 3.1. Using Theorem 1.5, we begin with a branching context-free grammar Γ with variable V and terminals T . We then perform the following two steps: 1. a new variable X a is introduced for each a ∈ T , and for each production X → x ∈ Γ , | x | > 1, we replace it with X → x ′ where x ′ is obtained from x by replacing each terminal a by the corresponding new variable X a ; 2. For productions of the form X → X 1 X 2 . . . X k , k > 2, we introduce new variables Z 1 , Z 2 , . . . , Z k − 2 and replace the production with the following → X X 1 Z 1 . . . → Z k − 3 X k − 2 Z k − 2 Z k − 2 → X k − 1 X k . � 12 / 21

  15. Regular Grammars (10.2) Chomsky Normal Form (10.3) Context-Free Languages (10) Bar-Hillel’s Pumping Lemma (10.4) Closure Properties (10.5) Chomsky Normal Form, Examples Consider the following branching context-free grammar S → aXbY , X → aX , Y → bY , X → a , Y → b The resulting grammar, respectively, from the two steps is: 1. S → X a XX b Y , X → X a X , Y → X b Y , X → a , X a → a , Y → b , X b → b 2. For the production S → X a XX b Y , we replace it with the following: S → X a Z 1 Z 1 → XZ 2 Z 2 → X b Y . The resulting grammar is in Chomsky normal form. 13 / 21

Recommend


More recommend