SlideShare a Scribd company logo
1 of 43
ISBN 0-321-49362-1
Chapter 5
Names, Bindings,
Type Checking, and
Scopes
Copyright © 2007 Addison-
Wesley. All rights reserved. 1-2
Chapter 5 Topics
• Introduction
• Names
• Variables
• The Concept of Binding
• Type Checking
• Strong Typing
• Type Equivalence
• Scope
• Scope and Lifetime
• Referencing Environments
• Named Constants
Copyright © 2007 Addison-
Wesley. All rights reserved. 1-3
Introduction
• Imperative languages are abstractions of von
Neumann architecture
– Memory
– Processor
• Variables characterized by attributes
– To design a type, must consider scope, lifetime, type
checking, initialization, and type compatibility
Copyright © 2007 Addison-
Wesley. All rights reserved. 1-4
Names
• Design issues for names:
– Are names case sensitive?
– Are special words reserved words or keywords?
Copyright © 2007 Addison-
Wesley. All rights reserved. 1-5
Names (continued)
• Length
– If too short, they cannot be connotative
– Language examples:
• FORTRAN I: maximum 6
• COBOL: maximum 30
• FORTRAN 90 and C89: maximum 31
• C99: maximum 63
• C#, Ada, and Java: no limit, and all are significant
• C++: no limit, but implementers often impose one
Copyright © 2007 Addison-
Wesley. All rights reserved. 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)
Copyright © 2007 Addison-
Wesley. All rights reserved. 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
– Potential problem with reserved words: If there are
too many, many collisions occur (e.g., COBOL has
300 reserved words!)
Copyright © 2007 Addison-
Wesley. All rights reserved. 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
Copyright © 2007 Addison-
Wesley. All rights reserved. 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)
Copyright © 2007 Addison-
Wesley. All rights reserved. 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
- The l-value of a variable is its address
- The r-value of a variable is its value
• Abstract memory cell - the physical cell or collection of
cells associated with a variable
Copyright © 2007 Addison-
Wesley. All rights reserved. 1-11
The Concept of Binding
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.
Copyright © 2007 Addison-
Wesley. All rights reserved. 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 C or C++ static variable
to a memory cell)
• Runtime -- bind a nonstatic local variable to a
memory cell
Copyright © 2007 Addison-
Wesley. All rights reserved. 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
Copyright © 2007 Addison-
Wesley. All rights reserved. 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
Copyright © 2007 Addison-
Wesley. All rights reserved. 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)
Copyright © 2007 Addison-
Wesley. All rights reserved. 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
Copyright © 2007 Addison-
Wesley. All rights reserved. 1-17
Variable Attributes (continued)
• Type Inferencing (ML, Miranda, and Haskell)
– Rather than by assignment statement, types are
determined (by the compiler) from the context of the
reference
• Storage Bindings & Lifetime
– Allocation - getting a cell from some pool of available
cells
– Deallocation - putting a cell back into the pool
• The lifetime of a variable is the time during
which it is bound to a particular memory cell
Copyright © 2007 Addison-
Wesley. All rights reserved. 1-18
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., C and C++
static variables
– Advantages: efficiency (direct addressing), history-
sensitive subprogram support
– Disadvantage: lack of flexibility (no recursion)
Copyright © 2007 Addison-
Wesley. All rights reserved. 1-19
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)
Copyright © 2007 Addison-
Wesley. All rights reserved. 1-20
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
Copyright © 2007 Addison-
Wesley. All rights reserved. 1-21
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
Copyright © 2007 Addison-
Wesley. All rights reserved. 1-22
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
Copyright © 2007 Addison-
Wesley. All rights reserved. 1-23
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
• Advantage of strong typing: allows the detection
of the misuses of variables that result in type
errors
Copyright © 2007 Addison-
Wesley. All rights reserved. 1-24
Strong Typing
Language examples:
– FORTRAN 95 is not: parameters, EQUIVALENCE
– C and C++ are not: parameter type checking can be
avoided; unions are not type checked
– Ada is, almost (UNCHECKED CONVERSION is
loophole)
(Java and C# are similar to Ada)
Copyright © 2007 Addison-
Wesley. All rights reserved. 1-25
Strong Typing (continued)
• Coercion rules strongly affect strong typing--
they can weaken it considerably (C++ versus
Ada)
• Although Java has just half the assignment
coercions of C++, its strong typing is still far less
effective than that of Ada
Copyright © 2007 Addison-
Wesley. All rights reserved. 1-26
Name Type Equivalence
• Name type equivalence means the two
variables have equivalent types if they are in
either the same declaration or in declarations
that use the same type name
• Easy to implement but highly restrictive:
– Subranges of integer types are not equivalent with
integer types
– Formal parameters must be the same type as their
corresponding actual parameters
Copyright © 2007 Addison-
Wesley. All rights reserved. 1-27
Structure Type Equivalence
• Structure type equivalence means that two
variables have equivalent types if their types
have identical structures
• More flexible, but harder to implement
Copyright © 2007 Addison-
Wesley. All rights reserved. 1-28
Type Equivalence (continued)
• Consider the problem of two structured types:
– Are two record types equivalent if they are
structurally the same but use different field
names?
– Are two array types equivalent if they are the
same except that the subscripts are different?
(e.g. [1..10] and [0..9])
– Are two enumeration types equivalent if their
components are spelled differently?
– With structural type equivalence, you cannot
differentiate between types of the same structure
(e.g. different units of speed, both float)
Copyright © 2007 Addison-
Wesley. All rights reserved. 1-29
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
Copyright © 2007 Addison-
Wesley. All rights reserved. 1-30
Static Scope
• Based on program text
• To connect a name reference to a variable, you (or the
compiler) must find the declaration
• Search process: search declarations, first locally, then in
increasingly larger enclosing scopes, until one is found
for the given name
• Enclosing static scopes (to a specific scope) are called
its static ancestors; the nearest static ancestor is called
a static parent
• Some languages allow nested subprogram definitions,
which create nested static scopes (e.g., Ada,
JavaScript, and PHP)
Copyright © 2007 Addison-
Wesley. All rights reserved. 1-31
Scope (continued)
• Variables can be hidden from a unit by having a
"closer" variable with the same name
• C++ and Ada allow access to these "hidden"
variables
– In Ada: unit.name
– In C++: class_name::name
Copyright © 2007 Addison-
Wesley. All rights reserved. 1-32
Blocks
– A method of creating static scopes inside program units--
from ALGOL 60
– Examples:
C-based languages:
while (...) {
int index;
...
}
Ada: declare Temp : Float;
begin
...
end
Copyright © 2007 Addison-
Wesley. All rights reserved. 1-33
Evaluation of Static Scoping
• Assume MAIN calls A and B
A calls C and D
B calls A and E
MAINMAIN
E
A
C
D
B
A B
C D E
Copyright © 2007 Addison-
Wesley. All rights reserved. 1-34
Static Scope Example
MAIN MAIN
A B
C D E
A
C
B
ED
Copyright © 2007 Addison-
Wesley. All rights reserved. 1-35
Static Scope (continued)
• Suppose the spec is changed so that D must
now access some data in B
• Solutions:
– Put D in B (but then C can no longer call it and D
cannot access A's variables)
– Move the data from B that D needs to MAIN (but then
all procedures can access them)
• Same problem for procedure access
• Overall: static scoping often encourages many
globals
Copyright © 2007 Addison-
Wesley. All rights reserved. 1-36
Dynamic Scope
• Based on calling sequences of program units,
not their textual layout (temporal versus spatial)
• References to variables are connected to
declarations by searching back through the
chain of subprogram calls that forced execution
to this point
Copyright © 2007 Addison-
Wesley. All rights reserved. 1-37
Scope Example
Big
- declaration of X
Sub1
- declaration of X -
...
call Sub2
...
Sub2
...
- reference to X -
...
...
call Sub1
…
Big calls Sub1
Sub1 calls Sub2
Sub2 uses X
Copyright © 2007 Addison-
Wesley. All rights reserved. 1-38
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 (called subprogram is
executed in the context of the caller)
– Disadvantage: poor readability
Copyright © 2007 Addison-
Wesley. All rights reserved. 1-39
Scope and Lifetime
• Scope and lifetime are sometimes closely
related, but are different concepts
• Consider a static variable in a C or C++
function
Copyright © 2007 Addison-
Wesley. All rights reserved. 1-40
Referencing Environments
• The referencing environment of a statement is the
collection of all names that are visible in the statement
• In a static-scoped language, it is the local variables plus
all of the visible variables in all of the enclosing scopes
• A subprogram is active if its execution has begun but
has not yet terminated
• In a dynamic-scoped language, the referencing
environment is the local variables plus all visible
variables in all active subprograms
Copyright © 2007 Addison-
Wesley. All rights reserved. 1-41
Named Constants
• A named constant is a variable that is bound to a value
only when it is bound to storage
• Advantages: readability and modifiability
• Used to parameterize programs
• The binding of values to named constants can be either
static (called manifest constants) or dynamic
• Languages:
– FORTRAN 95: constant-valued expressions
– Ada, C++, and Java: expressions of any kind
– C# has two kinds, readonly and const
- the values of const named constants are bound at
compile time
- The values of readonly named constants are
dynamically bound
Copyright © 2007 Addison-
Wesley. All rights reserved. 1-42
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;
Copyright © 2007 Addison-
Wesley. All rights reserved. 1-43
Summary
• Case sensitivity and the relationship of names to special
words represent design issues of names
• Variables are characterized by the sextuples: name,
address, value, type, lifetime, scope
• Binding is the association of attributes with program
entities
• Scalar variables are categorized as: static, stack
dynamic, explicit heap dynamic, implicit heap dynamic
• Strong typing means detecting all type errors

More Related Content

What's hot

Lecture 01 introduction to compiler
Lecture 01 introduction to compilerLecture 01 introduction to compiler
Lecture 01 introduction to compilerIffat Anjum
 
Software Engineering Layered Technology Software Process Framework
Software Engineering  Layered Technology Software Process FrameworkSoftware Engineering  Layered Technology Software Process Framework
Software Engineering Layered Technology Software Process FrameworkJAINAM KAPADIYA
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler DesignMuhammed Afsal Villan
 
Software cost estimation techniques presentation
Software cost estimation techniques presentationSoftware cost estimation techniques presentation
Software cost estimation techniques presentationKudzai Rerayi
 
introduction to programming languages
introduction to programming languagesintroduction to programming languages
introduction to programming languagesNaqashAhmad14
 
Halsted’s Software Science-An analytical technique
Halsted’s Software Science-An analytical techniqueHalsted’s Software Science-An analytical technique
Halsted’s Software Science-An analytical techniqueNur Islam
 
Compiler Design
Compiler DesignCompiler Design
Compiler DesignMir Majid
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design MAHASREEM
 
Agile Methodology - Software Engineering
Agile Methodology - Software EngineeringAgile Methodology - Software Engineering
Agile Methodology - Software EngineeringPurvik Rana
 
Chapter 5-programming
Chapter 5-programmingChapter 5-programming
Chapter 5-programmingAten Kecik
 
Compiler Design Lecture Notes
Compiler Design Lecture NotesCompiler Design Lecture Notes
Compiler Design Lecture NotesFellowBuddy.com
 
Introduction to Compiler design
Introduction to Compiler design Introduction to Compiler design
Introduction to Compiler design Dr. C.V. Suresh Babu
 
Language and Processors for Requirements Specification
Language and Processors for Requirements SpecificationLanguage and Processors for Requirements Specification
Language and Processors for Requirements Specificationkirupasuchi1996
 
Software Process Models
Software Process ModelsSoftware Process Models
Software Process ModelsAtul Karmyal
 
Software Process Improvement
Software Process ImprovementSoftware Process Improvement
Software Process ImprovementBilal Shah
 

What's hot (20)

Lecture 01 introduction to compiler
Lecture 01 introduction to compilerLecture 01 introduction to compiler
Lecture 01 introduction to compiler
 
Rad model
Rad modelRad model
Rad model
 
Software Engineering Layered Technology Software Process Framework
Software Engineering  Layered Technology Software Process FrameworkSoftware Engineering  Layered Technology Software Process Framework
Software Engineering Layered Technology Software Process Framework
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
 
Software cost estimation techniques presentation
Software cost estimation techniques presentationSoftware cost estimation techniques presentation
Software cost estimation techniques presentation
 
introduction to programming languages
introduction to programming languagesintroduction to programming languages
introduction to programming languages
 
Halsted’s Software Science-An analytical technique
Halsted’s Software Science-An analytical techniqueHalsted’s Software Science-An analytical technique
Halsted’s Software Science-An analytical technique
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
 
Agile Methodology - Software Engineering
Agile Methodology - Software EngineeringAgile Methodology - Software Engineering
Agile Methodology - Software Engineering
 
Chapter 5-programming
Chapter 5-programmingChapter 5-programming
Chapter 5-programming
 
Compiler Design Lecture Notes
Compiler Design Lecture NotesCompiler Design Lecture Notes
Compiler Design Lecture Notes
 
1. reason why study spl
1. reason why study spl1. reason why study spl
1. reason why study spl
 
1.Role lexical Analyzer
1.Role lexical Analyzer1.Role lexical Analyzer
1.Role lexical Analyzer
 
6 data types
6 data types6 data types
6 data types
 
Introduction to Compiler design
Introduction to Compiler design Introduction to Compiler design
Introduction to Compiler design
 
Language and Processors for Requirements Specification
Language and Processors for Requirements SpecificationLanguage and Processors for Requirements Specification
Language and Processors for Requirements Specification
 
Software Process Models
Software Process ModelsSoftware Process Models
Software Process Models
 
Software Process Improvement
Software Process ImprovementSoftware Process Improvement
Software Process Improvement
 
Compilers
CompilersCompilers
Compilers
 

Viewers also liked

2 evolution of the major
2 evolution of the major2 evolution of the major
2 evolution of the majorMunawar Ahmed
 
Java Variable Storage
Java Variable StorageJava Variable Storage
Java Variable StorageShahid Rasheed
 
CH # 1 preliminaries
CH # 1 preliminariesCH # 1 preliminaries
CH # 1 preliminariesMunawar Ahmed
 
8 statement level
8 statement level8 statement level
8 statement levelMunawar Ahmed
 
7 expressions and assignment statements
7 expressions and assignment statements7 expressions and assignment statements
7 expressions and assignment statementsMunawar Ahmed
 
3 describing syntax
3 describing syntax3 describing syntax
3 describing syntaxMunawar Ahmed
 
4 lexical and syntax
4 lexical and syntax4 lexical and syntax
4 lexical and syntaxMunawar Ahmed
 
2015 Upload Campaigns Calendar - SlideShare
2015 Upload Campaigns Calendar - SlideShare2015 Upload Campaigns Calendar - SlideShare
2015 Upload Campaigns Calendar - SlideShareSlideShare
 
What to Upload to SlideShare
What to Upload to SlideShareWhat to Upload to SlideShare
What to Upload to SlideShareSlideShare
 
Getting Started With SlideShare
Getting Started With SlideShareGetting Started With SlideShare
Getting Started With SlideShareSlideShare
 

Viewers also liked (11)

2 evolution of the major
2 evolution of the major2 evolution of the major
2 evolution of the major
 
Java Variable Storage
Java Variable StorageJava Variable Storage
Java Variable Storage
 
9 subprograms
9 subprograms9 subprograms
9 subprograms
 
CH # 1 preliminaries
CH # 1 preliminariesCH # 1 preliminaries
CH # 1 preliminaries
 
8 statement level
8 statement level8 statement level
8 statement level
 
7 expressions and assignment statements
7 expressions and assignment statements7 expressions and assignment statements
7 expressions and assignment statements
 
3 describing syntax
3 describing syntax3 describing syntax
3 describing syntax
 
4 lexical and syntax
4 lexical and syntax4 lexical and syntax
4 lexical and syntax
 
2015 Upload Campaigns Calendar - SlideShare
2015 Upload Campaigns Calendar - SlideShare2015 Upload Campaigns Calendar - SlideShare
2015 Upload Campaigns Calendar - SlideShare
 
What to Upload to SlideShare
What to Upload to SlideShareWhat to Upload to SlideShare
What to Upload to SlideShare
 
Getting Started With SlideShare
Getting Started With SlideShareGetting Started With SlideShare
Getting Started With SlideShare
 

Similar to Names, Bindings, Type Checking and Scopes in Programming Languages

ch6-Short.ppt eee cse www rrr www qqq rrr ttt
ch6-Short.ppt eee cse www rrr www qqq rrr tttch6-Short.ppt eee cse www rrr www qqq rrr ttt
ch6-Short.ppt eee cse www rrr www qqq rrr tttwinimag331
 
Pl9ch1
Pl9ch1Pl9ch1
Pl9ch1Ved Ed
 
Improved Developer Productivity In JDK8
Improved Developer Productivity In JDK8Improved Developer Productivity In JDK8
Improved Developer Productivity In JDK8Simon Ritter
 
introductiontoperl-springpeople-150605065831-lva1-app6891.pptx
introductiontoperl-springpeople-150605065831-lva1-app6891.pptxintroductiontoperl-springpeople-150605065831-lva1-app6891.pptx
introductiontoperl-springpeople-150605065831-lva1-app6891.pptxmayilcebrayilov15
 
Introduction To Perl - SpringPeople
Introduction To Perl - SpringPeopleIntroduction To Perl - SpringPeople
Introduction To Perl - SpringPeopleSpringPeople
 
Primitive data types in java
Primitive data types in javaPrimitive data types in java
Primitive data types in javaUmamaheshwariv1
 
1.2 Evaluation of PLs.ppt
1.2 Evaluation of PLs.ppt1.2 Evaluation of PLs.ppt
1.2 Evaluation of PLs.pptmeenabairagi1
 
chapter7.ppt java programming lecture notes
chapter7.ppt java programming lecture noteschapter7.ppt java programming lecture notes
chapter7.ppt java programming lecture noteskavitamittal18
 
Variables: names, bindings, type, scope
Variables: names, bindings, type, scopeVariables: names, bindings, type, scope
Variables: names, bindings, type, scopesuthi
 
Introduction to Software - Coder Forge - John Mulhall
Introduction to Software - Coder Forge - John MulhallIntroduction to Software - Coder Forge - John Mulhall
Introduction to Software - Coder Forge - John MulhallJohn Mulhall
 
A program _ is an abstraction in a programming language for the memor.pdf
A program _  is an abstraction in a programming language for the memor.pdfA program _  is an abstraction in a programming language for the memor.pdf
A program _ is an abstraction in a programming language for the memor.pdfshashanth1188
 
Java Basics.ppt
Java Basics.pptJava Basics.ppt
Java Basics.pptFraolMelkamu
 

Similar to Names, Bindings, Type Checking and Scopes in Programming Languages (20)

Subprogram
SubprogramSubprogram
Subprogram
 
ch6-Short.ppt eee cse www rrr www qqq rrr ttt
ch6-Short.ppt eee cse www rrr www qqq rrr tttch6-Short.ppt eee cse www rrr www qqq rrr ttt
ch6-Short.ppt eee cse www rrr www qqq rrr ttt
 
Pl9ch1
Pl9ch1Pl9ch1
Pl9ch1
 
Improved Developer Productivity In JDK8
Improved Developer Productivity In JDK8Improved Developer Productivity In JDK8
Improved Developer Productivity In JDK8
 
Ch06Part1.ppt
Ch06Part1.pptCh06Part1.ppt
Ch06Part1.ppt
 
introductiontoperl-springpeople-150605065831-lva1-app6891.pptx
introductiontoperl-springpeople-150605065831-lva1-app6891.pptxintroductiontoperl-springpeople-150605065831-lva1-app6891.pptx
introductiontoperl-springpeople-150605065831-lva1-app6891.pptx
 
Introduction To Perl - SpringPeople
Introduction To Perl - SpringPeopleIntroduction To Perl - SpringPeople
Introduction To Perl - SpringPeople
 
Primitive data types in java
Primitive data types in javaPrimitive data types in java
Primitive data types in java
 
1.2 Evaluation of PLs.ppt
1.2 Evaluation of PLs.ppt1.2 Evaluation of PLs.ppt
1.2 Evaluation of PLs.ppt
 
chapter7.ppt java programming lecture notes
chapter7.ppt java programming lecture noteschapter7.ppt java programming lecture notes
chapter7.ppt java programming lecture notes
 
Stay fresh
Stay freshStay fresh
Stay fresh
 
Variables: names, bindings, type, scope
Variables: names, bindings, type, scopeVariables: names, bindings, type, scope
Variables: names, bindings, type, scope
 
Introduction to Software - Coder Forge - John Mulhall
Introduction to Software - Coder Forge - John MulhallIntroduction to Software - Coder Forge - John Mulhall
Introduction to Software - Coder Forge - John Mulhall
 
Unit 1
Unit 1Unit 1
Unit 1
 
Data.ppt
Data.pptData.ppt
Data.ppt
 
Ch6
Ch6Ch6
Ch6
 
A program _ is an abstraction in a programming language for the memor.pdf
A program _  is an abstraction in a programming language for the memor.pdfA program _  is an abstraction in a programming language for the memor.pdf
A program _ is an abstraction in a programming language for the memor.pdf
 
Java Basics.ppt
Java Basics.pptJava Basics.ppt
Java Basics.ppt
 
sabesta3.ppt
sabesta3.pptsabesta3.ppt
sabesta3.ppt
 
pl12ch6.ppt
pl12ch6.pptpl12ch6.ppt
pl12ch6.ppt
 

Recently uploaded

Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Dr. Mazin Mohamed alkathiri
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 

Recently uploaded (20)

Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 

Names, Bindings, Type Checking and Scopes in Programming Languages

  • 1. ISBN 0-321-49362-1 Chapter 5 Names, Bindings, Type Checking, and Scopes
  • 2. Copyright © 2007 Addison- Wesley. All rights reserved. 1-2 Chapter 5 Topics • Introduction • Names • Variables • The Concept of Binding • Type Checking • Strong Typing • Type Equivalence • Scope • Scope and Lifetime • Referencing Environments • Named Constants
  • 3. Copyright © 2007 Addison- Wesley. All rights reserved. 1-3 Introduction • Imperative languages are abstractions of von Neumann architecture – Memory – Processor • Variables characterized by attributes – To design a type, must consider scope, lifetime, type checking, initialization, and type compatibility
  • 4. Copyright © 2007 Addison- Wesley. All rights reserved. 1-4 Names • Design issues for names: – Are names case sensitive? – Are special words reserved words or keywords?
  • 5. Copyright © 2007 Addison- Wesley. All rights reserved. 1-5 Names (continued) • Length – If too short, they cannot be connotative – Language examples: • FORTRAN I: maximum 6 • COBOL: maximum 30 • FORTRAN 90 and C89: maximum 31 • C99: maximum 63 • C#, Ada, and Java: no limit, and all are significant • C++: no limit, but implementers often impose one
  • 6. Copyright © 2007 Addison- Wesley. All rights reserved. 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)
  • 7. Copyright © 2007 Addison- Wesley. All rights reserved. 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 – Potential problem with reserved words: If there are too many, many collisions occur (e.g., COBOL has 300 reserved words!)
  • 8. Copyright © 2007 Addison- Wesley. All rights reserved. 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
  • 9. Copyright © 2007 Addison- Wesley. All rights reserved. 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)
  • 10. Copyright © 2007 Addison- Wesley. All rights reserved. 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 - The l-value of a variable is its address - The r-value of a variable is its value • Abstract memory cell - the physical cell or collection of cells associated with a variable
  • 11. Copyright © 2007 Addison- Wesley. All rights reserved. 1-11 The Concept of Binding 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.
  • 12. Copyright © 2007 Addison- Wesley. All rights reserved. 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 C or C++ static variable to a memory cell) • Runtime -- bind a nonstatic local variable to a memory cell
  • 13. Copyright © 2007 Addison- Wesley. All rights reserved. 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
  • 14. Copyright © 2007 Addison- Wesley. All rights reserved. 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
  • 15. Copyright © 2007 Addison- Wesley. All rights reserved. 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)
  • 16. Copyright © 2007 Addison- Wesley. All rights reserved. 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
  • 17. Copyright © 2007 Addison- Wesley. All rights reserved. 1-17 Variable Attributes (continued) • Type Inferencing (ML, Miranda, and Haskell) – Rather than by assignment statement, types are determined (by the compiler) from the context of the reference • Storage Bindings & Lifetime – Allocation - getting a cell from some pool of available cells – Deallocation - putting a cell back into the pool • The lifetime of a variable is the time during which it is bound to a particular memory cell
  • 18. Copyright © 2007 Addison- Wesley. All rights reserved. 1-18 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., C and C++ static variables – Advantages: efficiency (direct addressing), history- sensitive subprogram support – Disadvantage: lack of flexibility (no recursion)
  • 19. Copyright © 2007 Addison- Wesley. All rights reserved. 1-19 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)
  • 20. Copyright © 2007 Addison- Wesley. All rights reserved. 1-20 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
  • 21. Copyright © 2007 Addison- Wesley. All rights reserved. 1-21 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
  • 22. Copyright © 2007 Addison- Wesley. All rights reserved. 1-22 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
  • 23. Copyright © 2007 Addison- Wesley. All rights reserved. 1-23 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 • Advantage of strong typing: allows the detection of the misuses of variables that result in type errors
  • 24. Copyright © 2007 Addison- Wesley. All rights reserved. 1-24 Strong Typing Language examples: – FORTRAN 95 is not: parameters, EQUIVALENCE – C and C++ are not: parameter type checking can be avoided; unions are not type checked – Ada is, almost (UNCHECKED CONVERSION is loophole) (Java and C# are similar to Ada)
  • 25. Copyright © 2007 Addison- Wesley. All rights reserved. 1-25 Strong Typing (continued) • Coercion rules strongly affect strong typing-- they can weaken it considerably (C++ versus Ada) • Although Java has just half the assignment coercions of C++, its strong typing is still far less effective than that of Ada
  • 26. Copyright © 2007 Addison- Wesley. All rights reserved. 1-26 Name Type Equivalence • Name type equivalence means the two variables have equivalent types if they are in either the same declaration or in declarations that use the same type name • Easy to implement but highly restrictive: – Subranges of integer types are not equivalent with integer types – Formal parameters must be the same type as their corresponding actual parameters
  • 27. Copyright © 2007 Addison- Wesley. All rights reserved. 1-27 Structure Type Equivalence • Structure type equivalence means that two variables have equivalent types if their types have identical structures • More flexible, but harder to implement
  • 28. Copyright © 2007 Addison- Wesley. All rights reserved. 1-28 Type Equivalence (continued) • Consider the problem of two structured types: – Are two record types equivalent if they are structurally the same but use different field names? – Are two array types equivalent if they are the same except that the subscripts are different? (e.g. [1..10] and [0..9]) – Are two enumeration types equivalent if their components are spelled differently? – With structural type equivalence, you cannot differentiate between types of the same structure (e.g. different units of speed, both float)
  • 29. Copyright © 2007 Addison- Wesley. All rights reserved. 1-29 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
  • 30. Copyright © 2007 Addison- Wesley. All rights reserved. 1-30 Static Scope • Based on program text • To connect a name reference to a variable, you (or the compiler) must find the declaration • Search process: search declarations, first locally, then in increasingly larger enclosing scopes, until one is found for the given name • Enclosing static scopes (to a specific scope) are called its static ancestors; the nearest static ancestor is called a static parent • Some languages allow nested subprogram definitions, which create nested static scopes (e.g., Ada, JavaScript, and PHP)
  • 31. Copyright © 2007 Addison- Wesley. All rights reserved. 1-31 Scope (continued) • Variables can be hidden from a unit by having a "closer" variable with the same name • C++ and Ada allow access to these "hidden" variables – In Ada: unit.name – In C++: class_name::name
  • 32. Copyright © 2007 Addison- Wesley. All rights reserved. 1-32 Blocks – A method of creating static scopes inside program units-- from ALGOL 60 – Examples: C-based languages: while (...) { int index; ... } Ada: declare Temp : Float; begin ... end
  • 33. Copyright © 2007 Addison- Wesley. All rights reserved. 1-33 Evaluation of Static Scoping • Assume MAIN calls A and B A calls C and D B calls A and E MAINMAIN E A C D B A B C D E
  • 34. Copyright © 2007 Addison- Wesley. All rights reserved. 1-34 Static Scope Example MAIN MAIN A B C D E A C B ED
  • 35. Copyright © 2007 Addison- Wesley. All rights reserved. 1-35 Static Scope (continued) • Suppose the spec is changed so that D must now access some data in B • Solutions: – Put D in B (but then C can no longer call it and D cannot access A's variables) – Move the data from B that D needs to MAIN (but then all procedures can access them) • Same problem for procedure access • Overall: static scoping often encourages many globals
  • 36. Copyright © 2007 Addison- Wesley. All rights reserved. 1-36 Dynamic Scope • Based on calling sequences of program units, not their textual layout (temporal versus spatial) • References to variables are connected to declarations by searching back through the chain of subprogram calls that forced execution to this point
  • 37. Copyright © 2007 Addison- Wesley. All rights reserved. 1-37 Scope Example Big - declaration of X Sub1 - declaration of X - ... call Sub2 ... Sub2 ... - reference to X - ... ... call Sub1 … Big calls Sub1 Sub1 calls Sub2 Sub2 uses X
  • 38. Copyright © 2007 Addison- Wesley. All rights reserved. 1-38 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 (called subprogram is executed in the context of the caller) – Disadvantage: poor readability
  • 39. Copyright © 2007 Addison- Wesley. All rights reserved. 1-39 Scope and Lifetime • Scope and lifetime are sometimes closely related, but are different concepts • Consider a static variable in a C or C++ function
  • 40. Copyright © 2007 Addison- Wesley. All rights reserved. 1-40 Referencing Environments • The referencing environment of a statement is the collection of all names that are visible in the statement • In a static-scoped language, it is the local variables plus all of the visible variables in all of the enclosing scopes • A subprogram is active if its execution has begun but has not yet terminated • In a dynamic-scoped language, the referencing environment is the local variables plus all visible variables in all active subprograms
  • 41. Copyright © 2007 Addison- Wesley. All rights reserved. 1-41 Named Constants • A named constant is a variable that is bound to a value only when it is bound to storage • Advantages: readability and modifiability • Used to parameterize programs • The binding of values to named constants can be either static (called manifest constants) or dynamic • Languages: – FORTRAN 95: constant-valued expressions – Ada, C++, and Java: expressions of any kind – C# has two kinds, readonly and const - the values of const named constants are bound at compile time - The values of readonly named constants are dynamically bound
  • 42. Copyright © 2007 Addison- Wesley. All rights reserved. 1-42 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;
  • 43. Copyright © 2007 Addison- Wesley. All rights reserved. 1-43 Summary • Case sensitivity and the relationship of names to special words represent design issues of names • Variables are characterized by the sextuples: name, address, value, type, lifetime, scope • Binding is the association of attributes with program entities • Scalar variables are categorized as: static, stack dynamic, explicit heap dynamic, implicit heap dynamic • Strong typing means detecting all type errors