



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!




Scan the whole exam first (there are a total of 7 pages) and plan your time. You have 1 hour, 50 minutes to complete all five questions. There are a total of 100 points. The exam is open book, open notes, and closed neighbors. Good Luck and Have a Cool Yule!
The following grammar
defines a subset of Java expressions. E.g., it allows expressions of the form
a.b.c[1].x[2].y.z[3][4][5]
(a) (5 pts) What are the problems (if any) with this grammar that might prevent us writing a recursive descent parser for this grammar.
(b) (5 pts) Rewrite
(c) (5 pts) Rewrite
(d) (5 pts) Is the above grammar ambiguous? If yes, provide an input string that can be parsed in more than one way. If no, explain why not. Is your modified grammar ambiguous?
Consider the following program fragment in C-style syntax, but with static scoping:
void test() { int a[5]; int i;
void f(int x) { a[i] = 9; i++; i = x; }
i = 1; a[1] = 7; a[2] = 4; f(a[i]); // print i and a[1] }
What are the values of i and a[1] after function f returns if the parameter is passed by
i a[1]
value reference copy-in-copy-out need
For each of the following two ML functions, state in English what the function does and translate the function into Scheme. The types of the ML functions are provided in comments for reference.
(a) (10 pts) The function foo takes a function and a list as argument and returns a list. (* val foo = fn : (’a -> ’b) -> ’a list -> ’b list *) fun foo f nil = nil | foo f (h::t) = f h :: foo f t
(b) (10 pts) The function bar takes a binary function, a value, and a list as arguments and returns a value. (* val bar = fn : (’a * ’b -> ’b) -> ’b -> ’a list -> ’b *) fun bar f b nil = b | bar f b (h::t) = f (h, bar f b t)
The operator :: corresponds to cons in Scheme.
Hint: for finding out what the functions do, take ’a and ’b to be int, pick some simple argument values that fit the types, and trace the functions.
(Additional space for answering question 5.)