CMSC330 Fall 2014 Midterm 2 Solutions

Similar documents
CMSC330 Spring 2014 Midterm 2 Solutions

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

CMSC 330, Fall 2013, Practice Problems 3

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

CMSC 330, Fall 2018 Midterm 2

CMSC 330, Fall 2013, Practice Problem 3 Solutions

CMSC 330, Fall 2018 Midterm 2

CMSC330 Spring 2012 Final Exam Solutions

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

CMSC330 Fall 2010 Final Exam Solutions

CMSC330 Spring 2008 Final Exam

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

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

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

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

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

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

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

Question Points Score

CMSC330 Spring 2017 Midterm 2

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

CSE 401 Midterm Exam Sample Solution 2/11/15

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

CMSC330 Spring 2016 Final Exam

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

CSCI-GA Scripting Languages

CMSC330 Fall 2017 Final Exam

CMSC 330: Organization of Programming Languages. Operational Semantics

Problem Score Max Score 1 Syntax directed translation & type

CS143 Midterm Fall 2008

CMSC 330, Fall 2009, Practice Problem 3 Solutions

CMSC330 Fall 2014 Midterm 1 Solution

CMSC330 Fall 2014 Midterm 1 Grading

ECE251 Midterm practice questions, Fall 2010

CMSC330 Spring 2017 Midterm #1 Solution

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

CMSC 330: Organization of Programming Languages

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

CMSC 330 Practice Problem 4 Solutions

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

CMSC330 Fall 2017 Final Exam Solution

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

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

CSE431 Translation of Computer Languages

CMSC 330: Organization of Programming Languages

CMSC430 Spring 2014 Midterm 2 Solutions

Introduction to OCaml

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

Midterm I (Solutions) CS164, Spring 2002

CSE P 501 Exam 11/17/05 Sample Solution

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

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

Extra Credit Question

CMSC 330: Organization of Programming Languages

Principles of Programming Languages COMP251: Syntax and Grammars

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Compiler Theory. (Semantic Analysis and Run-Time Environments)

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

CMSC330 Fall 2014 Final Exam

1 Lexical Considerations

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

MIDTERM EXAMINATION - CS130 - Spring 2003

Top-Down Parsing and Intro to Bottom-Up Parsing. Lecture 7

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

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

Derivations vs Parses. Example. Parse Tree. Ambiguity. Different Parse Trees. Context Free Grammars 9/18/2012

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

MIDTERM EXAM (Solutions)

CMSC 330: Organization of Programming Languages

COP4020 Spring 2011 Midterm Exam

CMSC 330, Fall 2018 Midterm 1

CMSC 330, Fall 2018 Midterm 1

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

CMSC330 Fall 2016 Final Exam

CS Parsing 1

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

CS 44 Exam #2 February 14, 2001

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

A simple syntax-directed

Principles of Programming Languages COMP251: Syntax and Grammars

Midterm I - Solution CS164, Spring 2014

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

ML Built-in Functions

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

MODEL ANSWERS COMP36512, May 2016

Programming Languages. Example 5. Example 4. CSE 130 : Fall type, can reuse code for all types! let rec cat l = match l with

Homework 5. Notes. Turn-In Instructions. Reading. Problems. Handout 19 CSCI 334: Spring (Required) Read Mitchell, Chapters 6 and 7.

Top-Down Parsing and Intro to Bottom-Up Parsing. Lecture 7

CS 421, Fall Sample Final Questions & Solutions

CSE 505: Concepts of Programming Languages

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

CS143 Midterm Sample Solution Fall 2010

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

CPS 506 Comparative Programming Languages. Syntax Specification

CSE341 Spring 2016, Midterm Examination April 29, 2016

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. Lambda Calculus

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).

CSE341 Spring 2016, Midterm Examination April 29, 2016

Transcription:

CMSC330 Fall 2014 Midterm 2 Solutions 1. (5 pts) General. For the following multiple choice questions, circle the letter(s) on the right corresponding to the best answer(s) to each question. a. In the expression let x 1 = E 1 in E 2, where is x bound to x 1? A B C D A) only E 1 B) only E 2 C) both E 1 and E 2 D) neither E 1 nor E 2 b. In the expression let rec x 1 = E 1 in E 2, where is x bound to x 1? A B C D A) only E 1 B) only E 2 C) both E 1 and E 2 D) neither E 1 nor E 2 c. In the expression (fun x 1 -> E 1) E 2, where is x bound to x 1? A B C D A) only E 1 B) only E 2 C) both E 1 and E 2 D) neither E 1 nor E 2 d. OCaml modules are used to group together data, types, and functions. They are similar to which of the following? A B C D A) Java classes B) Java interfaces C).h files in C D).c files in C e. Context free grammars do not consist of which of the following? A B C D A) Productions B) Terminals C) Nonterminals D) Final symbol 2. (12 pts) OCaml Types and Type Inference a. (4 pts) Give the type of the following OCaml expressions i. (2 pts) let a b = b in a 2 Type = int ii. (2 pts) fun r u n -> (r u, n) Type = ( a -> b ) -> a -> c -> ( b * c ) b. (4 pts) Write an OCaml expression with the following type i. (2 pts) int -> (int -> a -> int) Code = fun x y z -> x+y ii. (2 pts) ( a list -> b) -> a -> b Code = fun x y -> x [y] c. (4 pts) Give the value of the following OCaml expressions. If an error exists, describe the error. i. (2 pts) (fun r u n -> u::[ r; n ] ) 1 2 3 Value / Error = [2;1;3] ii. (2 pts) let x = (fun x 3 x) in x 2 Value / Error = 1

3. (12 pts) OCaml higher-order & anonymous functions Using either map or fold and an anonymous function, write a curried function count which when given an element x of type a and an associative lst of type ( a * int) list, returns a new associative list where the int value associated with x is incremented by 1. The relative order of pairs in the new list must remain the same. You may assume lst includes a pair for x. Your function must run in linear time. You may not use any library functions, with the exception of functions in Pervasives and the List.rev function, which reverses a list in linear time. You may not use imperative OCaml (i.e., no ref variables). Example: count a [ ( a,0) ] [ ( a,1) ] count b [ ( a,2) ; ( b,3) ] [ ( a,2) ; ( b,4) ] count 3.14 [ (3.14,1) ] [ (3.14,2) ] let count x lst = map (fun (v,num) -> if (x=v) then (v,num+1) else (v,num)) lst let count x lst = List.rev (fold (fun a (v,num) -> if (v=x) then ((v,num+1)::a) else ((v,num)::a)) [ ] lst) 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 4. (10 pts) OCaml polymorphic types Consider the following OCaml user-variant type olist implementing an ordered linked list of ints. Assume ints are stored in descending order (i.e., largest value at head of list). type olist = Empty Node of int * olist Write a function insert of type (int -> olist -> olist) that takes an integer n and an olist p, and returns the the olist resulting from inserting n in p. Your function must run in linear time. You may not use any library functions, with the exception of functions in Pervasives and the List.rev function, which reverses a list in linear time. You may not use imperative OCaml (i.e., no ref variables). The following examples are given using int lists. Your code must produce the equivalent output for olist. Functions that work only on int list will receive zero partial credit. Example: insert 2 [ ] [ 2 ] insert 2 [ 1 ] [ 2 ; 1 ] insert 2 [ 3 ] [ 3 ; 2 ] insert 2 [ 3 ; 1 ] [ 3 ; 2 ; 1 ] let rec insert x lst = match lst with Empty -> Node (x, Empty) Node (y, z) -> if (x > y) then (Node (x, lst)) else (Node (y, (insert x z)))

5. (20 pts) OCaml programming Write a function pack with type ( a list -> a list list) which given a list lst, returns a new list composed of the elements of lst in lists, where consecutive identical elements from lst are placed in the same list. The relative order of elements in lst must be preserved in the new list. Your function must run in linear time. You may not use any library functions, with the exception of functions in Pervasives and the List.rev function, which reverses a list in linear time. You may not use imperative OCaml (i.e., no ref variables). You may use map & fold. Examples: pack [ ] [ ] pack [1;1;2;2;2] [ [ 1;1 ] ; [ 2;2;2 ] ] pack [1] [ [ 1 ] ] pack [1;1;2;2;1;3;3;3] [ [ 1;1 ] ; [ 2;2 ] ; [1] ; [3;3;3] ] let rec pack lst = let rec helper x lst = match (x,lst) with (_,[ ]) -> (x,lst) ([ ],h2::t2) -> helper [h2] t2 (h1::t1,h2::t2) -> if h1 = h2 then helper (h2::x) t2 else (x,lst) in match lst with [ ] -> [ ] h::t -> let (x,y) = (helper [h] t) in x::(pack y) let pack lst = let rec helper current acc lst = match lst with [ ] -> [ ] [x] -> (x :: current) :: acc a :: b :: c -> if a = b then helper (a :: current) acc (b::c) else helper [ ] ((a :: current) :: acc) (b::c) in List.rev (helper [ ] [ ] lst) let pack lst = List.rev (fold (fun a h -> match a with [ ] -> [[h]] (x::y)::t -> if x=h then ((h::x::y)::t) else ([h]::a) ) [ ] lst)

6. (8 pts) Context free grammars. Consider the following grammar for OCaml types involving int, int lists, and functions: T int T -> T T list ( T ) a. (1 pt each) Indicate whether the following strings are generated by this grammar i. int list -> int Yes No (circle one) ii. int list int Yes No (circle one) iii. ( int -> int ) list list Yes No (circle one) b. (2 pts) Draw a parse tree for the string int -> int list c. (3 pts) Is the grammar is ambiguous? Provide proof if you believe it is ambiguous. Yes, multiple parse trees exist for the same string (alternatively, can show multiple left-most derivations for the same string). Examples of strings with multiple parse trees include trees for int -> int list above, or trees for int -> int -> int below:

7. (12 pts) Using context free grammars. Consider the following grammar for OCaml types involving int, int lists, and functions: T int T -> T T list ( T ) a. (2 pts) Can the grammar be parsed using a recursive descent parser? Explain your answer. No. Left recursion (e.g., T T list) & conflicting FIRST sets (e.g., T -> T, T list) b. (4 pts) Modify the grammar to make the operator -> right associative, ( ) to have the highest precedence, and list to have the lowest precedence. Your grammar should be able to derive strings such as int list list. T T list A A B -> A B B int ( T ) c. (6 pts) Given the following productions for X and Y, create a grammar that generates Ruby array references for the integer arrays a & b, where arrays may be multidimensional. Your grammar should be able to generate strings such as a[0], a[1][0], b[1][0][1], a[b[1]], but not a, 1, [1], [a], or a[b]. S XA A A[B] [B] B S Y 8. (12 pts) Parsing a. (6 pts) Calculate FIRST sets for the grammar below right, where S, A are nonterminals, and a, b, c are terminals. FIRST(S) = { b, c } FIRST(A) = { epsilon, b, c } X a b Y 0 1 S Ab cab A Sac epsilon b. (6 pts) Using pseudocode, write only the parse_c function found in a recursive descent parser for a grammar that includes the productions C DaC epsilon, and where FIRST(D) = { c, d }. You may assume the function parse_d already exists. Use the utilities on the right. lookahead Variable holding next terminal parse_c( ) { // your code starts here match ( x ) Function to match next terminal to x error ( ) Reports parse error for input } if ((lookahead == c ) (lookahead == d )) { parse_d( ) ; match( a ) ; parse_c( ) ; } else ; // C DaC // C epsilon

9. (9 pts) Operational semantics Prove what value the expression let x = (fun x 3 x) in x 2 evaluates to in an empty environment when using static scoping. In other words, find a v such that you can prove the following: ; let x = (fun x 3 x) in x 2 v The operational semantics rules given in class for static scoping are below. Show the complete proof that evaluates the expression using these rules.