Docsity

CS 164: Final Examination - Modifications to a Compiler and Runtime System, Exams of Programming Languages

Toggle sidebar
bg1
UNIVERSITY OF CALIFORNIA
Department of Electrical Engineering
and Computer Sciences
Computer Science Division
CS 164 P. N. Hilfinger
Spring 2010
CS 164: Final Examination (revised)
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 9 problems on 15 pages.
1. /10
2. /4
3. /8
4. /6
5. /4
6. /
7. /4
8. /6
9. /8
TOT /50
Logins of person to your left: and to your right:
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download CS 164: Final Examination - Modifications to a Compiler and Runtime System and more Exams Programming Languages in PDF only on Docsity!

UNIVERSITY OF CALIFORNIA

Department of Electrical Engineering and Computer Sciences Computer Science Division

CS 164 P. N. Hilfinger Spring 2010

CS 164: Final Examination (revised)

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 9 problems on 15 pages.

TOT /

Logins of person to your left: and to your right:

  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. Change the scoping rules so that the bodies of compound statements (if, while, for) are declarative regions. For example, the following program is no longer legal:

def f(x, y): if x < y: a = x else: a = y return a

because the return statement is outside of the if construct and therefore outside the scope of a. However, an assignment inside a compound statement does not create a new declaration if a variable with that name is declared in an enclosing scope.

b. Allow the “infinite” looping construct:

while: S

which repeats the statements S until they exit with a break, return, etc.

e. Detect attempts to use the values of uninitialized variables for boxed types. By “use” here, we mean “try to use as a function” (as in v(...)), “select a method from” (as in v.f(...)), or “select an instance variable from” (as in v.a). You need not (but may) consider assignments of an undefined value to a variable as uses. The detection should not be conservative: detect exactly those cases where an uninitialized variable would actually be used.

  1. [4 points] Consider the following grammar:

p : s ⊣ s : ’[’ E ’]’ s ’/’ s s : V ’=’ E

Suppose that we have a shift-reduce parser for this grammar. Write a regular expression that describes the possible contents of the parsing stack just after a shift or a reduce. You do not need to include the final shifting of ⊣.

Put %token and %type definitions here, as needed

program : /* EMPTY */ | program inst NEWLINE ; Fill in the rest

  1. [6 points] We’d like to determine what statements in a program might blow up from dereferencing a null pointer. We’ll assume a vastly simplified, statically typed programming language. The only expressions are new (indicating a storage allocation of some appropriate type), v (for v a pointer variable), v.a (for v a pointer variable and a the name of a field), and null. The only statements are

Assignments: v = E or v.a = E, where E is an expression;

Conditionals: if E 1 == E 2 then S 1 else S 2 fi, where S 1 and S 2 are lists of (zero or more) statements and E 1 and E 2 are expressions;

Loops: while E 1 != null do S 1 od.

Whenever the program evaluates an expression v.m and v is null, the program “blows up” and control never reaches the next statement. At any point in the program, we’d like to know (conservatively) what variables might be null. For example, your analysis should be able to annotate the following program as shown in its comments:

1. x, y, L, q, z might be null

while L != null do

2. x, y, q, z might be null

x = new

3. y, q, z might be null

y.a = x

4. q, z might be null

z = x

5. q might be null

L = L.n

6. L, q might be null

if L == null then

7. L, q might be null

q = new

8. L might be null

else

9. q might be null

q = L

10. no variable can be null

fi

11. L might be null

od

12. x, y, L, q, z might be null

We’ll assume that we never know anything about the contents of fields (such as y.a); that is, your analysis need not track their contents. Questions begin on next page.

  1. [4 points] Ingrid Hackersdottir noticed that she could almost get the effect of dynamic variable binding in Python by converting definitions such as def f(x): ... h() ... def h() print x

into def f(x0): global x x = x ... h() ... def h() global x print x (where x0 is some variable name that is not used elsewhere in f.) This does cause h, when called, to see and print the right value for x (namely, the one established by the call to f). Unfortunately, this transformation does not quite implement dynamic scoping; it is missing a vital piece.

a. Give an example where this transformation does not work.

b. Describe how to fix the problem—that is, how to complete the implementation of dy- namic scoping within Python. Give sufficient detail for Ingrid to implement the proce- dure. For simplicity, assume that all functions have a single exit point (either a single return statement or the end of the function) and that there are no try blocks.

  1. [1 point] Who, responding to the Earl of Sandwich’s taunt, “Egad sir, I do not know whether you will die on the gallows or of the pox,” said “That will depend, my Lord, on whether I embrace your principles or your mistress.”?
  2. [4 points] In a statically typed language without subtypes (that is, without extends and implements in Java), consider the following looping construct:

for V = E 0 while C 0 incr E 1 do S od

meaning that we set V to the value of E 0 , and then execute S as long as C 0 is true, evaluating E 1 and assigning it to V after each iteration of S (and before re-testing C 0 ). Unlike C or Python, all expressions used for tests have type bool. The type of S is irrelevant, as long as it has a proper type. The type of the for as a whole is void. This statement defines variable V , whose scope then includes C 0 , E 1 , and S. Provide a typing rule for this construct, using the Prolog predicates typeof and defn as described in the lecture notes: typeof(V, T, E) means the static type of expression V is T in environment E (a list of the form [def(V 1 , T 1 ),.. .,def(Vn, Tn)] for variables Vi and types Ti); and defn(I, T, E) means that identifier I has static type T in environment E. Assume that there are already other rules describing the other constructs in the language. The AST of these loops in Prolog syntax has the form

for(V ′, E 0 ′, C 0 ′, E 1 ′, S′)

where the primed quantities are the ASTs for V , E 0 , etc.

Put diagram here

  1. [8 points] For each of the following questions, provide a short, succinct answer.

a. Demonstrate that a grammar can be ambiguous, even though there are strings with exactly one parse according to the grammar.

b. We chose (as do most implementations of similar OOP features) to put virtual table pointers in objects. But it is also possible to keep virtual table pointers in variables instead. That is, objects could contain only instance variables and each variable (local variable, instance variable, parameter, or global variable) could then contain a pointer to an object and another pointer to the virtual table pointer for that object. What advantages or disadvantages in performance can you see for such a scheme?