2 - names, bindings, and scopes
DESCRIPTION
pplTRANSCRIPT
Names, Bindings, and Scopes
Principles of Programming Languages (CS213)
Prof. M. N. [email protected]@gmail.com
Outline
• Names• Variables• The Concept of Binding• Object Lifetime and Storage Management• Scope • Scope and Lifetime• Referencing Environments
1
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?
2
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
3
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 multi-
word 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)
5
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)
– A reserved word is a special word that cannot be used as a user-defined name
6
Names (continued)
• Special words
– Reserve words are better than keywords bcoz keywords may lead to confusion. E.g.
Real IntegerInteger 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)
7
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)
9
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
- also called r-value of the variable
10
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
• Binding time is also defined as the time at which any implementation decision is made to create a binding
11
• Language design timethe design of specific program constructs (syntax), primitive types, and meaning (semantics)– if (a>0) b=a; in C– meaning e.g. + (add)– what are the fundamental types?
• Language implementation time– bind floating point type to a representation– bind data-types to their ranges– maximum size of stack and heap– Internal representation of literals e.g. 3.1 and "foo
bar"
• Compile time– bind a variable to a type in C or Java– decide layout of statically defined data in memory
Possible Binding Times
12
• Link time– bind a reference(call) to its subroutine
• 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
Possible Binding Times
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
• Static bindings are more efficient because of early decisions
• Dynamic bindings give flexibility, or expressiveness– Can write generic programs
14
Type Binding
Design issues:• 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
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 @
• C#: implicit data type is decided from the context (mandates initialization)
var sum = 1var total = 2.5
17
Dynamic Type Binding
• Specified through an assignment statement (here type binding is temporary, so as address binding)
e.g., JavaScript
list = [2, 4.33, 6, 8];
list = 17.3;– Advantage: flexibility (generic program units)– Disadvantages:
• High cost w.r.t time (dynamic type checking)• Type error detection by the compiler is difficult a, x are scalars; y is array a=y instead of a=x will convert a to
array with may be a problem for subsequent use of a
18
Object Lifetime & Storage Management
• Key events– creation of objects– creation of bindings– references to variables (which use bindings)– (temporary) deactivation of bindings– reactivation of bindings– destruction of bindings– destruction of objects
19
Object Lifetime & Storage Management
• The period of time from creation to destruction is called the LIFETIME of a binding– Object may outlive binding (e.g. passing an object by
reference)– If binding outlives object it's a dangling reference (a
dynamic object is pointed by two pointers and then deleted by one pointer, the other pointer is a dangling reference)
• Storage Allocation mechanisms– Static (once given absolute address that is not changed
during execution)– Stack (objects are allocated and deallocated in LIFO order)– Heap (objects may be allocated and deallocated at arbitrary
times)20
Object Lifetime & Storage Management
• Static allocation for– code– globals– static local variables– constant literals– tables generated by compiler that are used during run time
for debugging, exception handling, garbage collection etc.
• Static allocation for local variables– Earlier versions of Fortran90 was not supporting recursion– So, local variables can be allocated statically in this case
• Static allocation for constants– Compile time constant (e.g. const x = 15; in C) can be
allocated statically– Elaboration time constant – are variables that don’t chagne
their value once assigned are not allocated statically 21
Object Lifetime & Storage Management
• Stack based allocation for recursion/subroutine calls– each frame (activation record), on the stack, for an active
subroutine contains:• Arguments and return values• Local variables• Temporaries (intermediate values produced)• Bookkeeping information (subroutine’s return address, a
reference to the caller’s stack frame, etc.)
22
Object Lifetime & Storage Management
23
Object Lifetime & Storage Management
• The stack pointer (sp) register always points to the first unused location on the stack.
• The frame pointer (fp) register points to a known location within the frame of the current subroutine.
• Objects that are suppose to put on the stack frame are assigned static offset from fp
• Local variables, temporaries, and bookkeeping informations have negative offsets
• Arguments and returns have positive offsets since they reside in callers frame
24
• Advantages of Stack-based allocation:– Useful in recursion– Reuse of stack space
Object Lifetime & Storage Management
25
• Heap based allocation– Memory blocks are allocated & deallocated from heap
dynamically
Object Lifetime & Storage Management
White- free blocks | grey- allocated | crossed-internal fragmentation
*there is possibility of external fragmentation
26
Object Lifetime & Storage Management
• Memory management for Heap– A single free list : search using first-fit, best-fit– Multiple lists (Buddy system) – standard block sizes are
power of 2
Eg. Say minimum block size is 8K, total memory is 128K
27
Buddy system
28
• Memory management for Heap– Multiple lists (Fibonacci heap) – similar to buddy
system, but uses Fibonacci numbers for block sizes
• Advantages of multiple lists over single list– By merging blocks in close proximity reduces
external fragmentation but not completely eliminates it
(to eliminate it, we follow compaction)
29
• Collecting leak memories
• Garbage collection is an essential language feature
Garbage collection
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
Static Scope (nested subroutines)
• nested subprogram definitions create nested static scopes
function big() {var x = 3;
function sub1() {var x = 7;sub2();
}
function sub2() {var y = x;
}
sub1();}
big.x sub1.x
hole
33
Static Scope (nested subroutines)
• Reference to x in sub2 binds to x in its ancestor i.e. big
• The scope of x in big is hidden in sub1creating a hole– But there is provision in Ada to access the hidden scope.
E.g. big.x in sub2 will access big’s x
– :: to access global variables in C++
How to access nonlocal variables from an ancestor’s stack frame ???
Ans: each frame keeps a STATIC Link to its static parent
34
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.
Static Scope (nested subroutines)
35
Static Scope (declaration order)
• Some languages (Algol60, Lisp) mandates the declarations to appear at the beginning of their scope
• In Pascal– Names must be declared before their use– the scope of a declaration is the entire block
– Problem:
36
Static Scope (declaration order)
• Pascal successors [Ada, C, C++, Java, etc.] specify that the scope of an identifier is from the point of its declaration to the end of that block – So the previous program (line 5) will access outer N
• Whole-block scope in C#, just like Pascal. So the following is invalid in C#
37
Static Scope (declaration order)
• Modula-3 adopts whole-block scope, but the declaration order doesn’t matter.
• “Local if written” in Python– If x is written on L.H.S of = operator in a subroutine and
its parent, then they are different
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.
– The scope of a name starts after the declaration list only; so the B is assigned the outer A’s value
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
• 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
Global Scope
42
Global Scope
43
• 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
Global Scope
44
ERROR
Global Scope
45
Evaluation of Static Scoping
• Advantages – Better readability– Less run-time overhead
• 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
Dynamic Scope Example
Big - declaration of X Sub1 - declaration of X - ... call Sub2 ...
Sub2 ... - reference to X - ...
... call Sub1 …
Big calls Sub1Sub1 calls Sub2Sub2 uses X
• Static scoping – Reference to X is to Big's X
• Dynamic scoping – Reference to X is to Sub1's X
48
Evaluation of Dynamic Scoping
• Advantages: – Convenient because you don’t need to pass the
arguments to the called subroutines
• Disadvantages: – While a subprogram is executing, all its variables are
visible to all subprograms it calls– Impossible to statically type check– Poor readability
49
Scope and Lifetime
• The lifetime of a variable is the time during which it is bound to a particular memory cell
• Scope and lifetime are sometimes closely related, but are different concepts
• Consider a static variable in a C or C++ function, -- different
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
Function EXAMPLE{A, B : Integer;Function SUB1{
X, Y : Integer;…
}Function SUB2{
X, Z : Integer;Function SUB3{
X : Integer;…
}…
}… }
X, Y of SUB1 & A,B of EXAMPLE
Referencing Environments for static-scoped language
X of SUB3, Z of SUB2 & A,B of EXAMPLE
X, Z of SUB2 & A,B of EXAMPLE
A,B of EXAMPLE
52
a, b of sub1; c of sub2; d of main
b, c of sub2; d of main
c, d of main
Referencing Environments for dynamic-scoped language
53