Sebesta - Programming Languages - Chapter5
Sebesta - Programming Languages - Chapter5
Scope and
Lifetime
Names Is a set of characters used to identify some
entities in a program.
Any Name must have design issues :-
Variables Maximum length*?
Are connector characters allowed?
Are names case sensitive?
Are special words reserved words or keywords?
Binding
Type
Checking
Type
Compatibili
ty
Scope and
Lifetime * Earliest Programming Languages used single-character nam
Names Length:-
If too short, they cannot be connotative.
Variables Language Max Limit Language Max Limit
Fortran 6 COBOL 30
Ada&Java No Limit ++C No Limit
Binding
Connectors:-
Type Pascal, Modula-2, and FORTRAN 77
don't allow.
Checking
Type Case sensitivity:-
Compatibili C, C++, and Java names only are case
ty sensitive.
Scope and
Lifetime
Names A binding is an association, such as
between an attribute and an entity,
or between an operation and a
Variables symbol.
Binding time:- is the time at which
a binding takes place.
Binding
Type
Checking
Type
Compatibili
ty
Scope and
Lifetime
Names Language design time:- Bind
operator symbols to operations.
Variables Language implementation time-
Bind floating point type to a
representation.
Binding Compile time:- Bind a variable to a
type in C or Java.
Type Load time:- Bind variable to
Checking memory cell.
Type Runtime:- Bind a non-static local
Compatibili variable to a memory cell.
ty
Scope and
Lifetime
Names A binding is static if it first occurs
before run time and remains
unchanged throughout program
Variables execution.
A binding is dynamic if it first occurs
during execution or can change
Binding during execution of the program.
Type
Checking
Type
Compatibili
ty
Scope and
Lifetime
Names An explicit declaration is a
program statement used for
Variables declaring the types of variables.
An implicit declaration is a default
mechanism for specifying types of
Binding variables (the first appearance of
the variable in the program(.
Type FORTRAN, PL/I, BASIC, and Perl
Checking provide implicit declarations.
Type
Compatibili
ty
Scope and
Lifetime
Names 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.
Variables 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.
Binding This automatic conversion is called a coercion.
A type error is the application of an operator to an
operand of an inappropriate type.
A programming language is strongly typed if type errors
Type are always detected.
Checking Advantage of strong typing: allows the detection of the
misuses of variables that result in type errors.
Type
Compatibili
ty
Scope and
Lifetime
Names type compatibility:- means the two variables
have compatible types if they are in either
the same declaration or in declarations that
Variables use the same type name.
Easy to implement but highly restrictive.
Structure type compatibility:- means that two
variables have compatible types if their types
Binding have identical structures.
Structure Type compatibility is More flexible,
Type but harder to implement.
Checking
Type
Compatibili
ty
Scope and
Lifetime
Names Consider the problem of two structured types:-
Are two record types compatible if they
are structurally the same but use different
Variables field names?
Are two array types compatible if they are
the same except that the subscripts are
different?
Binding (e.g. [1..10] and [0..9](
Are two enumeration types compatible if
Type their components are spelled differently?
With structural type compatibility, you
Checking cannot differentiate between types of the
Type same structure (e.g. different units of
speed, both float(
Compatibili
ty
Scope and
Lifetime
Names The scope of a variable is the range of
statements over which it is visible.
Variables The nonlocal variables of a program
unit are those that are visible but not
declared there.
Binding The scope rules of a language
determine how references to names
are associated with variables.
Type
Checking
Type
Compatibili
ty
Scope and
Lifetime
Names
Example:-
C and C++:
Variables for (...) {int index;
........ ; }
Binding
Type
MAIN MAIN
Checking A
Type C
Compatibili A B
ty D
C D E
Scope and B
Lifetime E
Names Ex
Variables MAIN
- declaration of x
SUB1
- declaration of x -
Binding ...
call SUB2
...
Type
Checking SUB2
...
Type - reference to x -
...
Compatibili
ty ... MAIN calls SUB1
call SUB1 SUB1 calls SUB2
… SUB2 uses x
Scope and
Lifetime
Names Static scoping :-
Reference to x is to MAIN's x.
Variables Dynamic scoping :-
Reference to x is to SUB1's x.
Evaluation of Dynamic Scoping:
Binding Advantage:- convenience
Disadvantage:- poor readability
Type
Checking
Type
Compatibili
ty
Scope and
Lifetime
:-References
[1] -