Professional Documents
Culture Documents
LANGUAGES
Dr. D. C. Kiran
Associate Professor
Department of
Computer Science & Engineering
PRINCIPLES OF PROGRAMMING LANGUAGES
Dr. D. C. Kiran
Department of Computer Science and Engineering
PRINCIPLES OF PROGRAMMING LANGUAGES
Topics To Be Covered
• Scope of Variable
• Lexical Scoping
• Dynamic Scoping
PRINCIPLES OF PROGRAMMING LANGUAGES
Scope of Variable
Deep Access:
Non-local references are found by searching the activation
record instances on the dynamic chain.
Example in previous slide
Shallow Access:
• Put locals in a central place
• One stack for each variable name
• Central table with an entry for each variable name
PRINCIPLES OF PROGRAMMING LANGUAGES
int fun1( )
{
int res, a,b,g,h,i,j;
fun2()
}
int fun2( )
{
int a,b,g,l,m,n;
Draw run time stack
m= i+f;
}
PRINCIPLES OF PROGRAMMING LANGUAGES
int fun1( )
{
int res, a,b,g,h,i,j;
Fun2()
}
int fun2( )
{
int a,b,g,l,m,n;
m= i+f;
}
PRINCIPLES OF PROGRAMMING LANGUAGES
int fun1( )
{
int res, a,b,g,h,i,j;
Fun2()
}
int fun2( )
{
int a,b,g,l,m,n;
m= i+f;
}
PRINCIPLES OF PROGRAMMING LANGUAGES
Exercise:
void main()
fun2
{
int a,b,c,d,e,f; fun1
fun1() main
fun2()
}
int fun1( )
{
int res, a,b,g,h,i,j;
}
int fun2( )
{
int a,b,g,l,m,n;
Draw run time stack
Fill the visible variables
}
Calculate the address of non local variable
PRINCIPLES OF PROGRAMMING LANGUAGES
Exercise:
void main()
fun2 a b g l m n c d e f
{
int a,b,c,d,e,f; fun1 a b g h i j c d e f
fun1() main a b c d e f
fun2()
}
int fun1( )
{
int res, a,b,g,h,i,j;
}
int fun2( )
{
int a,b,g,l,m,n;
Draw run time stack
Fill the visible variables
}
Calculate the address of non local variable
PRINCIPLES OF PROGRAMMING LANGUAGES
Example 1 :Static vs Dynamic
int i = 10;
void fun1() {
printf(“Inside fun1…%d\n”, i);
}
void fun2() { Static Scope
int i = 20; Inside fun1… 10
fun1(); Inside fun1… 10
}
Inside fun1… 10
int main() {
fun1();
fun2(); Dynamic Scope
fun1(); Inside fun1… 10
} Inside fun1… 20
Inside fun1… 10
PRINCIPLES OF PROGRAMMING LANGUAGES
Example 2 :Static vs Dynamic
var a : integer;
procedure first
a := 1;
procedure second
var a : integer;
first();
begin
a := 2;
second();
write_integer(a);
end;
15
PRINCIPLES OF PROGRAMMING LANGUAGES
Example 2 :Static vs Dynamic
var a : integer;
procedure first
a := 1;
var a : integer;
main()
procedure second a := 2;
var a : integer; second()
first(); var a : integer;
first()
begin a := 1;
a := 2; write_integer(a);
second();
write_integer(a);
The program prints 1
end;
16
PRINCIPLES OF PROGRAMMING LANGUAGES
Example 2 :Static vs Dynamic
var a : integer;
procedure first
a := 1;
var a : integer;
main()
procedure second a := 2;
var a : integer; second()
first(); var a : integer;
first()
begin a := 1;
a := 2; write_integer(a);
second();
write_integer(a);
The program prints 2
end;
17
PRINCIPLES OF PROGRAMMING LANGUAGES
Summary: Takeaway
• Scope of Variable
• Lexical Scoping
Compiler will identify
A.K.A Static Scoping
• Dynamic Scoping
Identified during runtime
Deep access
Shallow access
• Examples
THANK YOU
Dr. D. C. Kiran
Department of Computer Science and Engineering
dckiran@pes.edu
9829935135