COMS W4115
Programming Languages and Translators
Homework Assignment #2
Submit solutions in pdf format on
     Courseworks/COMSW4115/Assignments
     by 2:40pm, April 23, 2014


Instructions

Problems

  1. Consider the syntax-directed definitions in Figs. 6.19, 6.36 and 6.37 in ALSU for expressions, if-statements and booleans.
    1. State the associativities and precedences that the boolean operators &&, and ||, and the inequality operator != customarily have.
    2. Construct a parse tree for the C-like if-statement
    3. if( i < 10 && i > 20 || i != j ) i = 0;
    4. Show the values of all the attributes computed at each node in the parse tree by these SDDs.
    5. Show the three-address code produced for this if-statement.
    6. Can you see any ways in which the three-address code can be optimized?

  2. Let fib(n) be the function
  3. 
         int fib(n) {
           if (n == 0)
             return 0;
           else if (n == 1)
             return 1;
           else
             return fib(n-1) + fib(n-2);
         }
    
    1. Show the activation tree for fib(3).
    2. 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.

  4. Consider the arithmetic expression v * (w - x) + y / z and a register machine with instructions of the form
  5. 
         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.

  6. Consider the following sequence of three-address code:
  7. 
       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.

  8. Consider the lambda-calculus expression (λu. (λx. u) u) ((λy. y) (λw. (λv.v) w)).
    1. 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.
    2. Identify all redexes in this expression.
    3. Evaluate this expression using applicative order evaluation.
    4. Evaluate this expression using normal order evaluation.

aho@cs.columbia.edu