``````program p;
var a,b:integer;
c:real;
forward p2; {for procedure p1 to call procedure p2}
procedure p1 (x:integer; var y:integer);
var c:integer;
pt:^integer; {pointer declaration}
begin                            { (1) }
writeln(a,b,x,y);
new (pt);
if (x<2) and (y<2) then
if x>y then begin
y:=x; writeln(a,b,x,y); p2(x,b)
end;
dispose(pt);
end;
procedure p2 (x:integer; var y:integer);
begin                            { (2) }
writeln(a,b,x,y);
c:=a;
if (a<2) and (b<2) then
if x=y then begin
x:=y+1; writeln(a,b,x,y); p1(a,y)
end;
end;
begin                            { (3) }
a:=1; b:=0; c:=0.0;
p1(a,b);
writeln(a,b);
end;
``````

1) Find the output of the above PASCAL program using a) static b) dynamic scoping rules.

2) Show the lifetimes of the variables and parameters for the above program.

3) Write the environments of the above program using static and dynamic scoping rules at points (1), (2), and (3).

listen carefully :D

``````program p;
(*the main program's global section*)
uses crt;

var a,b:integer;
c:real;

(*p2 forward,so can be call from anywhere*)
procedure p2(x:integer; var y:integer); forward;

(*p1 declaration---------------------*)
procedure p1 (x:integer; var y:integer);
(*here is the local section of p1
these variables are lives inside p1
and wiped out when p1 ends its running*)
var c:integer;
pt:^integer; {pointer declaration}

begin { (1) } (*p1's body*)
writeln(a,b,x,y);
new (pt);
if (x<2) and (y<2) then
if x>y then begin
y:=x;
writeln(a,b,x,y);
p2(x,b);
end;
dispose(pt);
end;
(*end of p1--------------------------*)

(*main declaration of p2-------------*)
procedure p2 (x:integer; var y:integer);
begin { (2) }
writeln(a,b,x,y);
c:=a;
if (a<2) and (b<2) then
if x=y then begin
x:=y+1;
writeln(a,b,x,y);
p1(a,y);
end;
end;
(*end of p2-------------------------*)

(*here begins the main program*)
begin { (3) }
clrscr;
(*initialize the global vars*)
a:=1;
b:=0;
c:=0.0;
(*call p1 with a,and b parameters.*)
p1(a,b);(*a=1,b=0*)
(*the main program remember the position of calling
and jump up to the p1 procedure to take action*)
(*
First call of p1 with these parameters
a=1,b=0
p1(a,b);
so after that
p1 creates its vars
var c:integer;
pt:^integer;(it is contains a memoria address,that stores an integer value)
when the program reaches the p1's begin section then
p1 writes out the variables' data to the screen.
writeln(a,b,x,y); {like 1010}
then,we reserves a memoria place manually for our pointer
new (pt);
then p1 is analyzes the results as
if (x<2) and (y<2) then {if it is true,of course true 'cause x=1,and y=0}
the following commands are running
if x>y then begin {it is true too,'cause x=1 and y=0}
y:=x; {y's value changes to x's value,so y=1,till y is lives,the end of p1 running}
writeln(a,b,x,y);{these results appears on the screen: 1111}
then p1 is calling p2 to take action with its parameters.
{x=1 and b=1}
p2(x,b); {p2 running...}
p2 writes these results to the screen
writeln(a,b,x,y); {1111}
and then p2 adds a new value to the global variable c
c:=a;  {so c=1.0}
and p2 is checking ...
if (a<2) and (b<2) then  {a=1 ,b=1 so a<2=true,b<2=true,...true and true = true}
{so the following commands are running...}
if x=y then begin  {it is true ,so then}
x:=y+1;         {x's value changes to y+1=2,so x=2}
writeln(a,b,x,y);{the results are: 1121}
{p2 remembers its body's position and calls p1}
p1(a,y);{'cause the if statement result is true,p2 calls p1}
{a=1,y=1}
p1(a,y); is running...
p1 creates its vars
var c:integer;
pt:^integer;
when the program reaches the p1's begin section then
p1 writes out the variables' data to the screen.
writeln(a,b,x,y); {like 1111}
then,we reserves a memoria place manually for our pointer
new (pt);
then p1 is analyzes the results
if (x<2) and (y<2) then {it is true so commands follows..}
if x>y then begin {but it is false}
y:=x;
writeln(a,b,x,y);
p2(x,b);
end;
so p1 is wipe out the reserved area....
dispose(pt);
and then end follows,so jump back to p2
and p2 is ends too,so jump back to p1

end;
p1 is back,and continues its work
dispose(pt);{wipes out reserved area.. }
and when p1 is ends its running the controlling is back to the caller main
program
end;
*)

{the main program writes the results to the screen}
writeln(a,b);{11}
end.
(*and this is the end of the main program.*)

(*Created by FlamingClaw 2010.03.28.Hungary*)``````

Thank you so much Flaming Claw. next time i do on my own if i learn it well :D.