Programming Languages and Translators

Homework Assignment #2

Submit solutions in pdf format on

Courseworks/COMSW4115/Assignments

by 2:40pm, April 23, 2014

- You may discuss the questions with others but your answers must be in your own words.
- Problems 1-5 are each worth 20 points.
- Solutions to these problems will be posted on Courseworks on April 30, 2014.
- This assignment may submitted electronically on Courseworks by 2:40pm, April 30, 2014 for 50% credit.
- Be sure to put your name and uni on your answers.

- Consider the syntax-directed definitions in Figs. 6.19, 6.36 and 6.37 in ALSU for expressions, if-statements and booleans.
- 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 < 10 && i > 20 || 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 - 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 actual parameter, the control link (which is a pointer the caller's return address), the return address, and the return value. - Consider the arithmetic expression
`v * (w - x) + y / z`

and a register machine with instructions of the form - 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 following sequence of three-address code:
- Draw a flow graph for this three-address code.
- Optimize this code by eliminating common subexpressions, performing reduction in strength on induction variables, and eliminating all the induction variables that you can. State what transformations you are using at each optimization step.
- 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.

```
int fib(n) {
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
return fib(n-1) + fib(n-2);
}
```

```
LD reg, src
ST dst, reg
OP reg1, reg2, reg3 // the registers need not be distinct
```

```
x = 0
i = 0
L: t1 = i * 4
t2 = a[t1]
t3 = i * 4
t4 = b[t3]
t5 = t2 * t4
x = x + t5
i = i + 1
if i < n goto L
```

aho@cs.columbia.edu