|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.ObjectsymbolHandling.SymbolTable<T>
public class SymbolTable<T>
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.
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 |
---|
public SymbolTable()
Method Detail |
---|
public void enterScope()
public void exitScope() throws NoScopePresentException
NoScopePresentException
public void addId(AbstractSymbol id, T info) throws NoScopePresentException
id
- the symbolinfo
- the data asosciated with id
NoScopePresentException
public T lookup(AbstractSymbol sym) throws NoScopePresentException
null
.
sym
- the symbol
NoScopePresentException
public T probe(AbstractSymbol sym) throws NoScopePresentException
sym
. If found, return the information field. If not
return null
.
sym
- the symbol
NoScopePresentException
public java.lang.String toString()
toString
in class java.lang.Object
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |