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

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

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

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

Midterm 2. CMSC 430 Introduction to Compilers Fall 2018

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

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

(Practice) Midterm 1

COP5621 Exam 3 - Spring 2005

Midterm 2. CMSC 430 Introduction to Compilers Spring Instructions Total 100. Name: April 18, 2012

CMSC 330: Organization of Programming Languages. Operational Semantics

UNIVERSITY OF CALIFORNIA

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Lambda Calculus

CMSC330 Spring 2017 Midterm 2

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

CMSC 330: Organization of Programming Languages. Lambda Calculus

CS 415 Midterm Exam Spring 2002

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

CSE 401/M501 18au Midterm Exam 11/2/18. Name ID #

CSE3322 Programming Languages and Implementation

DO NOT OPEN UNTIL INSTRUCTED

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

CSCE 531 Spring 2009 Final Exam

CMSC 330: Organization of Programming Languages

Project 6 Due 11:59:59pm Thu, Dec 10, 2015

Project 2 Due 11:59:59pm Wed, Mar 7, 2012

Using an LALR(1) Parser Generator

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Downloaded from Page 1. LR Parsing

CSE P 501 Compilers. LR Parsing Hal Perkins Spring UW CSE P 501 Spring 2018 D-1

Question Points Score

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

CS 411 Midterm Feb 2008

ECE251 Midterm practice questions, Fall 2010

MIDTERM EXAMINATION - CS130 - Spring 2005

Type Checking and Type Inference

COS 320. Compiling Techniques

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

CS 3360 Design and Implementation of Programming Languages. Exam 1

CSE 582 Autumn 2002 Exam 11/26/02

CMSC330 Fall 2017 Final Exam

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

Problem Score Max Score 1 Syntax directed translation & type

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

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

MIDTERM EXAMINATION - CS130 - Spring 2003

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

CMSC 330: Organization of Programming Languages

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

CS 415 Midterm Exam Spring SOLUTION

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

CSCI Compiler Design

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

UNIT III & IV. Bottom up parsing

MASSACHUSETTS INSTITUTE OF TECHNOLOGY Fall Test I

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

CMSC 330 Exam #2 Fall 2008

CMSC330 Fall 2009 Final Exam

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:

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

CS 3360 Design and Implementation of Programming Languages. Exam 1

Syntax-Directed Translation

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

Let us construct the LR(1) items for the grammar given below to construct the LALR parsing table.

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

MP 5 A Parser for PicoML

CSCE 531, Spring 2015 Final Exam Answer Key

CMSC330 Spring 2008 Final Exam

CSCI 3155: Principles of Programming Languages Exam preparation #1 2007

CSCI-GA Scripting Languages

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions

Compiler Design (40-414)

News. Programming Languages. Complex types: Lists. Recap: ML s Holy Trinity. CSE 130: Spring 2012

Question Points Score Total 100

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

CIS 120 Midterm II November 8, 2013 SOLUTIONS

Programs as data Parsing cont d; first-order functional language, type checking

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

Principles of Programming Languages 2017W, Functional Programming

Building a Parser Part III

CSE 401 Midterm Exam Sample Solution 11/4/11

LL(1) predictive parsing

CSE 401 Final Exam. December 16, 2010

CSE 582 Autumn 2002 Exam Sample Solution

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

COL728 Minor1 Exam Compiler Design Sem II, Answer all 5 questions Max. Marks: 20

CSE 401 Compilers. LR Parsing Hal Perkins Autumn /10/ Hal Perkins & UW CSE D-1

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Compilers: The goal. Safe. e : ASM

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Context-free grammars

Implementing a VSOP Compiler. March 20, 2018

CIS 120 Midterm I February 16, 2015 SOLUTIONS

CMSC 331 Final Exam Section 0201 December 18, 2000

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

CS 11 Ocaml track: lecture 6

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Transcription:

Name: Midterm 1 CMSC 430 Introduction to Compilers Spring 2012 March 14, 2012 Instructions This exam contains 8 pages, including this one. Make sure you have all the pages. Write your name on the top of this page before starting the exam. Write your answers on the exam sheets. If you finish at least 15 minutes early, bring your exam to the front when you are finished; otherwise, wait until the end of the exam to turn it in. Please be as quiet as possible. If you have a question, raise your hand. If you feel an exam question assumes something that is not written, write it down on your exam sheet. Barring some unforeseen error on the exam, however, you shouldn t need to do this at all, so be careful when making assumptions. Question Score Max 1 32 2 34 3 34 Total 100 1

Question 1. Short Answer (32 points). a. (8 points) In at most 3 sentences, explain the difference between a compiler front-end and a compiler back-end. Answer: The front-end of the compiler is responsible for lexing and parsing, and the back-end of the compiler is responsible for code generation. b. (8 points) As part of project 2, you extended Rube to include type annotations for fields and local variables. However, fields and local variables included no explicit initialization. But then, how can Rube initialize fields and local variables to ensure their initial values are type safe? Explain your answer briefly. Answer: Fields and local variables can be safely initialized with nil, since the type of nil is a subtype of all other types. 2

c. (8 points) Explain briefly what a basic block is. Answer: A basic block is a sequence of instructions with no branches from it except from the last statement, and no branches into it except to the beginning of the block. d. (8 points) In project 1, we allowed dimensional quantities to be converted between units. In project 2, we included subtyping so that subtypes could be used where supertypes were expected. Compare and contrast these two ideas. In what ways are they similar, and how are they different? Give at least one similiarity and one difference. Write at most a few sentences. Answer: There are many possible answers. Both ideas allow values of different kinds to be converted to or from each other. In project 1, we only had explicit casts, whereas in project 2 subtyping could be used both explicitly and implicitly. In subtyping, one direction is always valid (sub- to supertype), and the other direction may not be valid. With unit conversion, both directions of conversion are always possible. Subtyping in project 2 relates many different types, whereas by design, in project 1 we only converted to or from SI base units. 3

Question 2. Parsing (34 points). Consider the grammar for the lambda calculus, which we can write down with the following.mly file (actions omitted):... %% expr: ID (* variable; production 1 *) LAM ID DOT expr (* function binding; production 2 *) expr expr (* function application; production 3 *) ; a. (4 points) List all the tokens that are referred to in the grammar. Answer: LAM, ID, and DOT. b. (10 points) Suppose we defined the following data type for abstract syntax trees for lambda calculus: type expr = Var of string Lam of string * expr App of expr * expr Fill in the actions for the lambda calculus grammar so that the parser will generate abstract syntax trees of type expr.... %% expr: ID { } LAM ID DOT expr { } expr expr { } ; Answer: expr: ID { Var($1) } LAM ID DOT expr { Lam($2, $4) } expr expr { App($1, $2) } 4

c. (15 points) Following is the (slightly simplified).output file produced when we run ocamlyacc on this grammar. We ve numbered the productions above to correspond with how the productions are referred to in the output file: state 1 %entry% :. expr (4). error expr goto 5 state 5 expr : expr. expr (3) %entry% : expr. (4) $end reduce 4 expr goto 7 state 8 expr : LAM ID DOT. expr (2). error expr goto 9 state 3 expr : ID. (1). reduce 1 state 6 expr : LAM ID. DOT expr (2) DOT shift 8. error state 9 shift/reduce conflict (shift 3, reduce 2) on ID (shift 4, reduce 2) on LAM expr : LAM ID DOT expr. (2) expr : expr. expr (3) $end reduce 2 expr goto 7 state 4 expr : LAM. ID DOT expr (2) ID shift 6. error state 7 shift/reduce conflict (shift 3, reduce 3) on ID (shift 4, reduce 3) on LAM expr : expr. expr (3) expr : expr expr. (3) $end reduce 3 expr goto 7 5

Fill in the following table to show the steps taken to parse the input string LAM ID DOT ID $end. That is, on each row list (a) the state stack, with the top of the stack on the left; (b) the input with a. indicating what input characters have been scanned and which remain to be scanned; and (c) the action that leads to the next state stack; an action should either be shift or reduce n, where n is a production number. Stop filling out the table when you reach the action reduce 4. We ve filled out some of the table to get you started. (Note that there may or may not be some extra rows in the table.) State stack Input Action 1. LAM ID DOT ID $end shift 4, 1 LAM. ID DOT ID $end d. (5 points) The parser generated from this grammar has shift/reduce conflicts in state 7. What specific problem with the grammar is exhibited by these conflicts? The parser generator has resolved the conflict by shifting on ID or LAM ; will this resolution lead to correct parsing, according to the conventions of lambda calculus? Explain. Answer: The conflicts in state 7 are due to ambiguity in the grammar, because we have not specified the associativity of function application. In this case, shifting is incorrect, as it corresponds to right associativity, whereas in lambda calculus, function application should be left-associative. 6

Question 3. Operational semantics and type checking (34 points). typed lambda calculus to include lists and a corresponding type: Consider extending the simply e ::= n x λx.e e e [] e :: e hd e tl e t ::= int t list t t A ::= x : t, A Here [] is the empty list; e 1 :: e 2 creates a list with head e 1 and tail e 2 ; the expression hd e returns the head of list e; and the expression tl e returns the tail of list e. The type t list is the type of a list whose elements have type t. Here are the type rules for this language: Int A n : int Var x dom(a) A x : A(x) Lam x : t, A e : t A λx.e : t t App A e 1 : t t A e 2 : t A e 1 e 2 : t Nil A [] : t list List A e 1 : t A e 2 : t list A e 1 :: e 2 : t list Hd A e : t list A hd e : t Tl A e : t list A tl e : t list a. (10 points) Write down a derivation that shows (λx.λy.x :: tl y) : int int list int list. Answer: y : int list, x : int y : int list y : int list, x : int x : int y : int list, x : int tl y : int list y : int list, x : int (x :: tl y) : int list x : int (λy.x :: tl y) : int list int list (λx.λy.x :: tl y) : int int list int list b. (4 points) Consider the following operational semantics rule: hd (e 1 :: e 2 ) e 2 Show that this rule is incorrect by showing that using it will cause Preservation to be violated, i.e., give an expression e such that A e : t and e e but A e : t. Explain your answer briefly, and explain how to fix the above rule. Answer: Consider hd (1 :: []). This expression has type int, but if we evaluate it one step using the above rule, the result is [], which has type int list. The rule should have e 1 on the right-hand side of the arrow. 7

c. (10 points) Suppose we were to add floating point numbers to our language, with appropriate subtyping rules: e ::= f n x λx.e e e [] e :: e hd e tl e t ::= float int t list t t Refl t t Sub-base int float Sub-fun t 2 t 1 t 1 t 2 t 1 t 1 t 2 t 2 Sub-list t 1 t 2 t 1 list t 2 list For each of the following subtyping relationships, write yes or no to indicate whether or not the relationship holds according to the rules above. int int float int int list list float list list int float float float int int int float int float d. (10 points) Write down an OCaml data type typ and a function is subtype t1 t2 : typ -> typ -> bool that returns true iff and only t1 is a subtype of t2 according to the rules above. Answer: type typ = TFloat TInt TList of typ TFun of typ * typ let rec is_subtype t1 t2 = match t1, t2 with TFloat, TFloat TInt, TInt TInt, TFloat -> true TList t1, TList t2 -> is_subtype t1 t2 TFun (t1, t1 ), TFun(t2, t2 ) -> (is_subtype t2 t1) && (is_subtype t1 t2 ) _ -> false 8