0% found this document useful (0 votes)
4K views

Local Data & Local Referencing Environments

The document discusses local environments and scope rules for procedures and variables in programming. It describes two approaches: 1) Static scope rules specify that a reference to a variable X in procedure Q refers to the local declaration of X in the header of Q. 2) Dynamic scope rules specify that a reference to X during execution of Q refers to the current activation of Q. The document also compares retention vs deletion of local environments between procedure calls.

Uploaded by

Simin Thekoot
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4K views

Local Data & Local Referencing Environments

The document discusses local environments and scope rules for procedures and variables in programming. It describes two approaches: 1) Static scope rules specify that a reference to a variable X in procedure Q refers to the local declaration of X in the header of Q. 2) Dynamic scope rules specify that a reference to X during execution of Q refers to the current activation of Q. The document also compares retention vs deletion of local environments between procedure calls.

Uploaded by

Simin Thekoot
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

LOCAL DATA & LOCAL REFERENCING

ENVIRONMENTS
• Local environment of a subprogram consists of
identifiers, variable names, subprogram
names .
• Example:
The local environment of the subprogram Q
contains the identifiers, variable names formal
parameters.
EX : FOR LOCAL REFERENCING ENVIROMENT

• procedure R;

end;
procedure Q;
var X; integer=30;
begin
write (X)
R;
X:=X+1;
end;
procedure P;

Q;

end;
• For local environments,
there are static scope rules & dynamic scope
rules.
• Static scope rules:
 Specifies that a reference to an identifier X in the
body of the subprogram Q is related to the local
declaration for X in the head of the program.
• Dynamic scope rule:
 Specifies that a reference to X during execution of
Q refer to the association for X in the current
activation of Q.
IMPLEMENTATION OF STATIC SCOPE RULES

• The compiler maintains a table of the local declaration


for the identifiers that appear in the head of Q
• On compiling the body of Q, compiler refers to local
declaration table whenever the declaration of an
identifier is required.
• Local environment table consists of pairs .
• Each pair containing an identifier and associated data
objects
• Each data object is represented as a type and its
location in the memory.
• procedure SUB(X:integer)
var Y:real;
Z:array [1..3] of real;
procedure SUB2; Local environment table in compile time
begin
NAME TYPE LVALUE CONTENT
… X integer value parameter
Y real local value
end{sub2}; Z real local array
begin -----------------------
descriptor[1,3]
… SUB2 procedure pointer to code
segment
end{sub}
IMPLEMENTATION OF DYNAMIC SCOPE
RULES
• Consider the subprogram P, Q & R.
• The local variable X is declared in Q.
The sequence is:
1. When P is execution, X is not visible to P because x is local to Q.
2. When P calls Q, X becomes visible with initial value 30.Q
executes X & prints value 30.
3. When Q calls R, the association of X becomes hidden.
4. R returns control to Q, association of X becomes visible.
5. Q resumes its execution, X is incremented & its new value is
printed ie, 31.
6. Q returns its control to P. The association of X is hidden.
EX : FOR LOCAL REFERENCING ENVIROMENT

• procedure R;

end;
procedure Q;
var X; integer=30;
begin
write (X)
R;
X:=X+1;
end;
procedure P;

Q;

end;
• We have two different meaning here,
• Retention: the association of X, is retained until
Q is called again. If the association is retained
then when Q is called second time, it have the
value 31. if it is called third time it will be 32 &
so on.
• Deletion: the association of X is deleted. When
Q is called again, a new data object is created
and assigned a initial value 30 and association is
recreated . Q prints 30 every time Q is executed.
IMPLEMENTATION OF RETENTION
• A single local environment table containing the retained
variables is allocated as a part of the code segment.
• Code segment is allocated statically and remain
throughout the execution.
• For example-If the initial value of the variable Y is 30, it
is stored in the storage allocated.
• If the local variable is declared at the start of the sub
program definition, the compiler can determine the size
of the variables & compute the offset of the variable
from the start of the code segment
procedure SUB(X:integer) ALLOCATION AND REFERENCING
OF RETAINED LOCAL VARIABLES
var Y:real;
Z:array [1..3] of real; EXECUTABLE CODE FOR
sub
procedure SUB2;
begin constants

retained value for X
end{sub2};
begin retained value for Y

… retained value for Z


end{sub}
EXECUTABLE
CODE FOR sub2
IMPLEMENTATION OF DELETION
• If the local environment of sub is to be deleted between
calls, the local environment table containing the deleted
variables is allocated as activation record for sub.
• If each deleted local variable is declared at the start of
the definition of sub, the compiler can determine the
number of variables & its size in the local environment
table & its offset
• A current-environment pointer is maintained which
points to the base address of the activation record in
the stack for the subprogram that is executing currently.
procedure SUB(X:integer) ALLOCATION AND REFERENCING
OF DELETED LOCAL VARIABLES
var Y:real;
activation record for
Z:array [1..3] of real; main
procedure SUB2;
begin return point in
calling activation record

r- value for X
end{sub2};
begin r- value for Y CEP
… r- value for Z
end{sub}
Activation record
for subprograms
called from sub
ADVANTAGE
1. Retention approach allows the programmer to
write subprograms that are history-sensitive ie,
their results are partially determined by the i/p &
local data values computed during previous
activations.
DISADVANTAGE
2. In retention Local environment tables for all
subprograms exist throughout execution . wastage
of storage space.
3. A variable that must be retained should be
declared nonlocal to the program.
ADVANTAGE
1. It provides saving of storage space in that
local environment table for those
subprograms which are in execution or are
suspended.
2. It does not allow local data to be carried over
from one call to next .
APPLICATIONS
• RETENTION : C, java, pascal, Ada, LISP, APL.
• DELETION : COBOL, some version of FORTRAN.

You might also like