Symbol Table and Subroutine Closures (Sections 3.3 - PowerPoint PPT Presentation

About This Presentation
Title:

Symbol Table and Subroutine Closures (Sections 3.3

Description:

Variable lookup in a dynamically-scoped language corresponds to symbol table ... Under deep binding, the program prints a 1. Under shallow binding, it prints a 2 ... – PowerPoint PPT presentation

Number of Views:56
Avg rating:3.0/5.0
Slides: 10
Provided by: csU50
Learn more at: https://www.cs.unca.edu
Category:

less

Transcript and Presenter's Notes

Title: Symbol Table and Subroutine Closures (Sections 3.3


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
Symbol Table
  • 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.
  • 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

3
Symbol TableStatic Scope Example
  • Symbol table in Modula-2

4
Symbol TableStatic 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).

5
Symbol TableScope Stack Example
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.
  • 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.

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.
  • Original LISP works this way by default.
  • Not used by languages that use static scoping
    rules

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

9
An Example
  • procedure C begin end
  • procedure A (p1 procedure i integer)
  • procedure B
  • begin B
  • writeln(i)
  • end
  • begin A
  • if i 1 then A(B,2)
  • else p1
  • end A
  • begin main
  • A(C,1)
  • end main.

Note there are 2 activations of A when B is
finally called
Under deep binding, the program prints a 1 Under
shallow binding, it prints a 2
Write a Comment
User Comments (0)
About PowerShow.com