1 / 22

Scope Rules (Section 3.3)

CSCI 431 Programming Languages Fall 2003. Scope Rules (Section 3.3). A compilation of material developed by Felix Hernandez-Campos and Michael Scott. Binding Time (Review). A binding is an association between two things E.g. Name of an object and the object

effie
Télécharger la présentation

Scope Rules (Section 3.3)

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. CSCI 431 Programming Languages Fall 2003 Scope Rules(Section 3.3) A compilation of material developed by Felix Hernandez-Campos and Michael Scott

  2. Binding Time (Review) • A binding is an association between two things • E.g. Name of an object and the object • Binding time is the time at which a binding is created • Language design time • Language implementation • Program writing time • Compile Time • Link Time • Load Time • Run Time

  3. Binding Time Example • Java program • What is the binding time of • Value of argument x? • Set of values of argument x? • Type of argument x? • Set of types of argument x? • Properties of operator +? publicstaticint increment(int x) { return x + 1; }

  4. Binding Time Example • Perl program • What is the binding time of • Value of argument @_? • Set of values of argument @_? • Type of argument @_? • Set of types of argument @_? • Properties of operator +? sub increment { return shift @_ + 1; }

  5. Scope • Scopeis the textual region of a program in which a binding is active • The word 'scope' used as a noun is a program section of maximal size in which no bindings change. • In most languages with subroutines, we OPEN a new scope on subroutine entry. • Algol 68 introduced the term elaboration for the process of creating bindings when entering a scope. Elaboration time is a useful concept.

  6. Referencing Environment • The referencing environment (of a statement or expression) is the set of active bindings. • A referencing environment corresponds to a collection of scopes that are examined (in order) to find a binding.

  7. Scope Rules • Programming languages implement • Static Scoping: active bindings are determined using the text of the program. The determination of scopes can be made by the compiler. • Most recent scan of the program from top to bottom • Closest nested subroutine rule • Most compiled languages employ static scope rules • Dynamic Scoping: active bindings are determined by the flow of execution at run time (i.e., the call sequence). • The determination of scopes can NOT be made by the compiler. • Dynamic scope rules are usually encountered in interpreted languages; in particular, early LISP dialects assumed dynamic scope rules.

  8. Static Scope • The classical example of static scope rules is the most closely nested rule used in block structured languages such as Algol 60 and Pascal. • An identifier is known in the scope in which it is declared and in each enclosed scope, unless it is redeclared in an enclosed scope. • To resolve a reference to an identifier, we examine the local scope and statically enclosing scopes until a binding is found.

  9. Nested SubroutinesClosest Nested Subroutine Rule

  10. Storage Management (revisited) • Static allocation for: • code, globals, "own" variables, explicit constants • scalars may be stored in the instructions themselves • Central stack for: • parameters, local variables, temporaries, bookkeeping information • Why a stack? • allocate space for recursive routines • (not necessary in FORTRAN, etc) • reuse space • (useful in any language) • Heap for: • dynamic allocation

  11. Stack-based Allocation

  12. Static Links • Local variables and arguments are assigned fixed offsets from the stack pointer or frame pointer at compile time • Access to non-local variables via static links • Each frame points to the frame of the (correct instance of) the routine inside which it was declared. • In the absense of formal subroutines, "correct" means closest to the top of the stack. • you access a variable in a scope k levels out by following k static links and then using the known offset within the frame thus found.

  13. Nested SubroutinesDetermining Scope Static Links

  14. An Example Procedure A2 { int a = 0; Procedure C { Procedure D { print a; } Call D; } Procedure B { char a = 'b'; Call C; } Call B; }

  15. Static Scope Variants • The key idea in static scope rules is that bindings are defined by the physical (lexical) structure of the program. • A newer example of static scope rules is the import/export strategies of modular languages such as Modula-2. • The modules of Modula, Ada, etc. give you closed scopes (no identifier is automatically inherited from enclosing scopes) without the limited lifetime. • ALGOL 60 blocks implement OPEN scopes (identifiers which are not redeclared are automatically inherited from the enclosing scope). • Bindings to variables declared in a module are inactive outside the module, but not destroyed.

  16. Other Static Scope Variants • Euclid is an example of a language with lexically-nested scopes in which all scopes are closed. • The Euclid rules were designed to avoid ALIASES, which complicate optimization and correctness arguments. • It forces you to document side effects by explicitly importing any external variables that are read or written. • Euclid prevents you from passing a variable by reference to a procedure that imports the same variable.

  17. Evolution of Data Abstraction Facilities subroutines, variables, arrays Fortran, Basic subroutine nesting Algol 60, Pascal, many others own (static) variables Algol 68, Fortran ("save"), C, others module as manager Modula, C files (sorta) module as type Simula*, Euclid classes, with inheritance Simula*, Smalltalk, C++, Eiffel, Java, others *predates Modula; clearly a language ahead of its time time

  18. Dynamic Scope Rules • Bindings cannot always be resolved by examining the program because they are dependent on calling sequences. • Dynamic scope rules are usually encountered in interpreted languages. • Such languages do not normally have type checking at compile time because type determination isn't always possible when dynamic scope rules are in effect.

  19. Dynamic Scope • Bindings between names and objects depend on the flow of control at run time • The current binding is the one found most recently during execution • Example • If the scoping is static, the output of the program is 1 • If the scoping is dynamic, output is 1 or 2 depending on the value read at line 8 (>0 or <=0 respectively)

  20. Why Dynamic Scope • Perhaps the most common use of dynamic scope rules is to provide implicit parameters to subroutines. • This is generally considered bad programming practice nowadays. Ex: -Given a function print_integer() capable of printing in several bases. -Want to use decimal notation most times -set variable print_base to 10 early in execution -to change base: begin --nested block print_base : integer := 16 print_integer(n)

  21. Accessing Variables with Dynamic Scope • Two approaches: • Keep a stack (*association list*) of all active variables. • When you need to find a variable, hunt down from top of stack. • This is equivalent to searching the activation records on the dynamic chain. • Keep a central table with one slot for every variable name. • If names cannot be created at run time, the table layout (and the location of every slot) can be fixed at compile time. Otherwise, you'll need a hash function or something to do lookup. • Every subroutine changes the table entries for its locals at entry and exit.

  22. Implementation of Dynamic Scoping

More Related