Problem set 2

Assigned: Feb. 3.
Due: Feb. 17

Problem 1

Consider the following pseudo-code. (The syntax is like C, but with lexical embedding of fuctions.) Assume that the language uses static scoping.
int I,J;                      // Global variables

void F()                      // F has global scope
{ int I;
  void G()                    // G is lexically inside F
  { int K;

    void H()                  // H is lexically inside G
      {  I = J + K;           // Body of H 
      }                       // end of H

     K=2;                     // Body of G
     J = J+8;      
     if (I < 8) F();           
   }                          // end of G

  G();                        // body of F
}                             // end of F

void G()                      // G has global scope.
{ I = I+30;                   // Body of G.
}                             // end of G

main() { 

A. What does this program print?

B. Show the structure of the stack at the point when it has the maximal number of activation records on it. Be sure to indicate:

Problem 2

Consider the following pseudo-code
int I = 1;
    J = 2;

void F()
{  I = I+2;
   J = J+2;

void G()
{  int I;
   I = 1;

void main() { G(); }

A. If variables are statically scoped, what is printed?

B. If variables are dynamically scoped, what is printed?

Problem 3

Let M be a two-dimensional array of integers. Assume 0-based indexing and row-major ordering. Let Q be a function that contains a reference to (a) M[2,3] and (b) M[I,J]. Describe how the compiled code allocates the space for M and locates the address corresponding to these references in each of the following cases: (You need not describe how variables I and J are located; you can assume that they are simply available, in registers or whatever. You need not worry about bounds checking.)

In all but (E) below, assume that A is a "true" two-dimensional array, and not an array of pointers to arrays. In all but (F), assume that the language uses static scoping.

Note: The answer has 12 parts: Each of M[2,3] and M[I,J] for each of A-F below.