Scoping Exercise

Purpose:

Exercise:

1. Question #10 from the textbook. Consider the following program:

procedure Main is
    X, Y, Z : Integer;
    procedure Sub1 is
        A, Y, Z: Integer;
        procedure Sub2 is
            A, B, Z : Integer;
            begin -- of Sub2
              . . .
            end; -- of Sub2
        begin -- of Sub1
          . . .
        end; -- of Sub1
    procedure Sub3 is
       A, X, W : Integer;
       begin -- of Sub3
            . . .
       end;  -- of Sub3
    begin -- of Main
      . . .
    end; -- of Main

List all the variables, along with the program units where they are declared, that are visible in the bodies of Sub1, Sub2 and Sub3, assuming static scoping is used.

2. Question #13 from the textbook.   Consider the following skeletal C program:

void fun1(void); /* prototype */
void fun2(void); /* prototype */
void fun3(void); /* prototype */
void main() {
    int a, b, c;
    . . .
}

void fun1(void) {
    int b, c, d;
   . . .
}

void fun2(void) {
    int c, d, e;
    . . .
}

void fun3(void) {
    int d, e, f;
    . . .
}

Given the following calling sequences and assuming that dynamic scoping is used, what variables are visible during execution of the last function called? Include with each visible variable the name of the function in which it was defined.

a. main calls fun1; fun1 calls fun2; fun2 calls fun3
b. main calls fun1; fun1 calls fun3
c. main calls fun2; fun2 calls fun3; fun3 calls fun1
d. main calls fun3; fun3 calls fun1
e. main calls fun1; fun1 calls fun3; fun3 calls fun2
f. main calls fun3; fun3 calls fun2; fun2 calls fun1

Specific Requirements

This exercise is worth 2 points.

Submit

Submit your .txt or .doc file on Blackboard.