# COMS W4115 Programming Languages and Translators Homework Assignment #4 Submit solutions electronically on     Courseworks/COMSW4115/Assignments     by 2:40pm, April 24, 2013

## Instructions

• Problems 1-4 are each worth 25 points.
• You may discuss the questions with the TAs and others in the class but your answers must be in your own words. You must not copy someone else's solutions. If you consult others or use external sources, please cite the people or sources used in your answers.
• Solutions to these problems will be posted on Courseworks on May 1, 2013.
• This assignment may submitted electronically on Courseworks by 2:40pm, May 1, 2013 for 50% credit.
• Pdf files are preferred.

## Problems

1. Consider the arithmetic expression `u * (v - w) + x / y` and a register machine with instructions of the form
2. ``````
LD reg, src
ST dst, reg
OP reg1, reg2, reg3      // the registers need not be distinct
``````
1. Draw an abstract syntax tree for the expression and label the nodes with Ershov numbers.
2. Generate machine code for the expression on a two-register machine minimizing the number of spills.

3. Consider the following sequence of three-address code:
4. ``````
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
``````
1. Draw a flow graph for this three-address code.
2. 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.

5. Consider the lambda-calculus expression (λu. (λx. u) u) ((λy. y) (λw. (λv.v) w) ).
1. Draw a parse tree for this expression.
2. Identify all redexes in this expression.
3. Evaluate this expression using normal order evaluation.
4. Evaluate this expression using applicative order evaluation.

6. Evaluate the lambda-calculus expression `(λx.(λy.(x(λx.xy))))y`. What order of evaluation did you use? Explain all the steps you used in the evaluation.

aho@cs.columbia.edu