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