Sample Program for demonstrating static vs. dynamic scoping


Consider the following Pascal-like program structure.

program CSC350;
  var X, Y, Z: integer;

  procedure Alpha;
    var X: integer;
  begin  // Alpha
    X := 1;
    Writeln(X, Y, Z);
    Y := X + Y + Z;
    Bravo;
    Writeln(X, Y, Z);
  end;  // Alpha

  procedure Bravo;
    var Y: integer;

    procedure Charlie;
      var Z: integer;
    begin  // Charlie
      Z := 10;
      Writeln(X, Y, Z);
      X := X + Y + Z;
      Alpha;
      Writeln(X, Y, Z);
    end;  // Charlie

  begin // Bravo
    Y := 100;
    Writeln(X, Y, Z);
    Z := X + Y + Z;
    Charlie;
    Writeln(X, Y, Z);
  end; // Bravo

begin //CSC350
  X := 1000;
  Y := 10000;
  Z := 100000;
  Writeln(X, Y, Z);
  Alpha;
  Writeln(X, Y, Z);
end. //CSC350

 
a. Give the first 10 lines that would be written by this program assuming static binding of scope.

i. Show the activation stack at the time the 10th. line is written.
ii. Check each executable statement in the program that is within the scope of the global variable Z.


b. Give the first 10 lines that would be written by this program assuming dynamic binding of scope.

i. Show the activation stack at the time the 10th. line is written.