Professional Documents
Culture Documents
Class 3
Class 3
Here current binding for a given name is the one encountered mostly
recently during execution and not yet destroyed
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
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