



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
Prof. R. Fateman Fall, 2002
SOLUTIONS CS 164 Final Examination: December 18, 2002, 12:30-3:30PM
A[[x]]σ = σ(x)
solution: evaluate x in the environment σ
b. In this statement A[[e 1 + e 2 ]]σ = A[[e 1 ]]σ + A[[e 2 ]]σ
there are two occurrences of the character +. Write one or perhaps two complete English sentences explaining what they mean. Some words or phrases you might or might not use include syntax, programming language, mathematical, operational, binary, assembler, token, AST, finite state, parser, not a clue.
Solution: The + on the left is the syntactic symbol in the programming language. The + on the right means mathematical addition.
c. What kinds of statements require ⊥ for their denotational semantics?
solution: This is used for statements that might not terminate: looping for exam- ple.
f. P 1 (x − 1) g. P 2 (x) h. P 3 (y) i. x > y j. y − 1 = x solutions : c,d,f,i
Solution: S->UV, U-> , U->aUb, V-> , V->cV
(b) Assume you have a lexical analysis program that will provide you, when you call it, with the next token on the input, and that it is a Lisp symbol like a, b, c, d, etc. It will return nil if there are no more tokens. You have a choice. Do (c-1) or (c-2). (c-1) Write an LALR grammar with augments that parses exactly this language and returns a pair of numbers n, m corresponding to the counts. Or (c-2) write a recursive descent parser that does the same thing. If you write both, we will not give you the maximum grade of the two. Here are two programs for recursive descent that you could use:
(defun peek()(car tokens)) ;look at next token
(defun eat(z) (or (eql z (pop tokens)) (error "illegal sentence")))
If you devise an LALR parser, for full credit you will have to figure out how to initialize the counts. Use the space below or on the reverse side of this page for your answer.
(defparameter mcount 0) (defparameter ncount 0) (defparameter tokens ’(a a a b b b c))
(defun peek()(car tokens)) ;look at next token
(defun eat(z) (or (eql z (pop tokens)) (error "illegal sentence")))
(defun S()(let ((mcount 0)(ncount 0)) (U)(V) (list mcount ncount)))
(defun U()(cond ((eql (peek) ’a)
Explain what would be wrong (if anything) with these alternative implementations, imple- menting IfExp in place, by
(IfExp (if (tig-interp(elt x 1))(tig-interp (elt x 2))(tig-interp (elt x 3)) env)) ;;A
or
(IfExp (tig-interp (if (equal 0 (tig-interp(elt x 1)))(elt x 3) (elt x 2) env)) ;;B
Hint: You might use words or phrases like Boolean, true, nil, environment, can’t tell, applicative order, normal order, correct, Go Bears.
There are many things wrong. You had to mention at least a few of them including (a) the version of true and false used in lisp is different from Tiger, so you must change nil/ non-nil to 0/1 as in (B). You need to put environment parameters in the calls to tig-interp and not in the if. There is also a missing right paren. in B. If you said something about normal order and “if” evaluating too many things you were wrong. Go Bears.
real procedure SIGMA (i,L,U,x); value L, U; integer i, L, U; real x; begin real s; s:=0 for i:=L step 1 until U do s:= s+x; SIGMA :=s end.
(a) Not all the arguments are call by name here. Which ones are and how do you know they are call by name?
solution: i, x because they do not appear in the value declaration are name pa- rameters.
(b) What does SIGMA(k,1,3,k*k) compute?
solution: 14
7(c) Does this call work: SIGMA(s,1,100,A[s])? (Note that there is a variable named s inside the SIGMA procedure.) Explain why or why not in one or more complete sentences. You may wish to use words or phrases like function, scope, think, thank, thunk, compiler, polymorphic, mysterious, scheme, ascii, aspic, not a clue.
Solution: Yes, this works perfectly because s and A[s] are passed in to the SIGMA program as thunks, and there is no conflict with the s that is local to SIGMA. Changing the parameter i is reflected in a change in the s in the thunk, which is used to compute A[s].
Solution: look at this discussion in Graham’s book. Sketch: (a) add :parent; (b) more than one parent, and requiring precedence to resolve conflicts; (c) space and speed are improved.
(a) How does Graham add inheritance to this model? (b) What is multiple inheritance, and how does this complicate matters? (c) In most object-oriented models there is a difference between the notion of a class and an instance. What advantages are there to making this distinction?
solution
(car p) = (p (lambda(z) (lambda(w) z))) (cdr p) = (p (lambda(z) (lambda(w) w))) ;; or similar
a := x ;;; partial solution shown here... L0: a := a*x n := n- d := a f := 34 jump L1 ;;; maybe dead code e := c ;;; certainly dead d := a ;;; certainly dead L1: b := d ;;; not dead, just pointless jumpz n L b := f L3: d := d - 1
Put a box around each basic block. Draw all control flow edges. Draw a line through dead code, assuming that only b and d are live after this portion of code has completed execution.