Appendix a Answers to Some of the Exercises
Total Page:16
File Type:pdf, Size:1020Kb
Appendix A Answers to Some of the Exercises Answers for Chapter 2 2. Let f (x) be the number of a 's minus the number of b 's in string x. A string is in the language defined by the grammar just when • f(x) = 1 and • for every proper prefix u of x, f( u) < 1. 3. Let the string be given as array X [O .. N -1] oflength N. We use the notation X ! i, (pronounced: X drop i) for 0:::; i :::; N, to denote the tail of length N - i of array X, that is, the sequence from index ion. (Or: X from which the first i elements have been dropped.) We write L for the language denoted by the grammar. Lk is the language consisting of the catenation of k strings from L. We are asked to write a program for computing the boolean value X E L. This may be written as X ! 0 E L1. The invariant that we choose is obtained by generalizing constants 0 and 1 to variables i and k: P : (X E L = X ! i ELk) /\ 0:::; i :::; N /\ 0:::; k We use the following properties: (0) if Xli] = a, i < N, k > 0 then X ! i E Lk = X ! i + 1 E Lk-1 (1) if Xli] = b, i < N, k ~ 0 then X ! i E Lk = X ! i + 1 E Lk+1 (2) X ! N E Lk = (k = 0) because X ! N = 10 and 10 E Lk for k = 0 only (3) X ! i E LO = (i = N) because (X ! i = 10) = (i = N) and LO = {€} The program is {N ~ O} k := Ij i:= OJ {P} *[ k =1= 0/\ i =1= N -+ [ Xli] = a -+ k := k - 1 404 Appendix A. Answers to Some of the Exercises I Xli] = b -+ k := k + 1 ]; i:= i + 1 { P } ]; {P 1\ (k = 0 V i = N) } [k = 01\ i = N -+ write('yes') ~ k > 0 V i < N -+ write('no')] The initialization establishes P. The invariance of P follows from properties (0) and (1). Every step increases integer i, and since i is bounded by N, the loop terminates. It does so in a state in which invariant P holds and in which the negation of the guard holds. On account of properties (2) and (3), and in conjunction with P and the negation of the guard, the final statement produces the correct result. 4. We write ===}k for a k -step derivation. • induction hypothesis: (H ===}k x) = (alt(x) 1\ Ixl = k) • base case, k = 1 : H ===}1 X = { the only production rule that finishes in one step is H~a } x=a = { one string x of length 1 satisfies alt(x) } alt(x) 1\ Ixl = 1 • step, k> 1: H ===}k X = { the only production rule that does not finish in one step is H --+ H b H } (3y, z, i,j :: H ===}i Y 1\ H ===}j z 1\ x = ybz 1\ i ;::: 1 1\ j;::: 1 1\ i + j = k - 1) = { induction hypothesis } (3y, z, i,j :: alt(y) 1\ IYI = i 1\ alt(z) 1\ Izl = j 1\ x = ybz 1\ i;::: 1 1\ j;::: 1 1\ i + j = k - 1) = { property of alt } alt(x) 1\ Ixl = k In our two proofs, each step expresses equality of two conditions, not just implication in one direction. Often, equality of conditions is expressed more easily in formulae than in English. It is a pain in the neck having to say "if and only if" or "just when" all the time. 8. Yes, there is a problem: the grammar is ambiguous. For example, iii x i can be parsed as i/{i x i) or as (iii) x i. Appendix A. Answers to Some of the Exercises 405 10. A grammar that generates every string that has twice as many a 's as b 's is {{a, b}, {S,B}, {S ~ aaSB IE, aB ~ Ba,B ~ b}, S). 12. The grammar is {{a}, {B,C,D,R,L,E}, rules, aBRCD) where the (large) set of rules is BR ~E EC ~E ED ~E RC ~ aaCR Ca ~aC Ba ~aB RD ~LCD CL ~LC BL ~aBR First we prove that each string of the form an2 is in the language, for n ~ 1. We do so by proving by mathematical induction that an2 BRcn D can be derived, that is, aBRCD =::}* an2 BRcn D. Ba sis n = 1 follows from the reflexivity of =::}* . For all n ~ 1, we have aBRCD ==> * {induction hypothesis } an2 BRCnD ==> * {n applications of RC ~ aaCR } an2 B( aaC)n RD =::} * {n( n - 1) applications of Ca ~ aC } an2 Ba2n cnRD ==> * {2n applications of Ba ~ aB } an2 +2n Bcn RD =::} {RD ~ LCD } an2 +2n Bcn LCD =::} * {n applications of CL ~ LC } an2 +2n BLcn+! D =::} {BL ~ aBR } an2+2n+l BRcn+! D Thus the induction hypothesis holds for n + 1 also. We conclude that 2 aBRCD ==>* an BRcn D for all n ~ 1. On account of BR ~ 406 Appendix A. Answers to Some of the Exercises E we then have aBRCD ===** an2 Ecn D, and n applications of EC ---+ E yields aBRCD ===** an2 ED, and finally ED ---+ f yields 2 aBRCD ===** an , for all n ~ 1. Next, we prove that no other strings can be derived from the gram mar. For this part of the proof we claim that each derivable string is of one of the four forms: where n ~ 1, 0 ~ i ~ n. X is an interleaving of a2i and BCi , whereas Y is an interleaving of a2n and BCi . The proof is by mathematical induction on the length of the derivation. For length o the claim is true because aBRCD is of the first form (with n = 1, i = 0). Now, assume the hypothesis holds for some derived string. We show that applying anyone of the production rules yields a string of one of the four forms, thus showing that the claim holds for the longer derivation. BR ---+ E applies to the first form if i = 0, that is, an2 BRcn D, yielding a string of the third form EC ---+ E applies to the third form if n > i, yielding a string of the same form with i := i + 1 ED ---+ f applies to the third form if n = i, yielding a string of the fourth form RC ---+ aaCR applies to the first form if n > i, yielding a string of the same form with i := i + 1 Ca ---+ aC, Ba ---+ aB apply to the first or second form, yielding a string of the same form with X or Y a bit permuted RD ---+ LCD applies to the first form if n = i, yielding a string of the second form CL ---+ LC applies to the second form if i > 0 yielding a string of the same form with i := i-I BL ---+ aBR applies to the second form if i = 0 yielding a string of the first form with n:= n + 1 We have now established that each string that can be derived from the initial string has some special form, given above. The only strings of this form that are in the language are the strings containing terminal symbols only. Of the four forms above, only the last one qualifies, hence each string in the language is of the form an2 , for some n ~ 1. This completes the second part of the proof. Here is an alternative solution. First generate syn xn E and observe that the number of X - Y inversions is n2 • Add rule YX ---+ Xa Y Appendix A. Answers to Some of the Exercises 407 plus "movers" like Ya -+ aY and aX -+ Xa, and "terminators" SX -+ S and YE -+ E. 14. The grammar is ({a,b}, {A,B,M,E}, R, ME) where R is the set M ---+ aMA I bMB I E AE ---+ aE BE ---+ bE Aa ---+ aA Ab ---+ bA Ba ---+ aB Bb ---+ bB E ---+ E of production rules. (One may think of this as a program that generates sequences of the form xMyE where x is a sequence of a's and b 's, and y is a sequence of a's, b's, A's and B's. The relation between x and y is: the lower-case letters in y form the beginning of x and the capital letters in y form the remainder of x, but in reverse order and written in capitals. Capitals A and B can travel to the right from token M to end E, where they are converted to lower-case.) First, we prove that each string of the form xx can be derived. This we do by giving a derivation of each such string. We prove by mathematical induction on n that every string of the form xMxE where x E {a, b}n can be derived from the initial string, that is, ME ~. xMxE. Basis n = 0 follows from the reflexivity of ~ •. For all n ~ 0, we have ME ~* {induction hypothesis } xMxE ~ { application of M ---+ aMA } xaMAxE ===} * {n applications of Aa ---+ aA and Ab ---+ bA } xaMxAE ~ { application of AE ---+ aE } xaMxaE and similarly ME ~ * xbMxbE. Thus the induction hypothesis holds for strings of length n + 1 also. We conclude that ME ~* 408 Appendix A. Answers to Some of the Exercises xMxE for all x. On account of M --+ € and E --+ € we then have ME ==>* xx, for all x over {a, b}. Next, we prove that no other strings can be derived from the gram mar. For this part of the proof we claim that each derivable string is of the form xMiyEi where 0 ~ i,j ~ 1, and x = J(y) g(y).