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

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

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

UNIVERSITY OF CALIFORNIA

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

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

LECTURE 3. Compiler Phases

Syntax Errors; Static Semantics

Lecture 16: Static Semantics Overview 1

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

1 Lexical Considerations

Syntax-Directed Translation. Lecture 14

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

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

Lexical Considerations

Lexical Considerations

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

Introduction to Parsing. Lecture 5

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

A Simple Syntax-Directed Translator

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

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

CS 6353 Compiler Construction Project Assignments

MIDTERM EXAMINATION - CS130 - Spring 2003

Problem Score Max Score 1 Syntax directed translation & type

Introduction to Parsing. Lecture 5

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

CS164: Midterm I. Fall 2003

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

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

Midterm II CS164, Spring 2006

CSCE 531, Spring 2015 Final Exam Answer Key

The SPL Programming Language Reference Manual

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

Question Points Score

Semantic Analysis. Lecture 9. February 7, 2018

The role of semantic analysis in a compiler

COP 3402 Systems Software Syntax Analysis (Parser)

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

CS 6353 Compiler Construction Project Assignments

CSE 401 Midterm Exam 11/5/10

Bottom-Up Parsing. Lecture 11-12

Writing Evaluators MIF08. Laure Gonnord

Lecture #31: Code Generation

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

CS558 Programming Languages

CS606- compiler instruction Solved MCQS From Midterm Papers

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Decaf Language Reference

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

Code Generation. Lecture 12

CSE 413 Final Exam. June 7, 2011

Compiler Construction Assignment 3 Spring 2018

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

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

Bottom-Up Parsing. Lecture 11-12

CS558 Programming Languages

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

Compilers Project 3: Semantic Analyzer

CS664 Compiler Theory and Design LIU 1 of 16 ANTLR. Christopher League* 17 February Figure 1: ANTLR plugin installer

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

A programming language requires two major definitions A simple one pass compiler

Syntax and Grammars 1 / 21

programming languages need to be precise a regular expression is one of the following: tokens are the building blocks of programs

Context-Free Grammars

Lecture #19: Code Generation

CSE 401 Midterm Exam Sample Solution 11/4/11

Intermediate Languages and Machine Languages. Lecture #31: Code Generation. Stack Machine with Accumulator. Stack Machines as Virtual Machines

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010

ECE251 Midterm practice questions, Fall 2010

The Environment Model. Nate Foster Spring 2018

EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing. Görel Hedin Revised:

CSE 3302 Programming Languages Lecture 2: Syntax

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

Section A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous.

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

cmps104a 2002q4 Assignment 3 LALR(1) Parser page 1

Principle of Compilers Lecture VIII: Intermediate Code Generation. Alessandro Artale

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

CS143 Final Fall 2009

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

CSE 401 Final Exam. December 16, 2010

Compiler Design. Computer Science & Information Technology (CS) Rank under AIR 100

CPS 506 Comparative Programming Languages. Syntax Specification

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

The PCAT Programming Language Reference Manual

2.2 Syntax Definition

Properties of Regular Expressions and Finite Automata

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

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

CSE 5317 Midterm Examination 4 March Solutions

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

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

CS5363 Final Review. cs5363 1

COP4020 Programming Assignment 2 Spring 2011

flex is not a bad tool to use for doing modest text transformations and for programs that collect statistics on input.

CSCE 531 Spring 2009 Final Exam

CS558 Programming Languages. Winter 2013 Lecture 3

G Programming Languages - Fall 2012

COP4020 Programming Assignment 2 - Fall 2016

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

Transcription:

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division CS 164 Spring 2009 P. N. Hilfinger CS 164: Final Examination (corrected) Name: Login: You have three hours to complete this test. Please put your login on each sheet, as indicated, in case pages get separated. Answer all questions in the space provided on the exam paper. Show all work (but be sure to indicate your answers clearly.) The exam is worth a total of 50+ points (out of the total of 200), distributed as indicated on the individual questions. You may use any notes or books you please, but not computers, cell phones, etc. anything inanimate and unresponsive. We suggest that you read all questions before trying to answer any of them and work first on those about which you feel most confident. You should have 8 problems on 14 pages. 1. /10 5. /5 2. /6 6. / 3. /5 7. /4 4. /10 8. /10 Login of person to your left: TOT /50 Login of person to your right:

Login: 2 1. [10 points] For each of the following possible modifications to a fully functional system for compiling and executing our Python dialect, tell which components of the compiler and run-time system would have to be modified: lexical analyzer, parser and tree-generator, static semantic analyzer, standard prelude, intermediate-code generator, machine-language code generator, and run-time libraries. In each case, indicate a minimal set of components from this list that would have to be changed, and indicate very briefly what change would be needed. When you have a choice of two equal-sized sets of modules that might reasonably be changed, choose the one that makes for the simplest change or whose modules appear earlier in the list (e.g., prefer changing the lexical analyzer to the parser, if either change would be about equally difficult). a. Allow the following program to execute indefinitely without blowing up: while True: L = [] i = 0 while i < 1000: L.append (i) i += 1 b. Allow arbitrary delimiters on strings, analogously to PHP, so that <<<DELIM <CHARS >DELIM >>> is a string containing CHARS, where DELIM is any sequence of characters other than < and > that does not appear anywhere in CHARS. c. Introduce the built-in function hex, which converts any integer into a hexadecimal string.

Login: 3 d. Allow attribute references E.x (including in method calls, as in E.x(...)) only in cases where E is guaranteed either to have the value None or have a value for which the x attribute is defined, rejecting other references before any execution of the program. e. Allow programmers to write things like def f(): body of f enddef so that one may optionally mark the end of a def with the keyword enddef at the same indentation level as the def.

Login: 4 2. [6 points] The term extended BNF refers to ordinary BNF augmented with regularexpression-like constructs. In this problem, we ll look at one form of it and how it can be converted to ordinary BNF. In this version, one can write rules with standard right-hand sides such as A : B ; A: c D ; D : ; /* EMPTY */ A : Q R f ; plus extended forms such as this: A1 : B* ; /* "An A1 may be formed from 0 or more Bs" */ A2 : B ( C D ) ; /* "An A2 may be formed from a B followed by either a C or a D." */ A3 : B ( C d E )* ; and so forth. These new forms add no new power, since we can convert them to ordinary BNF. For example, we could rewrite A2 s definition as A3 : B Q ; Q : C D ; In this problem, you are to write a Bison grammar for this extended BNF notation (a metagrammar) that translates it into regular BNF (that s right, we re going to parse Bison with Bison). To make this easy (or at least easier), we are going to allow your program to work iteratively. There will be a global variable, G, that holds sets of strings and that the semantic actions of your Bison program may modify. Each string in this set is to contain a Bison rule (either in plain Bison BNF or extended BNF). We will use your grammar as follows: def Convert(aGrammar): gram = agrammar G0 = the empty set while True: G = the empty set Parse gram with your Bison program gram = the grammar resulting from concatenating \ all the strings in G if G == G0: return gram G0 = G On the next page, write a grammar that recognizes an extended BNF definition as described above and whose semantic actions set G to an equivalent set of BNF rules (the same set if there are no extended BNF constructs in the input). Assume that the lexer returns the strings it matches as the semantic values (i.e., $1, etc.) of all terminal symbols. Assume in particular that the terminal symbol SYM matches any ordinary Bison terminal or nonterminal symbol (yes, that means it would allow input like c : d ; but just assume we only care about correct input). Finally, assume that there is a function, gensym(), that your actions may call to create new, previously unused nonterminal symbols. We won t be fussy about the notation you use in actions for string or set operations.

Login: 5 Grammar: /* Empty */ Grammar Rule ; /* Fill in the rest: All semantic values are strings, and actions * may modify the global variable G, a set of strings. Add any * other nonterminals you need. */ Rule:

Login: 6 3. [5 points] We d like to make conservative estimates of the maximum values of integer variables in a program. We ll assume a vastly simplified, statically typed programming language. The only expressions are C (for C any integer literal constant), v (for v an integer variable), and E 1 + E 2 (for E 1 and E 2 expressions). The only statements are Assignments: v = E; Conditionals: if E 1 E 2 then S 1 else S 2 fi, where S 1 and S 2 are lists of statements; Exit: return. At any point in the program, we d like to know (conservatively) the maximum possible values of all integer variables. At any such point, your analysis should give each variable either a constant upper bound (like 42) or, indicating that you cannot determine an upper bound. For example, your analysis should be able to annotate the following program as shown in its comments: # x <= ; i <= if i <= 99 then # i <= 99; x <= x = i + 1 # i <= 99; x <= 100 else # x <= ; i <= x = 2 # x <= 2; i <= i = 102 # x <= 2; i <= 102 fi # i <= 102; x <= 100 return a. How might a compiler use the results of such an analysis? Problem continues on next page.

Login: 7 b. Describe an appropriate analysis along the lines of the flow analyses described in lecture. c. Give an example in which your analysis gives an overly conservative bound on a variable. d. What would go wrong with your analysis if we introduced while loops into this language?

Login: 8 4. [10 points] Consider the Java code below: class Base { public void e() { /*e*/ class A extends Base { public void f(int r) { /*f_a*/ public int x; class B extends A { public void f(int r) { /*f_b*/ public void g() { /*g*/ public int y; public int z; void call_f(a ana) { ana.f(ana.x); A a = new A(); B b = new B(); call_f(a); call_f(b); This problem involves the implementation of the object-oriented mechanisms for this program. The language is Java; you should not introduce anything into your solution except what is needed for implementing Java (which, unlike Python, is completely statically typed). In part (b) below, we ask you to produce three-address code. Use the following notation, where r i indicates a register, O i indicates a virtual register or immediate constant, and L i indicates a label: r 0 := O 1 r 0 := O 1 O 2 where is one of the arithmetic operators. r 0 := *(r 1 + N) Load r 0 from memory whose address is r 1 + N, where N is an integer constant. *(r 0 + N) := O 1 Store O 1 to address r 0 + N. push O 1 pushes argument for function call. call *O 1 call to function whose address is value of O 1. if O 1 O 2 goto L 0 conditional jump ( is a comparison operator). goto L 1 unconditional jump. L 2 : Defines label. Questions begin on the next page.

Login: 9 a. Show what the objects pointed to by a and b look like. Also show any relevant virtual method tables. Choose a representation in which virtual method tables are as small as possible that is, just big enough to implement the classes Base, A, and B in Java. b. Provide an implementation of the body of function call f, excluding the function prologue and epilogue. Assume that the parameters to a function are in registers p 0, p 1, etc.

Login: 10 c. In Project #3, we did not make virtual tables as small as possible, but allocated space for all method names in the entire program. Below we ve added the use of an interface to the code sample from above. Fill the blanks to give a definition of class YourClass, followed by a use of it (statements or method declarations) that demonstrates how the virtual method dispatch scheme for single inheritance you used for part (a) is insufficient for interface method dispatch. Briefly explain why it is insufficient, and how the strategy used in Project #3 solves the problem. interface I1 { public void f(int r); class Base { public void e() { /*e*/ class A extends Base implements I1 { public void f(int r) { /*f_a*/ public int x; class YourClass { class B extends A implements I1 { public void f(int r) { /*f_b*/ public void g() { /*g*/ public int y; public int z;

Login: 11 5. [5 points] Consider the following program: while i > 0: c = a*b + i if c > b + i: a = c c = 0 i = i - 1 Assume that all quantities are known to be machine integers and that only the variable a is live after the loop. a. Convert this into the three-address code from problem 4 above. Assume that a, b, c, and i all denote registers and introduce any new registers you need. b. In your answer to (a), identify the basic blocks, clearly showing the first and last statement of each, and indicating edges between them in the CFG. c. Show which registers are alive at each point in the program (that is, at the beginning and before each assignment and each conditional jump). If this information indicates that statements can be eliminated, indicate where.

Login: 12 6. [1 point] Name a poem in which old trees in a forest are compared with ancient Celtic priests. 7. [4 points] In a statically typed language, consider a let statement that, as in Scheme, allows any number of variable definitions: let x = a * z, y = q ** 5 in f (x * y) Also, somewhat similarly to Scheme, we evaluate all the right-hand sides between the let and in keywords independently and then define the left-side identifiers to have those values (as constants) during the evaluation of the body the expression after the in keyword. The value of the body is the value of the let statement as a whole. Someone proposes the following typing rules for this construct: typeof(let([],body), T, Env) :- typeof(body, T, Env). typeof(let([bind(x, Expr) Defs], Body), T, Env) :- typeof(expr,t1,env), typeof(let(defs, Body), T, [def(x,t1) Env]). where, for example, the AST for the sample let statement above would be let([bind(x, AST for a*z), bind(y, AST for q**5)], AST for f(x*y)) a. What s wrong with this rule? Give an example where it rejects a program with no type errors or accepts an incorrect one. (Make any reasonable assumptions you want about the rest of the language.) b. Revise the rule to fix the problem.

Login: 13 8. [10 points] For each of the following questions, provide a short, succinct answer. a. How can you determine reliably whether a particular input has more than one parse? Will this tell you whether you have an ambiguous grammar? b. In calling a function in the project, one must generally provide a count of the number of arguments, either as an operand of the CALL instruction (in the intermediate language) or as an explicit parameter (in ia32 code). Why is this necessary in our dialect of Python, and why is it not necessary in Java (unless using exotic features such as java.lang.reflect)?

Login: 14 c. Consider a statically typed and statically scoped language in which all variables contain either integers or some kind of object pointer (but not functions), and in which function declarations may be nested. What language feature or combination of features still requires that we provide static links (or equivalent mechanism) at execution time and why? d. Typical Java implementations store a virtual-table pointer in each object and use this for method calls. What language features of full Python require a different approach and why? e. I d like to implement a feature of a certain highly dynamic language that allows me to change the set of instance variables and methods of all objects of a given type after these objects have been created. Imagine that in Java, after evaluating new Foo( ) some large number of times, I decide to modify the class Foo during execution of the program, add some instance variables to it, change some of its methods, and then continue executing so that some of the updated methods get called with objects created before the change. How might I do this? In particular, what existing mechanisms in Java might I be able to use to bring this about?