









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










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.
Logins of person to your left: and to your right:
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.
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
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:
while L != null do
x = new
y.a = x
z = x
L = L.n
if L == null then
q = new
else
q = L
fi
od
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.
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.
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
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?