|
CS345H Fall 2007 |
|
Instructor: William
Cook |
Teaching Assistant:Ali
Ibrahim |
1. Show the activation records, with control and access links, that are created during the execution of this program:
fun g(f, x) {
return f(x) + x;
}
fun mul(a) = {
fun f(b) = a * b; // local function definition
return f;
}
val f2 = mul(2);
val r = g(f2, 5);
2. Consider the following function:
int B(int x, int y, int z)
{
if (x == 0)
return z;
else if (y == 0)
return B(x - 1, y, x + z);
else
return y * (B(x, y - 1, z));
}
a) Which function calls in this definition of B are tail calls?
b) Assuming that activation records are reused for tail calls, what is the maximum number of activation records on the stack (that is, depth of the stack) during the execution of B(5, 2, 0) ?
(note that this question does not ask you to compute the actual result of the function)
Here is a description of the project. If you need it,
here is a solution
to project one.
Please
follow these instructions on how
to turn in project 2. Following instructions is worth 10 points.
Homework #3: Due Tuesday Oct 2
1. Design and describe an algorithm for ensuring that every variable in Clite is assigned a value before it is used. Are there any cases where the program would execute correctly, but your algorithm will reject it has possibly using an undefined variable?
2. The intent of this code is to find the first all-zero row of an array. Is there way clean, structured way to write it in C or Java without using goto?
int n = 100;
int A[100][100];
int first = -1;
int i, j;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (A[i][j] != 0)
goto next;
}
first = i;
break;
next: ;
}
I have posted a working example with Flex and Cup. The directory
contains a build.bat and run.bat file, which contains a list of commands to build/run
the system. To run on windows, just type
build
run
If you are using
this on unix/Mac, you can use
sh
build.bat
sh
run.bat
BUT NOTE: you must change the “;” to a “:” in the classpath inside these two files.
Program #1: Parser due Tuesday,
September 25
Homework #1: DUE Tuesday September 11
From book: 2.6,
2.7, 2.8, 2.9, 2.10, 2.15
Also everyone code and run Euclid’s algorithm in Clite
using
Clite.jar (include print-out of execution).
See the book web
site to get the jar file. Example command line to run is:
java
-jar Clite.jar programs/factorial.cpp