0% found this document useful (0 votes)
11 views

Ch05

Chapter 5 discusses the concepts of names, bindings, type checking, and scopes in programming languages. It covers variable attributes, binding times, type compatibility, and the importance of scope in determining variable visibility. The chapter also highlights the differences between static and dynamic binding, as well as the implications of type checking on programming language design.

Uploaded by

Tayyaba Noor
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Ch05

Chapter 5 discusses the concepts of names, bindings, type checking, and scopes in programming languages. It covers variable attributes, binding times, type compatibility, and the importance of scope in determining variable visibility. The chapter also highlights the differences between static and dynamic binding, as well as the implications of type checking on programming language design.

Uploaded by

Tayyaba Noor
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Chapter 5

Names, Bindings,
Type Checking, and
Scopes

ISBN 0-321-33025-0
Chapter#5:Names, Bindings, Type Checking, and
Scopes

• Introduction
• Names
• Variables
• The Concept of Binding
• Type Checking
• Strong Typing
• Scope and Lifetime

1-2
Introduction

• Imperative languages are abstractions of


von Neumann architecture
– Memory
– Processor
• Variables characterized by attributes
– Type: to design, must consider scope, lifetime,
type checking, initialization, and type
compatibility

1-3
Names

• Design issues for names:


– Maximum length?
– Are connector characters allowed?
– Are names case sensitive?
– Are special words reserved words or keywords?

1-4
Names (continued)

• Length
– If too short, they cannot be connotative
– 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-5
Names (continued)

• Connectors
– Pascal, Modula-2, and FORTRAN 77 don't allow
– Others do

1-6
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-7
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-8
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-9
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
– Aliases are harmful to readability (program readers must
remember all of them)

1-10
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-11
The Concept of Binding

• The l-value of a variable is its address


• The r-value of a variable is its value
• A binding is an association, 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-12
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.
• A binding is dynamic if it first occurs during
execution or can change during execution
of the program

1-14
Type Binding

• How is a type specified?


• When does the binding take place?
• If static, the type may be specified by either
an explicit or an implicit declaration

1-15
Explicit/Implicit Declaration

• An explicit declaration is a program


statement used for declaring the types of
variables
• An implicit declaration is a default
mechanism for specifying types of variables
(the first appearance of the variable in the
program)
• FORTRAN, PL/I, BASIC, and Perl provide
implicit declarations
– Advantage: writability
– Disadvantage: reliability (less trouble with Perl)

1-16
Dynamic Type Binding

• Dynamic Type Binding (JavaScript and PHP)


• Specified through an assignment statement
e.g., JavaScript
list = [2, 4.33, 6, 8];
list = 17.3;
– Advantage: flexibility (generic program units)
– Disadvantages:
• High cost (dynamic type checking and
interpretation)
• Type error detection by the compiler is difficult

1-17
Categories of Variables by Lifetimes

• 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-18
Categories of Variables by Lifetimes

• Implicit heap-dynamic--Allocation and


deallocation caused by assignment
statements
– all variables in APL; all strings and arrays in Perl
and JavaScript
• Advantage: flexibility
• Disadvantages:
– Inefficient, because all attributes are dynamic
– Loss of error detection

1-19
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 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-20
Type Checking (continued)

• If all type bindings are static, nearly all type


checking can be static
• If type bindings are dynamic, type checking
must be dynamic
• A programming language is strongly typed
if type errors are always detected

1-21
Structure Type Compatibility

• Structure type compatibility means that two


variables have compatible types if their
types have identical structures
• More flexible, but harder to implement

1-22
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-23
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-24
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 D E
B
E

1-25
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-26

You might also like