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

📄 symtable.cpp

📁 一个c语言的编译器的源代码
💻 CPP
字号:
// This file includes the functions used to
// build the symbol table
//
// Written by bood, boodweb@163.com, http://boodweb.126.com
// 2004-08-06

// Function 'st_lookup' now no longer do searches in the
// parameter list, coz parameters are now integrated into
// the symbol table
// by bood, 2005-01-17

#pragma warning(disable:4786)

#include <iostream>
#include <string>
#include <sstream>
#include "global.h"
#include "util.h"
#include "symtable.h"

using namespace std;

const int SHIFT = 4;
int nestlevel=0;

// Global symbol table
BucketList GlobalBucket={NULL,{NULL}};

// Pointer to symbol table
BucketList * pSymbolTable=&GlobalBucket;

// Symbols are inserted into this table
BucketList * pCurrentST=&GlobalBucket;

// This hash function is introduced by Louden in his book
// book:
// h=sum(alpha^(n-i)*Ci,i=1..n) mod size
int hash ( const string &key )
{
	int temp = 0;
	int i = 0;
	while (i<key.length())
	{
		temp = ((temp << SHIFT) + key[i]) % BUCKET_SIZE;
		++i;
	}
	return temp;
}

// Add a symbol table to the front of the table list
//
// Scope is implemented by a symbol table link-list,
// when entering a new scope, its symbol table is added
// in front of the list, so the same-name variables can
// be overlayed
void st_addon(BucketList *p)
{
	p->pNext=pSymbolTable;
	pSymbolTable=p;
	nestlevel++;
}

// Take off the lastest added symbol table
void st_takeoff()
{
	pSymbolTable=pSymbolTable->pNext;
	nestlevel--;
}

// Create a new symbol table and make it clear
BucketList * st_new()
{
	BucketList *p=new BucketList;
	p->pNext=NULL;
	for(int i=0;i<BUCKET_SIZE;i++) p->pRecord[i]=NULL;
	return p;
}

// Insert a new symbol to the symbol table pointed by
// 'pCurrentST'
SymbolRecord * st_insert(const string &name, const Type type,
               const SymbolType symboltype,
			   int location,int lineno)
{
	int h=hash(name);
	SymbolRecord * p=pCurrentST->pRecord[h];
	if(p==NULL){
		p=pCurrentST->pRecord[h]=new SymbolRecord;
	}
	else{
		bool bRedefine = false;
		while(p->pNext!=NULL) {
			if(p->name==name) {
				bRedefine = true;
				break;
			}
			p=p->pNext;
		}
		if(bRedefine || p->name==name) {
            SemanticError("symbol redefinition", lineno, p->lineno);
			bSemanticError = 1;
		}
		p->pNext=new SymbolRecord;
		p=p->pNext;
	}
	p->location=location;
	p->lineno=lineno;
	p->name=name;
	p->type=type;
	p->symboltype=symboltype;
	p->pParamList=NULL;
	p->nestlevel=nestlevel;
	p->pNext=NULL;

	//Will be set later in traverse
	p->paramAlloc=0;
	p->localAlloc=0;

    return p;
}

// Look up for a symbol in the symbol table list, the
// order that symboltable arranged make the scope overlay
// work in a right way
//
// Return NULL if not found
SymbolRecord * st_lookup(const string &name)
{
	BucketList *p= pSymbolTable;
	int h=hash(name);

    // Search in the current symbol table first
	while(p!=NULL){
		SymbolRecord * s=p->pRecord[h];
		while(s!=NULL){
			if(s->name==name)
				return s;
			s=s->pNext;
		}
		p=p->pNext;
	}

	return NULL;
}

⌨️ 快捷键说明

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