Ch05
Ch05
Names, Bindings,
and Scopes
ISBN 0-321-49362-1
Chapter 5 Topics
• Introduction
• Names
• Variables
• The Concept of Binding
• Scope
• Scope and Lifetime
• Referencing Environments
• Named Constants
1-2
Introduction
1-3
Names
1-4
Names (continued)
• Length
– If too short, they cannot be connotative
– Language examples:
• FORTRAN 95: maximum of 31
• C99: no limit but only the first 63 are significant;
also, external names are limited to a maximum of
31
• C#, Ada, and Java: no limit, and all are significant
• C++: no limit, but implementers often impose one
1-5
Names (continued)
• Special characters
– PHP: all variable names must begin with dollar
signs
– Perl: all variable names begin with special
characters, which specify the variable’s type
– Ruby: variable names that begin with @ are
instance variables; those that begin with @@
are class variables
1-6
Names (continued)
• Case sensitivity
– Disadvantage: readability (names that look
alike are different)
• Names in the C-based languages are case
sensitive
• Names in others are not
• Worse in C++, Java, and C# because predefined
names are mixed case (e.g.
IndexOutOfBoundsException)
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)
1-10
Variables Attributes (continued)
1-11
The Concept of Binding
1-12
1-13
Possible Binding Times
1-15
Type Binding
1-16
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, BASIC, and Perl provide implicit
declarations (Fortran has both explicit and
implicit)
– Advantage: writability
– Disadvantage: reliability (less trouble with Perl)
1-17
Dynamic Type Binding
1-18
Variable Attributes (continued)
1-20
Categories of Variables by
Lifetimes
• Stack-dynamic--Storage bindings are created for
variables when their declaration statements are
elaborated.
(A declaration is elaborated when the
executable code associated with it is executed)
• If scalar, all attributes except address are
statically bound
– local variables in C subprograms and Java methods
• Advantage: allows recursion; conserves storage
• Disadvantages:
– Overhead of allocation and deallocation
– Subprograms cannot be history sensitive
– Inefficient references (indirect addressing)
1-21
Categories of Variables by
Lifetimes
• Explicit heap-dynamic -- Allocated and
deallocated by explicit directives, specified by
the programmer, which take effect during
execution
• Referenced only through pointers or references,
e.g. dynamic objects in C++ (via new and delete),
all objects in Java
• Advantage: provides for dynamic storage
management
• Disadvantage: inefficient and unreliable
1-22
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, JavaScript, and PHP
• Advantage: flexibility (generic code)
• Disadvantages:
– Inefficient, because all attributes are dynamic
– Loss of error detection
1-23
Variable Attributes: Scope
1-24
Static Scope
1-25
Scope (continued)
1-26
Blocks
1-27
Declaration Order
1-28
Declaration Order (continued)
1-29
Global Scope
• PHP
– Programs are embedded in XHTML markup
documents, in any number of fragments, some
statements and some function definitions
– The scope of a variable (implicitly) declared in
a function is local to the function
– The scope of a variable implicitly declared
outside functions is from the declaration to the
end of the program, but skips over any
intervening functions
• Global variables can be accessed in a function
through the $GLOBALS array or by declaring it global
1-31
Global Scope (continued)
• Python
– A global variable can be referenced in
functions, but can be assigned in a function
only if it has been declared to be global in the
function
1-32
Evaluation of Static Scoping
1-33
Dynamic Scope
1-34
Scope Example
Big
- declaration of X
Sub1
- declaration of X -
...
call Sub2
...
Big calls Sub1
Sub1 calls Sub2
Sub2
...
Sub2 uses X
- reference to X -
...
...
call Sub1
…
1-35
Scope Example
• Static scoping
– Reference to X is to Big's X
• Dynamic scoping
– Reference to X is to Sub1's X
• Evaluation of Dynamic Scoping:
– Advantage: convenience
– Disadvantages:
1. While a subprogram is executing, its variables
are visible to all subprograms it calls
2. Impossible to statically type check
3. Poor readability- it is not possible to statically
determine the type of a variable
1-36
Scope and Lifetime
1-37
Referencing Environments
1-38
Named Constants
1-39
Summary
1-40