0% found this document useful (0 votes)
124 views19 pages

J PPL Unit 2 Lecture 4 Scope Part 1

The document discusses scope of variables in programming languages. It defines scope as the range of statements over which a variable is visible. It describes lexical (static) scoping where variable binding is determined at compile time based on the program structure, and dynamic scoping where binding is determined at runtime based on the call stack. Lexical scoping is used in languages like Pascal and C, while dynamic scoping was used in early Lisp dialects. Examples are provided to illustrate the differences between static and dynamic scoping.

Uploaded by

anon_999010792
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
124 views19 pages

J PPL Unit 2 Lecture 4 Scope Part 1

The document discusses scope of variables in programming languages. It defines scope as the range of statements over which a variable is visible. It describes lexical (static) scoping where variable binding is determined at compile time based on the program structure, and dynamic scoping where binding is determined at runtime based on the call stack. Lexical scoping is used in languages like Pascal and C, while dynamic scoping was used in early Lisp dialects. Examples are provided to illustrate the differences between static and dynamic scoping.

Uploaded by

anon_999010792
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

PRINCIPLES OF PROGRAMMING

LANGUAGES

Dr. D. C. Kiran
Associate Professor
Department of
Computer Science & Engineering
PRINCIPLES OF PROGRAMMING LANGUAGES

Scope of Variable: Part 1

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

• The scope of a variable is the range of statements over which


it is visible
• The non local variables (free 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.
• Disallowing them altogether (except keywords and special
forms)
• Lexical Scoping: Compile Time
-- Only two scoping Levels- Global and Local.
-- Nested scoping
• Dynamic Scoping: At run time.
PRINCIPLES OF PROGRAMMING LANGUAGES
Static Scope

• Also called Lexical Scope


• Lexical scope rules specify the association of variables
with declarations based on just the examination of the
source code
• The binding of variables to declarations is done at compile
time
• It is based on their spatial relationship
• Pascal and C use Static Scoping rules
PRINCIPLES OF PROGRAMMING LANGUAGES
Static Scope
void main() {
int x,y,z,m; f, g while 3
while(…) ---1
{
d, e, m while 2
int a,b,c,m;
…………… while 1
a ,b, c, m
while(…)-2
{
x, y, z, m main
int d,e,m;
d = b + x + m;
} Statically visible variables
} While 3 F G X Y Z M
while(………)-3
While 2 D E M A B C X Y Z
{
int f,g;
While 1 A B C M X Y Z
f=b + x + m; Main X Y Z M
}
}
PRINCIPLES OF PROGRAMMING LANGUAGES
Dynamic Scope

• The binding of variables to declarations is done at run time


• It is based on the calling sequence of subprograms.

void sub3() { Call Sequence In sub3:


int x, z; main calls sub1
reference to x is local
x = u + v; sub1 calls sub1
} sub1 calls sub2 reference to u searches all activation
void sub2() { sub2 calls sub3 records on stack until main
int w, x; ... Run time Stack reference to v is in most recent
} activation record of sub1
void sub1() { Sub3 :x,z
int v, w; ...
Sub2 :w,x
} • Pure Lisp and original
Sub1: v,w
int main() { Common Lisp, as well
int v, u; ... Sub1: v,w as Snobol and APL use
} Main: v,u Dynamic Scoping Rules
PRINCIPLES OF PROGRAMMING LANGUAGES
Implementation of Dynamic Scope

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

Dynamic Scoping check the visibility of Non-local variable


void main()
fun2
{
int a,b,c,d,e,f; fun1
fun1() main a b c d e f
}

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

Dynamic Scoping check the visibility of Non-local variable


void main()
fun2
{
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
}

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

Dynamic Scoping check the visibility of Non-local variable


void main()
fun2 a b g l m n c d e f h i j
{
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
}

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
[email protected]
9829935135

You might also like