⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 symboltab.cpp

📁 C-MINUS编译器
💻 CPP
字号:

/**:	symbolTab.cpp 
&
*    implement symbol table.
* author: lonelyforest. data: 2006.4.6
*/

#include "symbolTab.h"

//-----------------------------------------------------------------------------
symTable::symTable()
{
	for (int i = 0; i < TabSIZE; ++i)
	{
		hashTable[i] = NULL;
	}
}

symTable::~symTable()
{
    initial();
}
//-----------------------------------------------------------------------------
void symTable::initial()
{
	for (int i = 0; i < TabSIZE; ++i)
	{
		if (hashTable[i])
		{
			delete hashTable[i];
		}

        hashTable[i] = NULL;
	}
}

/*--------------------------------------------------------------------------------------*/
/**:	hash(const string &key)
&
*    get hash number
* author: lonelyforest. data: 2006.4.6
*/
int symTable::hash(const string& key)
{
	int temp = 0;
	int i = 0;
	int	size = key.length();

	while (i < size)	{
		temp = ((temp <<SHIFT) + key[i]) % TabSIZE;
		++i;
	}

	return temp;
}

/**:	insert
&
*    important interface
* author: lonelyforest. data: 2006.4.6
*/
/*------------------------------------------------------------------------------------*/
void symTable::insert(const string &name, const string &scope, const tokenType type,
					  const int lineno, const long memloc, const bool isArr)
{
	int h = hash(name);
	BucketList *l = hashTable[h];

	while (l && ((l->name != name) || (l->scope != scope))) l = l->next;
	if (l == NULL)	// not found in table,
	{
		l = new BucketList();
		l->name = name;
		l->scope = scope;
		l->type = type;
		l->memloc = memloc;
		l->isArr = isArr;
		l->lines = new LineList();
		l->lines->lineno = lineno;

		l->next = hashTable[h];	
		hashTable[h] = l;		// dot worry,
	}
	else
	{	// if found in table, just add lineno,
		LineList *t = l->lines;
		
		while (t->next)	t = t->next;

		t->next = new LineList();
		t->next->lineno = lineno;
	}
}


/**:	insert
&
*    main important interface
* author: lonelyforest. data: 2006.4.6
*/
//-----------------------------------------------------------------------------
tokenType symTable::search_type(const string &name, const string &scope)
{
	int h = hash(name);
	BucketList *l = hashTable[h];

	while ( l && (l->name == name) && (l->scope == name)) l = l->next;

	return (l==NULL) ? k_ERROR: l->type;
}


/**:	lookup
&
*    return the varible memory location
* author: lonelyforest. data: 2006.4.6
*/
//-----------------------------------------------------------------------------
long symTable::lookup(const string &name, const string &scope)
{
	int h = hash(name);
	BucketList *l = hashTable[h];

	while (l &&  ! (l->name == name && l->scope == scope)) l = l->next;

	return (l == NULL) ? -1 : l->memloc ;
}

/**:	search_arr
&
*    return the varible  is or not is a array ?
* author: lonelyforest. data: 2006.4.6
*/
//-----------------------------------------------------------------------------
bool symTable::search_arr(const string& name, const string& scope)
{
	int h = hash(name);
	BucketList *l = hashTable[h];

	while (l && (l->name != name || l->scope != scope))	l=l->next;

	return (l == NULL) ? false : l->isArr;
}

/**:	printSymTable
&
*    print symbol table to symbol file
* author: lonelyforest. data: 2006.4.6
*/
//-----------------------------------------------------------------------------
void symTable::printSymTable(const string &symFileName)
{
	ofstream out(symFileName.c_str());
	if (!out)
	{
		sprintf(msg_temp, "open symbol table file: \"%s\" fail, print symbol table stopping now ...... ", symFileName.c_str());
		outputMsg(-1, msg_temp);
		// return ;
	}
	else
	{
        out << "                          Simple C minus Compier [symbol table]" << endl;
        out << "---------------------------------------------------------------------------------------\n"<<endl;
		out << "    Scope        Variable Name     Type     Location    Line Numbers" << endl;
		out << "-------------    -------------    ------    --------    ------------" <<endl;

		for (int i=0; i < TabSIZE; ++i)
		{
			if (hashTable[i])
			{
				BucketList *l = hashTable[i];
				while (l)
				{
					sprintf(msg_temp, "%10s", l->scope.c_str());
					out << msg_temp;
					sprintf(msg_temp, "%16s", l->name.c_str());
					out << msg_temp;
					// "int", "else", "return", "void", "if","while",	"read", "write"
					switch (l->type) {
						case k_INT:
							sprintf(msg_temp, "%12s", "int");
							out << msg_temp; 
							break;
						case k_VOID:
							sprintf(msg_temp, "%12s", "void");
							out << msg_temp;
							break;
						default:
							out << "unknow type";
							break;
					}

					sprintf(msg_temp, "%11d\t", l->memloc);
					out << msg_temp;
				
					LineList *t = l->lines;
					if (t){
                        out << " " << t->lineno;
					   while (t)
					   {
						  out << ", " << t->lineno;
						  t = t->next;
					   }
                    }
					out << endl;	// new line;
					l = l->next;
				}	// end while (l)
			}	// end if (hashTable[i]
		}	// end for

		sprintf(msg_temp, "symbol table successfully save to \"%s\"...", symFileName.c_str());
		outputMsg(-4, msg_temp);
	}	// end if (!out) ... else ...

	out.close();
}

⌨️ 快捷键说明

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