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

Dynamic Scope

Binding between names and objects depends on the flow of control at


run time & in particular on the order in which subroutines are called

Here current binding for a given name is the one encountered mostly
recently during execution and not yet destroyed

Ex: Static Versus Dynamic Scoping


n:integer – global declaration
1. else
Procedure first
2. first()
n=1
3. write_integer(n)
Procedure second
4.

n=integer – local declaration


5.

first()
6.

n=2
7.

If read_integer()>0
8.

second()
9.
Example: Static vs. Dynamic
• Of static scoping is in effect this program prints 1
• Of dynamic scoping is in the effect the output depends on
the value read at line 8 at run time
if the input is positive the program prints as 2 otherwise it
prints as 1
• With dynamic scoping ,errors associated with the
referencing environment may be not detected until run
time
Example: Static vs. Dynamic
• Of static scoping is in effect this program prints 1
• Of dynamic scoping is in the effect the output depends on
the value read at line 8 at run time
if the input is positive the program prints as 2 otherwise it
prints as 1
• With dynamic scoping ,errors associated with the
referencing environment may be not detected until run
time
Implementing scope
• To keep track of the names in a statically scope program a compiler
relies on a symbol table
• Symbol table maps names to the information the compiler knows
about them
• Basic operations
A)to insert a new mapping
B)to lookup the information that is already present for a given name

• There are tow organization to implementing scoping


• i)association list
• ii) central reference table
Implementing scope
i) Association list( A list)
• It is a simply a list of name /value pairs
• It functions a list
• Bindings are found by searching down the list from the top

ii)Central Reference table


• It avoids the need for linear time search by maintaining an explicit
mapping from names to their current meanings
• Look up is faster but scope entry and exit are somewhat more
complex
The Meaning of Names within a Scope

1)Aliasing
Aliases occur in the common blocks & equivalent statement of
fotran like pascal and c
They also occur in program that make of pointer based data
structures
A more suitable way to create is to pass a variable by reference
to a subroutine that also access that variable directly
Eg: void accumulate (doube &x)
{
Sum + =x;
Sum_of_square += x*x
}
accumulate(sum)
The Meaning of Names within a Scope

2)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
Overloading in c++
Void print_num(int n)
{
}
Void print_num(int n,base b)
{
}
Void print_num(complex c)
{

}
Print_num(i);
Print_num(i,hex);
Print_num(x);
The Meaning of Names within a Scope
3) Polymorphism and Related Concepts
•To compute the minimum of two values of either integer or floating
point type
•Eg
function min(a,b:integer) return integer
function min(x,y:real) return real

Coercion
A process by which a compiler automatically converts a value of one
type into a value of another type when the second type is required by
the surrounding context
Eg double min(double x ,double y) Suppose
{ int i,j,k;
i= min(j,k);
}
The Meaning of Names within a Scope
4) Polymorphism
-it allows a single subroutine to accept unconverted arguments of
multiple types
-polymorphic means having multiple forms

-There are two types


a)Parametric polymorphism
•The code takes a type as a parameter either explicitly or implicitly
•Explicit parametric polymorphism is also known as generic (appears
in ada,C++,clu,eiffe,Java,C#)
•C++ calls it by the name of templates.
•Ex – C++
•We need three separate function in c to swap two integers,floats and
characters
The Meaning of Names within a Scope
To avoid this C++ uses a ‘function template’
<template class T>
Void swap(T a ,T b)
{
t = temp;
Temp =a;
a=b;
b = temp;
}

Implicit Parametric polymorphism appears in lisp and ml families of


languages
Chapter 3:: Control Flow

Programming Language Pragmatics


Control Flow
• Control flow in a program execution determines what should be
done first, what second and so on to complete the desire task.
 There are eight categories of language mechanisms to specify
ordering
1. Sequencing: Statements are to be executed (or expressions
evaluated) in a certain specified order
2. Selection: Depending on some run-time condition, a choice is to be
made among two or more statements or expressions
3. Iteration: A given fragment of code is to be executed repeatedly,
either a certain number of times or until a certain run-time condition
is true. Iteration constructs include while, do, and repeat loops.
Control Flow & Language Mechanisms
• Control flow in a program execution determines what should be
done first, what second and so on to complete the desire task.
 There are eight categories of language mechanisms to specify
ordering
1. Sequencing: Statements are to be executed (or expressions
evaluated) in a certain specified order
2. Selection: Depending on some run-time condition, a choice is to be
made among two or more statements or expressions
3. Iteration: A given fragment of code is to be executed repeatedly,
either a certain number of times or until a certain run-time condition
is true. Iteration constructs include while, do, and repeat loops.
Language Mechanisms
4. Procedural abstraction: A potentially complex collection of
control constructs (a subroutine) is encapsulated in a way that allows
it to be treated as a single unit, often subject to parameterization.
5. Recursion: An expression is defined in terms of (simpler versions
of) itself, either directly or indirectly
6. Concurrency: Two or more program fragments are to be
executed/evaluated “at the same time,” either in parallel on separate
processors or interleaved
7.Exception handling: A program fragment is executed
optimistically, on the assumption that some expected condition will
be true.
8. No determinacy: The ordering or choice among statements or
expressions is deliberately left unspecified, implying that any
alternative will lead to correct results.
Value and reference Model
A)value model
Consider the following assignments in C:
d=a; -----i)
a = b+c ------ii)
In the first statement, the right-hand side refers to the value of a placed
in d
In the second statement, the left-hand side refers to the refers to the
location of a, where we want to put the sum of b and c.
L-values: They are expression that denote memory locations
R-values: They are expression that denotes values
•Languages like C use a value model of variables.
•As Not all expression can be l-values and not all values have a location
–Eg: 2+3 =a is wrong and a= 2+3 if a is constant
Reference model
• A language can make the distinction between I-values and R-values
more explicit by employing a reference model of variables.
• Languages that do this include Algol 68, CLU, Lisp/Scheme, ML,
Haskell, and Smalltalk,
• A variable is not a named container for a value rather it is a named
reference to a value.
• Eg: b= 2;
c=b;
a= b+c;
Initialization

There are three reasons, why initial values may be useful:


1.a static variable that is local to a subroutine needs an
initial value in order to be useful.
2.For any statically allocated variable, an initial value that is
specified in the declaration can be preallocated in global
memory by the compiler.
3.Accidental use of an uninitialized variable is one of the
most common programming errors.
a) Dynamic Checks
•Instead of giving every uninitialized variable a default value, a
language or implementation can choose to define the use of an
uninitialized variable as a dynamic semantic error, and can catch
these errors at run time.
•The advantage of the semantic checks is that they will often identify
a program bug.
•The cost of catching all uses of an uninitialized variable at run time
are considerably high this is due to maintenance and checking of an
initialized/uninitialized flag.
b) Definite assignment:
•For local variables of methods java and c# define a notion of definite
assignment that prevents the use of uninitialized variables.
•This notion is based on the control flow of the program
•Every control path to an expression must assign a value to every
variable in that expression. This is a conservative rule; it can
sometimes prohibit programs that would never initialized a value
•Eg:int i;
int j=3; if (j>0)
If (j>0) {
{ System.out.println(i);//error
i =2; }
}
c)Constructors
•C ++ distinguishes between initialization and assignment
•Initialization means a call  to a constructors function for variable
type with an initial value as as  argument
•Assignment means a simple bit wise and copy of the vale on the
assignment right hand side.
D) Ordering with Expressions
•Precedence and Associativity rules define the order in which the
binary infix operators are applied
Eg: a-f(b)-c*d
For the first ‘-’ operator a-f(b)
For  the second operator c*d
Without additional operators we can evaluate the total expression
•Two main reasons
i) side effects
ii)code improvement
e) short circuit evaluation
• Boolean expression provide  a special and important for code
improvement and increased reliability
• A compiler that performs short circuit evaluation of Boolean expression
will generate code that skips the second half of both of these computations
when the overall value can be determined from the first half.
• It changes the semantics of Boolean expression
•   Eg:Consider (a<b) & (b<c)
•  If a is greater than b there is no need to check whether b is less than c
..since the overall expression is false
If (a>b) | (b>c)
If a is greater than b ,then there is no need to check the second condition
b>c since the overall expression is true.
• 
Question to be covered

• Explain Dynamic Scoping with an Example and implementing the


scope 10M
• Explain a)aliases b)overloading c)polymorphism with an Examples
10M
• Explain the different categories of Language Mechanisms?10M?
• Explain the value model and reference model with an Example?6M
• Why initialization is important? how can you achieve it.
10M

You might also like