170 likes | 197 Views
CS314 – Section 5 Recitation 7. Long Zhao ( lz311@rutgers.edu ) Static & Dynamic Scope Midterm Q & A. Slides available at http://www.ilab.rutgers.edu/~lz311/CS314. Names and Binding. Programs use names to refer to things E.g., in x = x + 1 , x refers to a variable
E N D
CS314 – Section 5Recitation 7 Long Zhao (lz311@rutgers.edu) Static & Dynamic Scope Midterm Q & A Slides available at http://www.ilab.rutgers.edu/~lz311/CS314
Names and Binding Programs use names to refer to things E.g., in x = x + 1, x refers to a variable A binding is an association between a name and what it refers to int x; /* x is bound to a stack location containing an int */ int f (int) { ... } /* f is bound to a function */ class C { ... } /* C is bound to a class */ let x = e1 in e2 (* x is bound to e1 *)
Names and Scopes A scope is the region of a program where a binding is active The same name in a different scope can have a different binding A name is in scope if it's bound to something within the particular scope we’re referring to Two names bound to the same object are aliases
Example i is in scope in the body of w, the body of y, and after the declaration of j in z but all those i’s are different j is in scope in the body of x and z void w(int i) { ... } void x(float j) { ... } void y(float i) { ... } void z(void) { int j; char *i; ... }
Ordering of Bindings Languages make various choices for when declarations of things are in scope Generally, all declarations are in scope from the declaration onward What about function calls? int x = 0; int f() { return x; } int g() { int x = 1; return f(); } What is the result of calling g()?
Static Scope In static scoping, a name refers to its closest binding, going from inner to outer scope in the program text Languages like C, C++, Java, Ruby, and OCaml are statically scoped inti; { int j; { float i; j = (int) i; } }
Effect of Static Scoping • The following pseudo-code program demonstrates the effect of scoping on variable bindings: • a:integerprocedure firsta:=1procedure seconda:integer first()procedure maina:=2 second()write_integer(a) Program execution: a:integer main() a:=2 second() a:integer first() a:=1 write_integer(a) binding Program prints “1”
Dynamic Scope In a language with dynamic scoping, a name refers to its closest binding at runtime
Effect of Dynamic Scoping • The following pseudo-code program demonstrates the effect of scoping on variable bindings: • a:integerprocedure first a:=1procedure seconda:integer first()procedure maina:=2 second() write_integer(a) Program execution: a:integer main() a:=2 second() a:integer first() a:=1 write_integer(a) Binding depends on execution binding Program prints “2”
Ordering of Bindings Back to the example: int x = 0; int f() { return x; } int g() { int x = 1; return f(); } What is the result of calling g() ... ... with static scoping? ... with dynamic scoping?
Static & Dynamic Scope (Exercise) 1. 2. a : integer; procedure foo a = 10 goo() hoo() write(a) procedure goo a = 20 procedure hoo write(a) procedure main a : integer a = 30 foo() write(a) int x; int main() { x = 14; f(); g(); } void f() { int x = 13; h(); } void g() { int x = 12; h(); } void h() { printf("%d\n",x); } • What is the result of calling main() • ... with static scoping? • ... with dynamic scoping?
Static & Dynamic Scope (Exercise) 1. static scoping: 14 14 main() x = 14 // x = 14 (global) f() int x = 13 // x = 14 (global) h() printf // x = 14 (global) g() int x = 12 // x = 14 (global) h() printf // x = 14 (global) int x; int main() { x = 14; f(); g(); } void f() { int x = 13; h(); } void g() { int x = 12; h(); } void h() { printf("%d\n", x); }
Static & Dynamic Scope (Exercise) 1. main() x = 14 // x = 14 f() int x = 13 // x = 13 (f) h() printf // x = 13 g() int x = 12 // x = 12 (f) h() printf // x = 12 dynamic scoping: 13 12 int x; int main() { x = 14; f(); g(); } void f() { int x = 13; h(); } void g() { int x = 12; h(); } void h() { printf("%d\n",x); }
Static & Dynamic Scope (Exercise) 2. static scoping: 20 20 30 main() a : integer a = 30 // a = 30 (main) foo() a = 10 // a = 30 (main); a = 10(global) goo() a = 20 // a = 30 (main); a = 20(global) hoo() write(a) // a = 30 (main); a = 20(global) write(a) // a = 30 (main); a = 20(global) write(a) // a = 30 (main); a = 20(global) a : integer; procedure foo a = 10 goo() hoo() write (a) procedure goo a = 20 procedure hoo write(a) procedure main a : integer a = 30 foo() write (a)
Static & Dynamic Scope (Exercise) 2. main() a : integer a = 30 // a = 30 (main) foo() a = 10 // a = 10 (main) goo() a = 20 // a = 20 (main) hoo() write(a) // a = 20 (main) write(a) // a = 20 (main) write(a) // a = 20 (main) a : integer; procedure foo a = 10 goo() hoo() write (a) procedure goo a = 20 procedure hoo write(a) procedure main a : integer a = 30 foo() write (a) dynamic scoping: 20 20 20
Static vs. Dynamic Scope Static scoping Local understanding of function behavior Know at compile-time what each name refers to A bit trickier to implement • Dynamic scoping • Can be hard to understand behavior of functions • Requires finding name bindings at runtime • Easier to implement (just keep a global table of stacks of variable/value bindings )
Midterm Checklist • rewriting systems • how to design them • how to apply them to a given input • regular expressions • is ? • find an or • FSAs (NFAs, DFAs) • context-free grammars • leftmost, rightmost derivations • parse trees, abstract syntax trees • grammar ambiguity • operator precedence, associativity • LL(1) grammars • FIRST, FOLLOW, FIRST+ • recursive-descent parsing • interpreters, code generation, type checking, etc. • C pointers, memory management • static & dynamic scope