Main Page | Class Hierarchy | Compound List | File List | Compound Members | File Members | Related Pages

SymbolTable< T > Class Template Reference

Symbol table class. More...

#include <symboltable.hpp>

List of all members.

Public Member Functions

 SymbolTable ()
 Constructor.

virtual ~SymbolTable ()
 Destructor.

void openScope ()
 Open a new scope.

void closeScope ()
 Close current scope.

unsigned int currentLevel () const
 Get the current scope level.

T * retrieve (const std::string &id)
 Check if an identifier is declared.

void enter (const std::string &id, T *newEntry)
 Insert identifier into current scope.


Private Attributes

std::vector< std::map< std::string,
T * > * > 
Scopes
unsigned int CurrentLevel


Detailed Description

template<class T>
class SymbolTable< T >

Symbol table class.

Simple template class for the symbol tabels that we use. It provides scoping functionality and stores an identifier an a pointer to an object for each symbol tabel entry. The symbol table opens a global scope, scope 0 on creation. Usage example:

 SymbolTable<IdEntry> symbol_table; // create a new symbol table with IdEntries
 symbol_table.enter( "bob", new IdEntry( whatever params IdEntry has ) );
 IdEntry *entry = symbol_table.retrieve( "bob" ); // fetch the item we just inserted

SymbolTable will delete all entries added with enter when either the added entry goes out of scope or when the symbol table is deleted. The symbol table starts out in scope level 0 which represents the global scope.

Todo:
Current implementation uses map which is not hashed and might be slow. hash_map is an option but it is not widely supported.

Definition at line 116 of file symboltable.hpp.


Constructor & Destructor Documentation

template<class T>
SymbolTable< T >::SymbolTable< T >  ) 
 

Constructor.

Default constructor

template<class T>
SymbolTable< T >::~SymbolTable< T >  )  [virtual]
 

Destructor.

Destroys the object and all the objects in de symbol table.


Member Function Documentation

template<class T>
void SymbolTable< T >::closeScope  ) 
 

Close current scope.

All identifiers in the current scope level will be removed from the symbol table.

Exceptions:
SymbolTableException when trying to close scope 0.

Definition at line 60 of file symboltable.cpp.

template<class T>
void SymbolTable< T >::enter const std::string &  id,
T *  newEntry
 

Insert identifier into current scope.

Parameters:
id Identifier to insert
newEntry The entry to add into the symbol table. It will be deleted when it goes out of scope or when the symbol table is destroyed.
Exceptions:
SymbolTableException when
  • the id is already declared in this scope.

Definition at line 102 of file symboltable.cpp.

template<class T>
T * SymbolTable< T >::retrieve const std::string &  id  ) 
 

Check if an identifier is declared.

Returns:
The entry on the highest scope level if found. 0 if the identifier is not declared.

Definition at line 83 of file symboltable.cpp.


The documentation for this class was generated from the following files:
Generated on Mon Dec 1 14:26:28 2003 for Ck by doxygen 1.3.3