1. Show the contents of the stack at its highest point using a) static links and b) displays.
1 program P2(output); 2 var A: integer; 3 procedure S (procedure T(var X: integer)); forward; 4 procedure Q (var B: integer); 5 var C: integer; 6 begin 7 C := B + A; 8 writeln(C); 9 if B > 0 then S(Q) 10 end; 11 procedure S; 12 var C: integer; 13 begin 14 C := 0; 15 T(C) 16 end; 17 begin 18 A := 7; 19 Q(A) 20 end.
1 program P3(output); 2 var Z: integer; 3 procedure T(procedure U); 4 begin 5 Z := Z - 2; 6 U 7 end; 8 procedure Q(procedure R(procedure S); var X: integer); 9 var Y: integer; 10 procedure V; 11 begin 12 X := X + 1 13 end; 14 begin 15 Y := 2; 16 X := Z - Y; 17 R(V) 18 end; 19 begin 20 Z := 6; 21 Q(T,Z); 22 writeln(Z) 23 end.
2. There are many possible binding times for nonlocal references in procedure parameters. Given the following program, for each possible general binding rule you can devise: state the rule; give the output of the program using the rule; and give the contents of the stack at its highest point.
1 program Main(input, output); 2 var A, B: integer; 3 procedure P(procedure W); 4 var A: integer; 5 begin 6 A := 10; 7 W; 8 writeln(A) 9 end; 10 procedure Q; begin A := A + 2 end; 11 procedure R; 12 var A: integer; 13 procedure S(A: integer; procedure X); 14 begin 15 if A = 5 then S(6,X) else P(X); 16 writeln(A) 17 end; 18 begin 19 A := 20; 20 S(5,Q); 21 writeln(A) 22 end; 23 begin 24 A := 1; 25 R; 26 writeln(A) 27 end.
For example, one possible rule is to bind variable references to the most recently allocated copies of the variables. The output of the program would be 12, 6, 5, 20, 1. No display entries are need in any activation records to support this binding method.
3. Using static links, show the stack at its highest point for the program in problem 7 of homework 12.