Data Control
Attributes of Data Control
It is defined as control of transmission of data for each
operation of program.
Data control features determine the accessibility of data at
different points during program execution.
1.How data is provided for each operation
2.How the result of operation is saved
3.Later use as operand by a subsequent operation
Central problem:
the meaning of variable names, i.e. the correspondence between
names and memory locations.
Names and Referencing
Environments
Two ways to make a data object available as
an operand for an operation.
1.Direct transmission
2.Referencing through a named data object
Direct transmission
A data object computed at one point as
the result of an operation may be
directly transmitted to another operation
as an operand
Example: x = y + 2*z;
The result of multiplication is transmitted
directly as an operand of the addition
operation
Referencing through
a named data object
A data object may be given a name
when it is created,
the name may then be used to
designate it as an operand of an
operation.
Program elements that may be
named
A name is a string of character used to identify a data object
in a program.
For example:
Variables, Formal parameters, ,Subprograms,
Defined types
Defined constants
Labels
Exception names
Primitive operations
Literal constants
Simple name that designate an entire data structure e.g. an
identifier name
Composite name that designate a component of a data
structure e.g. Student[7].firstname
Associations and Referencing Environments
Association: binding identifiers to particular data objects
and subprograms.
• At the beginning of main program variables, subprogram
names are associated.
• During execution of main, referencing operation are
invoked.
• A new set of associations is created when a new
subprogram is called
• During execution of subprogram, referencing operation
are invoked.
• Set of associations are destroyed when subprogram
returns.
Referencing Environments
Referencing environment: the set of
identifier associations for a given
subprogram.
Referencing operations during program
execution: determine the particular data
object or subprogram associated with an
identifier
Local referencing
environment
The set of associations created on
entry to a subprogram
formal parameters,
local variables, and
subprograms defined only
within that subprogram
Non-local referencing
environment
The set of associations for identifiers
• used within a subprogram
• not created on entry to it
It means they are visible in subprogram but are not
locally declared.
Global referencing environment:
associations created at the start of execution of the main
program, available to be used in a subprogram
Predefined referencing environments:
predefined associations in the language definition
Global referencing environment
It is a set of identifier association created at the
start of execution of main program.
They can be used in subprogram
For example:
Global variables
Subprogram declared in main program
Predefined referencing
environment
It is a set of identifier association defined directly
in the language definition that may be used
without explicitly creating them.
For example:
1.Maximum integer value(MAXINT)
2. Subprogram such as read, write and Sqrt
Scope:
Scope of a program variable is the range of
statement in which the variable is visible.
Variable is visible if it can be referenced in that
statement.
Scope of a variable is from its declaration to end
reserved word of the procedure.
Lifetime:
Lifetime of a variable is the time during which
variable is bound to a specific memory location.
It is the period of time beginning when the
procedure is entered and ending when execution of
procedure reaches the end
There are two types of scopes:
1. Static scope
2. Dynamic scope
Static scope:
Scope of the variable is statically determined before
execution.
Example of static scope language: C, C++, Java,
Fortran
Importance:
1.The scope of a variable can be determined prior to
execution
2.Some variable declaration can be hidden from some
subprogram.
Scope
3.The static type checking makes the program execution
faster and more reliable.
4.The static scoping makes program easier to read and
understand.
5.It provide a method of accessing non-local variables.
6. It provide different set of specifications during translation
that makes execution of program more efficient.
7. It play an important role in design and implementation of
most PL e.g. C, Fortran, Pascal and Cobol.
Dynamic Scope
It is based on the calling sequence of subprograms,
not on the spatial relationship to each other.
Dynamic scope of an association for an identifier is
that set of subprogram activation in which the
association is visible during execution.
It can be determined only at the run time.
When the search of local declarations fail then the
declaration of dynamic parent , or calling procedure
is searched.
If search fail then search continue in that
procedure’s parent and so on, until a declaration for
variable is found.
If none is found in any ancestor, it is a run time error
Dynamic Scope
Advantages:
1. No need to determine the scope at compile time.
2.The correct attribute of non-local variables can be
easily determined at run time.
3. A statement in a subprogram can refer to different
non-local variables during different executions of
subprograms.
Dynamic Scope
Disadvantages:
1. There is no way to protect local variables from being
accessed during the execution of the subprogram.
2. It makes programs much more difficult to read.
3.The implementation of dynamic scope rule is costly.
4. It is unable to statically type checking references to
non-local variables.
Static and Dynamic Scope
The dynamic scope of an association for an
identifier:
• the set of subprogram activations in which the
association is visible during execution.
• tied to the dynamic chain of subprogram
activations.
The static scope of a declaration
the part of the program text where the
declared identifier is used.
Dynamic scope rules
Static scope rules
Dynamic scope rules :
Relate references with associations
for names during program execution
Static scope rules :
relate references with declarations
of names in the program text.
Block structure
Block-structured languages :
∙ Each program or subprogram is
organized as a set of nested blocks.
∙ Each block introduces a new local
referencing environment.
Subprogram A
Declaration of X
Declaration of Y
Subprogram B Static scope
Declaration of Y rules for
Declaration of Z block-
Use of Y
structured
programs
Use of X
Use of Z Hidden to A
Local Data and Local Referencing
Environments
Local environment of a subprogram:
various identifiers declared in the subprogram :
variables, parameters, subprogram
names.
Static scope rules: implemented by means
of a table of the local declarations
Dynamic scope rules:
Retention - Associations and the bound
values are retained after execution
Deletion - Associations are deleted
Implementation of dynamic
scope rules
By means of a local environment table to
associate names, types and values.
Retention: the table is kept as part of the
code segment
Deletion: the table is kept as part of the
activation record, destroyed after each
execution.