Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 34

Chapter 2::

Names, Scopes, and Bindings

Programming Language Pragmatics


Name, Scope, and Binding
• A name is character string used to represent something else
– Most names are identifiers
– symbols (like '+') can also be names
• A binding is an association between two things, such as a
name and the thing it names
• The scope of a binding is the part of the program (textually)in
which the binding is active

• Binding Time:Binding Time is the point at which a binding is


created or, more generally, the point at which any
implementation decision is made
Different Times at which decision is made

• Different Times at which decision is made are


a)language design time
Control flow constructs, data types are chosen during the
design time

b)language implementation time


Most languages have a variety of issues during the
implementation time
Precision(no of bites) is the fundamental data types,file
management, organization of stack,run time exceptions
Binding

c)program writing time


Programmers choose algorithms, data structure and names
d)compile time
Compilers choose the mapping between high level constructs to
machine code
e)link time
Most compiler support separate compilation
A program is not complete until all the various modules are
combined together.
The linker chooses the overall layout of the modules
Binding
f)load time
It refers to the point at which the os loads the program into
memory so that it can run
Most operating systems distinguish b/w virtual and physical
address
Virtual address are choosen at link time and physical address
are choosen at run tim
g)run time
 It covers the entire span from the beginning to the end of
execution
 Binding of values to variables occur at run time and
other decisions vary from language to language
Lifetime and Storage Management
• Binding lifetime: The period of time from
creation to destruction is called the LIFETIME of
a binding
• Object Life time: the time between the creation
and destruction of an object is the object life time
• Object Life time generally Corresponds to one of
the three principle storage locations mechanisms
1. Static Objects
2. Stack Objects
3. Heap Objects
Static Allocation

• Global variables are the examples


• The instructions that constitute a programs machine language
translations can be a statistically allocated objects
• Number and String Valued Constants literals
• Most compilers produce a variety of tables are used by the run
time support libraries
• Statically allocated objects whose value should not change
during program execution are often allocates in protected,read
only memory
• Local variables are created when their subroutine is called and
destroyed
• In many languages a named constant is required to have a
value that can be determined at compile time
Stack Allocation

• Contents of a stack frame (cf., Figure 3.1)


– arguments and returns
– local variables
– temporaries
– bookkeeping (saved registers, line number static
link, etc.)
• Local variables and arguments are assigned
fixed OFFSETS from the stack pointer or
frame pointer at compile time
Stack Allocation
Stack Allocation
• The natural nesting of sub routine calls makes it easy to allocate
space for locals on a stack
• Each instance of a subroutine at run time has its own frame on the
stack
• Frame contains arguments ,temporaries etc
• Arguments are passed to subsequent routines lie at the top of the
frame.
• Maintenance of stack is the responsibility of the subroutine calling
sequence
a)The code executed by the caller immediately before & after the call
(calling Sequence)
b)Prologue(code executed at the beginning)
c)Epilogue(code executed at the end of subroutine)
Stack Allocation

• The offset of objects within a frame usually can be statically


determined.
• Frame pointer is a register that always point to a known location
within a frame of the current subroutine
• Code that need to access any content within the current frame adds
a pre determined offset to the value in the frame pointer
• Some machines provide a special push and pop instructions that
assume the direction of growth
Heap Based Allocation
• A heap is a region of storage in which sub blocks can be allocated
and de allocated at arbitrary times
• Heaps are required for the dynamically allocate pieces of linked
data structures and for objects like fully general character strings,
lists and sets.
• Heaps consists of allocated and free space blocks
• Space concerns can be Further dived into two issues
a)Internal Fragmentation-occurs when a process does not use its
allocated space completely
Eg: Block size: 2048 kb
process size: 2000 KB
unused : 48 kb
b)external Fragmentation: occurs when the blocks that have been
assigned to active objects are scattered through the heap in such a
way that the remaining unused is composed of multiple blocks
There may be a lot of free pace but no block may be large enough
Heap Based Allocation

• Heap for dynamic allocation


Heap Based Allocation
• A free list is maintained by algorithms not
currently in use
• At each allocation request the algorithm searches
the list for a block of appropriate size
• A)First Fit Algorithm: Selects the first block on the
list that is large enough the satisfy the request
• B)Best Fit Algorithm: Searches the entire list to
find the smallest block that is large enough to
satisfy the request
• Best fit looks better but it has a higher allocation
cost
Heap Based Allocation
• To reduce the cost to a constant some storage
management algorithms maintain a separate free
lists for blocks of different size
• Here the heap is divide into pools one for each
standard size
• The division may be static or dynamics
• Two mechanism for dynamic pool adjustment
i) Buddy System
ii) Fibonacci Heap
Scope Rules

• Scope-the textual region of the program in


which a binding is active in its scope
• Two types scope
• A)Static scope: The Scope of binding is
determined statically at compile time
• B) dynamic Scope: The binding depend on the
flow of execution at run time
Static scoping
• The binding between the names and objects can be determined at
compile time by examining the text of the program
• In early versions of Basic, there was only a single, global scope
there was no explicit declarations
• Fotran- variable declaration are optional i.e distinguished between
global and local variables
• if a variable is not declared it is assumed to be local
• Life time of local variable in fotran encompassed a single execution
• We can override this by using a explicit save statement
void label_name(char *c)
{

static short int n;


sprint(s ,”L%d\0”,++n);
}
Nested Scoping

• It was introduced in Algol 60


• It is a feature of many modern langauges includih
pascal,ml,python,scheme,common lisp
• It is based on closed nested scope rule
A name that is introduced in a declaration is known in
the scope in which it is declared and in each
internally nested scope, unless it is hidden by
another declaration of the same name in one or more
nested scopes
Scope Rules

• From the above figure


• P2 is called only p1
• P4 is visible only within p1
• P3 is visible only within p2
• F1 is visible only within p4
• F1 could call p2
• P4 could call f1
• P2 could not call f1
Nested Scoping

a) Declaration order: i) names must be declared before they are


used
ii) The scope of declaration is the entire surrounding block

b) Declaration and definition


Declaration introduces a name & initiates its scope but may it may
omit certain implementation details
Definition describes the object in sufficient detail for the compiler to
determines its implementation
Modules

 Major challenge in a construction of any large body of


a s/w is how to divide the effort among programmers
 Bigger Applications depends on the notion of
information hiding
 Properly modularized code reduces the load on the
programmer
Modules as abstraction

 A module allows a collection of objects to be encapsulated in


such a way that
 i) objects inside are visible to each other
 ii)Objects on the inside are not visible on the outside unless
explicitly exported
 iii)objects outside are not visible on the inside unless explicitly
imported
 These rules affect only the visibility of objects but not their
lifetime
 Modules were one of the principle language innovations of late
1970’s
 In ada,java,perl –package
 C++,C# - namespace
• If the stack is declared at the programmer outermost nesting level
then s and top retain their values through out the execution and
they are visible only to the code inside push and pop
• If stack is declared inside some routine then s and top have the
same time as the local variables
• If stack is declared inside some other module mod then s and top
have the same life time as they have had

• Module into which names must be explicitly imported are said to


be closed scope
• Module that do not require imports are said be open scope
Question Covered

• Define Binding and Binding Time? Explain the different times at which decisions
may be bound? 10M
• Explain Stack Based Allocation with a neat block Diagram 10M
• Explain Heap Based Allocation with a neat block Diagram 10M
• Explain Static Scoping & Nested Subroutines with an Example 10M
• Explain Modules as abstraction and managers with an Example 10M
Scope Rules
Example: Static vs. Dynamic

program scopes (input, output );


var a : integer;
procedure first;
begin a := 1; end;
procedure second;
var a : integer;
begin first; end;
begin
a := 2; second; write(a);
end.
Scope Rules
Example: Static vs. Dynamic

• If static scope rules are in effect (as would be


the case in Pascal), the program prints a 1
• If dynamic scope rules are in effect, the
program prints a 2
• Why the difference? At issue is whether the
assignment to the variable a in procedure
first changes the variable a declared in the
main program or the variable a declared in
procedure second
Scope Rules
Example: Static vs. Dynamic
• Static scope rules require that the reference resolve
to the most recent, compile-time binding, namely
the global variable a
• Dynamic scope rules, on the other hand, require that
we choose the most recent, active binding at run
time
– Perhaps the most common use of dynamic scope rules is to
provide implicit parameters to subroutines
– This is generally considered bad programming practice
nowadays
• Alternative mechanisms exist
– static variables that can be modified by auxiliary routines
– default and optional parameters
Scope Rules
Example: Static vs. Dynamic

• At run time we create a binding for a when


we enter the main program.
• Then we create another binding for a when
we enter procedure second
– This is the most recent, active binding when
procedure first is executed
– Thus, we modify the variable local to procedure
second, not the global variable
– However, we write the global variable because the
variable a local to procedure second is no longer active
The Meaning of Names within a Scope
• Aliasing
– What are aliases good for? (consider uses of
FORTRAN equivalence)
• space saving - modern data allocation methods are
better
• multiple representations - unions are better
• linked data structures - legit
– Also, aliases arise in parameter passing as an
unfortunate side effect
• Euclid scope rules are designed to prevent this
The Meaning of Names within a Scope
• Overloading
– some overloading happens in almost all
languages
• integer + vs. real +
• read and write in Pascal
• function return in Pascal
– some languages get into overloading in a big
way
• Ada
• C++
The Meaning of Names within a Scope
• It's worth distinguishing between some closely
related concepts
– overloaded functions - two different things with
the same name; in C++
• overload norm
int norm (int a){return a>0 ? a : -a;)
complex norm (complex c ) { // ...
– polymorphic functions -- one thing that works in
more than one way
• in Modula-2: function min (A : array of integer); …
• in Smalltalk
The Meaning of Names within a Scope
• It's worth distinguishing between some
closely related concepts (2)
– generic functions (modules, etc.) - a syntactic
template that can be instantiated in more than
one way at compile time
• via macro processors in C++
• built-in in C++
• in Clu
• in Ada

You might also like