Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Symbol Table and Subroutine Closures (Sections 3.3 & 3.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos.

Similar presentations


Presentation on theme: "1 Symbol Table and Subroutine Closures (Sections 3.3 & 3.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos."— Presentation transcript:

1 1 Symbol Table and Subroutine Closures (Sections 3.3 & 3.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael Scott

2 2 Symbol Table Variable lookup in a dynamically-scoped language corresponds to symbol table lookup in a statically- scoped language.Variable lookup in a dynamically-scoped language corresponds to symbol table lookup in a statically- scoped language. Because static scope rules tend to be more complicated, the data structure and lookup algorithm also have to be more complicated.Because static scope rules tend to be more complicated, the data structure and lookup algorithm also have to be more complicated. In statically scoped languages, the compiler maintains the data structure called the symbol tableIn statically scoped languages, the compiler maintains the data structure called the symbol table The symbol table maps names to information about objectsThe symbol table maps names to information about objects –It works like a hash in Perl name type …

3 3 Symbol Table Static Scope Example Symbol table in Modula-2Symbol table in Modula-2

4 4 Symbol Table Static Scope and the Scope Stack Static scoping is implemented using a scope stack and a linked list of object records associated with the same name (see Figure 3.14 in your text).Static scoping is implemented using a scope stack and a linked list of object records associated with the same name (see Figure 3.14 in your text).

5 5 Symbol Table Scope Stack Example

6 6 Binding Rules The Referencing Environment of a statement at run time is the set of active bindings. It corresponds to a collection of scopes that are examined (in order) to find a binding.The Referencing Environment of a statement at run time is the set of active bindings. It corresponds to a collection of scopes that are examined (in order) to find a binding. Scope rules determine that collection and its order.Scope rules determine that collection and its order. Binding Rules determine which instance of a scope should be used to resolve references when calling a procedure that was passed as a parameter.Binding Rules determine which instance of a scope should be used to resolve references when calling a procedure that was passed as a parameter. Binding rules have no relevance to local and global references.Binding rules have no relevance to local and global references. Binding rules are therefore irrelevant in languages like C or Modula-2 which only allows outer-most subroutines to be passed as parameters.Binding rules are therefore irrelevant in languages like C or Modula-2 which only allows outer-most subroutines to be passed as parameters.

7 7 Shallow Binding With shallow binding the nonlocal referencing environment of a procedure instance is the referencing environment in force at the time it (the procedure) is invoked.With shallow binding the nonlocal referencing environment of a procedure instance is the referencing environment in force at the time it (the procedure) is invoked. Original LISP works this way by default.Original LISP works this way by default. Not used by languages that use static scoping rulesNot used by languages that use static scoping rules

8 8 Deep Binding With deep binding the nonlocal referencing environment of a procedure instance is the referencing environment in force at the time the procedure is first passed as a parameter.With deep binding the nonlocal referencing environment of a procedure instance is the referencing environment in force at the time the procedure is first passed as a parameter. This environment is the same as would be extant if the procedure were actually called at the point where it was passed as an argument.This environment is the same as would be extant if the procedure were actually called at the point where it was passed as an argument. When the procedure is passed as an argument, this referencing environment is passed as well.When the procedure is passed as an argument, this referencing environment is passed as well. When the procedure is eventually invoked (by calling it using the corresponding formal parameter), this saved referencing environment is restored.When the procedure is eventually invoked (by calling it using the corresponding formal parameter), this saved referencing environment is restored. LISP funarg and procedures in Algol and Pascal work this way.LISP funarg and procedures in Algol and Pascal work this way.

9 9 An Example procedure C; begin end; procedure C; begin end; procedure A (p1 : procedure; i : integer); procedure A (p1 : procedure; i : integer); procedure B; procedure B; begin B begin B writeln(i); writeln(i); end; end; begin A begin A if i = 1 then A(B,2) if i = 1 then A(B,2) else p1; else p1; end A; end A; begin main begin main A(C,1); A(C,1); end main. end main. Under deep binding, the program prints a 1 Under shallow binding, it prints a 2 Note: there are 2 activations of A when B is finally called


Download ppt "1 Symbol Table and Subroutine Closures (Sections 3.3 & 3.4) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos."

Similar presentations


Ads by Google