Documente Academic
Documente Profesional
Documente Cultură
m
o
c
.
rs
Names, Bindings,
Type Checking, and
Scopes
E
O
g
n
a
a
F
o
D
e
e
in
Introduction
Imperative languages are abstractions of
von Neumann architecture
A machine consists of
m
o
c
.
rs
e
e
Memory - stores both data and instructions
n
i
Processor - can modify
the contents of
g
n
memory
E
O
Variables are
used as an abstraction for
o
D
memory
cells
a
a
For
F primitive types correspondence is direct
For structured types (objects, arrays) things
are more complicated
Names
Why do we need names?
m
o
c
.
rs
e
e
Design issues for names:
n
i
Maximum length?ng
E
What characters
are allowed?
O
o
Are names
case sensitive?
D
a and Java names are case sensitive
C,
C++,
a
F this is not true of other languages
Are special words reserved words or
keywords?
Length of Names
If too short, they cannot be
m
o
connotative
c
.
s
r
Language examples: e
e
n
FORTRAN I: maximum
6
i
g
n
COBOL: maximum
30
E
O
FORTRAN
90
and ANSI C: maximum 31
o
D Java: no limit, and all are
Ada a
and
a
significant
F
C++: no limit, but implementers often
impose one
m
o
c
.
rs
e
e
in
g
n
E
O
o
D
a
a
F
Variables
A variable is an abstraction of a memory
cell
Variables can be characterized as a
sextuple of attributes:
m
o
c
.
rs
Name
Address
Value
Type
Lifetime
Scope
e
e
in
a
a
F
o
D
E
O
g
n
Variable Attributes
Name - not all variables have them
m
o
c
Address - the memory address
with
.
s
r
which it is associated (l-value)
e
e
n
i
Type - allowed range
of values of
g
n set of defined
variables andE
the
O
operations
o
Dthe contents of the location
a
Value
a
F which the variable is associated
with
(r-value)
n
E
O
o
D
a
Binding
time is the time at which a
a
F
binding takes place.
m
o
c
.
rs
e
e
in
g
n
E
O
a
a
F
o
D
Type Binding
How is a type specified?
m
o
c
.
rs
e
e
in
g
n
E
O
a
a
F
o
D
Type Checking
Generalize the concept of operands and
operators to include subprograms and
assignments
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 compilergenerated code, to a legal type
A type error is the application of an operator
to an operand of an inappropriate type
m
o
c
.
rs
e
e
in
g
n
E
O
a
a
F
o
D
m
o
c
.
rs
e
e
in
g
n
E
O
a
a
F
o
D
m
o
c
.
rs
e
e
in
g
n
E
O
Ada is almost
o
D CONVERSION is loophole
UNCHECKED
a
a
Java
F is similar
Coercion
The automatic conversion between
m
o
c
types is called coercion. s.
r
e weaken
Coercion rules they can
e
n
i
typing considerably
g
n
E
C and C++ allow
both widening and
O
o
narrowing
coercions
D
a
Java
allows only widening coercions
a
F
m
o
c
.
rs
e
e
in
g
n
E
O
o
D
list = 17.3;
This provides a lot of flexibility
But
a
a
F
m
o
c
.
rs
e
e
Allocation - getting a cell
from some pool of
n
i
g
available cells
n
E
Deallocation - putting
a cell back into the
O
pool
o
D
a
Depending
on the language, allocation
a
canFbe either controlled by the
programmer or done automatically
Categories of Variables by
Lifetimes
Static--lifetime is same as that of the
program
Stack-dynamic--lifetime is duration of
subprogram
Explicit heap-dynamic -- Allocated and
deallocated by explicit directives,
specified by the programmer, which take
effect during execution
Implicit heap-dynamic--Allocation and
deallocation caused by assignment
statements
m
o
c
.
rs
e
e
in
g
n
E
O
a
a
F
o
D
Variable Scope
The scope of a variable is the range of
statements over which it is visible
The nonlocal variables of a program unit
are those that are visible but not declared
there
The scope rules of a language determine
how references to names are associated
with variables
Scope and lifetime are sometimes closely
related, but are different concepts
m
o
c
.
rs
e
e
in
g
n
E
O
a
a
F
o
D
Static Scope
The scope of a variable can be
determined from the program text
To connect a name reference to a
variable, you (or the compiler) must find
the declaration
Search process: search declarations, first
locally, then in increasingly larger
enclosing scopes, until one is found for
the given name
Enclosing static scopes (to a specific
scope) are called its static ancestors; the
nearest static ancestor is called a static
m
o
c
.
rs
e
e
in
g
n
E
O
a
a
F
o
D
m
o
c
.
rs
e
e
in
g
n
E
In Ada: unit.name
O
o
In C++:D
class_name::name or ::name for
a
globals
a
F
In Java: this.name for variables declared at
class level
i
g
n
E
O
MAIN
A
o
D
C
a
a
F
MAIN
D
C
B
E
e
Put D in B (but thenin
C can no longer
g
call it and D cannot
access A's
n
variables) OE
o
Move the
data from B that D needs to
D
a(but then all procedures can
MAIN
a
F
access them)
Comments
Using nested functions with static
scoping often encourages many globals
m
o
c
.
s
not considered to be good programming
r
e
practice
e
n
i
Current thinking is that
we can accomplish
g
the same thing better
with modules (classes)
n
E
Static scoping
is still preferred to the
O
o
alternative
D
a
Most
current languages use static scoping at
a
F block level even if they don't allow nested
the
functions
n
i
scoping rules aregsimilar to those of
n
nested functions
E
O
o
D
a
a
F
Dynamic Scope
Based on calling sequences of program
units, not their textual layout (temporal
versus spatial)
References to variables are connected to
declarations by searching back through the
chain of subprogram calls that forced
execution to this point
This is easy to implement but it makes
programs hard to follow
Used in APL, SNOBOL, early versions of LISP
m
o
c
.
rs
e
e
in
g
n
E
O
o
D
a
a
F
Scope Example
MAIN
m
o
- declaration of x
SUB1
- declaration of x ...
call SUB2
...
a
a
F
o
D
...
call SUB1
g
n
E
O
SUB2
...
- reference to x ...
c
.
rs
SUB2 uses x
Scope Example
Static scoping
c
.
rs
Reference to x is to MAIN's x
Dynamic scoping
m
o
e
e
in
g
n
Reference to x is to SUB1's x
E
Evaluation of
O Dynamic Scoping:
o
D convenience
Advantage:
a
a
Disadvantage:
poor readability
F
Referencing Environments
The referencing environment of a
statement is the collection of all names
that are visible in the statement
In a static-scoped language, it is the
local variables plus all of the visible
variables in all of the enclosing scopes
In a dynamic-scoped language, the
referencing environment is the local
variables plus all visible variables in all
active subprograms
m
o
c
.
rs
e
e
in
g
n
E
O
o
D
a
a
F