GOALS OF DECLARATION CHECKING Check that: 1. Each declaration has a unique name in its potential scope don't have duplicate declarations 2. Each use of an identifier is it's been declared in a surrounding potential scope don't use undeclared names SYMBOL TABLE def: a *symbol table* is a mapping from identifiers to their attributes Data Structures: a stack of: hash tables arrays lists binary search trees ATTRIBUTES def: an *attribute* is information about a name (from the declaration) Examples of attributes: source code location kind of name (const, var, procedure) type size offset OPERATIONS FOR SYMBOL TABLES void initialize(); int size(); bool full(); bool declared(name); id_use *lookup(name); bool declared_in_current_scope(name); void insert(name, id_attrs); void enter_scope(); void leave_scope(); SYMBOL TABLES AND POTENTIAL SCOPES def: a declaration's *scope* is the area of a program's text where that declaration has effect def: a *potential scope* is an area of a program's text where declarations can be made, e.g., a block Grammar of a PL/0 program ::= . ::= { } ::= procedure ; ; Example: procedure p; var x; x := 2; begin x := 1; # is this use declared? call p end. Example: What's the final value of x? var x; procedure p; var x; x := 2; procedure q; x := 3; begin x := 1; call q; call p; end. STRATEGIES FOR NESTED SCOPES Needs: start a new potential scope so that declarations of names don't generate errors Approaches: - [Active Deletion] when leave potential scope, delete the mappings for the names declared in it - [Stack-Based] when leave potential scope, pop the mappings for it off the stack - [Functional] - [Decorations in AST] GOALS OF DECLARATION CHECKING Check that: 1. Each declaration has a unique name (in its potential scope) 2. Each use of an identifier is for a name that has already been declared (in a surrounding scope) PROCESS OF DECLARATION CHECKING In each potential scope: add each declaration's name and attributes in a mapping, while checking for duplicates look up name in current potential scope, if it's not there, add it if it is there, issue an error message In each statement, expression: check each use of a name make sure it's declared (if not, issue an error message) (and check privacy restrictions)