Docsity

Final Exam Problems - Programming Languages | CSC 4101, Exams of Programming Languages

Partial preview of the text

Download Final Exam Problems - Programming Languages | CSC 4101 and more Exams Programming Languages in PDF only on Docsity!

Final Exam

CSC 4101, Fall 2004

7 December 2004

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!

Name:

The following grammar

-> IDENT | DOT IDENT

-> INTCONST | | DOT IDENT | LBRACK RBRACK

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 so it can be parsed with a recursive descent parser. Do not use EBNF.

(c) (5 pts) Rewrite so it can be parsed with a recursive descent parser assuming that is a terminal symbol. Do not use EBNF.

(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.)