Transcript Slide 1
Chapter 5: Names, Binding, Type Checking, and Scopes
Lecture # 8
Topics
Introduction Names Variables The Concept of Binding
Type Checking Strong Typing Type Compatibility Scope and Lifetime Referencing Environments Named Constants
Chapter 5: Names, Binding, Type Checking, and Scopes 2
Introduction
Imperative languages are abstractions of Von Neumann architecture.
Memory, which stores both instructions and data.
Processor, provides operations for modifying the contents of memory.
Chapter 5: Names, Binding, Type Checking, and Scopes 3
Names
A
name
is a string of characters used to identify some entity in a program.
The names given to the programming language objects such as variables, functions, procedures, and formal parameters.
Chapter 5: Names, Binding, Type Checking, and Scopes 4
Names: Maximum Length and Connector Characters
Maximum length?
Fortran I:
max length = 6.
COBOL:
max length = 30.
Fortran 90
,
ANSI C:
max length = 31.
Ada:
no max length, all characters significant.
C++:
no limit defined, but may be implementation dependent.
Are
connector characters
permitted?
Pascal
,
Modula-2
,
FORTRAN 77:
don’t permit them.
Other languages do.
Underscore is used to build multiple word names.
Chapter 5: Names, Binding, Type Checking, and Scopes 5
Names: Case Sensitivity
Case sensitivity
(names look a like are different): Negative impact on readability, since names spelled the same with different case are different.
C
,
C++
,
Java
,
Modula-2
are case-sensitive.
Other languages are not case-sensitive.
Chapter 5: Names, Binding, Type Checking, and Scopes 6
Names: Special Words
Special words:
keywords or reserved words
Keywords:
Special only in certain contexts. Can be used as identifiers.
Poor readability.
PL/I
uses keywords.
Ex:
REAL var_name REAL = 3.14
keyword Float variable
Reserved word:
Cannot be used as a user-defined name.
Pascal
,
C
,
Java
,
Ada
,
C++
, most others treat special words as reserved words.
Chapter 5: Names, Binding, Type Checking, and Scopes 7
Names: Predefined Names
Predefined names:
user defined names.
names between reserved words and Predefined in libraries and/or other program units and made visible to programmers in
C/C++
,
Ada
, and
Java
.
These names can be redefined by the programmer.
Ex:
printf
and
scanf
in
C
.
Chapter 5: Names, Binding, Type Checking, and Scopes 8
Constants
The different data constants that you can specify in the statements of a program. You must know the rules for writing those constants:
Character constants:
Ex:
‘A’ , ‘ % ’ ,
and
‘\n’
in
C/C++
.
Integer constants (decimal, octal, hexadecimal):
Ex:
-25
(decimal),
0253
(octal),
1A5
(hexadecimal) in
C/C++
.
9 Chapter 5: Names, Binding, Type Checking, and Scopes
Constants
(cont.)
Floating-point/Real constants:
Ex:
-12.35
,
.5E+4
,
3.45
,
2.1E-9
in
C/C++
.
Boolean constants:
Ex:
true
and
false
in
C++
.
String (character) constants:
Ex:
“John Doe”
in
C/C++
.
Chapter 5: Names, Binding, Type Checking, and Scopes 10
Variables
An abstraction of a memory cell.
Variables can be characterized as a sextuple of attributes:
Name of a variable.
Address of a variable.
Value.
Type.
Lifetime.
Scope.
Chapter 5: Names, Binding, Type Checking, and Scopes 11
Attributes of Variables
Name:
How do you give a name to variable?
Address of a variable:
The address of the associated memory location.
Can you access the address of a variable in a program?
Ex: in
C++
, we use the address of (
&
) operator:
& num
.
A variable may have different addresses at different times.
Ex: two variables with the same name in a recursively called routines.
Chapter 5: Names, Binding, Type Checking, and Scopes 12
Attributes of Variables (cont.)
A variable may have different addresses at different places in a program.
Ex: two separate procedures uses the same variable name.
Two names that can be used to access the same memory location are called
aliases
.
Aliases
and
C++
are created via pointers, reference variables,
C
unions.
Aliases are harmful to readability.
13 Chapter 5: Names, Binding, Type Checking, and Scopes
Attributes of Variables (cont.)
Type:
Determines the range of values of variables and the set of operations defined for those values.
Value:
The contents of the location with which a variable is associated.
Scope:
The range of program instructions over which the variable is visible.
Lifetime:
Time during which the variable is bound to a storage location.
Chapter 5: Names, Binding, Type Checking, and Scopes 14
The Concept of Binding
Abstract memory cell:
variable.
the physical cell(s) associated with a
Binding:
an association, such as: between an attribute and an entity, or between an operation and an operator symbol.
Binding time:
the time at which a binding takes place.
L-value:
the address of a variable.
R-value:
the value of a variable.
Chapter 5: Names, Binding, Type Checking, and Scopes 15
Possible Binding Times
Language design time:
Ex: binding of operator symbols to operations.
Language implementation time:
Ex: binding a floating point type to a representation.
Compile time:
Ex: binding of a variable to a type in
C
or
Java
.
Load time:
Ex: binding of a
C
static variable to a memory cell.
Run time:
Ex: binding of a non-static local variable to a memory cell.
Chapter 5: Names, Binding, Type Checking, and Scopes 16
Type Bindings
Before a variable can be referenced in a statement of a program, it must first be bound (or associated) to a data type.
A binding is
static
if it: Occurs before run time and remains unchanged throughout execution.
A binding is
dynamic
if it: Occurs during execution, or can change during execution.
Chapter 5: Names, Binding, Type Checking, and Scopes 17
Static Type Binding
This type of binding occurs through the declaration of variables. The declaration of variables can either be
explicit
or
implicit
.
Explicit Declaration:
is a program statement used for declaring the types of a variables.
Implicit Declaration:
is a default mechanism for specifying the type (typically the first appearance of the variable in the program).
FORTRAN
,
PL/I
,
BASIC
, and
Perl
provide implicit declarations
Advantage:
writability.
Disadvantage:
reliability (difficult to diagnose errors).
Chapter 5: Names, Binding, Type Checking, and Scopes 18
Dynamic Type Binding
Specified via an assignment statement.
Variable is bound to the type when the assignment statement executed.
Example of dynamic binding languages:
JavaScript
and
PHP
.
For example, a
JavaScript
statement: script may contain the following
LIST = [10.2, 3.5];
1 D array of length 2
LIST = 47;
integer variable
Chapter 5: Names, Binding, Type Checking, and Scopes 19
Dynamic Type Binding (cont.)
Advantage:
very flexible.
Disadvantages:
Cost (dynamic type checking and interpretation).
Type error detection by the compiler is difficult.
Ex:
i = x; i = y; // // i i
of type integer of type real now
Chapter 5: Names, Binding, Type Checking, and Scopes 20
Type Inference
Used in
ML
,
Miranda
, and
Haskell
.
Types are determined from context of the reference (not by assignment statement).
Example:
ML
function declarations:
fun circleArea(r) = 3.1415*r*r;
takes a real argument and returns a real result.
//
specifies a function that
fun times10(r) = 10*r; //
specifies a function that takes an integer argument and returns an integer result.
fun square(r) : int = r*r; //
the type is explicitly specified
fun square(r) = r*(r : int); //
the type is explicitly specified
Chapter 5: Names, Binding, Type Checking, and Scopes 21
Storage Bindings & Lifetime
Allocation:
Obtaining a cell from some pool of available cells.
Deallocation:
Returning a cell to the available pool.
Lifetime of a variable:
The time during which the variable is bound to a particular memory cell (begins when it is bound to a memory cell and ends when it is unbound from that cell).
Chapter 5: Names, Binding, Type Checking, and Scopes 22
Categories of Variables by Lifetime
Static.
Stack-dynamic.
Explicit heap-dynamic.
Implicit heap-dynamic.
Chapter 5: Names, Binding, Type Checking, and Scopes 23
Static Variables
Bound to memory cells before execution begins and remains bound to the same memory cell throughout execution.
Examples: all
FORTRAN
variables in
C/C++
. variables, global and static local such as
static int x; Advantages:
Efficiency (ability to use direct addressing).
History-sensitive subprogram support.
Disadvantage:
Lack of flexibility (no support for recursion).
Waste storage.
Chapter 5: Names, Binding, Type Checking, and Scopes 24
Stack-dynamic Variables
Bound to a memory location (in the stack) when the block in which the variable is defined is entered, and is deallocated when the block is exited.
Examples: local variables in
C/C++
.
Advantages:
Allows recursion.
Conserves storage.
Disadvantages:
Overhead of allocation/deallocation.
Subprograms cannot be history sensitive.
Inefficient references (indirect addressing).
Chapter 5: Names, Binding, Type Checking, and Scopes 25
Explicit heap-dynamic Variables
Memory cell is allocated and deallocated (in the heap) by explicit statements specified by the programmer, which takes effect during the execution of the program.
Referenced only via pointers or references.
Examples: Dynamic objects in
C++
(via
new
and
delete
).
All objects in
Java
.
Advantage:
provides for dynamic storage management.
Disadvantage:
inefficient, unreliable.
Chapter 5: Names, Binding, Type Checking, and Scopes 26
Explicit heap-dynamic Variables:
Examples (in C++)
int *pt; pt = new int; /*
allocate a memory location to hold an integer value and assigns its address to pointer
pt */ …… delete pt; //
deallocate the memory location
int *table; table = new int[10]; /*
allocate 10 consecutive memory locations to hold integer values and assigns the address of the first item to pointer
table */ …… delete [ ] table; //
deallocate the memory locations
Chapter 5: Names, Binding, Type Checking, and Scopes 27
Implicit heap-dynamic Variables
Allocation/deallocation of a memory location is caused by assignment statements.
Example: all variables in
APL
; all strings and arrays in
Perl
and
JavaScript
.
Example in
Perl
we have the following:
@ array = (10,15,20);
# allocation of three memory cells
Advantage:
flexibility.
Disadvantages:
Inefficient, because all attributes are dynamic.
Loss of error detection.
Chapter 5: Names, Binding, Type Checking, and Scopes 28