- Static scoping
procedure Example is
A, B : Integer ;
.....
procedure Sub1 is
X, Y : Integer ;
begin --- of sub1
...... <----- 1
end -- of sub1
procedure Sub2 is
X : Integer ;
......
procedure Sub3 id
X: Integer ;
begin -- of Sub3
...... <------ 2
end --- of Sub3
begin -- of Sub2
..... <--------3
end -- of Sub2
begin --- of Example
..... <--------- 4
end. -- of Example
- X and Y of Sub1, A, B of Example
- X of Sub3, (X of Sub2 is hidden), A and B of Example
- X of Sub2 , A and B of Example
- Only A and B of Example
- Dynamic
void sub1() {
int a , b ;
...... <------1
} // end of sub1
void sub2() {
int b,c ;
....... <---- 2
sub1() ;
} // end of sub2
void main() {
int c , d ;
....... <----- 3
sub2() ;
} // end of main
- a and b of sub1, c of sub2, d of main. (c of main and b of sub2 are hidden )
- b and c of sub2, d of main . (c of main is hidden)
- c and d of main
- #5-8 page 226-227
Consider the following Ada skeletal program :
procedure Main is
X : Integer;
procedure Sub3 ; -- This is a declaration of Sub3
-- It allows Sub1 to call it
procedure Sub1 is
X : Integer ;
procedure Sub2 is
begin -- of Sub2
....
end; -- of Sub2
begin -- of Sub1
....
end ; --- of Sub1
procedure Sub3 is
begin -- Sub3
...
end ; -- of Sub3
begin -- of Main
....
end ; -- of Main
Main calls Sub1
Sub1 calls Sub2
Sub2 calls Sub3
a) Assuming Static scoping, which declaration of X is thecorrect one for a reference to X in
following :
i) Sub1
ii) Sub2
iii) Sub3
b) Repeat part a, but assume dynamic scoping.
- #5-9 page 245
Assume static scoping rules , what value of X is printed in Sub1 ?
Under dynamic scoping rules, what value of X is printed in Sub1?
procedure Main is
X : Integer;
procedure Sub1 is
begin -- of Sub1
Put(X) ;
end; -- of Sub1
procedure Sub2 is
X : Integer ;
begin -- of Sub2
X := 10 ;
Sub1
end ; -- of Sub2
begin -- of Main
X := 5 ;
Sub2 ;
end --- of Main
- #5-10 page 246
List all the variables, along with the program units where they are declared, that are visible in
the bodies of Sub1, Sub2, and Sub3, assuming static scoping is used.
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
- #5-11 page 246
List all the variables, along with the program units where they are declared, that are visible in
the bodies of Sub1, Sub2, and Sub3, assuming static scoping is used.
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
- #5-12 page 247
For each of the four marked points in this function, list each visible variable,
along with the number of the definition statement that defines it.
void fun(void) {
int a, b, c ; /* defintion 1 */
...
while(...) {
int b, c, d ; /* defintion 2 */
.... <-------- 1
while ( ... ) {
int c, d, e ; /* defintion 3 */
... <------- 2
}
.... <------------- 3
}
.... <--------------- 4
}
- #5-13 page 247
Given the following calling sequences and assuming that dynamic scoping is used,
what variables are visible during execution of the last function called?
Include with each variable the name of the function in which it was defined.
- main calls fun1; fun1 calls fun2; fun2 call fun3.
- main calls fun1; fun1 calls fun3.
- main calls fun2; fun2 calls fun3; fun3 calls fun1.
- main calls fun3; fun3 calls fun1 .
- main calls fun1; fun1 calls fun3; fun3 calls fun2.
- main calls fun3; fun3 calls fun2; fun2 calls fun1.