symboltab.h

来自「C-MINUS编译器」· C头文件 代码 · 共 84 行

H
84
字号
#ifndef	MY_SYMBOLTABLE_H_
#define	MY_SYMBOLTABLE_H_

/**:	symboltable.h , header file
&
*  define symbol table.
* author: lonelyforest. data: 2006.4.4
*/


#include "parser.h"
// size of the hash table
const int TabSIZE = 211;

// SHIFT is use as multiplier in hash function
const int SHIFT = 4;	// 2*2*2*2, times 16, 4bit;

/*-----------------------------------------------------------------------*/
struct LineList {	// LineListRec
	int lineno;	// Remember the line NO. in sourcefile
	LineList* next;
	
	LineList():lineno(0), next(NULL) {}
	~LineList() { if (next) delete next; }
};

/*-----------------------------------------------------------------------*/
struct BucketList {	// BucketListRec
	BucketList* next;	// the next hash
	LineList*   lines;	// reference line
	
	string	name;
	string	scope;
	tokenType	type;
	int	memloc;		// memory location
	bool	isArr;
public:
	BucketList():memloc(0), lines(NULL), next(NULL) {}
	~BucketList()
	{
		if (lines)	delete lines;	// ...
		if (next)	delete next;
	}
};

/*-----------------------------------------------------------------------*/
/**:	class symTable, symbol table;
&
* Encapsulation all method of symbol table
*
* author: lonelyforest. data: 2006.4.5
*/

class symTable {
public:
	symTable();
	~symTable();

	void initial();

	// functino insert, insert symbol intot symbol table
    void insert(const string& name, const string &scope, const tokenType type,
				const int lineno, const long memloc, const bool isArr = false);

	// return the symbols' type(tokenType)
	tokenType	search_type(const string& name, const string& scope);

	bool	search_arr(const string& name, const string& scope);

	// if found, then return symbols' memory locations, else return -1;
	long	lookup(const string& name, const string &scope);

	// print symbol table to file: symFileName
	void printSymTable(const string& symFileName);
protected:
	int hash(const string& key);
	BucketList* hashTable[TabSIZE];
};

/*-----------------------------------------------------------------------*/


#endif	// end #ifndef

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?