Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Scope Rules (Section 3.3) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael Scott.

Similar presentations


Presentation on theme: "1 Scope Rules (Section 3.3) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael Scott."— Presentation transcript:

1 1 Scope Rules (Section 3.3) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael Scott

2 2 Binding Time (Review) A binding is an association between two thingsA 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 createdBinding 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 3 Binding Time Example Java programJava program What is the binding time ofWhat 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 +? public static int increment(int x) { return x + 1; }

4 4 Binding Time Example Perl programPerl program What is the binding time ofWhat 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 5 Scope Scope is the textual region of a program in which a binding is activeScope is 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.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.Algol 68 introduced the term elaboration for the process of creating bindings when entering a scope. Elaboration time is a useful concept.

6 6 Referencing Environment The referencing environment (of a statement or expression) is the set of active bindings.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.A referencing environment corresponds to a collection of scopes that are examined (in order) to find a binding.

7 7 Scope Rules Programming languages implementProgramming 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 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.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.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.To resolve a reference to an identifier, we examine the local scope and statically enclosing scopes until a binding is found.

9 9 Nested Subroutines Closest Nested Subroutine Rule

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

11 11 Stack-based Allocation

12 12 Static Links Local variables and arguments are assigned fixed offsets from the stack pointer or frame pointer at compile timeLocal variables and arguments are assigned fixed offsets from the stack pointer or frame pointer at compile time Access to non-local variables via static linksAccess 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 13 Nested Subroutines Determining Scope StaticLinks

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

15 15 Static Scope Variants The key idea in static scope rules is that bindings are defined by the physical (lexical) structure of the program.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.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.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 16 Other Static Scope Variants Euclid is an example of a language with lexically- nested scopes in which all scopes are closed.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.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.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.Euclid prevents you from passing a variable by reference to a procedure that imports the same variable.

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

18 18 Dynamic Scope Rules Bindings cannot always be resolved by examining the program because they are dependent on calling sequences.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.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.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 19 Dynamic Scope Bindings between names and objects depend on the flow of control at run timeBindings between names and objects depend on the flow of control at run time –The current binding is the one found most recently during execution ExampleExample –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 or <=0 respectively)

20 20 Why Dynamic Scope Perhaps the most common use of dynamic scope rules is to provide implicit parameters to subroutines.Perhaps the most common use of dynamic scope rules is to provide implicit parameters to subroutines. This is generally considered bad programming practice nowadays.This is generally considered bad programming practice nowadays. Ex: -Given a function print_integer() capable of printing in several bases. 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 21 Accessing Variables with Dynamic Scope Two approaches: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 22 Implementation of Dynamic Scoping


Download ppt "1 Scope Rules (Section 3.3) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael Scott."

Similar presentations


Ads by Google