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


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. Assume the boolean operators &&, ||, and != have the customary associativities and precedences.
    1. Construct a parse tree for the C-like if-statement
    2. if( x < 10 && x > 20 || x != y ) x = 30;
    3. Show the values of all the attributes computed at each node in the parse tree by these SDDs.
    4. Show the three-address code produced for this if-statement.
    5. Can you see any ways in which the three-address code can be optimized?

  2. Write pseudcode for a function sequiv(exp1, exp2) that will test the structural equivalence of two type expressions exp1 and exp2. Show how your function computes sequiv(array(2, array(2, int)), array(2, array(3, int))).

  3. Let fib(n) be the function
  4. 
         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. Just show the return value, actual parameters, and the callerís frame pointer in each activation record.

  5. Give an example from some common programming languages to illustrate the difference between
    1. Normal-order evaluation and applicative-order evaluation.
    2. Call by reference and call by value.
    3. Lexical scope and dynamic scope.
    4. Stack allocation and heap allocation.
    5. A static type and a dynamic type.
    You can use different programming languages for each part.


aho@cs.columbia.edu