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

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

CMSC330 Spring 2017 Midterm 2

CMSC 330, Fall 2018 Midterm 2

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

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

CMSC 330, Fall 2018 Midterm 2

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

CMSC330 Fall 2017 Final Exam

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

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

CMSC330 Spring 2016 Final Exam

CMSC330 Fall 2017 Final Exam Solution

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

Problem Score Max Score 1 Syntax directed translation & type

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

CMSC330 Spring 2017 Midterm #1 Solution

CMSC330 Spring 2008 Final Exam

CMSC330 Fall 2009 Final Exam

CMSC330 Fall 2016 Final Exam

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

CMSC330 Fall 2014 Midterm 2 Solutions

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

Midterm I - Solution CS164, Spring 2014

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

CMSC 330: Organization of Programming Languages

CMSC330 Spring 2012 Final Exam Solutions

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine.

CMSC 330: Organization of Programming Languages

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

Midterm 1. CMSC 430 Introduction to Compilers Spring Instructions Total 100. Name: March 14, 2012

CMSC 330: Organization of Programming Languages. Operational Semantics

CMSC330 Fall 2014 Final Exam

CMSC 330, Fall 2013, Practice Problems 3

CS164: Midterm I. Fall 2003

CMSC330 Spring 2014 Midterm 2 Solutions

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

CMSC 330: Organization of Programming Languages

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

CSE P 501 Exam 8/5/04

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm Sample Solutions CSC324H1 Duration: 50 minutes Instructor(s): David Liu.

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

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

Fall 2013 Midterm Exam 10/22/13. This is a closed-book, closed-notes exam. Problem Points Score. Various definitions are provided in the exam.

CS 415 Midterm Exam Spring 2002

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

([1-9] 1[0-2]):[0-5][0-9](AM PM)? What does the above match? Matches clock time, may or may not be told if it is AM or PM.

UMBC CMSC 331 Final Exam

CS143 Midterm Fall 2008

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Lambda Calculus

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

Denotational Semantics; Lambda Calculus Basics Section and Practice Problems Week 4: Tue Feb 13 Fri Feb 16, 2018

Administrativia. PA2 assigned today. WA1 assigned today. Building a Parser II. CS164 3:30-5:00 TT 10 Evans. First midterm. Grammars.

Question Bank. 10CS63:Compiler Design

CMSC 330: Organization of Programming Languages

CMSC330 Fall 2010 Final Exam Solutions

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages

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

Midterm I (Solutions) CS164, Spring 2002

CMSC 330: Organization of Programming Languages

Compilers Course Lecture 4: Context Free Grammars

CMSC 330: Organization of Programming Languages. Context Free Grammars

A Simple Syntax-Directed Translator

Part I: Multiple Choice Questions (40 points)

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

BSCS Fall Mid Term Examination December 2012

MIDTERM EXAMINATION - CS130 - Spring 2003

CMSC 330: Organization of Programming Languages. Lambda Calculus

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

CSE 582 Autumn 2002 Exam 11/26/02

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

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

CS 536 Midterm Exam Spring 2013

CMSC 330 Exam #2 Fall 2008

COP5621 Exam 3 - Spring 2005

CMSC 330, Fall 2013, Practice Problem 3 Solutions

ECE251 Midterm practice questions, Fall 2010

CMSC 330: Organization of Programming Languages

CMSC411 Fall 2013 Midterm 1

MIDTERM EXAMINATION - CS130 - Spring 2005

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones

n Closed book n You are allowed 5 cheat pages n Practice problems available in Course Materials n Check grades in Rainbow grades

CMSC 330: Organization of Programming Languages. Context Free Grammars

Theory of Computation Dr. Weiss Extra Practice Exam Solutions

CIS 500 Software Foundations Midterm I

Midterm 2 Solutions Many acceptable answers; one was the following: (defparameter g1

CMSC 330: Organization of Programming Languages

CSE Qualifying Exam, Spring February 2, 2008

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

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

CMSC 330: Organization of Programming Languages. Lambda Calculus Encodings

CMSC 330, Fall 2018 Midterm 1

A simple syntax-directed

CMSC330 Fall 2013 Practice Problems 6 Solutions

COP4020 Spring 2011 Midterm Exam

CMSC 330, Fall 2018 Midterm 1

Midterm 2. CMSC 430 Introduction to Compilers Fall Instructions Total 100. Name: November 21, 2016

2068 (I) Attempt all questions.

Comp 311: Sample Midterm Examination

Transcription:

CMSC330 Spring 2016 Midterm #2 9:30am/12:30pm/3:30pm Name: Discussion Time: 10am 11am 12pm 1pm 2pm 3pm TA Name (Circle): Adam Anshul Austin Ayman Damien Daniel Jason Michael Patrick William Instructions Do not start this test until you are told to do so! You have 75 minutes to take this midterm. This exam has a total of 100 points, so allocate 45 seconds for each point. This is a closed book exam. No notes or other aids are allowed. Answer essay questions concisely in 2-3 sentences. Longer answers are not needed. For partial credit, show all of your work and clearly indicate your answers. Write neatly. Credit cannot be given for illegible answers. Problem Score 1 Finite Automata /20 2 Context Free Grammars /15 3 Parsing /10 4 OCaml /20 5 Programming Language Concepts /13 6 Operational Semantics /10 7 Lambda Calculus /12 Total /100 1

1. Finite Automata (20 pts) 1) (5 pts) Construct an NFA for the following regular expression: (aa bc)* 2) (5 pts) Describe the strings accepted by the following NFA. 2

3) (5 pts) Write a regular expression which accepts the same strings as the following NFA. 4) (5 pts) Convert the above NFA to a DFA 3

2. Context Free Grammars (15 pts) 1) (4 pts) Create a context-free grammar which accepts strings of the following form: a n b 2n+1 (n>=0) Strings include b, abbb, aabbbbb, etc. 2) (6 pts) a. Show that this grammar CFG is ambiguous, S -> SaS T T -> Tb b b. Rewrite the CFG so that it is not ambiguous. 3) (5 pts) Is the language defined by the following CFG a regular language? If so, give an equivalent regular expression. If not, say why it is not regular. S -> AyAyAS ε A -> xa ε 4

3. Parsing (10 pts) 1) (4 pts) Given this CFG, what would be the first set that is generated by S? S -> ASB B A -> wa w B -> cb hb ib kb eb nb 2) (6 pts) I ve made the world s best language and named it Sequel. All sequel can do is add and subtract numbers or expressions and output the value of an expression. type token = Tok_Num of int Tok_LParen Tok_RParen Tok_Semi Tok_Print Tok_Sum Tok_Sub type ast = Num of int Sum of ast * ast Sub of ast * ast Print of ast Grammar: printexp -> 'output ' additiveexp ';' additiveexp -> subtractiveexp ('+' subtractiveexp)* subtractiveexp -> primaryexp ('-' primaryexp)* primaryexp -> '(' additiveexp ')' INITLIT INTLIT-> ('0' ('1'..'9') ('0'..'9')* ) Given that you have a lookahead lst, parse_sub lst, parse_primary lst, parse_print lst and parse_int lst of type token list -> ast * token list. Write a parse_add lst that parses an additive expression. The lookahead function is defined as follows: let lookahead tok_list = match tok_list with [] -> raise (IllegalExpression "lookahead") (h::t) -> (h,t);; 5

4. OCaml (20 pts) let rec map f l = match l with [] -> [] h::t -> let r = f h in r :: map f t ;; let rec fold f a l = match l with [] -> a h::t -> fold f (f a h) t ;; 1) (6 pts ) Write a function app of type 'a list -> ('a -> 'b) list -> 'b list list that, given a list and a list of functions, applies each function in the function list to the a list. You should use at least one of fold and map. You cannot have rec in the function definition or in the definition of any helper functions. (Note: the order of the lists in the list list does not matter). Examples: f [1;2;3;4;5] [(fun x -> x); (fun x -> x * x); (fun x -> x * x * x)] = [[1; 8; 27; 64; 125]; [1; 4; 9; 16; 25]; [1; 2; 3; 4; 5]] OR [[1; 2; 3; 4; 5]; [1; 4; 9; 16; 25]; [1; 8; 27; 64; 125]] let app lst f_lst = 6

2) (8 pts) Recall the concept of an an AST, an intermediate representation of code used before execution. Given the following definition of an AST, write a recursive function eval_ast of type ast -> int that will evaluate a given AST. type ast = Sum of ast * ast (* Adds two sides *) Double of ast (* Doubles the result of the child node *) Num of int (* Contains a normal integer *) ;; Example: eval_ast (Sum(Sum(Num(3),Num(4)),Double(Num(8)))) = 23 3) (6 pts) Two trees are identical when they have same data and arrangement of data is also same. Given the definition of tree type tree = Leaf Node of tree * int * tree Write a function equal (t1, t2) of type tree * tree -> bool, which returns true if t1 and t2 are equal and returns false otherwise. 7

5. Programming Language Concepts (13 pts) 1) (3 pts) T/F OCaml s @ operator is an example of ad hoc polymorphism 2) (4 pts) Write the output of following OCaml code. let x = 10 ;; let f y = x + y;; let x = 5 ;; let y = 7 ;; let z = f (x + y) ;; What is the value of z: With Static Scoping With Dynamic Scoping 3) (3 pts) Consider the following OCaml code fragment. let foo a b = a + b in foo 5 6;; Does the code fragment above produce any side effects? a) Yes, because foo returns the result of a + b. b) Yes, because if you typed this into the interpreter you would get back - : int = 11. c) No, because it doesn t mutate any state. d) No, because the modifications it makes to the environment are contained in a closure. 4) (3 pts) Signatures are the enabling mechanism for overloading of members in classes. A method's signature is the a) name of the method and the type of its return value. b) name of the method and the names of its parameters. c) name of the method and the data types of its parameters. d) name of the method, its parameter list, and its return type. 8

6. Operational Semantics (10 pts) Domain of n: Z Domain of b: {true, false} Domain of v: {n, b} Axioms: Number: True: False: N -> n true -> true false -> false Arithmetic/Comparison operations: Op: E1 -> n1 E2 -> n2 E1 op E2 -> n1 op n2 (Hint: Arithmetic operations evaluate to values in the domain of n. Comparisons evaluate to b) Booleans: If-T : E1 -> true E2 -> v2 If-F: E1 -> false E3 -> v3 If E1 then E2 else E3 -> v2 If E1 then E2 else E3 -> v3 Fill in the missing pieces of the following deductions. 1) (4 pts) 2) (6 pts) 9

7. Lambda Calculus (12 pts) 1) (4 pts) Make all parentheses explicit in the following expression: λc. λa. a b ( λb. b a ) c 2) (4 pts each) Reduce the expressions as far as possible by showing the intermediate β- reductions and α-conversions. (λx.λy.x y) (λy.y) b 3) (4 pts) Given: succ = λz.λf.λy.f (z f y) 0 = λf.λy.y 1 = λf.λy.f y 2 = λf.λy.f (f y) Prove that succ 1 = 2 succ 1 = 10