COMS W4115
Programming Languages and Translators
Homework Assignment #2
Submit solutions in pdf format on
     by 2:40pm, April 22, 2015



  1. 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.
    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 < 20 && i > 30 || 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
         int fib(n) {
           if (n == 0)
             return 0;
           else if (n == 1)
             return 1;
             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 returned value, the argument, the control link (which is a pointer the caller's AR), and the return address.
    3. As a function of n, what is the time complexity of this program?

  4. 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;
                 e = b + a;             // point 2
               }; // end of h
               a = d + e;               // point 3
             }; // end of g2
           }; //end of f
           // execution of main begins here
         }; // end of main
      Suppose we have activation records with the following fields:
      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.
    1. 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.
    2. To which declarations are the references to variables a, b, c at point 1?
    3. To which declarations are the references to variables a, b, e at point 2?
    4. To which declarations are the references to variables a, d, e at point 3?

  6. 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
    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.

  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. 4/13/2015