X - Cap490

Scope
1
Scope
 Variable scope defines the range of statements
over which a variable is visible
 Local variables of a program unit X are variables that
are declared within X and therefore visible within X
 Non-local variables of a program unit X are variables
that are visible but not declared within X
2
Static Scope
 Static scope indicates that the scope of a variable
can be determined before execution
 How?
 Search local declarations first,
then search in increasingly
larger enclosing scopes, until
a valid declaration is found
 Variable x becomes hidden
 Not valid in Java or C#
3
main() {
/** C **/
int x = 5;
printf("%d\n", x);
{
int x = 10;
int y = 20;
printf("%d\n", x);
{
int x = 15;
int z = 20;
printf("%d\n", x);
}
}
}
Static Scope
 Static scope indicates that the scope of a variable
can be determined before execution
 JavaScript and PHP do not support nested static scopes
var abc = false;
if ( s == "HELLO" ) {
abc = true;
var abc = 500;
document.write("abc is " + abc)
}
document.write("abc is still " + abc)
 But JavaScript and PHP do support function-level scope
4
Dynamic Scoping
 Dynamic scoping defines a variable’s scope based
on the calling sequences of subprograms
 Used in APL, SNOBOL4, early versions of LISP
5
procedure Main is
X : Integer;
procedure Sub1 is
begin -- of Sub1
... X ...
end;
-- of Sub1
procedure Sub2 is
X : Integer;
begin -- of Sub2
...
end;
-- of Sub2
begin -- of Main
...
end;
-- of Main
Call Main, which calls Sub1
Call Main, which calls Sub2,
which then calls Sub1
Problem 8
 Given this Ada code:
 Assume execution of
this code is
Main calls Sub1
Sub1 calls Sub2
Sub2 calls Sub3
 Using static scoping,
which X is referenced
in Sub1? In Sub2?
In Sub3?
6
procedure Main is
X : Integer;
procedure Sub3;
-- allows Sub1 to
-- to call Sub3
procedure Sub1 is
X : Integer;
procedure Sub2 is
begin -- of Sub2
...
end;
-- of Sub2
begin -- of Sub1
...
end;
-- of Sub1
procedure Sub3 is
begin -- of Sub3
...
end;
-- of Sub3
begin -- of Main
...
end;
-- of Main
Problem 8
 Given this Ada code:
 Assume execution of
this code is
Main calls Sub1
Sub1 calls Sub2
Sub2 calls Sub3
 Using static scoping
7

Sub1 references Sub1.X

Sub2 references Sub1.X

Sub3 references Main.X
procedure Main is
X : Integer;
procedure Sub3;
-- allows Sub1 to
-- to call Sub3
procedure Sub1 is
X : Integer;
procedure Sub2 is
begin -- of Sub2
...
end;
-- of Sub2
begin -- of Sub1
...
end;
-- of Sub1
procedure Sub3 is
begin -- of Sub3
...
end;
-- of Sub3
begin -- of Main
...
end;
-- of Main
Problem 8
 Given this Ada code:
 Assume execution of
this code is
Main calls Sub1
Sub1 calls Sub2
Sub2 calls Sub3
 Using dynamic scoping
8

Sub1 references Sub1.X

Sub2 references Sub1.X

Sub3 references Sub1.X
procedure Main is
X : Integer;
procedure Sub3;
-- allows Sub1 to
-- to call Sub3
procedure Sub1 is
X : Integer;
procedure Sub2 is
begin -- of Sub2
...
end;
-- of Sub2
begin -- of Sub1
...
end;
-- of Sub1
procedure Sub3 is
begin -- of Sub3
...
end;
-- of Sub3
begin -- of Main
...
end;
-- of Main
Problem 9
 Given this Ada code:
 Assume program
execution using
static-scoping rules
 What is the output
of this program?
 What is the output
using dynamic-scoping
rules?
9
procedure Main is
X : Integer;
procedure SubA is
begin -- of SubA
Put(X); -- i.e. output X
end;
-- of SubA
procedure SubB is
X : Integer;
begin -- of SubB
X := 10;
SubA
end;
-- of SubB
begin -- of Main
X := 5;
SubB
end;
-- of Main
Problem 9
 Given this Ada code:
 Assume program
execution using
static-scoping rules
 What is the output
of this program?
 5
 What is the output
using dynamic-scoping?
 10
10
procedure Main is
X : Integer;
procedure SubA is
begin -- of SubA
Put(X); -- i.e. output X
end;
-- of SubA
procedure SubB is
X : Integer;
begin -- of SubB
X := 10;
SubA
end;
-- of SubB
begin -- of Main
X := 5;
SubB
end;
-- of Main
Problem 10
 Given this Ada code:
 Assume program execution
using static-scoping rules
 For Sub1, Sub2, and Sub3,
list the visible variables and
the program units within which
they are declared
11
procedure Main is
X, Y, Z : Integer;
procedure Sub1 is
A, Y, Z : Integer;
procedure Sub2 is
A, B, Z : Integer;
begin -- of Sub2
...
end;
-- of Sub2
begin -- of Sub1
...
end;
-- of Sub1
procedure Sub3 is
A, X, W : Integer;
begin -- of Sub3
...
end;
-- of Sub3
begin -- of Main
...
end;
-- of Main
Problem 10
 Given this Ada code:
 Using static-scoping rules, list
visible variables and the program
units in which they are declared
 Sub1:
A (Sub1), Y (Sub1),
Z (Sub1), X (Main)
 Sub2:
A (Sub2), B (Sub2),
Z (Sub2), Y (Sub1),
X (Main)
 Sub3:
A (Sub3), X (Sub3),
W (Sub3), Y (Main),
Z (Main)
12
procedure Main is
X, Y, Z : Integer;
procedure Sub1 is
A, Y, Z : Integer;
procedure Sub2 is
A, B, Z : Integer;
begin -- of Sub2
...
end;
-- of Sub2
begin -- of Sub1
...
end;
-- of Sub1
procedure Sub3 is
A, X, W : Integer;
begin -- of Sub3
...
end;
-- of Sub3
begin -- of Main
...
end;
-- of Main
Problem 11
 Given this Ada code:
 Assume program execution
using static-scoping rules
 For Sub1, Sub2, and Sub3,
list the visible variables and
the program units within which
they are declared
13
procedure Main is
X, Y, Z : Integer;
procedure Sub1 is
A, Y, Z : Integer;
begin -- of Sub1
...
end;
-- of Sub1
procedure Sub2 is
A, X, W : Integer;
procedure Sub3 is
A, B, Z : Integer;
begin -- of Sub3
...
end;
-- of Sub3
begin -- of Sub2
...
end;
-- of Sub2
begin -- of Main
...
end;
-- of Main
Problem 11
 Given this Ada code:
 Using static-scoping rules, list
visible variables and the program
units in which they are declared
 Sub1:
A (Sub1), Y (Sub1),
Z (Sub1), X(Main)
 Sub2:
A (Sub2), X (Sub2),
W (Sub2), Z (Main), Y (Main)
 Sub3:
A (Sub3), B (Sub3),
Z (Sub3), X (Sub2),
W (Sub2), Y (Main)
14
procedure Main is
X, Y, Z : Integer;
procedure Sub1 is
A, Y, Z : Integer;
begin -- of Sub1
...
end;
-- of Sub1
procedure Sub2 is
A, X, W : Integer;
procedure Sub3 is
A, B, Z : Integer;
begin -- of Sub3
...
end;
-- of Sub3
begin -- of Sub2
...
end;
-- of Sub2
begin -- of Main
...
end;
-- of Main
Problem 12
 Given this C code:
 For each of the marked
points A, B, C, and D,
list each visible variable
and the number of the
declaration statement
15
void doSomething()
{
int a, b, c; /** 1 **/
...
while ( ... )
{
int b, c, d; /** 2 **/
...
Point A
while ( ... )
{
int c, d, e; /** 3 **/
...
Point B
}
...
Point C
}
...
Point D
}
Problem 12
 Given this C code:
 For points A, B, C, and D,




16
list each visible variable
and the number of the
declaration statement
A: a (1), b (2), c (2), d (2)
B: a (1), b (2), c (3),
d (3), e (3)
C: a (1), b (2), c (2), d (2)
D: a (1), b (1), c (1)
void doSomething()
{
int a, b, c; /** 1 **/
...
while ( ... )
{
int b, c, d; /** 2 **/
...
Point A
while ( ... )
{
int c, d, e; /** 3 **/
...
Point B
}
...
Point C
}
...
Point D
}
Example
 Given this C-like code:
 Assume dynamic-scoping rules
 Given the following call sequences,
what variables are visible during
execution of the last function called ?
 Also specify the name of the function
in which each variable was defined
 main() calls f1(), which calls f3()
 main() calls f2(), which calls f3(),
which calls f1()
 main() calls f3(), which calls f1()
17
void main() {
int a, b, c;
...
}
void f1() {
int b, c, d;
...
}
void f2() {
int c, d, e;
...
}
void f3() {
int d, e, f;
...
}
Example
 Given this C-like code:
 Assume dynamic-scoping rules
 Given the following call sequences,
what variables are visible during
execution of the last function called ?
 main() calls f1(), which calls f3()
 In f3(): d, e, f declared in f3()
b, c declared in f1()
a declared in main()
18
void main() {
int a, b, c;
...
}
void f1() {
int b, c, d;
...
}
void f2() {
int c, d, e;
...
}
void f3() {
int d, e, f;
...
}
Example
 Given this C-like code:
 Assume dynamic-scoping rules
 Given the following call sequences,
what variables are visible during
execution of the last function called ?
 main() calls f2(), which calls f3(),
which calls f1()
 In f1(): b, c, d declared in f1()
e, f declared in f3()
a declared in main()
19
void main() {
int a, b, c;
...
}
void f1() {
int b, c, d;
...
}
void f2() {
int c, d, e;
...
}
void f3() {
int d, e, f;
...
}
Example
 Given this C-like code:
 Assume dynamic-scoping rules
 Given the following call sequences,
what variables are visible during
execution of the last function called ?
 main() calls f3(), which calls f1()
 In f1(): b, c, d declared in f1()
e, f declared in f3()
a declared in main()
20
void main() {
int a, b, c;
...
}
void f1() {
int b, c, d;
...
}
void f2() {
int c, d, e;
...
}
void f3() {
int d, e, f;
...
}
Problem 14
 Given this Ada code:
 Assume dynamic-scoping rules
 Given the following call sequences,
what variables are visible during
execution of the last function called ?
 Also specify the name of the function
in which each variable was defined
 main() calls Sub1(), which calls
Sub2(), which calls Sub3()
 main() calls Sub1() , which calls
Sub3()
 main() calls Sub2(), which calls
Sub3() , which calls Sub3()
21
procedure Main is
X, Y, Z : Integer;
procedure Sub1 is
A, Y, Z : Integer;
begin -- of Sub1
...
end;
-- of Sub1
procedure Sub2 is
A, B, Z : Integer;
begin -- of Sub2
...
end;
-- of Sub2
procedure Sub3 is
A, X, W : Integer;
begin -- of Sub3
...
end;
-- of Sub3
begin -- of Main
...
end;
-- of Main