Class SymbolTable

java.lang.Object
  extended bySymbolTable
Direct Known Subclasses:
CgenClassTable

class SymbolTable
extends java.lang.Object

Implements the symbol table data abstraction.

In addition to strings, compilers must also determine and manage the scope of program names. A symbol table is a data structure for managing scope. Conceptually, a symbol table is just another lookup table. The key is the symbol (the name) and the result is whatever information has been associated with that symbol (e.g., the symbol's type).

In addition to adding and removing symbols, symbol tables also support operations for entering and exiting scopes and for checking whether an identifier is already defined in the current scope. The lookup operation must also observe the scoping rules of the language; if there are multiple definitions of identifier x, the scoping rules determine which definition a lookup of x returns. In most languages, including Cool, inner definitions hide outer definitions. Thus, a lookup on x returns the definition of x from the innermost scope with a definition of x.

Cool symbol tables are implemented using Java hashtables. Each hashtable represents a scope and associates a symbol with some data. The ``data'' is whatever data the programmer wishes to associate with each identifier. An example illustrating the use of symbol tables is in the file SymtabExample.java.

See Also:
AbstractSymbol, SymtabExample

Field Summary
private  java.util.Stack tbl
           
 
Constructor Summary
SymbolTable()
          Creates an empty symbol table.
 
Method Summary
 void addId(AbstractSymbol id, java.lang.Object info)
          Adds a new entry to the symbol table.
 void enterScope()
          Enters a new scope.
 void exitScope()
          Exits the most recently entered scope.
 java.lang.Object lookup(AbstractSymbol sym)
          Looks up an item through all scopes of the symbol table.
 java.lang.Object probe(AbstractSymbol sym)
          Probes the symbol table.
 java.lang.String toString()
          Gets the string representation of the symbol table.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

tbl

private java.util.Stack tbl
Constructor Detail

SymbolTable

public SymbolTable()
Creates an empty symbol table.

Method Detail

enterScope

public void enterScope()
Enters a new scope. A scope must be entered before anything can be added to the table.


exitScope

public void exitScope()
Exits the most recently entered scope.


addId

public void addId(AbstractSymbol id,
                  java.lang.Object info)
Adds a new entry to the symbol table.

Parameters:
id - the symbol
info - the data asosciated with id

lookup

public java.lang.Object lookup(AbstractSymbol sym)
Looks up an item through all scopes of the symbol table. If found it returns the associated information field, if not it returns null.

Parameters:
sym - the symbol
Returns:
the info associated with sym, or null if not found

probe

public java.lang.Object probe(AbstractSymbol sym)
Probes the symbol table. Check the top scope (only) for the symbol sym. If found, return the information field. If not return null.

Parameters:
sym - the symbol
Returns:
the info associated with sym, or null if not found

toString

public java.lang.String toString()
Gets the string representation of the symbol table.

Returns:
the string rep