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

Similar documents
UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Functional Programming. Pure Functional Programming

Principles of Programming Languages 2017W, Functional Programming

Semantic Analysis. Lecture 9. February 7, 2018

MIDTERM EXAMINATION - CS130 - Spring 2005

CS 415 Midterm Exam Spring SOLUTION

UMBC CMSC 331 Final Exam

Comp 311: Sample Midterm Examination

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

CS 415 Midterm Exam Spring 2002

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

1 Lexical Considerations

Syntax Errors; Static Semantics

CMSC 331 Final Exam Section 0201 December 18, 2000

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division

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

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Programming Languages Third Edition. Chapter 7 Basic Semantics

The SPL Programming Language Reference Manual

CS 415 Midterm Exam Fall 2003

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

Lexical Considerations

Time : 1 Hour Max Marks : 30

The PCAT Programming Language Reference Manual

11. a b c d e. 12. a b c d e. 13. a b c d e. 14. a b c d e. 15. a b c d e

ECE251 Midterm practice questions, Fall 2010

CSE413 Midterm. Question Max Points Total 100

COP4020 Spring 2011 Midterm Exam

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

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

PL Revision overview

CSE 401 Midterm Exam Sample Solution 2/11/15

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

Documentation for LISP in BASIC

UMBC CMSC 331 Final Exam

CSE 5317 Midterm Examination 4 March Solutions

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

CS Lecture 2. The Front End. Lecture 2 Lexical Analysis

R13 SET Discuss how producer-consumer problem and Dining philosopher s problem are solved using concurrency in ADA.

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Answer: Early binding generally leads to greater efficiency (compilation approach) Late binding general leads to greater flexibility

CSE P 501 Exam 8/5/04

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

A Small Interpreted Language

Lexical Considerations

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

CMSC 330, Fall 2018 Midterm 2

Introduction to Scheme

CS164: Midterm I. Fall 2003

Syntax-Directed Translation. Lecture 14

Programming Languages and Techniques (CIS120)

CMSC 331 Final Exam Section 0201 December 18, 2000

CS 360 Programming Languages Interpreters

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

Error Detection in LALR Parsers. LALR is More Powerful. { b + c = a; } Eof. Expr Expr + id Expr id we can first match an id:

Principles of Programming Languages COMP251: Syntax and Grammars

MIDTERM EXAMINATION - CS130 - Spring 2003

Question Bank. 10CS63:Compiler Design

The Structure of a Syntax-Directed Compiler

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Lexical Analysis. COMP 524, Spring 2014 Bryan Ward

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

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

CS 314 Principles of Programming Languages

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

CS143 Final Spring 2016

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design

COMP 181 Compilers. Administrative. Last time. Prelude. Compilation strategy. Translation strategy. Lecture 2 Overview

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

(Not Quite) Minijava

CS 164 Handout 16. Final Examination. There are nine questions on the exam, some in multiple parts. You have 3 hours to work on the

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

CS 321 Homework 4 due 1:30pm, Thursday, March 15, 2012 This homework specification is copyright by Andrew Tolmach. All rights reserved.

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

The role of semantic analysis in a compiler

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

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

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

CSE 401 Midterm Exam Sample Solution 11/4/11

2.2 Syntax Definition

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

Lecture 16: Static Semantics Overview 1

CS558 Programming Languages

Briefly describe the purpose of the lexical and syntax analysis phases in a compiler.

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

CS164: Programming Assignment 5 Decaf Semantic Analysis and Code Generation

A simple syntax-directed

A LISP Interpreter in ML

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

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

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

Transcription:

Midterm 2 Solutions 1. [20 points] Consider the language that consist of possibly empty lists of the identifier x enclosed by parentheses and separated by commas. The language includes { () (x) (x,x) (x,x,x) } etc. (a) Write a short grammar suitable for use with your LALR parser generator, including augments, so that you accept only these sentences. The sentence () should produce an AST that looks like (ExpList). The sentence (x,x,x) should look like (ExpList x x x). Many acceptable answers; one was the following: (defparameter g1 '( (S --> ( L ) #'(lambda (o l c) (cons 'ExpList l))) (L --> #'(lambda () nil)) (L --> x O #'(lambda (x o) (cons 'x o))) (O --> #'(lambda () nil)) (O --> \, x O #'(lambda (c x o) (cons 'x o))))) (b) Stephen claims this language can be recognized by a DFA with 5 states. Is he right? If so, draw the DFA. If not, explain why this cannot be. Yes. The DFA looks like this: State 1 (start state) -- Transition on ( to State 2 State 2 -- Transition on x to State 3 -- Transition on ) to State 5 State 3 -- Transition on, to State 4 -- Transition on ) to State 5 State 4 -- Transition on x to State 3 State 5 (final state) -- No outgoing transitions 2. [8 points] In assignment 3 you made extensive use of the program (defun aug(&rest r)(if (null (cdr r)) (car r) r)) a. What program calls aug? lalr-parser calls aug. More precisely: reduce-cat, a local function within lalr-parser, calls aug. b. When is it called? It is called when a reduction is performed. c. What does r get bound to? r is bound to a list of the results of parsing the constituents of the rhs of the rule. d. What does (aug (1 2 3)) return? (1 2 3)

3. [16 points] Henry considers implementing the following modifications to the Tiger programming language. For each proposed modification, identify the component(s) of the compiler that Henry will need to change. Select from the lexical analyzer (L), parser (P), type-checker (T), or interpreter (I). It is possible that no change is necessary at all (N). Sometimes more than one may be appropriate or possible. Unless it is utterly clear to you that there is only one possible answer that we are after, we recommend that you write your answer here and also explain on the reverse of this page why you said so. 1. Forbid the type definition type foo= {} P. Forbid in grammar. Or T/I: make typechecker/interpreter cause an error here. 2. Make the expression () identical to VOID. 3. Make the expression () identical to nil. P. Parser's augment for () should return nil. Or, have T/I to return nil on empty explist. 4. Allow arithmetic on Boolean results like (1>0)+(2<3). 5. Make the statement (a<b<c) mean the same as (a<b) & (b<c). P. Have our parser mangle the AST similarly to a&b. 6. Introduce a type Boolean with two values true and false, such that comparisons result in one of these values rather than 1 or 0. L/P/T/I: We can make true and false reserved words in the lexer, and modify the parser to require them in boolean expressions. This would also require modifications to the typechecker and interpreter to do logic only on booleans. This is the preferred answer. Alternatively, we can make the modifications only to the typechecker/ interpreter and leave true/false as non-reserved tokens. 7. Allow a recursive x.next:=x given that x is of type z= { val=int, next=z }. 8. Addition of a procedure shell() that takes no argument and opens up an interactive shell window, returning an integer return code when the shell command is done. T/I. We add this function to the initial environment in the typechecker as well as implementing the function in the interpreter. 4. [10 points] Lee is building an implementation of Tiger (not necessarily built on top of Lisp). Lee needs to consider storage management, and needs your answers to the following questions in compe English sentences. 1. At what point in a Tiger computation would a garbage collection likely be started? Hint: you ordinarily do a garbage collection when you need more memory but don t seem to have enough free. Even bigger hint: What kinds of statements in Tiger allocate heap space? Garbage collection would start when we try to allocate an array or record and do not have enough memory. 2. What kinds of statements in Tiger allocate space on a stack? Variable declarations in statements, function calls, and for loops all allocate space on the stack. 3. What kinds of statements in Tiger de-allocate space? Nothing deallocates memory on the heap, but return from a function as well as the "end" of a for/ all deallocate stack space. 4. If you wish to provide explicit heap memory management, what changes would you have to make to Tiger? We would need to implement many free()-like statements, one for each type, to implement explicit memory management. 5. Any discussion of garbage collection refers to the roots needed to start the operation. In a Tiger implementation using garbage collection, what are the roots? Live identifiers in accessible environments are roots of the garbage collection in tiger.

5. [9 points] a. How does the Tiger interpreter discussed in class deal with logical and and or expressions such as A&B C? The parser translates logical and/or operators to if statements, which are then interpreted as ordinary if statements by the interpreter. The interpreter never sees and/or only if statements. b. Typechecking comparison (such as >) is more elaborate in Tiger than typechecking addition +. Explain why. Ordering comparison operators (such as >) work on string or int. Equality comparison operators (= and <>) operate on any type, provided that both operands are of the same type. Arithmetic operators only work on int. c. What do you know about the type of x give than the expression if x=nil then 1 else 2 passes a semantic check? x is a record type. 6. [6 points] Explain in compe English sentences what steps are necessary in the Tiger interpreter to interpret a Tiger expression call such as F(3,a). - 3 and a are evaluated by recursive calls to the interpreter. - The formal parameters, say x,y of F are bound to 3, a respectively and added into the environment of F. - The body of F is interpreted in that environment

7. [16 points] For each of the following Tiger programs, determine where the processing will detect an error. If possible, indicate a location in the program text. Mark the location L if it will fail in Lexical Analysis. Mark the location P if it will fail in Parsing. Mark the location T if it will fail Type Checking. If there are no errors, write OK. Give some explanation as to why the failure occurs. If you think there are several errors such that if you correct the first one, there are still others, mention them all. If you are not sure what will happen, explain that too, and maybe we can give you some partial credit. --01--- type t1={a:int,b:t1} type t2={c:t1} var x:t2:=nil in x.c.b.a end No errors (OK) - program fails at run-time only. --02---- type t1=int var x:t1 in x:= 3 end P, line 2: var x:t1 is not a legal variable declaration; it lacks an initializer. --03--- var x:=5 in x:=3+4^5 end L, line 1: ^ is not a legal token or part of a legal token in Tiger. --04--- type a= {x:int, y:int} type b= {x:int, y:int} type c=a var i:a :=(x=1,y=2} var j:b :=nil var k:c :=nil in b:=j; k:=j end P, line 4: ( is unmatched. T, line 7: b:=j is illegal because b is a type, not a variable. T, line 7: k:=j is illegal because k and j are different types. --05--- type intlist ={hd:int, tl:intlist} type b=c type c=b in 1234 end T, line 3/4: b and c are illegal mutually recursive types. --06--- type intarray = array of intarray var M:intarray := array[10] of M in 5676 end T, line 1: intarray is an illegal recursive type. P, line 2: illegal syntax for array initialization. --07--- function f(a:int):int=3*a*a end in print(f(2)); "done" end P, line 1: end token without matching /in. T, line 2: print is of type string -> void, but f returns int. --08--- 3+4*(("big"<"little")>0)-9) P, line 1: Unmatched ). There is no "T" error in this program.

8. [4 points] What is the amortized cost (that is, the cost per reclaimed cell) of a 2-space copying garbage collector processinwhich,attimet a garbage collection is begun, there are R live nodes and a total of H cells in the heap. a. Explain any additional symbols you introduce using compe English sentences. b. What happens if R = H/2? a) At time t, there are R live nodes and H total cells in the heap. A copying GC divides the heap in two equal sections, so the time to perform the GC is: (R * c) / (H/2 - R) where c is the cost of copying a single cell from one section of the heap to the other. b) If R=H/2, we see that the cost of a GC approaches infinity; in other words, the GC fails to free any memory and must either ask the OS for more memory or exit with an error. 9. [11 points] In the Leopard programming language, a language similar in some respects to Tiger, the following program is legal. function q(a,b)= in a:=3;b:=7 end type ar=array of int var m:=ar [6] of 0 var i:=2 in q(i, m[i+2]); for i:=0 to 5 do print(chr(m[i]+ ord("0"))) end a. If Leopard has call-by-reference parameter passing, what are the six values in the array m when they are printed? m[4] is 7; other elements are 0 b. If Leopard has call-by-name parameter passing, what are the six values in the array m? m[5] is 7; other elements are 0 c. If Leopard has the same parameter passing as Tiger, what are the six values in the array m? All six elements of m are 0 d. Why did we not write print(m[i])? print() requires a string argument, not an int. e. What prevents this program from being legal Tiger? q(a,b) is not properly type declared. The declaration should be q(a:int,b:int):int or something similar.