CSCI 431 Programming Languages Fall 2003 Symbol Table
CSCI 431 Programming Languages Fall 2003 Symbol Table and Subroutine Closures (Sections 3. 3 & 3. 4) A compilation of material developed by Felix Hernandez-Campos and Michael Scott 1
Symbol Table • Variable lookup in a dynamically-scoped language corresponds to symbol table lookup in a staticallyscoped language. • 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 table • The symbol table maps names to information about objects – It works like a hash in Perl name type … 2
Symbol Table Static Scope Example • Symbol table in Modula-2 3
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). 4
Symbol Table Scope Stack Example 5
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. • 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 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. 6
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. • Original LISP works this way by default. • Not used by languages that use static scoping rules 7
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. • 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 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. 8
An Example procedure C; begin end; procedure A (p 1 : procedure; i : integer); procedure B; begin B writeln(i); end; begin A Note: there are 2 activations of A if i = 1 then A(B, 2) when B is finally called else p 1; end A; begin main Under deep binding, the program prints a 1 A(C, 1); Under shallow binding, it prints a 2 end main. 9
- Slides: 9