symbolHandling
Class SymbolTable<T>

java.lang.Object
  extended by symbolHandling.SymbolTable<T>
Direct Known Subclasses:
CgenClassTable

public class SymbolTable<T>
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. When instanciating SymbolTable, a type T has to specified to restrict what objects can go into the symbol table. This provides type-safety.

See Also:
AbstractSymbol, SymtabExample

Constructor Summary
SymbolTable()
          Creates an empty symbol table.
 
Method Summary
 void addId(AbstractSymbol id, T info)
          Adds a new entry to the symbol table.
 void enterScope()
          Enters a new scope.
 void exitScope()
          Exits the most recently entered scope.
 T lookup(AbstractSymbol sym)
          Looks up an item through all scopes of the symbol table.
 T 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
 

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()
               throws NoScopePresentException
Exits the most recently entered scope.

Throws:
NoScopePresentException

addId

public void addId(AbstractSymbol id,
                  T info)
           throws NoScopePresentException
Adds a new entry to the symbol table.

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

lookup

public T lookup(AbstractSymbol sym)
         throws NoScopePresentException
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
Throws:
NoScopePresentException

probe

public T probe(AbstractSymbol sym)
        throws NoScopePresentException
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
Throws:
NoScopePresentException

toString

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

Overrides:
toString in class java.lang.Object
Returns:
the string rep