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

4 5 proving the correctness of grammars
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

4.5: Proving the Correctness of Grammars

In this section, we consider techniques for proving the correctness

  • f grammars, i.e., for proving that grammars generate the

languages we want them to.

1 / 21

slide-2
SLIDE 2

Definition of Π

Suppose G is a grammar and a ∈ QG ∪ 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,sG = L(G). For example, if G is the grammar A → % | 0A1 then Π0 = {0}, Π1 = {1} and ΠA = { 0n1n | n ∈ N } = L(G).

2 / 21

slide-3
SLIDE 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 ∈ QG, if q → % ∈ PG, then % ∈ ΠG,q. (3) For all q ∈ QG, n ∈ N − {0}, a1, . . . , an ∈ Sym and w1, . . . , wn ∈ Str, if q → a1 · · · an ∈ PG and w1 ∈ ΠG,a1, . . . , wn ∈ ΠG,an, then w1 · · · wn ∈ ΠG,q.

3 / 21

slide-4
SLIDE 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

slide-5
SLIDE 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

slide-6
SLIDE 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

slide-7
SLIDE 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 diffy = 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

slide-8
SLIDE 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

slide-9
SLIDE 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

slide-10
SLIDE 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 = 0x, 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 = 0yz, 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 = 0yz ∈ ΠA.

  • Suppose w = 1x, for some x ∈ {0, 1}∗. The proof is analogous

to the preceding case.

10 / 21

slide-11
SLIDE 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 = 1x, 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 = 0x, 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 = 0yz. To finish the proof that z ∈ Y , suppose v is a proper prefix of z. Thus 0yv is a proper prefix of w. Since w ∈ Y , it follows that diff v = diff(0yv) ≤ 0, as required. Since y, z ∈ Y , part (B)

  • f the inductive hypothesis tell us that y, z ∈ ΠB. Thus,

because B → 0BB ∈ P we have that w = 0yz ∈ ΠB.

(C) Suppose w ∈ Z. We must show that w ∈ ΠC. The proof is analogous to the proof of part (B).

11 / 21

slide-12
SLIDE 12

A Problem with Unit Productions

Suppose H is the grammar A → B | 0A3, B → % | 1B2, and let X = { 0n1m2m3n | n, m ∈ N } and Y = { 1m2m | m ∈ N }. 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 = 001m2m30 = 1m2m ∈ 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 1m−12m−1 ∈ Y , in the second case.

12 / 21

slide-13
SLIDE 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, 1m2m ∈ ΠB, and then use the result

  • f this induction to prove, by mathematical induction on n, that

for all n, m ∈ N, 0n1m2m3n ∈ ΠA.

13 / 21

slide-14
SLIDE 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

slide-15
SLIDE 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

slide-16
SLIDE 16

Principle of Induction on Π

Theorem 4.5.3 (Principle of Induction on Π) Suppose G is a grammar, Pq(w) is a property of a string w ∈ ΠG,q, for all q ∈ QG, and Pa(w), for a ∈ alphabet G, says “w = a”. If (1) for all q ∈ QG, if q → % ∈ PG, then Pq(%), and (2) for all q ∈ QG, n ∈ N − {0}, a1, . . . , an ∈ QG ∪ alphabet G, and w1 ∈ ΠG,a1, . . . , wn ∈ ΠG,an, if q → a1 · · · an ∈ PG and (†) Pa1(w1), . . . , Pan(wn), then Pq(w1 · · · wn), then for all q ∈ QG, for all w ∈ ΠG,q, Pq(w). We refer to (†) as the inductive hypothesis.

16 / 21

slide-17
SLIDE 17

Principle of Induction on Π

Proof. It suffices to show that, for all pt ∈ PT, for all q ∈ QG and w ∈ (alphabet G)∗, if pt is valid for G, rootLabel pt = q and yield pt = w, then Pq(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 ai ∈ alphabet G, Πai = {ai}, so that wi ∈ Πai will be ai. Hence it will be unnecessary to assume that Pai(ai), since this says “ai = ai”, and so is always true.

17 / 21

slide-18
SLIDE 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

slide-19
SLIDE 19

Using Induction on Π in Example

We have already proven that X ⊆ ΠA = L(G), Y ⊆ ΠB and Z ⊆ ΠC. To complete the proof that L(G) = ΠA = X, ΠB = Y and ΠC = Z, we will use induction on Π to prove that ΠA ⊆ X, ΠB ⊆ Y and ΠC ⊆ Z. We use induction on Π to show that: (A) for all w ∈ ΠA, w ∈ X; (B) for all w ∈ ΠB, w ∈ Y ; and (C) for all w ∈ ΠC, w ∈ Z. Formally, this means that we let the properties PA(w), PB(w) and PC(w) be “w ∈ X”, “w ∈ Y ” and “w ∈ Z”, respectively, and then use the induction principle to prove that, for all q ∈ QG, for all w ∈ Πq, Pq(w). But we will actually work more informally.

19 / 21

slide-20
SLIDE 20

Using Induction on Π in Example

There are seven productions to consider.

  • (A → %) We must show that % ∈ X (as “w ∈ X” is the

property of part (A)). And this holds since diff % = 0.

  • (A → 0BA) Suppose w1 ∈ ΠB and w2 ∈ ΠA (as 0BA is the

right-side of the production, and 0 is in G’s alphabet), and assume the inductive hypothesis, w1 ∈ Y (as this is the property of part (B)) and w2 ∈ X (as this is the property of part (A)). We must show that 0w1w2 ∈ X, as the production shows that 0w1w2 ∈ ΠA. Because w1 ∈ Y and w2 ∈ X, we have that diff w1 = 1 and diff w2 = 0. Thus diff(0w1w2) = −1 + 1 + 0 = 0, showing that 0w1w2 ∈ X.

20 / 21

slide-21
SLIDE 21

Using Induction on Π in Example

  • (B → 0BB) Suppose w1, w2 ∈ ΠB, and assume the inductive

hypothesis, w1, w2 ∈ Y . Thus w1 and w2 are nonempty. We must show that 0w1w2 ∈ Y . Clearly, diff(0w1w2) = −1 + 1 + 1 = 1. So, suppose v is a proper prefix of 0w1w2. We must show that diff v ≤ 0. There are three cases to consider.

  • Suppose v = %. Then diff v = 0 ≤ 0.
  • Suppose v = 0u, for a proper prefix u of w1. Because w1 ∈ Y ,

we have that diff u ≤ 0. Thus diff v = −1 + diff u ≤ −1 + 0 ≤ 0.

  • Suppose v = 0w1u, for a proper prefix u of w2. Because

w2 ∈ Y , we have that diff u ≤ 0. Thus diff v = −1 + 1 + diff u = diff u ≤ 0.

  • The remaining productions are handled similarly.

21 / 21