1 / 17

CS314 – Section 5 Recitation 7

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

kaczmarek
Download Presentation

CS314 – Section 5 Recitation 7

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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 *)‏

  3. 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

  4. 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; ... }

  5. 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()?

  6. 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; } }

  7. 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”

  8. Dynamic Scope In a language with dynamic scoping, a name refers to its closest binding at runtime

  9. 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”

  10. 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?

  11. 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?

  12. 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); }

  13. 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); }

  14. 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)

  15. 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

  16. 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 )‏

  17. 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

More Related