CHAPTER 4
NAMES, BINDING, TYPE
CHECKING, AND SCOPES
Imperative
programming languages are abstractions of the von Neumann Computer
architecture. The architecture’s two
primary components are its memory and its processor. The abstractions in a language for the memory cells of the
machine are variables.
·
A
variable can be characterized by a collection of properties, or attributes, the
most important of which is type, a fundamental concept in programming
languages.
Design
Issues:
·
What
is the max. length of a name?
·
Can
connector characters be used in names?
·
Are
names case sensitive?
·
Are
the special words reserved words or keywords?
Name
Forms:
·
A
name is a string of characters used
to identify some entity in a program.
·
The
commonly acceptable name form is a string with a long length limit, if any,
with some connector character such as the underscore (_) included.
·
In
some languages, like C, C++, and Java, uppercase and lowercase letters in names
are distinct, that is names in these languages are case sensitive.
Special
Words:
·
Special
words in programming languages are used to make programs more readable by
naming actions to be performed. They
also are used to separate the syntactic entities of programs.
·
Keyword is a word of programming
languages that is special only in certain context
·
Reserved word is a special word of a
programming language that cannot be used as a name.
Variables
·
A
program variable is an abstraction of a computer memory cell or collection of
cells.
·
A
variable can be characterized as a sextuple of attributes:
(name, address, value, type,
lifetime, scope)
NAME
·
Variable
names are he most common names in programs
·
Names
are often referred as identifiers.
ADDRESS
·
The
address of a variable is the memory
address with which it is associated.
·
In
many languages, it is possible for the same name to be associated with
different addresses at different places and at different times in the program.
·
It
is possible to have multiple identifiers reference the same address
·
When
more than one variable name can be used to access a single memory location, the
names are called aliases.
·
Aliasing
makes program verification more difficult
·
Two
pointer variables are aliases when they point to the same memory location. The same is true for reference variables
·
Aliasing
can be created in many languages through subprogram parameters
TYPE
·
The
type of a variable determines the
range of values the variable can have and the set of operations that are
defined for values of the type
Value
·
The value of the variable is the contents of the memory
cell or cells associated with the variable
·
When we use the term memory cell we mean abstract
memory cell
THE CONCEPT OF BINDING
- A BINDING is an
association, such as between an attribute and an entity or between an
operation and a symbol
- The time at which a
binding takes place is called binding
time
- Bindings can take place
at language design time, language implementation time, compile time, or
run time.
BINDING OF ATTRIBUTES TO VARIABLES
- A binding is static if it occurs before run time
and remains unchanged throughout program execution. If it occurs during run time or can
change in the course of program execution, it is called dynamic.
TYPE BINDINGS
VARIABLE
DECLARATION:
- An explicit declaration is a statement in the program that lists
variable names and specifies that they are a particular type
- An implicit declaration is a means of associating variables with
types through default conventions instead of declaration statements
- Both implicit and
explicit declarations create static bindings to types
- declarations specify
types and other attributes but do not cause allocation of storage
- definitions specify
attributes and cause storage allocation
DYNAMIC TYPE BINDING
- In dynamic type
binding, the type is not specified by a declaration statement. Instead, the variable is bound to a
type when it is assigned a value in an assignment statement.
- the primary advantage
of dynamic binding of variables to types is that it provides a great deal
of programming flexibility.
- there are 2 disadvantages
to dynamic type binding:
- First, the error
detection capability of the compiler is diminished relative to a compiler
for a language with static type bindings, because any two types can appear
on opposite sides of the assignment operation.
- in a language with
dynamic type binding, no error is detected by the compiler or run-time
system.
- in a language with
static type binding, the compiler would detect the error and the program
would not get to execution.
- The second advantage of
dynamic type binding is cost. The
cost of implementing dynamic attribute binding is considerable,
particularly in execution time.
STORAGE BINDING AND ALLOCATION
- The memory cell to
which a variable is bound is somehow must be taken from a pool of
available memory. This process is
called allocation. Deallocation
is the process of placing a memory cell that has been unbound from a
variable back into the pool of available memory.
- The lifetime of a variable is the time
during which the variable is bound to a specific memory location.
- Static variables are those that are
bound to memory cells before program execution begins and remain bound to
those same memory cells until program execution terminates.
- One advantage of static
variables is efficiency.
- One disadvantage of
static binding to storage is reduced flexibility
- Another disadvantage is
that storage cannot be shared among variables
- Static-dynamic variables are those whose storage
bindings are created when their declaration statements are
elaborated. The disadvantages are
the run-time overhead of allocation and deallocation and the fact that
locals cannot be history sensitive.
- Explicit heap-dynamic variables are nameless (abstract)
memory cells that are allocated and deallocated by explicit run-time
instructions specified by the programmer.
- Implicit heap-dynamic variables are bound to heap
storage only when they are assigned values. The advantage of such variables is that they have the
highest degree of flexibility, allowing high generic code to be written. The disadvantage is the run-time
overhead of maintaining all the dynamic attributes. Another disadvantage is the loss
of some error detection by the
compiler.
TYPE CHECKING
- Type checking is the activity of ensuring that the
operands of an operator are of compatible types
- A compatible type is one that is either legal for the operator
or is allowed under language rules to be implicitly converted by
compiler-generated code to legal type.
This automatic conversion is called coercion. A type error is the application of
an operator to an operand of an inappropriate type.
STRONG TYPING
- A strongly typed
language is one in which each name in a program in the language has a
single type associated with it, and that type is known as compile
time. The essence of this definition
is that all types are statically bound.
The weakness of this definition is that it ignores the possibility
that the storage location to which it is bound may store values of
different types at different times.
We define a programming language to be strongly typed if type errors are always detected.
TYPE COMPATIBILITY
- There are two types of
compatibility methods: name compatibility and structure
compatibility. Name type compatibility means that
2 variables have compatible types only if they are in either the same
declaration or in declarations that use the same type name. Structure
type compatibility means that 2 variables have compatible types if
either types have identical structures.
SCOPE
- The scope of a program variable is the
range of statements in which the variable is visible. A variable is visible in a statement if it can be referenced in that
statement.
STATIC SCOPE
- Binding names to
non-local variables is called static
scoping. Static scoping is
thus named because the scope of the variable can be statically determined,
that is prior to execution.
BLOCKS
- A concept introduced in
Algol 60 allows a section of code to have its own local variables whose
scope is minimized. Such variables
are stack dynamic, so they have their storage allocated when the section
is entered and deallocated when the section is exited. Such a section of code is called a block.
EVALUATIOIN OF STATIC SCOPING
·
It is convenient to view the structure of program as a
tree in which each node represents a procedure and thus a scope.
- A programmer could by
mistake call a subprogram that should not have been callable, which would
not be detected as an error by the compiler.
DYNAMIC SCOPE
- Dynamic scoping is based on the
calling sequence of subprograms, not on their spatial relationship to each
other. But the scope can be
determined only at run time.
EVALUATION OF DYNAMIC SCOPING
Several problems come from dynamic scoping:
- First, during the time
span beginning when a subprogram begins its execution and ending when that
execution ends, the local variables of the subprograms are all visible to
any other executing subprogram, regardless of its textual proximity. There is no way to protect local
variables from this accessibility.
Subprograms are always executed in the immediate environment of the
caller; therefore dynamic scoping results in less reliable programs than
static scoping.
- Also, dynamic scoping
makes programs much more difficult to read, because the calling sequence
of subprograms must be known to determine the meaning of references to non-local
variables. This can be impossible
for a human reader.
The
referencing environment of a
statement is the collection of all names that are visible in the statement.
A named constant is a variable that is
bound to a value only at the time it is bound to storage; its value cannot be
changed by assignment or by an input statement.