You are on page 1of 39

UNIT-2

Names, Bindings, and Scopes


Topics

Introduction
Names
Variables
The Concept of Binding
Scope
Scope and Lifetime
Referencing Environments
Named Constants

Copyright 2009 Addison-Wesley. All rights reserved. 1-2


Introduction

Imperative languages are abstractions of von Neumann architecture


Memory
Processor
The abstractions in a language for the memory cells of the machine
are variables.

Copyright 2009 Addison-Wesley. All rights reserved. 1-3


Names

Names are associated with variables, functions, user-defined


types, labeled statements, subprograms, formal parameters,
and other program constructs. The term identifier is often
used interchangeably with name.
Design issues for names:
1. Maximum length?

2. What characters are allowed?


3. Are names case sensitive?
4. Are special words reserved words or keywords?
1-4
Names (continued)

If too short, they cannot be meaningful


Language examples:
FORTRAN I: maximum 6
COBOL: maximum 30
FORTRAN 95 and ANSI C: maximum 31
C99 has no length limit
Ada , C# and Java: no limit, and all are significant
C++: no limit

Copyright 2009 Addison-Wesley. All rights reserved. 1-5


Names (continued)

Special characters
Under score( _ ) notation is less popular in c
based
C++ introduce camel notation ex: myStack
PHP: all variable names must begin with dollar
signs
Perl: all variable names begin with special
characters, which specify the variables type
Ruby: variable names that begin with @ are
instance variables; those that
begin with @@ are class variables
Copyright 2009 Addison-Wesley. All rights reserved. 1-6
Names (continued)
In many languages, notably the C-based languages, uppercase and
lowercase letters in names are distinct; that is, names in these languages
are case sensitive.
For example, the following three names are distinct in C++: rose , ROSE ,
and Rose .
C, C++, C# and Java names are case sensitive

Ada, BASIC, Fortran, SQL and Pascal


are case insensitive.

Copyright 2009 Addison-Wesley. All rights reserved. 1-7


Names (continued)
Keywords vs Reserved Words
Words that have a special meaning in the language
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.
most reserved words are also keywords
There is one potential problem with reserved words: If the language includes a
large number of reserved words, the user may have difficulty making up names
that are not reserved. The best example of this is COBOL, which has 300
reserved
Copyright words
2009 Addison-Wesley. All rights reserved. 1-8
Variables

A variable is an abstraction of a memory cell


Variables can be characterized as a sixtuple of attributes:
Name
Address
Value
Type
Lifetime
Scope
Variables Attributes
Name Most variables have names.

Address - The address of a variable is the machine memory address with


which it is associated. The address of a variable is sometimes called its l-value,
because the address is what is required when the name of a variable appears in
the left side of an assignment.

Type - The type of a variable determines the range of values the variable can
store and the set of operations that are defined for values of the type. For
example, the int type in Java specifies a value range of -2147483648 to
2147483647 and arithmetic operations for addition, subtraction, multiplication,
division, and modulus.
Value - the contents of the location with which the variable is
associated (r-value). A variables value is sometimes called its r-
value because it is what is required when the name of the
variable appears in the right side of an assignment statement. To
access the r-value, the l-value must be determined first.

Lifetime- The lifetime of a variable is the time during which the


variable is bound to a specific memory location

Scope- The scope of a variable is the range of statements


in which the variable is visible
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 2009 Addison-Wesley. All rights reserved. 1-13


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 2009 Addison-Wesley. All rights reserved. 1-14


l
Consider the following Java assignment
statement:
l
count = count + 5;
l
Some of the bindings and their binding times
for the parts of this assignment
l
statement are as follows:
l
The type of count is bound at compile time.
l
The set of possible values of count is bound
at compiler design time.
l
The meaning of the operator symbol + is
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 2009 Addison-Wesley. All rights reserved. 1-16


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 2009 Addison-Wesley. All rights reserved. 1-17


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)
In Fortran, If the identifier begins with one of the letters I , J , K , L , M , or N ,
or their lowercase versions, it is implicitly declared to be Integer type;
otherwise, it is implicitly declared to be Real type.

Advantage: writability
Disadvantage: reliability (less trouble with Perl)

Copyright 2009 Addison-Wesley. All rights reserved. 1-18


l
Implicit declaration some times called as
type inferencing.
l
For ex, in C# declaration of a variable must
include an initial value, whose type is made
the type of the variable. Consider the
following declarations:
l
var sum = 0;
l
var total = 0.0;
l
var name = "Fred";
l
The types of sum , total , and name are int ,
Dynamic Type Binding

Dynamic Type Binding (Python, Ruby, JavaScript and PHP)


Specified through an assignment statement e.g., JavaScript
List = [2, 4.33, 6, 8]; //1-D Aarray of length 4
List = 17.3; // scalar variable
Advantage: flexibility (generic program units)
Disadvantages:
High cost (dynamic type checking and interpretation)
Type error detection by the compiler is difficult

For example, JavaScript program,


i and x are currently the names ofscalar numeric variables
and y is currently the name of an array. Furthermore,
suppose that the program needs the assign-
ment statement
i = x;
but because of a keying error, it has the assignment
statement
i = y;
Copyright 2009 Addison-Wesley. All rights reserved. 1-20
l
In JavaScript (or any other language that uses dynamic type
binding), no error is detected in this statement by the
interpreterthe type of the variable named i is simply
changed to an array.
l
But later uses of i will expect it to be a scalar,
l
and correct results will be impossible.
l
In a language with static type binding,
l
such as Java, the compiler would detect the error in the
assignment i = y , and
l
the program would not get to execution.
l
Note that this disadvantage is also present to some extent in
some languages that use static type binding, such as
Fortran, C, and C++, which in many cases auto-matically
convert the type of the RHS of an assignment to the LHS.
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 2009 Addison-Wesley. All rights reserved. 1-22


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 2009 Addison-Wesley. All rights reserved. 1-23


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 2009 Addison-Wesley. All rights reserved. 1-25


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 2009 Addison-Wesley. All rights reserved. 1-26


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:
l
- flexibility (generic code)
Disadvantages:
Inefficient, because all attributes are dynamic
Loss of error detection

Copyright 2009 Addison-Wesley. All rights reserved. 1-27


Type Checking
l
Type checking is the activity of ensuring that the
operands of an operator are of compatible types.
l
A compatible type is one that either is legal for
l
the operator or is allowed under language rules to be
implicitly converted by compiler-generated code (or the
interpreter) to a legal type. This automatic conversion is
called a coercion.
l
For example, if an int variable and a float variable are
added in Java, the value of the int variable is coerced to
float and a floating-point add is done.
l
A type error is the application of an operator
to an operand of an inappropriate type.
l
For example, in the original version of C, if
an int value was passed to a function that
expected a float value, a type error would
occur
l
Type checking is complicated when a
language allows a memory cell to store
values of different types at different times
during execution.
l
Such memory cells can be created with Ada
variant records, C and C++ unions, and the
discriminated unions of ML, Haskell, and F#.
l
If all bindings of variables to types are static
in a language, then type checking can nearly
always be done statically.
l
Dynamic type binding requires type
checking at run time, which is called dynamic
type checking.
Strong Typing
l
A programming language is strongly typed if type
errors are always detected. This requires that the
types of all operands can be determined, either at
compile time or at run time.
l
The importance of strong typing lies in its ability to
detect all misuses of variables that result in type
errors.
l
A strongly typed language also allows the
detection, at run time, of uses of the incorrect type
values in variables that can store values of more
than one type.
l
C and C++ are not strongly typed languages
because both include union types, which are
not type checked.
l
Ada,ML ,F# ,Java and C# are strongly typed
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
for example, by using the name pi instead of the constant
3.14159265 .
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 2009 Addison-Wesley. All rights reserved. 1-34
Type Equivalence
l
Two types of Type equivalence
l
1. name type equivalence
l
2. structure type equivalence.
l
Name type equivalence means that two
variables have equivalent types if they are
defined either in the same declaration or in
declarations that use the same type name.
l
Structure type equivalence means that two
variables have equivalent types if their types
have identical structures.
l
Ada uses name type equivalence
l
type Indextype is 1..100;
l
count : Integer;
l
index : Indextype;
l
The types of the variables count and index
would not be equivalent; count could not be
assigned to index or vice versa.
l
Structure type equivalence is more flexible
than name type equivalence, but it is more
difficult to implement.
l
Under name type equivalence, only the two
type names must be compared to determine
equivalence
l
Under structure type equivalence, however,
the entire structures of the two types must be
compared.
l
Ex:- type Celsius is new Float;
l
The types of variables of these two derived
types are not equivalent, although their
structures are identical.
C uses both name and structure type
equivalece.Every struct , enum ,and union
declaration creates a new type that is not
equivalent to other types.

You might also like