CMSC330 Spring 2014 Midterm 2 Solutions

Similar documents
CMSC330 Spring 2014 Midterm #2. Score 1 OCaml types & type

CMSC330 Fall 2014 Midterm 2 Solutions

CMSC 330, Fall 2013, Practice Problems 3

CMSC330 Spring 2012 Final Exam Solutions

CMSC330 Spring 2018 Midterm 2 9:30am/ 11:00am/ 3:30pm

CMSC 330, Fall 2013, Practice Problem 3 Solutions

CMSC330 Fall 2010 Final Exam Solutions

CMSC 330, Fall 2018 Midterm 2

CMSC330 Fall 2016 Midterm #2 2:00pm/3:30pm

CMSC330 Spring 2015 Final Exam 9:30am/11:00am/12:30pm

CMSC 330, Fall 2018 Midterm 2

CMSC330 Spring 2017 Midterm 2

CMSC330 Spring 2008 Final Exam

Midterm I (Solutions) CS164, Spring 2002

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

CMSC330 Spring 2015 Final Exam 9:30am/11:00am/12:30pm

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

CMSC 330 Practice Problem 4 Solutions

CMSC 330, Fall 2009, Practice Problem 3 Solutions

Question Points Score

CMSC330 Fall 2017 Final Exam Solution

Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example

CMSC330 Spring 2016 Midterm #2 9:30am/12:30pm/3:30pm

CMSC330 Spring 2016 Midterm #1 9:30am/12:30pm/3:30pm Solution

Problem Score Max Score 1 Syntax directed translation & type

UVa ID: NAME (print): CS 4501 LDI Midterm 1

CMSC330 Spring 2016 Midterm #1 9:30am/12:30pm/3:30pm

CMSC 330: Organization of Programming Languages

CMSC330 Fall 2017 Final Exam

CMSC 330: Organization of Programming Languages

CMSC330 Fall 2014 Final Exam

CMSC330 Spring 2017 Midterm #1 Solution

CMSC330 Fall 2016 Midterm #1 2:00pm/3:30pm

ECE251 Midterm practice questions, Fall 2010

CMSC330 Spring 2016 Final Exam

Note that for recursive descent to work, if A ::= B1 B2 is a grammar rule we need First k (B1) disjoint from First k (B2).

CMSC430 Spring 2014 Midterm 2 Solutions

1. Explain the input buffer scheme for scanning the source program. How the use of sentinels can improve its performance? Describe in detail.

Natural Language Processing

1 Introduction. 2 Recursive descent parsing. Predicative parsing. Computer Language Implementation Lecture Note 3 February 4, 2004

CMSC330 Fall 2009 Final Exam

CMSC330 Fall 2014 Midterm 1 Solution

CMSC330 Fall 2014 Midterm 1 Grading

Outline. Top Down Parsing. SLL(1) Parsing. Where We Are 1/24/2013

CSCI-GA Scripting Languages

Test I Solutions MASSACHUSETTS INSTITUTE OF TECHNOLOGY Spring Department of Electrical Engineering and Computer Science

LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012

CMSC 330: Organization of Programming Languages. Context-Free Grammars Ambiguity

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

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

Building a Parser III. CS164 3:30-5:00 TT 10 Evans. Prof. Bodik CS 164 Lecture 6 1

Introduction to OCaml

CMSC330 Spring 2017 Midterm #1 9:30am/11:00am/12:30pm/5:00pm

COSE212: Programming Languages. Lecture 4 Recursive and Higher-Order Programming

F28PL1 Programming Languages. Lecture 14: Standard ML 4

Introduction to OCaml

CSE 130, Fall 2005: Final Examination

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5

Parametric types. map: ( a (a b) a list b list. filter: ( a bool) a list a list. Why? a and b are type variables!

CS Parsing 1

Parsing #1. Leonidas Fegaras. CSE 5317/4305 L3: Parsing #1 1

Ambiguity. Lecture 8. CS 536 Spring

1. Consider the following program in a PCAT-like language.

CMSC 330: Organization of Programming Languages

CPS 506 Comparative Programming Languages. Syntax Specification

CMSC330 Fall 2015 Midterm #1 12:30pm/2:00pm/5:00pm

Types of parsing. CMSC 430 Lecture 4, Page 1

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

CMSC330 Fall 2016 Final Exam

Introduction to Functional Programming and Haskell. Aden Seaman

max function Next concat function max function What s the pattern? concat function More on recursion Higher-order functions

CMSC 330: Organization of Programming Languages. Context Free Grammars

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

CMSC 330: Organization of Programming Languages

Question Marks 1 /12 2 /6 3 /14 4 /8 5 /5 6 /16 7 /34 8 /25 Total /120

Mini-ML. CS 502 Lecture 2 8/28/08

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

Eng. Maha Talaat Page 1

CMSC 330: Organization of Programming Languages. Closures (Implementing Higher Order Functions)

Top down vs. bottom up parsing

ML Built-in Functions

CSE431 Translation of Computer Languages

CMSC 330: Organization of Programming Languages. OCaml Data Types

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

Context-Free Grammars

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

COP4020 Spring 2011 Midterm Exam

CMSC 330: Organization of Programming Languages

MIDTERM EXAMINATION - CS130 - Spring 2003

CSE341 Section 3. Standard-Library Docs, First-Class Functions, & More

OCaml Data CMSC 330: Organization of Programming Languages. User Defined Types. Variation: Shapes in Java

MODEL ANSWERS COMP36512, May 2016

CIS 120 Midterm I October 2, Name (printed): Username (login id):

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

Chapter 3. Parsing #1

Transcription:

CMSC330 Spring 2014 Midterm 2 Solutions 1. (16 pts) OCaml Types and Type Inference Give the type of the following OCaml expressions: a. (2 pts) fun b -> b + 8 Type = int -> int b. (3 pts) fun b -> b 8 Type = (int -> a) -> a Write an OCaml expression with the following type: c. (2 pts) a -> 'a -> 'a list list Code = fun x y -> [ [x ; y] ] d. (3 pts) (int -> int) -> (int -> int) Code = fun x y -> (x 1) + y Give the value of the following OCaml expressions. If an error exists, describe it e. (3 pts) (fun k t m -> [m; t]) 2 4 6 Value / Error = [6 ; 4] f. (3 pts) (fun (k::t::m) -> (m, t)) [2;4;6;8] Value / Error = ( [6;8], 4 ) 2. (10 pts) OCaml higher-order & anonymous functions Using either map or fold and an anonymous function, write a curried function process which when given a predicate function p, a compute function f, and a list lst, returns a list of the results of applying f to the elements of lst, but only where p is true for that element of the list. If p is false for an element of the list, it does not contribute to the final result. The relative order of the elements in the new list must the same as in lst. Your function must run in linear time. You may not use any library functions, with the exception of the List.rev function, which reverses a list in linear time. You may not use imperative OCaml (i.e., no ref variables). For example, the results of calling process will be as follows: Example: let p x = x > 2;; let f x = x+1;; process p f [ ] = [ ] process p f [ 1; 2 ] = [ ] process p f [ 1; 3; 2; 4 ] = [ 4; 5 ] let rec map f l = match l with [ ] -> [ ] (h::t) -> (f h)::(map f t) let rec fold f a l = match l with [ ] -> a (h::t) -> fold f (f a h) t let process p f lst = List.rev (fold (fun a h -> if (p h) then (f h)::a else a) [ ] lst)

3. (18 pts) OCaml programming Write a function allpairs with type ( a list -> a list list) which given a list lst with n elements, returns a list of all n 2 pairs of elements in lst (in any order), where each pair of elements is in a list of length 2. You may not use any library functions, with the exception of the @ function (which concatenates two lists). You may use helper functions. You may use map or fold, but it is not required. Your function must execute in polynomial time. You may not use imperative OCaml constructs. Examples: allpairs [ ] ;; (* = [ ] *) allpairs [1] ;; (* = [ [1;1] ] *) allpairs [1;2] ;; (* = [ [1;1]; [1;2]; [2;1]; [2;2] ] *) allpairs [1;2;5] ;; (* = [ [1;1]; [1;2]; [1;5]; [2;1]; [2;2]; [2;5]; [5;1]; [5;2]; [5;5] ] *) Sample Solutions let rec help1 x y = match y with h::t -> [x;h] :: (help1 x t) ;; let rec help2 all z = match z with h::t -> (help1 h all) @ (help2 all t) ;; let allpairs lst = help2 lst lst ;; let allpairs lst = let rec help1 x y = match y with h::t -> [x;h] :: (help1 x t) in let rec help2 z = match z with h::t -> (help1 h lst) @ (help2 t) in help2 lst ;; let help1 x y = fold (fun a h -> [x;h]::a) [] y ;; let allpairs lst = fold (fun a h -> (help1 h lst) @ a) [] lst ;; let help1 x y = map (fun h -> [x;h]) y ;; let allpairs lst = fold (fun a h -> (help1 h lst) @ a) [] lst ;; let allpairs lst = let help1 x = fold (fun a h -> [x;h]::a) [] lst in fold (fun a h -> (help1 h) @ a) [] lst ;; let allpairs lst = let help1 x = map (fun h -> [x;h]) lst in fold (fun a h -> (help1 h) @ a) [] lst ;; let allpairs lst = fold (fun a h -> (map (fun z -> [h;z]) lst) @ a) [] lst ;;

4. (8 pts) OCaml polymorphic types Consider the OCaml type bitwiseexp implementing bitwise expressions (think of it as a binary tree where all leaves are 0 s or 1 s). For all answers, your code must work in linear time (i.e., avoid multiple passes over the tree). You are not allowed to use any OCaml library functions except +. You may use helper functions. type bitwiseexp = Zero (* 0 *) One (* 1 *) Pair of bitwiseexp * bitwiseexp (* b 1 b 2 *) bitwiseexp onesexp let x = Zero;; 0 let y = One;; 1 let p = Pair (x,y);; 1 let q = Pair (y,y);; 2 a. (2 pts) Write an OCaml expression with type bitwiseexp that is equivalent to the expression 0 (1 0) Pair (Zero, Pair (One, Zero)) b. (6 pts) Write a function onesexp of type (bitwiseexp -> int) that takes a bitwise expression and returns the number of 1 s in the expression. let rec onesexp b = match b with Zero -> 0 One -> 1 Pair (x,y) -> (onesexp x) + (onesexp y)

5. (10 pts) Context free grammars. Consider the following grammar (S = start symbol and terminals = a b % &): S a S%S &T T a b a. (1 pt each) Indicate whether the following strings are generated by this grammar i. %a Yes No (circle one) ii. a%&b Yes No (circle one) iii. &a&b Yes No (circle one) b. (3 pts) Draw a parse tree for the string &b%a S / \ S % S / & T a b c. (4 pts) Is the grammar is ambiguous? Provide proof if you believe it is ambiguous. Grammar is ambiguous. Multiple parse trees for strings such as a%a%a.

6. (14 pts) Using context free grammars. a. (6 pts) Given the following production for B, create a grammar that generates OCaml tuples of strings (for the 3 strings Joe, Laura, and Mike). Your grammar should be able to generate strings such as ( Joe ), ( Joe, Mike ), ( Joe, Laura, Laura ), etc... B Mike Laura Joe S ( A ) A B B, A b. (8 pts) Consider the following grammar: S c d e S%S S@S &S Modify the grammar above to make the % operator left associative, the @ operator right associative, and make @ have the lowest precedence and & have the highest precedence. S A@S A A A%B B B &B C (*B &C C is also acceptable *) C c d e

7. (15 pts) Parsing Consider the following grammar, where S, A, B are nonterminals, and a, b, c, d are terminals. S a bb Ac A SAd epsilon B c b a. (8 pts) Calculate FIRST sets for S, A, and B FIRST(S) = FIRST(A) = { a, b, c { a, b, c, epsilon FIRST(B) = { c, b b. (7 pts) Using pseudocode, write only the parse_a function found in a recursive descent parser for the grammar. You may assume the functions parse_s, parse_b already exist. Use the following utilities: lookahead Variable holding next terminal match ( x ) Function to match next terminal to x error ( ) Reports parse error for input parse_a( ) { // your code starts here if ((lookahead == a ) (lookahead == b ) (lookahead == c )) { parse_s( ); parse_a( ); match( d ); else ;

8. (9 pts) Multithreading Consider the following multithreaded Java 1.4 code. Assume there are multiple producer and consumer threads being executed in the program, but only a single Buffer object. Questions about the last statement executed by two threads x & y refer to the most recently executed statement by those threads at some arbitrary time during the program execution. It does not mean the last statement executed by a thread before the thread exits. If a situation is possible, you need to give an example of how it is possible (e.g., thread x gets to statement a, then thread y gets to statement b). If a situation is not possible, you need to explain why. class Buffer { void produce(o) { synchronized (this) { 1. while (...) wait( ); 2.... 3. notifyall( ); 4.... Object consume( ) { synchronized (this) { 5. while (...) wait( ); 6.... 7. notifyall( ); 8.... a. (3 pts) Is it possible given two threads x and y for the last statement executed by thread x to be statement 5, and the last statement executed by thread y to be statement 7? Explain your answer. Yes. Thread x reaches statement 5 and releases the lock by calling wait, thread y then acquires lock and reaches statement 7. b. (3 pts) Is it possible given two threads x and y for the last statement executed by thread x to be statement 7, and the last statement executed by thread y to be statement 6? Explain your answer. No. Whichever thread enters synchronized region first (and reaches statement 6 or 7) must still be holding the lock. The other thread cannot acquire the lock to enter the synchronized region. c. (3 pts) Is it possible given two threads x and y for the last statement executed by thread x to be statement 6, and the last statement executed by thread y to be statement 1? Explain your answer. Yes. Thread y reaches statement 1 and releases the lock by calling wait, thread x then acquires lock and reaches statement 6.