| 1 | /*
|
---|
| 2 | * SymbolTable.h
|
---|
| 3 | *
|
---|
| 4 | * This file contains the declaration of the "SymbolTable" structure.
|
---|
| 5 | * It is part of the assignment for the Compiler Construction course at the
|
---|
| 6 | * Leiden Institute of Advanced Computer Science (LIACS), Leiden University.
|
---|
| 7 | *
|
---|
| 8 | * Note: when a SymbolTable is deleted, all Scope objects contained in that
|
---|
| 9 | * SymbolTable are also deleted.
|
---|
| 10 | */
|
---|
| 11 |
|
---|
| 12 | #ifndef _SYMBOLTABLE_H_
|
---|
| 13 | #define _SYMBOLTABLE_H_
|
---|
| 14 |
|
---|
| 15 | #include <cstdio>
|
---|
| 16 | #include <string>
|
---|
| 17 | #include <vector>
|
---|
| 18 |
|
---|
| 19 | #include "Scope.h"
|
---|
| 20 | #include "Symbol.h"
|
---|
| 21 |
|
---|
| 22 | using namespace std;
|
---|
| 23 |
|
---|
| 24 | class SymbolTable {
|
---|
| 25 | public:
|
---|
| 26 | // Constructor
|
---|
| 27 | SymbolTable ();
|
---|
| 28 |
|
---|
| 29 | // Destructor: deletes all Scopes contained in this SymbolTable
|
---|
| 30 | ~SymbolTable ();
|
---|
| 31 |
|
---|
| 32 | // Creates a new Scope with the given name and sets it as current scope.
|
---|
| 33 | // Returns NULL if the name is already used.
|
---|
| 34 | Scope * AddScope (string scope);
|
---|
| 35 |
|
---|
| 36 | // Returns a pointer to the Scope object with the given name
|
---|
| 37 | Scope * GetScope (string scope);
|
---|
| 38 |
|
---|
| 39 | // Returns a pointer to the root scope
|
---|
| 40 | Scope * GetRootScope ();
|
---|
| 41 |
|
---|
| 42 | // Set the current scope
|
---|
| 43 | // Returns -1 on error
|
---|
| 44 | int SetCurrentScope (string scope);
|
---|
| 45 |
|
---|
| 46 | // Get the current scope name
|
---|
| 47 | string GetCurrentScopeName ();
|
---|
| 48 |
|
---|
| 49 | // Adds a symbol to the current scope
|
---|
| 50 | // Returns -1 on error
|
---|
| 51 | int AddSymbol (Symbol * symbol);
|
---|
| 52 |
|
---|
| 53 | // Adds a symbol to the given scope
|
---|
| 54 | // Returns -1 on error
|
---|
| 55 | int AddSymbol (string scope, Symbol * symbol);
|
---|
| 56 |
|
---|
| 57 | // Find a symbol in the current scope, or look in the parent scope, if it
|
---|
| 58 | // cannot be found in the current one.
|
---|
| 59 | // Returns NULL if the symbol cannot be found anywhere.
|
---|
| 60 | Symbol * GetSymbol (string name);
|
---|
| 61 |
|
---|
| 62 | // Find a symbol in the given scope; optionally look in the parent scope,
|
---|
| 63 | // if it cannot be found in the given one.
|
---|
| 64 | // Returns NULL if the symbol cannot be found.
|
---|
| 65 | Symbol * GetSymbol (string scope, string name, bool lookInParentScope);
|
---|
| 66 |
|
---|
| 67 | // Dump the contents of the symbol table
|
---|
| 68 | void Dump (FILE * file);
|
---|
| 69 |
|
---|
| 70 | private:
|
---|
| 71 | int myCurrentScope;
|
---|
| 72 | vector<Scope *> myScopes;
|
---|
| 73 | };//SymbolTable
|
---|
| 74 |
|
---|
| 75 | #endif
|
---|
| 76 |
|
---|