SWEN 224 Formal Foundations of Programming WITH ANSWERS

Similar documents
CS 330 Formal Methods and Models

Finite Automata-cont d

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 2 MODULE, SPRING SEMESTER LANGUAGES AND COMPUTATION ANSWERS

Homework 3 Solutions

Minimal DFA. minimal DFA for L starting from any other

CS 330 Formal Methods and Models Dana Richards, George Mason University, Spring 2016 Quiz Solutions

Convert the NFA into DFA

Let's start with an example:

Chapter Five: Nondeterministic Finite Automata. Formal Language, chapter 5, slide 1

Harvard University Computer Science 121 Midterm October 23, 2012

Assignment 1 Automata, Languages, and Computability. 1 Finite State Automata and Regular Languages

Worked out examples Finite Automata

1 Nondeterministic Finite Automata

CS 311 Homework 3 due 16:30, Thursday, 14 th October 2010

First Midterm Examination

CS 275 Automata and Formal Language Theory

Lecture 08: Feb. 08, 2019

Designing finite automata II

First Midterm Examination

CS 373, Spring Solutions to Mock midterm 1 (Based on first midterm in CS 273, Fall 2008.)

1. For each of the following theorems, give a two or three sentence sketch of how the proof goes or why it is not true.

Formal languages, automata, and theory of computation

CMSC 330: Organization of Programming Languages

CSE : Exam 3-ANSWERS, Spring 2011 Time: 50 minutes

Homework 4. 0 ε 0. (00) ε 0 ε 0 (00) (11) CS 341: Foundations of Computer Science II Prof. Marvin Nakayama

AUTOMATA AND LANGUAGES. Definition 1.5: Finite Automaton

CHAPTER 1 Regular Languages. Contents

CS 330 Formal Methods and Models

Closure Properties of Regular Languages

CS375: Logic and Theory of Computing

CS 330 Formal Methods and Models

Types of Finite Automata. CMSC 330: Organization of Programming Languages. Comparing DFAs and NFAs. NFA for (a b)*abb.

Types of Finite Automata. CMSC 330: Organization of Programming Languages. Comparing DFAs and NFAs. Comparing DFAs and NFAs (cont.) Finite Automata 2

Talen en Automaten Test 1, Mon 7 th Dec, h45 17h30

Some Theory of Computation Exercises Week 1

5. (±±) Λ = fw j w is string of even lengthg [ 00 = f11,00g 7. (11 [ 00)± Λ = fw j w egins with either 11 or 00g 8. (0 [ ffl)1 Λ = 01 Λ [ 1 Λ 9.

Finite-State Automata: Recap

Formal Languages and Automata

Non-deterministic Finite Automata

1 From NFA to regular expression

Intermediate Math Circles Wednesday, November 14, 2018 Finite Automata II. Nickolas Rollick a b b. a b 4

Parse trees, ambiguity, and Chomsky normal form

ɛ-closure, Kleene s Theorem,

Non-Deterministic Finite Automata. Fall 2018 Costas Busch - RPI 1

Regular expressions, Finite Automata, transition graphs are all the same!!

Context-Free Grammars and Languages

CS 301. Lecture 04 Regular Expressions. Stephen Checkoway. January 29, 2018

Nondeterminism and Nodeterministic Automata

Name Ima Sample ASU ID

CSCI 340: Computational Models. Kleene s Theorem. Department of Computer Science

3 Regular expressions

The University of Nottingham

Regular Expressions (RE) Regular Expressions (RE) Regular Expressions (RE) Regular Expressions (RE) Kleene-*

1. For each of the following theorems, give a two or three sentence sketch of how the proof goes or why it is not true.

Scanner. Specifying patterns. Specifying patterns. Operations on languages. A scanner must recognize the units of syntax Some parts are easy:

Grammar. Languages. Content 5/10/16. Automata and Languages. Regular Languages. Regular Languages

a,b a 1 a 2 a 3 a,b 1 a,b a,b 2 3 a,b a,b a 2 a,b CS Determinisitic Finite Automata 1

More on automata. Michael George. March 24 April 7, 2014

Overview HC9. Parsing: Top-Down & LL(1) Context-Free Grammars (1) Introduction. CFGs (3) Context-Free Grammars (2) Vertalerbouw HC 9: Ch.

1.3 Regular Expressions

Exercises Chapter 1. Exercise 1.1. Let Σ be an alphabet. Prove wv = w + v for all strings w and v.

Lecture 09: Myhill-Nerode Theorem

Chapter 2 Finite Automata

1.4 Nonregular Languages

input tape head moves current state

CS 275 Automata and Formal Language Theory

CS415 Compilers. Lexical Analysis and. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

CMPSCI 250: Introduction to Computation. Lecture #31: What DFA s Can and Can t Do David Mix Barrington 9 April 2014

Theory of Computation Regular Languages. (NTU EE) Regular Languages Fall / 38

80 CHAPTER 2. DFA S, NFA S, REGULAR LANGUAGES. 2.6 Finite State Automata With Output: Transducers

CHAPTER 1 Regular Languages. Contents. definitions, examples, designing, regular operations. Non-deterministic Finite Automata (NFA)

CS 275 Automata and Formal Language Theory

CS 310 (sec 20) - Winter Final Exam (solutions) SOLUTIONS

Theory of Computation Regular Languages

Formal Language and Automata Theory (CS21004)

NFAs continued, Closure Properties of Regular Languages

CISC 4090 Theory of Computation

Table of contents: Lecture N Summary... 3 What does automata mean?... 3 Introduction to languages... 3 Alphabets... 3 Strings...

Non Deterministic Automata. Linz: Nondeterministic Finite Accepters, page 51

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

PART 2. REGULAR LANGUAGES, GRAMMARS AND AUTOMATA

Converting Regular Expressions to Discrete Finite Automata: A Tutorial

Thoery of Automata CS402

Tutorial Automata and formal Languages

FABER Formal Languages, Automata and Models of Computation

Finite Automata. Informatics 2A: Lecture 3. Mary Cryan. 21 September School of Informatics University of Edinburgh

Finite Automata. Informatics 2A: Lecture 3. John Longley. 22 September School of Informatics University of Edinburgh

Java II Finite Automata I

Compiler Design. Fall Lexical Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Non-deterministic Finite Automata

Fundamentals of Computer Science

Formal Languages and Automata Theory. D. Goswami and K. V. Krishna

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 2 MODULE, SPRING SEMESTER MACHINES AND THEIR LANGUAGES ANSWERS

NFAs and Regular Expressions. NFA-ε, continued. Recall. Last class: Today: Fun:

Lecture 6 Regular Grammars

Lexical Analysis Finite Automate

12.1 Nondeterminism Nondeterministic Finite Automata. a a b ε. CS125 Lecture 12 Fall 2016

Deterministic Finite Automata

Languages & Automata

CSCI 340: Computational Models. Transition Graphs. Department of Computer Science

Transcription:

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