- Consider the syntax-directed definitions in Figs. 6.19, 6.36 and 6.37 in ALSU
for expressions, if-statements and booleans.
Add an appropriate translation rule for
E → num
to Fig. 6.19.
- State the associativities and precedences that the boolean operators
&&
,
and ||
, and the inequality operator !=
customarily have.
- Construct a parse tree for the C-like if-statement
if( i < 20 && i > 30 || i != j ) i = 0;
- Show the values of all the attributes computed at each node in the parse tree
by these SDDs.
- Show the three-address code produced for this if-statement.
- Can you see any ways in which the three-address code can be optimized?
- Let
fib(n)
be the function
int fib(n) {
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
return fib(n-1) + fib(n-2);
}
- Show the activation tree for
fib(3)
.
- Show the activation records that are on the run-time stack
when
fib(1)
is invoked for the first time
during the invocation of fib(3)
. Just show four
fields in each activation record: the returned value, the
argument, the control link (which is a pointer the caller's AR),
and the return address.
- As a function of n, what is the time complexity of this program?
- Consider the following program written in a hypothetical
statically scoped
language that allows nested functions. In this program,
main calls f which calls g2 which calls h
which calls g1.
function main() {
int i;
function f() {
int a, b, c;
function g1() {
int a, d;
a = b + c; // point 1
}; // end of g1
function g2(int x) {
int b, e;
function h() {
int c, e;
g1();
e = b + a; // point 2
}; // end of h
h();
a = d + e; // point 3
}; // end of g2
g2(1);
}; //end of f
// execution of main begins here
f();
}; // end of main
- Suppose we have activation records with the following fields:
- Returned value
- Arguments
- Control Link
- Access Link
- Return Address
- Local data
- If function p is nested immediately within function q, then the
access link in any AR for p points to the most recent AR for q.
- Show the values in the relevant fields of the activation records on the run-time stack
when execution first arrives at point 1 in the program above.
- To which declarations are the references to variables a, b, c at point 1?
- To which declarations are the references to variables a, b, e at point 2?
- To which declarations are the references to variables a, d, e at point 3?
- Consider the arithmetic expression
u * (v - w) + x / y
and a register machine with instructions of the form
LD reg, src
ST dst, reg
OP reg1, reg2, reg3 // the registers need not be distinct
- Draw an abstract syntax tree for the expression and label the nodes with Ershov numbers.
- Generate machine code for the expression on a two-register machine
minimizing the number of spills.
- Consider the lambda-calculus expression
(λu. (λx. u) u) ((λy. y) (λw. (λv.v) w))
.
- Draw a parse tree for this expression using the grammar
E → λ var.E | E E | ( E ) | var
and the conventional associativities and precedences of lambda expressions.
- Identify all redexes in this expression.
- Evaluate this expression using applicative order evaluation.
- Evaluate this expression using normal order evaluation.