






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
Material Type: Exam; Professor: Kamin; Class: Progrmg Languages & Compilers; Subject: Computer Science; University: University of Illinois - Urbana-Champaign; Term: Spring 2010;
Typology: Exams
1 / 11
This page cannot be seen from the preview
Don't miss anything!







A front-end
B back-end
C lexer
D parser
F symbol table
G optimization
H code generation
(a) Give the following translations. (You may use functions getloc() and getlabel() to get fresh memory locations and fresh instruction labels, respectively.) i. [e 1 + e 2 ]
let t1, t2, t3 = getloc() in [e 1 ]t [e 2 ]t t3 = t1 + t
ii. [e 1? e 2 : e 3 ]x (for full credit, use the short-circuit scheme for e 1 )
[e 1 ]L1,L L1: [e 2 ]x JUMP L L2: [e 3 ]x L3:
iii. [e 1 && !e 2 ]Lt,Lf (e 2 should not be evaluated if e 1 is false)
[e 1 ]L1,Lf L1: [e 2 ]Lf,Lt
(b) (5 pts extra credit) Give IR code for a for loop. A for loop has the form “for(S 1 ; e; S 2 ) S 3 ”, where S 1 is executed before the loop begins, the loop ends when e evaluates to false, S 2 is executed at the end of each iteration of the loop, and S 3 is the loop body. For full credit, use the short-circuit scheme for e.
L2: [e]L1,L L3:
(a) Give the type of the following function: fun f -> fun g -> fun x -> g (f x) x
(α → β) → (β → α → γ) → α → γ
(b) Write an OCaml function update such that update f a b is a function that returns b when given a as input but otherwise behaves the same as f.
let update f a b = fun x -> if x = a then b else f x
(c) Write an OCaml function double that duplicates each element of a list, using fold right instead of explicit recursion. For example, double [1; 2; 3] = [1; 1; 2; 2; 3; 3]. Remember that fold right has type (α - > β - > β) -> α list -> β - > β.
let double lis = fold right (fun x y -> x :: x :: y) lis []
(d) Write an OCaml function sum pairs that takes a list of pairs and returns a list containing the sum of the elements of each pair, using map instead of explicit recursion. For example, sum pairs [(1, 2); (3, 4); (5, 6)] = [3; 7; 11].
let sum pairs = map (fun (x, y) -> x + y)
(e) (5 pts extra credit) Write an OCaml function maxf that takes a function f and a list lst and returns a pair (max, index), where max is the largest value produced by applying f to an element of lst, and index is the index in lst of the element x such that f x = max, where the first element of the list has index 0. If there are multiple such elements, you may return the index of any one of them. For example, maxf (fun x -> x + 2) [1; 2; 3] = (5, 2). You may assume that lst is never empty. You may also assume that f takes elements of lst and returns only positive integers. Your function should use fold right instead of explicit recursion.
let maxf f lst = fold right (fun x (m, i) -> if f x > m then (f x, 0) else (m, i+1)) lst (0,0)
apply pos f lst = map (fun x -> if x > 0 then f x else x) lst For simplicity, we assume that lst is a list of integers. As in the OCaml code, your Java solution should call Map.map, which is given here:
interface IntFun { int apply (int n); }
class Map { static int[] map (IntFun f, int lis[]) { int lis2[] = new int[lis.length]; for(int i = 0; i < lis.length; i++) lis2[i] = f.apply(lis[i]); return lis2; } }
class Apply_Pos { static int[] apply_pos (final IntFun f, int lis[]) { // complete this method IntFun g = new IntFun(){ int apply(int n){ return n > 0? f.apply(n) : n; } }; return Map.map(g, lis); } }
APL Reference
Operation Expression Value
Sample data A ; a 2,3-matrix
V ; a 3-vector 2 4 6 C ; a logical 2-vector 1 0 D ; a logical 3-vector 1 0 1 Arithmetic A *@ A
V -@ (newint 1) 1 3 5 Relational A >@ (newint 4)
Reduction !+ V 12 maxR A 3 6 Compression D % V 2 6 C % A 1 2 3 (a 1,3-matrix) Shape shape A 2 3 Ravelling ravel A 1 2 3 4 5 6 ravel (newint 1) 1 Restructuring rho (shape A) V
rho (shape V) C 1 0 1 Catenation A ^@ C 1 2 3 4 5 6 1 0 Index generation indx (newint 5) 1 2 3 4 5
Transposition trans A
Subscripting V @@ (indx (newint 2)) 2 4 A @@ (newint 1) 1 2 3 (a 1,3-matrix) (trans A) @@ (indx (newint 2))