CS 536 Fall 2007, Project 5

Class SymbolTable

Object
  extended by SymbolTable

public class SymbolTable
extends Object

A block-structured symbol table. Logically, a SymbolTable is a stack of blocks. Each block contains a mapping from Strings to Symbol objects.

See Also:
Symbol

Constructor Summary
SymbolTable()
          Constructs an empty SymbolTable.
 
Method Summary
 void define(Symbol sym)
          Adds symbol "sym" to the current block of the symbol table.
 Symbol globalLookup(String name)
          Returns the Symbol with the given name in the block that is highest (nearest the top) of the stack that has such a Symbol.
 Symbol localLookup(String name)
          Returns the Symbol with the given name in the top block of the table.
 void pop()
          Pops the top block off the stack, destroying it.
 void print(PrintStream out)
          Prints the contents of this symbol table.
 void push()
          Pushes a new empty block onto the stack of blocks.
 String toString()
          Returns a string representation of this SymbolTable, for debugging.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SymbolTable

public SymbolTable()
Constructs an empty SymbolTable.

Method Detail

push

public void push()
Pushes a new empty block onto the stack of blocks.


pop

public void pop()
         throws EmptySymbolTableException
Pops the top block off the stack, destroying it.

Throws:
EmptySymbolTableException - if the stack is empty.

define

public void define(Symbol sym)
            throws DuplicateSymbolException,
                   EmptySymbolTableException
Adds symbol "sym" to the current block of the symbol table.

Parameters:
sym - the Symbol to add.
Throws:
DuplicateSymbolException - if there is already a Symbol with the same name defined in the current block.
EmptySymbolTableException - if there is no current block.

localLookup

public Symbol localLookup(String name)
Returns the Symbol with the given name in the top block of the table. Returns null if the table is empty or the top block does not contain a symbol with the given name.

Parameters:
name - the name to look up.
Returns:
the corresponding Symbol from the local block, or null.

globalLookup

public Symbol globalLookup(String name)
Returns the Symbol with the given name in the block that is highest (nearest the top) of the stack that has such a Symbol. Returns null if no block on the stack contains a matching Symbol.

Parameters:
name - the name to look up.
Returns:
the corresponding Symbol from most recent block, or null.

print

public void print(PrintStream out)
Prints the contents of this symbol table.

Parameters:
out - the destination of the listing.

toString

public String toString()
Returns a string representation of this SymbolTable, for debugging. The string is "SymbolTable(n)", where n is the number of blocks currently in the table.

Overrides:
toString in class Object
Returns:
a string representation of this SymbolTable

CS 536 Fall 2007, Project 5