









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 2008
CS 164: Final Examination (with corrections)
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 15 pages.
a. When using character sets that support it, allow programmers to write ≤ in place of <=, ≥ in place of >=, and → in place of - >.
b. Introduce a statement “until E: suite” that is just like the while construct, but loops until E becomes true rather than false.
c. Cause a fatal error when an integer multiplication overflows.
expr → term | expr OP term term → primary | term primary primary → ID | ‘(’ expr ‘)’ | ‘λ’ VAR ‘.’ ‘(’ expr )’
Here, everything in single quotes or capital letters is a terminal symbol, and everything else is a non-terminal. Fill in a recursive-descent parser for this language. The function next() returns the lexer’s current token, and scan(T ) checks that next() is T (causing an error if not) and then reads the next token in the input (thus changing the value of next()). The lexer returns the token EOF when it runs out of tokens. The grammar as written is not quite suitable for direct conversion to recursive descent. Feel free to add additional functions or take other steps to correct for this. We are interested only in recognizing correct programs, not in translating them
def expr ():
def term ():
def primary ():
Continue on next page, if needed.
Continue here, if needed.
if x < y goto L1 L3: push 3 if x > 0 goto L push 4 push 3 call(1) f push 4 goto L2 call(1) f L1: goto L push 5 L4: push 6 call(2) g L2:
Here, push C means “push the constant C on the stack, ” and call(n) h means “call h, popping the top n elements of the stack and passing them as its actual parameters, and leaving the value on the stack upon return.” The left-hand example results in two different possible stack sizes at L2, depending on whether the program branches to L1. The right-hand example causes the stack size to increase by two on each iteration through the loop. Describe how to use global flow analysis to check this property of a bytecode program. That is, show a modification of one of the methods we used in lecture for constant propagation or dead code elimination to solve the problem of computing the size of the stack at each point in a procedure (where ‘⊤’, meaning inconsistent, is one of the possible “sizes”). We assume that the stack has size 0 at the start of every procedure (that is, we give each procedure its own private stack). We’re interested in a reasonably high-level description, so give sufficient detail to convince us that you know what you’re talking about.
Continue on next page, if needed.
p p / s p / e p / i < e > p / i < i > s / i < i > d / i < i > i f / i < i > i < f > / i < i > i < # f > / i < i > i < # i > / i < i >
a. [1 point] Read top to bottom, is this a leftmost, rightmost, or reverse rightmost deriva- tion?
b. [1 point] Which symbols are terminal symbols and which are non-terminals?
c. [2 points] What is the parse tree corresponding to this derivation?
d. [2 points] Reconstruct as much of the BNF grammar corresponding to this parse as possible.
e. [1 point] Show an alternative parse tree for the same sentence, thus showing that the grammar is ambiguous.
a. Assume also that we introduce a construct (similar to one in Python) that allows Tuple- valued expressions of this form:
( E for v in L )
meaning “the tuple whose elements are computed by evaluating expression E with variable v set to each item in L (a sequence-valued expression) in turn.” The scope of variable v is limited to this construct (it is independent of any v declared outside). For example,
(2*i for i in xrange (0, 5)) == (0, 2, 4, 6, 8) (x[1:] for x in ("the", "quick", "brown", "fox")) == ("he", "uick", "rown", "ox")
The value of L may be Tuple(T ) or List(T ) (in which case, v will take on values of type T ), or Xrange (in which case v will take on integer (Int) values). Provide typing rules for this construct, using the Prolog notation from lecture. We are looking for rules for typeof(tuplegen(E, V, L), T, Env), where tuplegen(E, V, L) is the AST for this type of expression. Use ≤ to denote the subtyping relation (as in Int ≤ Any). Your rules (combined with rules for other constructs in Pyth), should allow us to con- clude that (2*i for i in xrange(0,5)) is both a Tuple(Int) and a Tuple(Any).
b. The typing rule
Tuple(X) ≤ Tuple(Y) :- X ≤ Y.
(where, again, ≤ is intended to mean “is a subtype of”) turns out to work without any problem. However, the analogous rule
List(X) ≤ List(Y) :- X ≤ Y.
will cause problems. That is, we will eventually run into a little trouble if we allow an assignment x = E whenever x is declared to have static type List(A) and expression E has dynamic type List(B) where B is a subtype of A but not the same type. Why is this? Why is it a problem for Lists, but not Tuples?
a. What exactly might go wrong if you failed to initialize the space reserved to hold return values to some valid Pyth value, such as None?
b. We required that all variables on the stack be initialized to valid Pyth values. As it turns out, any values will do, not just None. So why not save time and initialize the entire stack to None values, so that all the variables came pre-initialized? Since the other code in Pyth programs always write only valid Pyth values, wouldn’t this guarantee that all variables always have valid values without having to initialize each time? Assume that we know an upper limit to the stack size and that we change Pyth semantics to say that until we assign into a local variable, its value is some arbitrary (but valid) Pyth value.