Scope 1 Scope Variable scope defines the range
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 main() { /** C **/ int x = 5; Search local declarations first, printf("%dn", x); { then search in increasingly x = 10; larger enclosing scopes, until int y = 20; a valid declaration is found printf("%dn", x); { int x = 15; Variable x becomes hidden int z = 20; Not valid in Java or C# printf("%dn", x); } } } How? 3
Static Scope Static scope indicates that the scope of a variable can be determined before execution Java. Script 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 Java. Script and PHP do support function-level 4 scope
Dynamic Scoping Dynamic scoping defines a variable’s scope based on the calling sequences of subprograms Used in APL, SNOBOL 4, early versions of LISP 5 procedure Main is X : Integer; procedure Sub 1 is begin -- of Sub 1. . . X. . . end; -- of Sub 1 procedure Sub 2 is X : Integer; begin -- of Sub 2. . . end; -- of Sub 2 begin -- of Main. . . end; -- of Main Call Main, which calls Sub 1 Call Main, which calls Sub 2, which then calls Sub 1 ly be n o n a of X c e p time o n c u s r t ned a i m r e t de
Problem 8 Given this Ada code: Assume execution of procedure Main is X : Integer; procedure Sub 3; this code is Main calls Sub 1 calls Sub 2 calls Sub 3 Using static scoping, which X is referenced in Sub 1? In Sub 2? In Sub 3? 6 sing u t a e rep oping c s c i dynam -- allows Sub 1 to -- to call Sub 3 procedure Sub 1 is X : Integer; procedure Sub 2 is begin -- of Sub 2. . . end; -- of Sub 2 begin -- of Sub 1. . . end; -- of Sub 1 procedure Sub 3 is begin -- of Sub 3. . . end; -- of Sub 3 begin -- of Main. . . end; -- of Main
Problem 8 SOLUT Given this Ada code: Assume execution of this code is Main calls Sub 1 calls Sub 2 calls Sub 3 Using static scoping references Sub 1. X Sub 2 references Sub 1. X Sub 3 references Main. X Sub 1 7 procedure Main is X : Integer; procedure Sub 3; IONS -- allows Sub 1 to -- to call Sub 3 procedure Sub 1 is X : Integer; procedure Sub 2 is begin -- of Sub 2. . . end; -- of Sub 2 begin -- of Sub 1. . . end; -- of Sub 1 procedure Sub 3 is begin -- of Sub 3. . . end; -- of Sub 3 begin -- of Main. . . end; -- of Main
Problem 8 SOLUT Given this Ada code: Assume execution of this code is Main calls Sub 1 calls Sub 2 calls Sub 3 Using dynamic scoping references Sub 1. X Sub 2 references Sub 1. X Sub 3 references Sub 1. X Sub 1 8 procedure Main is X : Integer; procedure Sub 3; IONS -- allows Sub 1 to -- to call Sub 3 procedure Sub 1 is X : Integer; procedure Sub 2 is begin -- of Sub 2. . . end; -- of Sub 2 begin -- of Sub 1. . . end; -- of Sub 1 procedure Sub 3 is begin -- of Sub 3. . . end; -- of Sub 3 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 Sub. A is begin -- of Sub. A Put(X); -- i. e. output X end; -- of Sub. A procedure Sub. B is X : Integer; begin -- of Sub. B X : = 10; Sub. A end; -- of Sub. B begin -- of Main X : = 5; Sub. B end; -- of Main
Problem 9 SOLUT 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 IONS procedure Main is X : Integer; procedure Sub. A is begin -- of Sub. A Put(X); -- i. e. output X end; -- of Sub. A procedure Sub. B is X : Integer; begin -- of Sub. B X : = 10; Sub. A end; -- of Sub. B begin -- of Main X : = 5; Sub. B end; -- of Main
Problem 10 Given this Ada code: Assume program execution using static-scoping rules For Sub 1, Sub 2, and Sub 3, list the visible variables and the program units within which they are declared 11 procedure Main is X, Y, Z : Integer; procedure Sub 1 is A, Y, Z : Integer; procedure Sub 2 is A, B, Z : Integer; begin -- of Sub 2. . . end; -- of Sub 2 begin -- of Sub 1. . . end; -- of Sub 1 procedure Sub 3 is A, X, W : Integer; begin -- of Sub 3. . . end; -- of Sub 3 begin -- of Main. . . end; -- of Main
S O L UT Problem 10 Given this Ada code: Using static-scoping rules, list visible variables and the program units in which they are declared Sub 1: A (Sub 1), Y (Sub 1), Z (Sub 1), X (Main) Sub 2: A (Sub 2), B (Sub 2), Z (Sub 2), Y (Sub 1), X (Main) Sub 3: A (Sub 3), X (Sub 3), W (Sub 3), Y (Main), Z (Main) 12 IONS procedure Main is X, Y, Z : Integer; procedure Sub 1 is A, Y, Z : Integer; procedure Sub 2 is A, B, Z : Integer; begin -- of Sub 2. . . end; -- of Sub 2 begin -- of Sub 1. . . end; -- of Sub 1 procedure Sub 3 is A, X, W : Integer; begin -- of Sub 3. . . end; -- of Sub 3 begin -- of Main. . . end; -- of Main
Problem 11 procedure Main is X, Y, Z : Integer; procedure Sub 1 is A, Y, Z : Integer; Assume program execution begin -- of Sub 1 using static-scoping rules. . . end; -- of Sub 1 procedure Sub 2 is For Sub 1, Sub 2, and Sub 3, A, X, W : Integer; list the visible variables and procedure Sub 3 is A, B, Z : Integer; the program units within which begin -- of Sub 3 they are declared. . . end; -- of Sub 3 begin -- of Sub 2. . . end; -- of Sub 2 begin -- of Main. . . end; -- of Main Given this Ada code: 13
S O L UT Problem 11 Given this Ada code: Using static-scoping rules, list visible variables and the program units in which they are declared Sub 1: A (Sub 1), Y (Sub 1), Z (Sub 1), X(Main) Sub 2: A (Sub 2), X (Sub 2), W (Sub 2), Z (Main), Y (Main) Sub 3: A (Sub 3), B (Sub 3), Z (Sub 3), X (Sub 2), W (Sub 2), Y (Main) 14 IONS procedure Main is X, Y, Z : Integer; procedure Sub 1 is A, Y, Z : Integer; begin -- of Sub 1. . . end; -- of Sub 1 procedure Sub 2 is A, X, W : Integer; procedure Sub 3 is A, B, Z : Integer; begin -- of Sub 3. . . end; -- of Sub 3 begin -- of Sub 2. . . end; -- of Sub 2 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 do. Something() { 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 }
S OLUT Problem 12 Given this C code: For points A, B, C, and D, 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) 16 IONS void do. Something() { 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 f 1(), which calls f 3() main() calls f 2(), which calls f 3(), which calls f 1() main() calls f 3(), which calls f 1() 17 void main() { int a, b, c; . . . } void f 1() { int b, c, d; . . . } void f 2() { int c, d, e; . . . } void f 3() { int d, e, f; . . . }
Example SOLUT 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 f 1(), which calls f 3() In f 3(): d, e, f declared in f 3() b, c declared in f 1() a declared in main() 18 IONS void main() { int a, b, c; . . . } void f 1() { int b, c, d; . . . } void f 2() { int c, d, e; . . . } void f 3() { int d, e, f; . . . }
Example SOLUT 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 f 2(), which calls f 3(), which calls f 1() In f 1(): b, c, d declared in f 1() e, f declared in f 3() a declared in main() 19 IONS void main() { int a, b, c; . . . } void f 1() { int b, c, d; . . . } void f 2() { int c, d, e; . . . } void f 3() { int d, e, f; . . . }
Example SOLUT 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 f 3(), which calls f 1() In f 1(): b, c, d declared in f 1() e, f declared in f 3() a declared in main() 20 IONS void main() { int a, b, c; . . . } void f 1() { int b, c, d; . . . } void f 2() { int c, d, e; . . . } void f 3() { int d, e, f; . . . }
Problem 14 procedure Main is X, Y, Z : Integer; procedure Sub 1 is A, Y, Z : Integer; Assume dynamic-scoping rules begin -- of Sub 1 Given the following call sequences, . . . end; -- of Sub 1 what variables are visible during procedure Sub 2 is execution of the last function called ? A, B, Z : Integer; begin -- of Sub 2 Also specify the name of the function. . . in which each variable was defined end; -- of Sub 2 main() calls Sub 1(), which calls procedure Sub 3 is Sub 2(), which calls Sub 3() A, X, W : Integer; begin -- of Sub 3 main() calls Sub 1() , which calls. . . Sub 3() end; -- of Sub 3 main() calls Sub 2(), which calls begin -- of Main Sub 3() , which calls Sub 3(). . . end; -- of Main Given this Ada code: 21
- Slides: 21