Sei sulla pagina 1di 20

(Chapter (5

Names, Bindings, Type Checking, and


Scopes
By: Eng.Ismail El-Gayar
Submitted To :
Prof.Dr. Abd El Baith Mohamed
Names The von Neumann architecture is a design
model for a stored-program digital computer that
uses a processing unit and a single separate
storage structure to hold both instructions and
Variables data. It is named after the mathematician and
early computer scientist John von Neumann.
Imperative languages are abstractions of von
Neumann architecture.
Binding Memory
Processor
Type
Checking
Type
Compatibili
ty

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 Keyword & Reserved


Lifetime word:-
A keyword is a word that is special only
in certain contexts.
Names
Is an abstraction of a memory
cell.
Variables The current value of the
variable is the data actually
stored in the variable.
Binding
Variables can be characterized as a
sextuple of attributes:-
Type Name
Checking Address
Type Value
Compatibili
Type
ty
Lifetime
Scope and Scope
Lifetime
Names Name:- Not all variables have Names.
Address:- The memory address with which
it is associated .
Variables A variable may have different addresses at different
times during execution or at different places in a
program.
If two variable names can be used to access the
Binding same memory location, they are called aliases.
Aliases are created via pointers, reference
variables, C and C++ unions.
Type Aliases are harmful to readability (program readers must
remember This(.
Checking Type:- Determines the range of values of
Type variables and the set of operations that
Compatibili are defined for values of that type; in the
ty case of floating point, type also
determines the precision.
Scope and
Lifetime
Names Value:- The contents of the location
with which the variable is associated.
Variables
Abstract memory cell:- The physical
cell or collection of cells associated
Binding with a variable.
Note About Variables:-
* The l-value of a variable is its address.
Type * The r-value of a variable is its value.
Checking
Type
Compatibili
ty

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] -

Potrebbero piacerti anche