SWEN 224 Formal Foundations of Programming

Similar documents
EXAMINATIONS 2009 END-OF-YEAR. COMP 202 / SWEN 202 Formal Methods of Computer Science / Formal Foundations of Software Engineering

EXAMINATIONS 2009 MID-TERM TEST. COMP 202 / SWEN 202 Formal Methods of Computer Science / Formal Foundations of Software Engineering WITH ANSWERS

Compiler Construction

CS 44 Exam #2 February 14, 2001

Theory Bridge Exam Example Questions Version of June 6, 2008

Theory of Computation Dr. Weiss Extra Practice Exam Solutions

CMPSCI 250: Introduction to Computation. Lecture #28: Regular Expressions and Languages David Mix Barrington 2 April 2014

CS 164 Handout 11. Midterm Examination. There are seven questions on the exam, each worth between 10 and 20 points.

Automata Theory TEST 1 Answers Max points: 156 Grade basis: 150 Median grade: 81%

Multiple Choice Questions

Skyup's Media. PART-B 2) Construct a Mealy machine which is equivalent to the Moore machine given in table.

(a) R=01[((10)*+111)*+0]*1 (b) ((01+10)*00)*. [8+8] 4. (a) Find the left most and right most derivations for the word abba in the grammar

QUESTION BANK. Formal Languages and Automata Theory(10CS56)

Programming Languages Third Edition

CS 403 Compiler Construction Lecture 3 Lexical Analysis [Based on Chapter 1, 2, 3 of Aho2]

Ambiguous Grammars and Compactification

CSCE 314 Programming Languages

Theory of Computation, Homework 3 Sample Solution

CSE 311 Lecture 21: Context-Free Grammars. Emina Torlak and Kevin Zatloukal

Midterm I - Solution CS164, Spring 2014

Compiler Design Aug 1996

CSE 431S Scanning. Washington University Spring 2013

Formal Languages and Automata

Last lecture CMSC330. This lecture. Finite Automata: States. Finite Automata. Implementing Regular Expressions. Languages. Regular expressions

CSE au Final Exam Sample Solution

1. [5 points each] True or False. If the question is currently open, write O or Open.

Midterm Exam. CSCI 3136: Principles of Programming Languages. February 20, Group 2

Regular Languages and Regular Expressions

Finite automata. We have looked at using Lex to build a scanner on the basis of regular expressions.

1. (10 points) Draw the state diagram of the DFA that recognizes the language over Σ = {0, 1}

Non-deterministic Finite Automata (NFA)

University of Nevada, Las Vegas Computer Science 456/656 Fall 2016

JNTUWORLD. Code No: R

I have read and understand all of the instructions below, and I will obey the Academic Honor Code.

Implementation of Lexical Analysis

Part I: Multiple Choice Questions (40 points)

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08008 INFORMATICS 2A: PROCESSING FORMAL AND NATURAL LANGUAGES

Languages and Finite Automata

Formal Languages and Grammars. Chapter 2: Sections 2.1 and 2.2

UNIT I PART A PART B

Final Exam 2, CS154. April 25, 2010

CSE P 501 Exam 8/5/04

14.1 Encoding for different models of computation

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: CS2352 Principles of Compiler Design Year/Sem : III/VI

Solutions to Homework 10

Gujarat Technological University Sankalchand Patel College of Engineering, Visnagar B.E. Semester VII (CE) July-Nov Compiler Design (170701)

CpSc 421 Final Solutions

Glynda, the good witch of the North

ECS 120 Lesson 7 Regular Expressions, Pt. 1

Where We Are. CMSC 330: Organization of Programming Languages. This Lecture. Programming Languages. Motivation for Grammars

Lexical Analysis. Dragon Book Chapter 3 Formal Languages Regular Expressions Finite Automata Theory Lexical Analysis using Automata

Midterm Exam II CIS 341: Foundations of Computer Science II Spring 2006, day section Prof. Marvin K. Nakayama

CSE 401 Midterm Exam 11/5/10

Languages and Compilers

CS402 Theory of Automata Solved Subjective From Midterm Papers. MIDTERM SPRING 2012 CS402 Theory of Automata

Implementation of Lexical Analysis

We use L i to stand for LL L (i times). It is logical to define L 0 to be { }. The union of languages L and M is given by

CSE 413 Final Exam. December 13, 2012

CMSC 330: Organization of Programming Languages. Architecture of Compilers, Interpreters

Midterm I (Solutions) CS164, Spring 2002

CMSC 330: Organization of Programming Languages

Implementation of Lexical Analysis

CSE P 501 Exam 11/17/05 Sample Solution

ITEC2620 Introduction to Data Structures

Implementation of Lexical Analysis. Lecture 4

Chapter 3: Lexing and Parsing

Question Bank. 10CS63:Compiler Design

Lexical Analysis. COMP 524, Spring 2014 Bryan Ward

Lec-5-HW-1, TM basics

EXAMINATIONS 2012 END-OF-YEAR SWEN222. Software Design. Question Topic Marks 1. Design Quality Design Patterns Design by Contract 12

Introduction to Lexical Analysis

The Turing Machine. Unsolvable Problems. Undecidability. The Church-Turing Thesis (1936) Decision Problem. Decision Problems

CS402 - Theory of Automata FAQs By

CS154 Midterm Examination. May 4, 2010, 2:15-3:30PM

2068 (I) Attempt all questions.

Last time. Reasoning about programs. Coming up. Project Final Presentations. This Thursday, Nov 30: 4 th in-class exercise

Reasoning about programs

Assignment No.4 solution. Pumping Lemma Version I and II. Where m = n! (n-factorial) and n = 1, 2, 3

QUESTION BANK. Unit 1. Introduction to Finite Automata

LR Parsing. The first L means the input string is processed from left to right.

CHAPTER TWO LANGUAGES. Dr Zalmiyah Zakaria

Automata & languages. A primer on the Theory of Computation. The imitation game (2014) Benedict Cumberbatch Alan Turing ( ) Laurent Vanbever

8 ε. Figure 1: An NFA-ǫ

Theory of Computations Spring 2016 Practice Final Exam Solutions

Lexical Analysis - 2

CMSC 330: Organization of Programming Languages

Architecture of Compilers, Interpreters. CMSC 330: Organization of Programming Languages. Front End Scanner and Parser. Implementing the Front End

Specifying Syntax COMP360

R10 SET a) Construct a DFA that accepts an identifier of a C programming language. b) Differentiate between NFA and DFA?

Lexical Analyzer Scanner

EXAMINATIONS 2011 END-OF-YEAR. SWEN 102 Introduction to Software Modelling. Question Topic Marks. 1. Use Case Descriptions 30

Lexical Analyzer Scanner

CS/ECE 374 Fall Homework 1. Due Tuesday, September 6, 2016 at 8pm

Context-Free Languages and Parse Trees

CMSC 330: Organization of Programming Languages. Context Free Grammars

University of Technology Department of Computer Sciences. Final Examination st Term. Subject:Compilers Design

Verifying Java Programs Verifying Java Programs with KeY

1 Parsing (25 pts, 5 each)

Lexical Analysis. Chapter 2

CMSC330 Spring 2017 Midterm 2

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 EXAMINATIONS 2011 END-OF-YEAR SWEN 224 Formal Foundations of Programming Time Allowed: 3 Hours Instructions: Answer all four questions. The exam will be marked out of one hundred and eighty (180). Calculators ARE NOT ALLOWED. Non-electronic foreign language dictionaries are allowed. No other reference material is allowed. SWEN 224 continued...

Question 1. Assertions and Verification [60 marks] (a) [15 marks : 5 for each part] Write an assertion formalising each of the following statements, where A is an array of size N. You may use either JML or ordinary mathematical notation. (i) All elements of A are different. (ii) A contains exactly one occurrence of z. (iii) Between any two occurrences of z in A, there is at least one occurrence of y. (b) [15 marks : 5 for each part] For each of the following correctness assertions, write down the verification condition(s) that must hold in order for the correctness assertion to be valid, and give a brief explanation of why these verification conditions hold. (i) { k = 0s := 0{s = k 1 i=0 A[i] (ii) { x yif x > z then x := z else skip fi{x y 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] (c) Consider the following Java method, which counts the number of times 0 occurs in an integer array 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 marks] Therequires andensures annotations give the pre and postconditions for the method. What are the pre and postconditions for the loop? (ii) [5 marks] Give a loop invariant that can be used to verify the loop in this method. (iii) [15 marks] State the three verification conditions that must be proved in order to show that the loop is partially correct, and give a brief argument to show that each of them holds. (You may use ordinary mathematical notation instead of JML if you prefer.) (iv) [6 marks] Give a brief argument to show that the method terminates properly, i.e. that it does not give a run-time error or exception and does not loop forever. (You may ignore the possibility of arithmetic overflow.) SWEN 224 2 continued...

Question 2. Alloy [20 marks] Consider the following Alloy model for Nondeterministic Finite Acceptors: sig Symbol { sig State { sig NFA { initial: State, next: State -> Symbol -> State, final: set State sig Config { state: State, input: seq Symbol pred move[m: NFA, c1, c2: Config] { c2.state = m.next[c1.state][c1.input.first] && c2.input = c1.input.rest pred accepts[m: NFA, s: seq Symbol] { some ss: seq Config ss.first.state = m.initial && ss.last.state in m.final && (all i: ss.inds-#ss move[m, ss[i], ss[i+1]]) (a) [3 marks] Write a predicate to determine whether an NFA has no final state. (b) [3 marks] Write a predicate to determine whether an NFA has at least two final states. (c) [3 marks] Write a predicate to determine whether an NFA is deterministic. (d) [3 marks] Write a predicate to determine whether an NFA accepts the empty string. (e) [3 marks] Write a predicate to determine whether the languages accepted by two NFAs have any strings in common. (f) [5 marks] Write a predicate to determine whether two NFAs has any common states; i.e. states that are reachable from the initial states of both machines. SWEN 224 3 continued...

Question 3. Regular Languages [55 marks] (a) [8 marks : 4 for each part] Write a Regular Expression or Regular Grammar to describe each of the following languages, over the alphabet{a, b, c: (i) All strings in which all a s come before all b s and all c s. (ii) All strings of even length in which all a s come before all b s. (b) Consider the NFA M = (Q, q I, A, N, F), where: Q = {1, 2, 3, 4, 5 q I = 1 A = {a, b N = {(1, a, 1),(1, a, 2),(1, b, 1),(1, b, 3),(2, a, 4),(2, b, 2), (3, a, 3),(3, b, 5),(4, a, 4),(4, b, 4),(5, a, 5),(5, b, 5) F = {4, 5 (i) [4 marks] Draw a transition diagram for M. (ii) [4 marks] Show the sequence of configurations that M passes through in accepting the input ababa. Note that you should show all states that M may be in after accepting part of the input. (iii) [4 marks] Write a regular expression describing the language accepted by M. (iv) [8 marks] Draw a transition diagram for the DFA obtained by applying the subset construction to M. Show the correspondence between states of the DFA and those of the NFA. You only need to show reachable states. (c) [10 marks] Given an NFA M = (Q, q I, A, N, F), show how to construct an NFA, M = (Q, q I, A, N, F ), which accepts the language consisting of all strings in L(M) enclosed in a pair of a s. For example, if L(M) = {a, aa, aaa, then L(M ) = {aaa, aaaa, aaaaa, and if L(M) = {λ, b, c, bcb, then L(M ) = {aa, aba, aca, abcba. Give a brief argument to show that the resulting NFA does in fact accept the required language. (d) [6 marks] Draw a transition diagram for an NFA ǫ that accepts the language defined by the regular expression a (b c) (a b) c. (e) [3 marks] Explain briefly why allowing null transition makes it easier to construct an NFA from a regular expression. (f) [8 marks] Prove that, for any regular expressions x, y and z, x(y x) = xy xz. SWEN 224 4 continued...

Question 4. Context-Free Languages [45 marks] (a) [10 marks : 5 for each part] Write a Context Free Grammar to describe each of the following languages. You are not required to give a full formal definition of these grammars just write the list of rules. (i) All strings consisting of one or more d s, optionally followed by an a and one or more further d s. For example, d, ddd, dad and ddadd are in this language, but add, dda and dada are not. (ii) All strings formed by concatenating two non-empty palindromes over{a, b, c, where a palindrome is a strings that reads the same forwards and backwards (e.g. a, aa, aba, abcabccbacba). Thus, aa, abaaba, abab, bbaaa and abcabccbacbaaaa are in this language, but a, aba, abbc and abcabc are not. (b) Consider the following grammar, where!, $, a, b, ( and ) are terminal symbols: S T! T T!! (1, 2) T T $ T U (3, 4) U a b ( T) (5, 6, 7) (i) [3 marks] Construct a parse tree for a $ b!. Write the number of the rule applied beside each nonterminal in the parse tree; likewise for parts (ii) and (iii). (ii) [4 marks] Construct a parse tree for ( a $ b)a!!. (iii) [6 marks] Demonstrate that the grammar is ambiguous by drawing two differrent parse trees for a $ b $ a!. (iv) [6 marks] Write an equivalent, non-ambiguous grammar, treating $ as left-associative. (v) [8 marks] Write an equivalent LL(1) grammar, and show that it is LL(1). (c) [8 marks] Prove that the union of two context-free languages is context-free. Hint: Recall that a language is context-free if and only if it can be defined using a contextfree grammar. ******************************** SWEN 224 5