Structure of Programming
Languages
Names, Bindings, Type Checking, and
Scopes
The Concept of Variables
• What do x = 1 ; and x = x + 1; mean?
– “=“ is not the equal sign of mathematics!
– “x=1” does not mean “x is one” !
• “x” is the name of a variable; refer to a
variable
– A variable is an abstraction of a
memory cell
– “x” is an identifier (name) refers to a
location where certain values can be
stored.
2
Variables
• A variable is an abstraction of a memory
cell
• Variables can be characterized as a
sextuple of attributes:
–
–
–
–
–
–
Name
Address
Value
Type
Lifetime
Scope
1-3
Variables Attributes
• Name - not all variables have them
• Address - the memory address with which it is
associated
– A variable may have different addresses at different times during
execution
– A variable may have different addresses at different places in a
program
– If two variable names can be used to access the same memory
location, they are called aliases
– Aliases are created via pointers, reference variables, C and C++
unions
1-4
Variables Attributes (continued)
• Type - determines the range of values of
variables and the set of operations that
are defined for values of that type; in the
case of floating point, type also
determines the precision
• Value - the contents of the location with
which the variable is associated
• Abstract memory cell - the physical cell or
collection of cells associated with a
variable
1-5
Names
• Length
– Language examples:
• FORTRAN I: maximum 6
• COBOL: maximum 30
• FORTRAN 90 and ANSI C: maximum 31
• Ada and Java: no limit, and all are significant
• C++: no limit, but implementers often impose one
1-6
Names (continued)
• Connectors
– Pascal, Modula-2, and FORTRAN 77 don't allow
– Others do
1-7
Names (continued)
• Case sensitivity
– Disadvantage: readability (names that
look alike are different)
•worse in C++ and Java because
predefined names are mixed case (e.g.
IndexOutOfBoundsException)
– C, C++, and Java names are case
sensitive
•The names in other languages are not
1-8
Names (continued)
• Special words
– An aid to readability; used to delimit
or separate statement clauses
•A keyword is a word that is special only in
certain contexts, e.g., in Fortran
– Real VarName (Real is a data type followed with a
name, therefore Real is a keyword)
– Real = 3.4 (Real is a variable)
– A reserved word is a special word that
cannot be used as a user-defined
name
1-9
Variable Initialization
• The binding of a variable to a value at the
time it is bound to storage is called
initialization
• Initialization is often done on the
declaration statement, e.g., in Java
int sum = 0;
1-10
The Concept of Binding
• A binding is an association between a
name and the object its represents, such
as between an attribute and an entity, or
between an operation and a symbol
• Binding time is the time at which a binding
takes place.
1-11
The Concept of Binding
• Count = Count + 2
• Type of count is bound (compiler time)
• Set of possible Values of Count is bound
(compiler time)
• Meaning of “+” is bound (compiler time)
• Value of new Count is bounded (run time)
Possible Binding Times
• Language design time -- bind operator
symbols to operations
• Language implementation time-- bind
floating point type to a representation
• Compile time -- bind a variable to a type
in C or Java
• Load time -- bind a FORTRAN 77 variable
to a memory cell (or a C static variable)
• Runtime -- bind a nonstatic local variable
to a memory cell
1-13
Static and Dynamic Binding
• A binding is static if it first occurs before
run time and remains unchanged
throughout program execution.
1-14
Static and Dynamic Binding
• A binding is dynamic if it first occurs during
execution or can change during execution
of the program ( like stdin, and hostname
(for mobile code))
• In PHP
– List = [10.2, 3.5];
– ….
– List = 47;
Categories of Variables by Lifetimes
• The lifetime of a variable is the time during
which it is bound to a particular memory
cell
• Static--bound to memory cells before
execution begins and remains bound to the
same memory cell throughout execution,
e.g., all FORTRAN 77 variables, C static
variables
– Advantages: efficiency (direct addressing),
history-sensitive subprogram support
– Disadvantage: lack of flexibility (no recursion)
1-16
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 a
legal type
– This automatic conversion is called a coercion.
• A type error is the application of an operator to an
operand of an inappropriate type
1-17
Three Situations
1. Types are structurally equivalent, but
language requires name equivalence.
Conversion is trivial.
• Example (in C):
typedef number int;
typedef quantity int;
number n;
quantity m;
n = m;
Three Situations (cont’d)
2. Different sets of values, but same
representation.
• Example: subrange 3..7 of int.
• Generate run-time code to check for
appropriate values (range check).
Three Situations (cont’d)
3. Different representations.
• Example (in C):
int n;
float x;
n = x;
• Generate code to perform conversion at
run-time.
Name Type Compatibility
• Name type compatibility means the two
variables have compatible types if they are in
either the same declaration or in declarations
that use the same type name
• Easy to implement but highly restrictive:
– Subranges of integer types are not compatible with
integer types
– Formal parameters must be the same type as their
corresponding actual parameters (Pascal)
1-21
Type Checking
• Given all the type information, (and some inferences)
check for consistency:
– Assignment statement.
• Given v := e; and v : T1 and e : T2,
• We must verify T1 =: T2
– Return statement
• Given return e; and e : T,
• We must verify T is the return type of the enclosing
function.
– Conditional Expression
• Condition must be typed to bool.
• i.e. Given if c then e1 else e2
• We must verify c : bool
and if e1:T1 and e2:T2
we must verify T1 =: T2
22
Type Equivalence
• Structural equivalence
– Given
• Celsius is integer
• Fahrenheit is integer
• c : Celsius
• f : Fahrenheit
– Then, we can infer
• c + f : integer
– What about structures and unions?
• T1 * T2 =: S1 * S2 iff S1 =: T1 and S2 =: T2
• T1 + T2 =: S1 + S2 iff S1 =: T1 and S2 =: T2
– Arrays (or lists)?
• T[] =: S[] iff T =: S
23
Type Compatibility (continued)
• Consider the problem of two structured types:
– Are two record types compatible if they are
structurally the same but use different field
names?
– Are two array types compatible if they are the
same except that the subscripts are
different?
(e.g. [1..10] and [0..9])
– Are two enumeration types compatible if
their components are spelled differently?
– With structural type compatibility, you cannot
differentiate between types of the same
structure
(e.g. different units of speed,
1-24
Type Equivalence
• Structural equivalence:
– Consider Point = struct { x:Int, y:Int } and
FooBar = struct { a:Int, b:Int };
– then Point =: Int x Int and FooBar =: Int x Int
– and by structural equivalence Point =: FooBar.
• Name equivalence:
– Since Point and FooBar are different names
Point <:> FooBar
• These are the two typical options for equivalence.
Alternatives are possible:
– Consider modeling Point as a set of tagged pairs:
Point =: ({x} x Int) x ({y} x Int)
Then FooBar =: ({a} x Int) x ({b} x Int)
Now, if a language supports structural equivalence, Point
=: FooBar can be achieved by the programmer by
choosing appropriate labels – same set for both.
25
Variable Attributes: 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
1-26
Examples
const x=7;
Binds x to (7,type integer).
var x:integer;
Binds x to (address, type integer), if global
Binds x to (stack offset, type integer), if local
type T = record
y: integer;
x: real;
end;
Binds y to (record offset, type integer).
Binds x to (record offset, type real)
Static Scope
• Based on 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 parent
1-28
Types of Static Scope
• Flat: (BASIC, Cobol)
– All name sin the same scope, all
visible everywhere.
• Local/global (Fortan, C, Prolog)
– Only two referencing environments:
current and global.
Types of Static Scope (cont’d)
•Nested procedures (Algol,
Pascal, Ada, Modula,
RPAL).
–Each procedure has its own
scope, visible to itself and
all procedures nested
within it.
Types of Static Scope (cont’d)
• Modular scope (Modula, Ada, C++,
Java).
Scopes defined by modules, which
explicitly export names to:
– The global scope (public)
– The scopes of subclasses (protected)
– Nowhere (private)
Blocks
– A method of creating static scopes inside program
units--from ALGOL 60
– Examples:
C and C++: for (...) {
int index;
...
}
Ada: declare LCL : FLOAT;
begin
...
end
1-32
Evaluation of Static Scoping
• Assume MAIN calls A and B
A calls C and D
B calls A and E
MAIN
MAIN
A
C
A
B
D
C
B
D
E
E
1-33
Scoping
int x = 0;
int f( )
{ return x; }
int g( )
{ int x = 1; return f(); }
Why Scope?
• Given multiple bindings of a single name,
how do we know which binding does an
occurrence of this name refer to?
An Example of Block Scope in C
Static vs. Dynamic Scoping
37
program main
var y: Real;
procedure compute()
var x : Integer;
procedure initialize()
var z: Real;
begin {initialize}
z=x;
...
end {initialize}
procedure transform()
var x: Real;
begin {transform}
...
end {transform}
begin {compute}
...
end {compute}
begin {main}
...
end {main}
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
1-39
Scope Example
• Static scoping
– Reference to x is to MAIN's x
• Dynamic scoping
– Reference to x is to SUB1's x
• Evaluation of Dynamic Scoping:
– Advantage: convenience
– Disadvantage: poor readability
1-40
Scope and Lifetime
• Scope and lifetime are sometimes closely
related, but are different concepts
• Consider a static variable in a C or C++
function
1-41
Named Constants
• A named constant is a variable that is bound to a
value only when it is bound to storage
• Advantages: readability and modifiability
• Used to parameterize programs
• The binding of values to named constants can be
either static (called manifest constants) or dynamic
• Languages:
– FORTRAN 90: constant-valued expressions
– Ada, C++, and Java: expressions of any kind
1-42
Assuming static scoping or dynamic scoping rules are
used. What value does the program print?
•
•
•
•
•
•
•
•
•
•
•
•
var x : integer; /* global variable */
procedure Update
x := x + 1;
procedure DoCall(P: procedure)
var x : integer;
x := 1;
P();
write_integer(x);
begin /* body of main program */
x := 0;
DoCall(Update);
end /* body of main program */
static scoping, dynamic scoping
•
•
•
•
•
•
•
•
•
•
•
•
x : integer /* global */
procedure one
x := 1
procedure two
x : integer
x := 2
one()
begin /* main program */
x := 0
two()
write_integer(x)
end /* main program */
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
Consider the following outline of a QuickSort procedure in Pascal:
procedure QuickSort(var A: Array)
procedure Partition(var A: Array; j,k: integer)
var pivot: integer;
procedure Swap(var A: Array; s,t: integer)
var temp: Type;
begin (* Swap *)
... <== [1]
end; (* Swap *)
begin (* Partition *)
... <== [2]
end; (* Partition *)
begin (* QuickSort *)
... <== [3]
end; (* QuickSort *)
Indicate which variables, arguments, and subroutines are in scope at the
indicated points [1], [2], and [3] in the program. Mark the variables and
arguments with the procedure that defines it (for example, at [1] A of
Swap is in scope).
© Copyright 2026 Paperzz