4 5 proving the correctness of grammars
play

4.5: Proving the Correctness of Grammars In this section, we - PowerPoint PPT Presentation

4.5: Proving the Correctness of Grammars In this section, we consider techniques for proving the correctness of grammars, i.e., for proving that grammars generate the languages we want them to. 1 / 21 Definition of Suppose G is a grammar and


  1. 4.5: Proving the Correctness of Grammars In this section, we consider techniques for proving the correctness of grammars, i.e., for proving that grammars generate the languages we want them to. 1 / 21

  2. Definition of Π Suppose G is a grammar and a ∈ Q G ∪ alphabet G . Then Π G , a = { w ∈ ( alphabet G ) ∗ | w is parsable from a using G } . If it’s clear which grammar we are talking about, we often abbreviate Π G , a to Π a . Clearly, Π G , s G = L ( G ). For example, if G is the grammar A → % | 0A1 then Π 0 = { 0 } , Π 1 = { 1 } and Π A = { 0 n 1 n | n ∈ N } = L ( G ). 2 / 21

  3. Properties of Π Proposition 4.5.1 Suppose G is a grammar. (1) For all a ∈ alphabet G, Π G , a = { a } . (2) For all q ∈ Q G , if q → % ∈ P G , then % ∈ Π G , q . (3) For all q ∈ Q G , n ∈ N − { 0 } , a 1 , . . . , a n ∈ Sym and w 1 , . . . , w n ∈ Str , if q → a 1 · · · a n ∈ P G and w 1 ∈ Π G , a 1 , . . . , w n ∈ Π G , a n , then w 1 · · · w n ∈ Π G , q . 3 / 21

  4. Main Example Define diff ∈ { 0 , 1 } ∗ → Z by: for all w ∈ { 0 , 1 } ∗ , diff w = the number of 1’s in w − the number of 0’s in w . Then: • diff % = 0; • diff 1 = 1; • diff 0 = − 1; and • for all x , y ∈ { 0 , 1 } ∗ , diff ( xy ) = diff x + diff y . 4 / 21

  5. Main Example Our main example will be the grammar G : A → % | 0BA | 1CA , B → 1 | 0BB , C → 0 | 1CC . Let X = { w ∈ { 0 , 1 } ∗ | diff w = 0 } , Y = { w ∈ { 0 , 1 } ∗ | diff w = 1 and, for all proper prefixes v of w , diff v ≤ 0 } , Z = { w ∈ { 0 , 1 } ∗ | diff w = − 1 and, for all proper prefixes v of w , diff v ≥ 0 } . We will prove that L ( G ) = Π G , A = X , Π G , B = Y and Π G , C = Z . 5 / 21

  6. Main Example Lemma 4.5.2 Suppose x ∈ { 0 , 1 } ∗ . (1) If diff x ≥ 1 , then x = yz for some y , z ∈ { 0 , 1 } ∗ such that y ∈ Y and diff z = diff x − 1 . (2) If diff x ≤ − 1 , then x = yz for some y , z ∈ { 0 , 1 } ∗ such that y ∈ Z and diff z = diff x + 1 . 6 / 21

  7. Main Example Proof. We show the proof of (1), the proof of (2) being similar. Let y ∈ { 0 , 1 } ∗ be the shortest prefix of x such that diff y ≥ 1, and let z ∈ { 0 , 1 } ∗ be such that x = yz . Because diff y ≥ 1, we have that y � = %. Thus y = y ′ a for some y ′ ∈ { 0 , 1 } ∗ and a ∈ { 0 , 1 } . By the definition of y , we have that diff y ′ ≤ 0. Suppose, toward a contradiction, that a = 0. Since diff y ′ + − 1 = diff y ≥ 1, we have that diff y ′ ≥ 2, contradicting the definition of y . Thus a = 1, so that y = y ′ 1. Because diff y ′ + 1 = diff y ≥ 1, we have that diff y ′ ≥ 0. Thus diff y ′ = 0, so that diff y = diff y ′ + 1 = 1. By the definition of y , every prefix of y ′ has a diff that is ≤ 0. Thus y ∈ Y . Finally, because diff x = diff y + diff z = 1 + diff z we have that diff z = diff x − 1. ✷ 7 / 21

  8. Proving that Enough is Generated First we study techniques for showing that everything we want a grammar to generate is really generated. Since X , Y , Z ⊆ { 0 , 1 } ∗ , to prove that X ⊆ Π G , A , Y ⊆ Π G , B and Z ⊆ Π G , C , it will suffice to use strong string induction to show that, for all w ∈ { 0 , 1 } ∗ : (A) if w ∈ X , then w ∈ Π G , A ; (B) if w ∈ Y , then w ∈ Π G , B ; and (C) if w ∈ Z , then w ∈ Π G , C . 8 / 21

  9. Enough is Generated in Example We proceed by strong string induction. Suppose w ∈ { 0 , 1 } ∗ , and assume the inductive hypothesis: for all x ∈ { 0 , 1 } ∗ , if x is a proper substring of w , then: (A) if x ∈ X , then x ∈ Π A ; (B) if x ∈ Y , then x ∈ Π B ; and (C) if x ∈ Z , then x ∈ Π C . We must prove that: (A) if w ∈ X , then w ∈ Π A ; (B) if w ∈ Y , then w ∈ Π B ; and (C) if w ∈ Z , then w ∈ Π C . 9 / 21

  10. Enough is Generated in Example (A) Suppose w ∈ X . We must show that w ∈ Π A . There are three cases to consider. • Suppose w = %. Because A → % ∈ P , we have that w = % ∈ Π A . • Suppose w = 0 x , for some x ∈ { 0 , 1 } ∗ . Because − 1 + diff x = diff w = 0, we have that diff x = 1. Thus, by Lemma 4.5.2(1), we have that x = yz , for some y , z ∈ { 0 , 1 } ∗ such that y ∈ Y and diff z = diff x − 1 = 1 − 1 = 0. Thus w = 0 yz , y ∈ Y and z ∈ X . We have 0 ∈ Π 0 . Because y ∈ Y and z ∈ X are proper substrings of w , parts (B) and (A) of the inductive hypothesis tell us that y ∈ Π B and z ∈ Π A . Thus, because A → 0BA ∈ P , it follows that that w = 0 yz ∈ Π A . • Suppose w = 1 x , for some x ∈ { 0 , 1 } ∗ . The proof is analogous to the preceding case. 10 / 21

  11. Enough is Generated in Example (B) Suppose w ∈ Y . We must show that w ∈ Π B . Because diff w = 1, there are two cases to consider. • Suppose w = 1 x , for some x ∈ { 0 , 1 } ∗ .Because all proper prefixes of w have diffs ≤ 0, we have that x = %, so that w = 1. Since B → 1 ∈ P , we have that w = 1 ∈ Π B . • Suppose w = 0 x , for some x ∈ { 0 , 1 } ∗ . Thus diff x = 2. Because diff x ≥ 1, by Lemma 4.5.2(1), we have that x = yz , for some y , z ∈ { 0 , 1 } ∗ such that y ∈ Y and diff z = diff x − 1 = 2 − 1 = 1. Hence w = 0 yz . To finish the proof that z ∈ Y , suppose v is a proper prefix of z . Thus 0 yv is a proper prefix of w . Since w ∈ Y , it follows that diff v = diff (0 yv ) ≤ 0, as required. Since y , z ∈ Y , part (B) of the inductive hypothesis tell us that y , z ∈ Π B . Thus, because B → 0BB ∈ P we have that w = 0 yz ∈ Π B . (C) Suppose w ∈ Z . We must show that w ∈ Π C . The proof is analogous to the proof of part (B). 11 / 21

  12. A Problem with Unit Productions Suppose H is the grammar A → B | 0A3 , B → % | 1B2 , and let X = { 0 n 1 m 2 m 3 n | n , m ∈ N } Y = { 1 m 2 m | m ∈ N } . and We can prove that X ⊆ Π H , A = L ( H ) and Y ⊆ Π H , B using the above technique, but the production A → B, which is called a unit production because its right side is a single variable, makes part (A) tricky. If w = 0 0 1 m 2 m 3 0 = 1 m 2 m ∈ Y , we would like to use part (B) of the inductive hypothesis to conclude w ∈ Π B , and then use the fact that A → B ∈ P to conclude that w ∈ Π A . But w is not a proper substring of itself, and so the inductive hypothesis in not applicable. Instead, we must split into cases m = 0 and m ≥ 1, using A → B and B → %, in the first case, and A → B and B → 1B2, as well as the inductive hypothesis on 1 m − 1 2 m − 1 ∈ Y , in the second case. 12 / 21

  13. A Problem with Unit Productions Because there are no productions from B back to A, we could also first use strong string induction to prove that, for all w ∈ { 0 , 1 } ∗ , (B) if w ∈ Y , then w ∈ Π B , and then use the result of this induction along with strong string induction to prove that for all w ∈ { 0 , 1 } ∗ , (A) if w ∈ X , then w ∈ Π A . This works whenever two parts of a grammar are not mutually recursive. With this grammar, we could also first use mathematical induction to prove that, for all m ∈ N , 1 m 2 m ∈ Π B , and then use the result of this induction to prove, by mathematical induction on n , that for all n , m ∈ N , 0 n 1 m 2 m 3 n ∈ Π A . 13 / 21

  14. A Problem with % -Productions Note that %-productions, i.e., productions of the form q → %, can cause similar problems to those caused by unit productions. E.g., if we have the productions A → BC and B → % , then A → BC behaves like a unit production. 14 / 21

  15. Proving that Everything Generated is Wanted To prove that everything generated by a grammar is wanted, we introduce a new induction principle that we call induction on Π. 15 / 21

  16. Principle of Induction on Π Theorem 4.5.3 (Principle of Induction on Π ) Suppose G is a grammar, P q ( w ) is a property of a string w ∈ Π G , q , for all q ∈ Q G , and P a ( w ) , for a ∈ alphabet G, says “w = a”. If (1) for all q ∈ Q G , if q → % ∈ P G , then P q (%) , and (2) for all q ∈ Q G , n ∈ N − { 0 } , a 1 , . . . , a n ∈ Q G ∪ alphabet G, and w 1 ∈ Π G , a 1 , . . . , w n ∈ Π G , a n , if q → a 1 · · · a n ∈ P G and ( † ) P a 1 ( w 1 ) , . . . , P a n ( w n ) , then P q ( w 1 · · · w n ) , then for all q ∈ Q G , for all w ∈ Π G , q , P q ( w ) . We refer to ( † ) as the inductive hypothesis. 16 / 21

  17. Principle of Induction on Π Proof. It suffices to show that, for all pt ∈ PT , for all q ∈ Q G and w ∈ ( alphabet G ) ∗ , if pt is valid for G , rootLabel pt = q and yield pt = w , then P q ( w ). We prove this using the principle of induction on parse trees. ✷ When proving part (2), we can make use of the fact that, for a i ∈ alphabet G , Π a i = { a i } , so that w i ∈ Π a i will be a i . Hence it will be unnecessary to assume that P a i ( a i ), since this says “ a i = a i ”, and so is always true. 17 / 21

  18. Using Induction on Π in Example Consider, again, our main example grammar G : A → % | 0BA | 1CA , B → 1 | 0BB , C → 0 | 1CC . Let X = { w ∈ { 0 , 1 } ∗ | diff w = 0 } , Y = { w ∈ { 0 , 1 } ∗ | diff w = 1 and, for all proper prefixes v of w , diff v ≤ 0 } , and Z = { w ∈ { 0 , 1 } ∗ | diff w = − 1 and, for all proper prefixes v of w , diff v ≥ 0 } . 18 / 21

Recommend


More recommend