


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Questions and answers from uc berkeley cs164 midterm 2 held in fall 2002. Various topics related to the tiger programming language, including parsing, dfa recognition, lisp functions, and garbage collection. Students are required to write a short grammar, identify the components of the compiler that need to be changed for specific modifications, explain the behavior of logical expressions and typechecking in tiger, and determine where errors occur in given tiger programs.
Typology: Exams
1 / 4
This page cannot be seen from the preview
Don't miss anything!



UC Berkeley – CS164 Midterm 2
Fall 2002
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).
(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.
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?
(b) When is it called?
(c) What does r get bound to?
(d) What does (aug ’(1 2 3)) return?
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.
Lee needs to consider storage management, and needs your answers to the following questions in
complete English sentences.
a. How does the Tiger interpreter discussed in class deal with logical “and” and “or” expressions such as A&B|C?
b. Typechecking comparison (such as >) is more elaborate in Tiger than typechecking addition +. Explain why.
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?
6. [6 points] Explain in complete English sentences what steps are necessary in the Tiger
interpreter to interpret a Tiger expression “call” such as F(3,a).
3+4*(("big"<"little")>0)-9)
8. [4 points] What is the amortized cost (that is, the cost per reclaimed cell) of a 2-space copying garbage collector process in which, at time t 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 complete English sentences.
b. What happens if R = H /2?
9. [11 points] In the Leopard programming language, a language similar in some respects to
Tiger, the following program is legal. let function q(a,b)= let in a:=3;b:=7 end type ar=array of int var m:=ar [6] of 0 var i:= 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?
b. If Leopard has call-by-name parameter passing, what are the six values in the array m?
c. If Leopard has the same parameter passing as Tiger, what are the six values in the array m?
d. Why did we not write print(m[i])?
e. What prevents this program from being legal Tiger?