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 14, Spring 2008 1 / 25

  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 / 25

  3. Closure Properties (10.5) Context-Free Languages (10) Bracket Languages (10.7) R ∩ L Theorem 5.4. If R is a regular language and L is a context-free language, then R ∩ L is context-free. 3 / 25

  4. Closure Properties (10.5) Context-Free Languages (10) Bracket Languages (10.7) R ∩ L Theorem 5.4. If R is a regular language and L is a context-free language, then R ∩ L is context-free. Proof. Let A be an alphabet such that L , R ∈ A ∗ . Let L = L (Γ) or L (Γ) ∪ { 0 } , where Γ is a positive context-free grammar with variables V , terminals A and start symbol S . Let M be a dfa that accepts R with states Q , initial state q 1 ∈ Q , accepting states F ⊆ Q , and transition function δ . For each symbol σ ∈ A ∪ V , and each ordered pair p , q ∈ Q , we introduce a new symbol σ pq . We shall construct a positive context-free grammar ˜ Γ whose terminals are A , and whose variables consists of a start symbol ˜ S together with all the new symbols σ pq for σ ∈ A ∪ V and p , q ∈ Q . (Note that for a ∈ A , a is a terminal, but a pq is a variable for each p , q ∈ Q .) 3 / 25

  5. Closure Properties (10.5) Context-Free Languages (10) Bracket Languages (10.7) R ∩ L , Continued Proof of Theorem 5.4 (Continued). The productions of ˜ Γ are: S → S q 1 q for all q ∈ F . 1. ˜ 2. X pq → σ pr 1 . . . σ r n − 1 q 1 σ r 1 r 2 of all productions X → σ 1 σ 2 . . . σ n n 2 of Γ and all p , r 1 , r 2 , . . . , r n − 1 , q ∈ Q . 3. a pq → a for all a ∈ A and all p , q ∈ Q such that δ ( p , a ) = q . We shall now prove that L (˜ Γ) = R ∩ L (Γ). 4 / 25

  6. Closure Properties (10.5) Context-Free Languages (10) Bracket Languages (10.7) R ∩ L , Continued Proof of Theorem 5.4 (Continued). The productions of ˜ Γ are: S → S q 1 q for all q ∈ F . 1. ˜ 2. X pq → σ pr 1 . . . σ r n − 1 q 1 σ r 1 r 2 of all productions X → σ 1 σ 2 . . . σ n n 2 of Γ and all p , r 1 , r 2 , . . . , r n − 1 , q ∈ Q . 3. a pq → a for all a ∈ A and all p , q ∈ Q such that δ ( p , a ) = q . We shall now prove that L (˜ Γ) = R ∩ L (Γ). First let u = a 1 a 2 . . . a n ∈ R ∩ L (Γ). Since u ∈ L (Γ), we have S ⇒ ∗ Γ a 1 a 2 . . . a n . It follows that ˜ Γ S q 1 q n +1 ⇒ ∗ Γ a q 1 q 2 a q 2 q 3 . . . a q n q n +1 S ⇒ ˜ , where n ˜ 1 2 q 1 , q 2 , . . . , q n , q n +1 ∈ Q , q 1 is the initial state, and q n +1 ∈ F . Since u ∈ L ( M ), we can choose states so that δ ( q i , a i ) = q i +1 , for all i . This implies that a q i q i +1 → a i , for all i . We conclude that i ˜ Γ a 1 a 2 . . . a n , hence u ∈ L (˜ S ⇒ ∗ Γ). ˜ 4 / 25

  7. Closure Properties (10.5) Context-Free Languages (10) Bracket Languages (10.7) R ∩ L , Continued Γ S q 1 q ⇒ ∗ For the other direction, that if ˜ S ⇒ ˜ Γ a 1 a 2 . . . a n = u ˜ where q ∈ F , then S ⇒ ∗ Γ u , we need to prove the following lemma. Lemma. Let σ pq ⇒ ∗ Γ u ∈ A ∗ . Then, δ ∗ ( p , u ) = q . Moreover, if σ ˜ is a variable, then σ ⇒ ∗ Γ u . 5 / 25

  8. Closure Properties (10.5) Context-Free Languages (10) Bracket Languages (10.7) R ∩ L , Continued Γ S q 1 q ⇒ ∗ For the other direction, that if ˜ S ⇒ ˜ Γ a 1 a 2 . . . a n = u ˜ where q ∈ F , then S ⇒ ∗ Γ u , we need to prove the following lemma. Lemma. Let σ pq ⇒ ∗ Γ u ∈ A ∗ . Then, δ ∗ ( p , u ) = q . Moreover, if σ ˜ is a variable, then σ ⇒ ∗ Γ u . Proof of this lemma can be done by an induction on the length of a derivation of u from σ pq ∈ ˜ Γ. That is, for derivation of length > 2, we can write σ pq ⇒ ˜ . . . σ r n − 1 r n Γ σ r 0 r 1 σ r 1 r 2 ⇒ ∗ Γ u 1 u 2 . . . u n = u n 1 2 ˜ where r 0 = p , r n = q , and σ r i − 1 r i ⇒ ∗ Γ u i . The induction hypotheses ˜ i ensure that δ ∗ ( r i − 1 , u i ) = r i and σ i ⇒ ∗ Γ u i , for all i . From this we can show that δ ∗ ( p , u ) = q and σ ⇒ ∗ Γ u , hence complete the proof for the other direction. � 5 / 25

  9. Closure Properties (10.5) Context-Free Languages (10) Bracket Languages (10.7) Erased Symbols Let A , P be alphabets such that P ⊆ A . For each letter a ∈ A , let us write � 0 if a ∈ P a 0 = a if a ∈ A − P . If x = a 1 a 2 . . . a n ∈ A ∗ , we write Er P ( x ) = a 0 1 a 0 2 . . . , a 0 n In other words, Er P ( x ) is the word that results from x where all the symbols in it that are part of the alphabet P are “erased.” 6 / 25

  10. Closure Properties (10.5) Context-Free Languages (10) Bracket Languages (10.7) Erased Symbols, Continued If L ⊆ A ∗ , we also write Er P ( L ) = { Er P ( x ) | x ∈ L } . If Γ is any context-free grammar with terminal symbols T and if P ⊆ T , we write Er P (Γ) for the context-free grammar with terminals T − P , the same variables and start symbol as Γ, and production X → Er P ( v ) for each production X → v of Γ. 7 / 25

  11. Closure Properties (10.5) Context-Free Languages (10) Bracket Languages (10.7) A Theorem about Erased Symbols Theorem 5.5. If Γ is a context-free grammar and ˜ Γ = Er P (Γ), then L (˜ Γ) = Er P ( L (Γ)). 8 / 25

  12. Closure Properties (10.5) Context-Free Languages (10) Bracket Languages (10.7) A Theorem about Erased Symbols Theorem 5.5. If Γ is a context-free grammar and ˜ Γ = Er P (Γ), then L (˜ Γ) = Er P ( L (Γ)). Proof Outline. Suppose that w ∈ L (Γ), we have S = w 1 ⇒ Γ w 2 . . . ⇒ Γ w m = w . Let v i = Er P ( w i ) , i = 1 , 2 , . . . , m . Clearly, S = v 1 ⇒ ˜ Γ v 2 . . . ⇒ ˜ Γ v m = Er P ( w ) . so that Er P ( w ) ∈ L (˜ Γ). This proves that L (˜ Γ) ⊇ Er P ( L (Γ)). For the other direction, we need to show that whenever Γ v ∈ ( T − P ) ∗ , there is a word w ∈ T ∗ such that X ⇒ ∗ X ⇒ ∗ Γ w ˜ and v = Er P ( w ). This can be done by an induction on the length of a derivation of v from X in ˜ Γ. � 8 / 25

  13. Closure Properties (10.5) Context-Free Languages (10) Bracket Languages (10.7) A Theorem about Erased Symbols, Continued From Theorem 5.5, we may say that the “operators” L and Er P commute L (Er P (Γ)) = Er P ( L (Γ)) for any context-free grammar Γ. We prove the straightforward: Corollary 5.6. If L ⊆ A ∗ is a context-free language and P ⊆ A , then Er P ( L ) is also a context-free language. Proof. Let L = L (Γ), where Γ is context-free grammar. Let ˜ Γ = Er P (Γ). By Theorem 5.5, Er P (Γ) = L (˜ Γ) so is context-free. � 9 / 25

  14. Closure Properties (10.5) Context-Free Languages (10) Bracket Languages (10.7) Bracket Languages Let A be a finite set. Let B be an alphabet we get from A by adding 2 n new symbols ( i , ) i , i = 1 , 2 , . . . , n , where n is some given positive integer. We write PAR n ( A ) for the language consisting of all the strings in B ∗ that are correctly “paired,” thinking of each pair ( i , ) i as matching left and right brackets. More precisely, PAR n ( A ) = L (Γ 0 ), where Γ 0 is the context-free grammar with the single variables S , terminals B , and the productions 1. S → a for all a ∈ A , 2. S → ( i S ) i , i = 1 , 2 , . . . , n , 3. S → SS , S → 0. The languages PAR n ( A ) are called bracket languages . 10 / 25

  15. Closure Properties (10.5) Context-Free Languages (10) Bracket Languages (10.7) Bracket Languages, Examples Let A = { a , b , c } , and n = 2. For ease of reading we will use the symbol ( for ( 1 , ) for ) 1 , [ for ( 2 , and ] for ) 2 . Then we have cb [( ab ) c ]( a [ b ] c ) ∈ PAR 2 ( A ) as well as ()[] ∈ PAR 2 ( A ) 11 / 25

  16. Closure Properties (10.5) Context-Free Languages (10) Bracket Languages (10.7) Bracket Languages, Properties Theorem 7.1. PAR n ( A ) is a context-free language such that 1. A ∗ ⊆ PAR n ( A ); 2. if x , y ∈ PAR n ( A ), so is xy ; 3. if x ∈ PAR n ( A ), so is ( i x ) i , for i = 1 , 2 , . . . , n ; 4. if x ∈ PAR n ( A ) and x �∈ A ∗ , then we can write x = u ( i v ) i w , for some i = 1 , 2 , . . . , n , where u ∈ A ∗ and v , w ∈ PAR n ( A ). 12 / 25

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend