T E W H A R E W Ā N A N G A O T E Ū P O K O O T E I K A A M Ā U I VUW V I C T O R I A UNIVERSITY OF WELLINGTON Time Allowed: 3 Hours EXAMINATIONS 2011 END-OF-YEAR SWEN 224 Forml Foundtions of Progrmming WITH ANSWERS Instructions: Answer ll four questions. The exm will e mrked out of one hundred nd eighty (180). Clcultors ARE NOT ALLOWED. Non-electronic foreign lnguge dictionries re llowed. No other reference mteril is llowed. SWEN 224 continued...
Question 1. Assertions nd Verifiction [60 mrks] () [15 mrks : 5 for ech prt] Write n ssertion formlising ech of the following sttements, where A is n rry of size N. You my use either JML or ordinry mthemticl nottion. (i) All elements of A re different. i, j : 0 i, j < N i = j : A[i] = A[j] or i, j : 0 i < j < N : A[i] = A[j] (ii) A contins exctly one occurrence of z. i : 0 i < N : A[i] = z ( j : 0 j < N i = j : A[j] = z) or {i 0 i < N A[i] = z = 1 (iii) Between ny two occurrences of z in A, there is t lest one occurrence of y. i, j : 0 i < j < N : A[i] = z A[i] = z ( k : i < k < j : A[j] = y) () [15 mrks : 5 for ech prt] For ech of the following correctness ssertions, write down the verifiction condition(s) tht must hold in order for the correctness ssertion to e vlid, nd give rief explntion of why these verifiction conditions hold. (i) { k = 0s := 0{s = k 1 i=0 A[i] k = 0 0 = k 1 i=0 A[i] This holds ecuse 1 i=0 A[i] = 0. (ii) { x yif x > z then x := z else skip fi{x y x z x y x > z z y z z This holds ecuse: x y x > z implies z y (y trnsitivity), nd z z lwys holds. x y (x > z) x y x z The holds ecuse x y implies x y, nd (x > z) is equivlent to x z. (iii) { 0 k < n 1 s = k 1 i=0 A[i]k := k+1; s := s+a[k]{0 k < n s = k 1 i=0 A[i] 0 k < n s = k 1 i=0 A[i] k = n 1 0 k+1 < n s+[k+1] = k i=0 A[i] This holds ecuse: 0 k implies 0 k+1 k < n k = n 1 implies k+1 < n s = k 1 i=0 A[i] implies s+[k+1] = k i=0 A[i] (0 k < n k = n 1 ensures tht [k+1] is well-defined) SWEN 224 2 continued...
(c) Consider the following Jv method, which counts the numer of times 0 occurs in n integer rry A. //@ requires A!= null; //@ ensures \result == (\num_of int k; 0 <= k && k < A.length; A[k] == 0); int countzeroes(int[] A) { int i = 0; int c = 0; while (i < A.length) { if ( A[i] == 0 ) c = c + 1; i = i + 1; return c; (i) [4 mrks] Therequires ndensures nnottions give the pre nd postconditions for the method. Wht re the pre nd postconditions for the loop? Precondition: //@ ssert A!= null && i == 0 && c == 0; Postcondition: //@ ssert c == (\num_of int k; 0 <= k && k < A.length; A[k] == 0); (ii) [5 mrks] Give loop invrint tht cn e used to verify the loop in this method. /*@ loop_invrint A!= null && 0 <= i && i <= A.length && c == (\num_of int k; 0 <= k && k < i; A[k] == 0); @*/ (iii) [15 mrks] Stte the three verifiction conditions tht must e proved in order to show tht the loop is prtilly correct, nd give rief rgument to show tht ech of them holds. (You my use ordinry mthemticl nottion insted of JML if you prefer.) The precondition for the loop implies the loop invrint. A = null i = 0 c = 0 A = null 0 i i A.length c = (count k : 0 k k < i : A[k] = 0) c = (count k : 0 k k < i : A[k] = 0) holds ecuse c = 0, i = 0, nd count over n empty rnge is 0. The other conjuncts follow directly from the precondition, nd the fct tht rry size is nturl numer. The loop invrint is preserved when the loop test holds. When the if test holds: A = null 0 i i A.length c = (count k : 0 k k < A.length : A[k] = 0) i < A.length A[i] = 0 A = null 0 i+1 i+1 A.length c+1 = (count k : 0 k k < i+1 : A[k] = 0) SWEN 224 3 continued...
This holds, ecuse (count k : 0 k k < i+1 : A[k] = 0) = (count k : 0 k k < A.length : A[k] = 0)+1 when A[i] = 0. When the if test fils: A = null 0 i i A.length c = (count k : 0 k k < i : A[k] = 0) i < A.length A[i] = 0 A = null 0 i+1 i+1 A.length c = (count k : 0 k k < i+1 : A[k] = 0) This holds, ecuse (count k : 0 k k < i+1 : A[k] = 0) = (count k : 0 k k < A.length : A[k] = 0) when A[i] = 0. The loop invrint implies the postcondition when the loop test fils. A = null 0 i i A.length c = (count k : 0 k k < i : A[k] = 0) (i < A.length) c = (count k : 0 k k < A.length : A[k] = 0) This holds ecuse (count k : 0 k k < i : A[k] = 0) = (count k : 0 k k < A.length : A[k] = 0) when i = A.length. (iv) [6 mrks] Give rief rgument to show tht the method termintes properly, i.e. tht it does not give run-time error or exception nd does not loop forever. (You my ignore the possiility of rithmetic overflow.) The only wy the progrm could get run-time error (ignoring the possiility of rithmetic overflow) is y getting n rry index error. This cnnot occur, however, ecuse the loop invrint nd loop test ensure tht 0 i < A.length in the if sttement. The progrm cnnot loop forever ecuse it termintes fter A.length itertions. We cn prove this formlly using A.length i s loop vrint. This vlue decreses on ech itertion of the loop, nd cnnot go negtive, since the loop will exit when it ecomes zero. SWEN 224 4 continued...
Question 2. Alloy [20 mrks] Consider the following Alloy model for Nondeterministic Finite Acceptors: sig Symol { sig Stte { sig NFA { initil: Stte, next: Stte -> Symol -> Stte, finl: set Stte sig Config { stte: Stte, input: seq Symol pred move[m: NFA, c1, c2: Config] { c2.stte = m.next[c1.stte][c1.input.first] && c2.input = c1.input.rest pred ccepts[m: NFA, s: seq Symol] { some ss: seq Config ss.first.stte = m.initil && ss.lst.stte in m.finl && (ll i: ss.inds-#ss move[m, ss[i], ss[i+1]]) () [3 mrks] Write predicte to determine whether n NFA hs no finl stte. pred nofinl[m: NFA] { no m.finl () [3 mrks] Write predicte to determine whether n NFA hs t lest two finl sttes. pred twoplusfinls[m: NFA] { #m.finl <= 2 (c) [3 mrks] Write predicte to determine whether n NFA is deterministic. SWEN 224 5 continued...
pred isdeterministic[m: NFA] { ll s: Stte ll : Symol #m.next[s][] <= 1 (d) [3 mrks] Write predicte to determine whether n NFA ccepts the empty string. pred cceptsempty[m: NFA] { m.initil in m.finl (e) [3 mrks] Write predicte to determine whether the lnguges ccepted y two NFAs hve ny strings in common. pred ccceptnondisjointlnguges[m1: NFA, m2: NFA] { some s: seq Symol ccepts[m1, s] nd ccepts[m2, s] (f) [5 mrks] Write predicte to determine whether two NFAs hs ny common sttes; i.e. sttes tht re rechle from the initil sttes of oth mchines. pred rechle[m: NFA, s: Stte] { some ss: seq Stte ss.first = s && ss.lst = m.initil && (ll i: ss.inds-#ss some : Symol ss[i+1] in m.next[ss[i]][]) pred CommonSttes[m1, m2: NFA] { some s: Stte rechle[m1, s] nd rechle[m1, s] or: pred CommonSttes [m1, m2: NFA] { some s: Stte some ss1, ss2: seq Stte ss1.first = m1.initil && ss1.lst = s && ss2.first = m1.initil && ss2.lst = s && (ll i: ss1.inds-#ss1 some : Symol ss1[i+1] in m1.next[ss1[i]][]) && (ll i: ss2.inds-#ss1 some : Symol ss1[i+1] in m2.next[ss1[i]][]) SWEN 224 6 continued...
Question 3. Regulr Lnguges [55 mrks] () [8 mrks : 4 for ech prt] Write Regulr Expression or Regulr Grmmr to descrie ech of the following lnguges, over the lphet{,, c: (i) All strings in which ll s come efore ll s nd ll c s. ( c) (ii) All strings of even length in which ll s come efore ll s. (( c)( c)) (( c)( c)) ( c)(( c)( c)) ( c)(( c)( c)) or: (( c)( c)) ( λ)(( c)( c)) () Consider the NFA M = (Q, q I, A, N, F), where: Q = {1, 2, 3, 4, 5 q I = 1 A = {, N = {(1,, 1),(1,, 2),(1,, 1),(1,, 3),(2,, 4),(2,, 2), (3,, 3),(3,, 5),(4,, 4),(4,, 4),(5,, 5),(5,, 5) F = {4, 5 (i) [4 mrks] Drw trnsition digrm for M.,, 2 4 1, 3 5 (ii) [4 mrks] Show the sequence of configurtions tht M psses through in ccepting the input. Note tht you should show ll sttes tht M my e in fter ccepting prt of the input. Sttes Input {1 {1, 2 {1, 2, 3 {1, 2, 3, 4 {1, 2, 3, 4, 5 {1, 2, 3, 4, 5 Accept SWEN 224 7 continued...
(iii) [4 mrks] Write regulr expression descriing the lnguge ccepted y M. ( ) ( )( ) (i.e. ll strings over s nd s with either (t lest) two two s.) (iv) [8 mrks] Drw trnsition digrm for the DFA otined y pplying the suset construction to M. Show the correspondence etween sttes of the DFA nd those of the NFA. You only need to show rechle sttes. 1 2 4 3 5,, The stte correspondence is: DFAStte NFASttes 1 {1 2 {1, 2 3 {1, 2, 3 4 {1, 2, 3, 4 5 {1, 2, 3, 4 (c) [10 mrks] Given n NFA M = (Q, q I, A, N, F), show how to construct n NFA, M = (Q, q I, A, N, F ), which ccepts the lnguge consisting of ll strings in L(M) enclosed in pir of s. For exmple, if L(M) = {,,, then L(M ) = {,,, nd if L(M) = {λ,, c, c, then L(M ) = {,, c, c. Give rief rgument to show tht the resulting NFA does in fct ccept the required lnguge. M = (Q, q I, A, N, F ) where: Q = Q {q 1, q 2 ), where q 1 nd q 2 re new sttes, not in Q q I = q 1 A = A { N = N {(q 1,, q I ) {(q f,, q 2 ) q f F F = {q 2 M ccepts string γ iff M cn trverse pth from q I to some stte q n F, while consuming γ. But in this cse, M cn consume γ while pssing through sttes q 1, q I,..., q f, q 2, since M hs ll of the trnsitions of M, nd hs trnsitions(q 1,, q I), nd(q f,, q 2 ). So M ccepts γ. Similrly, M ccepts string γ iff γ is of the form γ, where γ is consumed on pth from q I to some stte q f F, since the only trnsition out of q 1 is (q 1,, q I), nd the only trnsitions leding to q 2 re of the form (q f,, q 2 ). But in this cse, M cn consume α while trversing the sme pth from q I to q f F. So M ccepts γ. (d) [6 mrks] Drw trnsition digrm for n NFA ǫ tht ccepts the lnguge defined y the regulr expression ( c) ( ) c. SWEN 224 8 continued...
,c 2 4 1, c 3 5 (e) [3 mrks] Explin riefly why llowing null trnsition mkes it esier to construct n NFA from regulr expression. Allowing null trnsitions llows us to uild the NFA in modulr wy, using null trnsitions to glue together NFAs uilt from the suexpressions. (f) [8 mrks] Prove tht, for ny regulr expressions x, y nd z, x(y z) = xy xz. We hve to show tht L(x(y z)) = L(xy xz). We prove this s follows: L(x(y z)) = L(x) (L(y) L(z)) Definition of L = {s v s L(x) v (L(y) L(z)) defn of = {s v s L(x) (v L(y) v L(z)) defn of = {s v (s L(x) v L(y)) (s S v L(z)) distriutes over = {s v s L(x) v L(y) {s v s L(x) v L(z) defn of = (L(x) L(y)) (L(x) L(z)) defn of = L(xy xz) Definition of L (The min prt of this proof ws given in the solutions to Assignment 4.) SWEN 224 9 continued...
Question 4. Context-Free Lnguges [45 mrks] () [10 mrks : 5 for ech prt] Write Context Free Grmmr to descrie ech of the following lnguges. You re not required to give full forml definition of these grmmrs just write the list of rules. (i) All strings consisting of one or more d s, optionlly followed y n nd one or more further d s. For exmple, d, ddd, dd nd dddd re in this lnguge, ut dd, dd nd dd re not. S T T T T d d T or S T U T d d T U λ T (ii) All strings formed y conctenting two non-empty plindromes over{,, c, where plindrome is strings tht reds the sme forwrds nd ckwrds (e.g.,,, cccc). Thus,,,, nd cccc re in this lnguge, ut,, c nd cc re not. S T T T U T T c T c U λ c () Consider the following grmmr, where!, $,,, ( nd ) re terminl symols: S T! T T!! (1, 2) T T $ T U (3, 4) U ( T) (5, 6, 7) (i) [3 mrks] Construct prse tree for $!. Write the numer of the rule pplied eside ech nonterminl in the prse tree; likewise for prts (ii) nd (iii). S (1) T (3)! T (4) $ T (4) U (5) U (6) SWEN 224 10 continued...
(ii) [4 mrks] Construct prse tree for ( $ )!!. S (2) T (4) T (4)!! U (7) U (5) ( T (3) ) T (4) $ T (4) U (5) U (6) (iii) [6 mrks] Demonstrte tht the grmmr is miguous y drwing two differrent prse trees for $ $!. S (1) S (1) T (3)! T (3)! T (3) $ T (4) T (4) $ T (3) T (4) $ T (4) U (5) U (5) T (4) $ T (4) U (5) U (6) U (5) U (6) (iv) [6 mrks] Write n equivlent, non-miguous grmmr, treting $ s left-ssocitive. S T! T T!! (1, 2) T T $ U U (3, 4) U ( T) (5, 6, 7) (v) [8 mrks] Write n equivlent LL(1) grmmr, nd show tht it is LL(1). SWEN 224 11 continued...
S T S (1) S! T!! (2, 3) T U T (3, 4) T λ $ T (5, 6) U ( T) (7, 8, 9) In the definition of S, first(!) = {! nd first(t!!) = {,,(, which re disjoint. In the definition of T, first(λ) = { nd first($t ) = {$, which re disjoint. In the definition of U, first() = {, first() = { nd first((t)) = {(, which re disjoint. T λ, so we hve to show tht first(t ) follow(t ) =. first(t ) = {$ nd follow(t ) = {!,), which re indeed disjoint. (c) [8 mrks] Prove tht the union of two context-free lnguges is context-free. Hint: Recll tht lnguge is context-free if nd only if it cn e defined using contextfree grmmr. Suppose lnguges L 1 nd L 2 re defined y CFG s G 1 = (N 1, T 1, S 1, P 1 ) nd G 2 = (N 2, T 2, S 2, P 2 ), we show how to construct grmmr G 3 which defines L 3 = L 1 L 2. First, we renme nonterminls to ensure tht N 1 nd N 2 re disjoint. We cn do this ecuse renming nonterminls does not lter the lnguge defined y grmmr. The required grmmr hs ll of the terminls, non-terminls nd rules of the given grmmrs, long with one new nonterminl, S 3, which is the strt symol, nd one new rule, llowing S 3 to e rewritten s either S 1 or S 2, i.e.: G 3 = (N 1 N 2 {S 3, T 1 T 2, S 3, P 1 P 2 {S 3 S 1 S 2 ) To show tht L(G 3 ) = L 1 L 2, we must show tht for ny string α (V T V T ) there is prse tree for α from G 3 iff there is prse tree for α from G 1 or from G 2. Suppose tht α is in L 1, then there must e prse tree T for α from G 1, with S 1 s its root, nd α s its fringe. But in tht cse, we cn extend T to otin prse tree from α from G 3, y dding new root, lelled S 3, with single edge to the root of T. This is prse tree from G 3 ecuse it hs S 3 s its root; the edge under the root corresponds to n ppliction of the rule S 3 S 1, nd the rest of the tree uses rules form G 1 which re lso in G 3. Similrly, if α is in L 2, there is prse tree from α from G 2, which cn e extended to otin prse tree for α from G 3 y dding n ppliction of the rule S 3 S 2. In the other direction, suppose tht T is prse tree for α from G 3. Since the only rule in G 3 with S 3 on the left hnd side is S 3 S 1 S 2, the root of T hs single child, lelled either S 1 or S 2. Thus, if we delete the root nd tht edge, the resulting tree is either prse tree for α from G 1 or prse tree for α from G 2, so α is either in L 1 or in L 2, i.e. it is in L 1 L 2. ******************************** SWEN 224 12