Академический Документы
Профессиональный Документы
Культура Документы
Prof. M. N. Sahoo
sahoom@nitrkl.ac.in
sahoo.manmath@gmail.com
Outline
Names
Variables
The Concept of Binding
Object Lifetime and Storage Management
Scope
Scope and Lifetime
Referencing Environments
Names
A name is a string of characters used to
identify some entity in a program
Design issues for names:
Length of the names?
Are names case sensitive?
Are special words reserved words or keywords?
Names (continued)
Length
If too short, they cannot be self-expressive
Language examples:
FORTRAN 95: maximum of 31
C99: no limit but only the first 63 are significant
C, C++, C#, Ada, and Java: no limit, and all are
significant
Names (continued)
Name forms
Commonly used - a letter followed by a string
consisting of letters, digits, and underscore
Camel notation : all of the words of a multiword name except the first are init-capitalized,
as in myGenericArray
Initial special characters :
PHP: all variable names must begin with $
signs
Perl: scalar variables start with $, array
variables start with @
4
Names (continued)
Case sensitivity
Disadvantage: affects readability (names that
look alike are different; Height of a cylinder,
height of a cone)
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. parseInt)
Names (continued)
Special words
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)
Names (continued)
Special words
Reserve words are better than keywords bcoz keywords may lead
to confusion. E.g.
Real Integer
Integer Real
Potential problem with reserved words: If there are too many,
many collisions occur (e.g., COBOL has 300 reserved words!
-- but LENGTH, COUNT are suppose to be used by the users)
Variables
A variable is an abstraction of a memory
cell
Variables can be characterized with a set
of attributes:
Name
Address
Value
Type
Lifetime
Scope
8
Variables Attributes
Name (already discussed)
Address - the memory address with which it is
associated
Address is called the l-value of the variable
A variable may have different addresses at different
times during execution
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
11
Compile time
bind a variable to a type in C or Java
decide layout of statically defined data in memory
12
Load time
bind instructions to memory cells (absolute address)
Bind static variables to memory cells (absolute
address)
Runtime
bind a nonstatic local variables to a memory cells
bind values to the variables
13
14
Type Binding
Design issues:
How is a type specified?
When does the binding take place?
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)
16
Explicit/Implicit Declaration
Fortran has both explicit and implicit:
Integers start with I, J, K, L, M, or N
rest are Real
Perl:
scalars starts with $
Arrays - starts with @
17
18
19
20
code
globals
static local variables
constant literals
tables generated by compiler that are used during run time
for debugging, exception handling, garbage collection etc.
22
23
24
25
crossed-internal fragmentation
26
27
Buddy system
28
Garbage collection
Collecting leak memories
Garbage collection is an essential language feature
30
Scope Rules
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
31
Static Scope
Based on program text, the binding is decided
To connect a name reference to the object, 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
32
big.x
sub1.x
h
o
l
e
function sub2() {
var y = x;
}
sub1();
}
33
C can access any local object of A with known offset, by following the static
link to B, then following the static link from B to A, then following the offset.
35
Problem:
36
37
38
Blocks
A method of creating static scopes inside program
units--from ALGOL 60
Example in C:
void sub() {
int count;
while (...) {
int count;
count++;
...
}
39
Blocks
In Lisp, the block structure is implemented by let
construct
e.g.
40
Global Scope
C/C++:
Globally declared and/or defined objects are
implicitly available to all subroutines following
it.
The subroutines before it, need to specify it
with extern qualifier to access
int main(){
extern int x;
printf("%d",x);
return 0;
}
int x=10;
41
Global Scope
PHP
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
42
Global Scope
43
Global Scope
Java Script
Similar to PHP, except that
there is no way to access a global variable in a
function that has declared a local variable with
the same name
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
44
Global Scope
ERROR
45
Disadvantage:
Less flexible
46
Dynamic Scope
Based on calling sequences of program
units, not their textual layout (temporal
versus spatial)
Bindings between names and objects are
decided by searching back through the
chain of subprogram calls
47
Static scoping
Reference to X is to Big's X
Dynamic scoping
Reference to X is to Sub1's X
48
Disadvantages:
While a subprogram is executing, all its variables are
visible to all subprograms it calls
Impossible to statically type check
Poor readability
49
50
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 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
51
EXAMPLE
}
A,B of EXAMPLE
}
52
b, c of sub2; d of main
c, d of main
53