Scope and Symbol Table
Scope and Symbol Table
Compiler Design
Torben Ægidius
Mogensen
Scopes and Symbol
Tables
Course: Introduction to Compiler Design & Construction (Level: UG) Copyright© 2011
Professor Mogensen, University of Copenhagen, Denmark; Dexter Kozen, Cornell University,
Ithaca, USA;
Scopes and Symbol Tables
The declaration of a name has a limited scope: a
portion of the program where the name will be
visible. Such declarations are called local
declarations, whereas a declaration that makes
the declared name visible in the entire program is
called global
Course: Introduction to Compiler Design & Construction (Level: UG) Copyright© 2011
Professor Mogensen, University of Copenhagen, Denmark; Dexter Kozen, Cornell University,
Ithaca, USA;
Binding
Binding Concepts
Objects in Programming
Course: Introduction to Compiler Design & Construction (Level: UG) Copyright© 2011
Professor Mogensen, University of Copenhagen, Denmark; Dexter Kozen, Cornell University,
Ithaca, USA;
Symbol Tables
A symbol table is a table that binds names to information. We need a number
of operations on symbol tables to accomplish this:
We need an empty symbol table, in which no name is defined.
We need to be able to bind a name to a piece of information. In case the name
is already defined in the symbol table, the new binding takes precedence over
the old.
We need to be able to look up a name in a symbol table to find the information
the name is bound to. If the name is not defined in the symbol table, we need
to be told that.
We need to be able to enter a new scope.
We need to be able to exit a scope, reestablishing the symbol table to what it
was before the scope was entered.
Course: Introduction to Compiler Design & Construction (Level: UG) Copyright© 2011
Professor Mogensen, University of Copenhagen, Denmark; Dexter Kozen, Cornell University,
Ithaca, USA;
Implementation of Symbol Tables
Course: Introduction to Compiler Design & Construction (Level: UG) Copyright© 2011
Professor Mogensen, University of Copenhagen, Denmark; Dexter Kozen, Cornell University,
Ithaca, USA;
Persistent Mechanism
Course: Introduction to Compiler Design & Construction (Level: UG) Copyright© 2011
Professor Mogensen, University of Copenhagen, Denmark; Dexter Kozen, Cornell University,
Ithaca, USA;
Simple Persistent Symbol Tables
LIST BASED
Course: Introduction to Compiler Design & Construction (Level: UG) Copyright© 2011
Professor Mogensen, University of Copenhagen, Denmark; Dexter Kozen, Cornell University,
Ithaca, USA;
Simple Persistent Symbol Tables (continued…)
Course: Introduction to Compiler Design & Construction (Level: UG) Copyright© 2011
Professor Mogensen, University of Copenhagen, Denmark; Dexter Kozen, Cornell University,
Ithaca, USA;
Simple Persistent Symbol Tables (continued…)
Course: Introduction to Compiler Design & Construction (Level: UG) Copyright© 2011
Professor Mogensen, University of Copenhagen, Denmark; Dexter Kozen, Cornell University,
Ithaca, USA;
Simple Persistent Symbol Tables (continued…)
Course: Introduction to Compiler Design & Construction (Level: UG) Copyright© 2011
Professor Mogensen, University of Copenhagen, Denmark; Dexter Kozen, Cornell University,
Ithaca, USA;
Simple Imperative Symbol Table
STACK
BASED
Course: Introduction to Compiler Design & Construction (Level: UG) Copyright© 2011
Professor Mogensen, University of Copenhagen, Denmark; Dexter Kozen, Cornell University,
Ithaca, USA;
Simple Imperative Symbol Table (continued…)
Course: Introduction to Compiler Design & Construction (Level: UG) Copyright© 2011
Professor Mogensen, University of Copenhagen, Denmark; Dexter Kozen, Cornell University,
Ithaca, USA;
Simple Imperative Symbol Table (continued…)
Course: Introduction to Compiler Design & Construction (Level: UG) Copyright© 2011
Professor Mogensen, University of Copenhagen, Denmark; Dexter Kozen, Cornell University,
Ithaca, USA;
Efficiency Issues
* [1] Coding the theoretical designed model is out of the scope of this course. Students may at PG level will find it
useful. Interested students may consult TAs for details and PG level courses.
Course: Introduction to Compiler Design & Construction (Level: UG) Copyright© 2011
Professor Mogensen, University of Copenhagen, Denmark; Dexter Kozen, Cornell University,
Ithaca, USA;
Shared or Separate Name Spaces
Course: Introduction to Compiler Design & Construction (Level: UG) Copyright© 2011
Professor Mogensen, University of Copenhagen, Denmark; Dexter Kozen, Cornell University,
Ithaca, USA;
Shared or Separate Name Spaces (continued…)
Course: Introduction to Compiler Design & Construction (Level: UG) Copyright© 2011
Professor Mogensen, University of Copenhagen, Denmark; Dexter Kozen, Cornell University,
Ithaca, USA;