symbol.cpp

来自「实现编译器的词法分析」· C++ 代码 · 共 64 行

CPP
64
字号
#include "Global.h"

#include "Symbol.h"


Entry KEY[KEYMAX]=
 {
  "const",CONST,0,
  "var",VAR,0,
  "procedure",PROCEDURE,0,
  "call",CALL,0,
  "begin",BEGIN,0,
   "end",END,0,
  "do",DO,0,
   "while",WHILE,0,
    "if", IF,0,
  "then",THEN,0,
  "odd",ODD,0
 };


Symbol::Symbol(void)
{
int i;
char * str;
this->lastentry=0;
this->Symtable=new  Entry[SYMMAX];
for(i=0;i<KEYMAX;i++)
this->Insert(KEY[i].lexptr,KEY[i].token);

}

int Symbol::Lookup(char * & s)
{
	int i;
	for(i=0;i<lastentry;i++)
		if(strcmp(this->Symtable[i].lexptr,s)==0)return i+1;
	return 0;
}

int Symbol::Insert(char *&s, int tok)
{   


	int len;
	len=strlen(s);
	if(lastentry==SYMMAX){std::cout<<"Symbol table if full!"<<std::endl;exit(0);}
    this->Symtable[lastentry].lexptr=s;
	this->Symtable[lastentry].token=tok;
	if(tok!=NUMBER&&tok!=ID)
	this->Symtable[lastentry].attribute=0;
	else
		this->Symtable[lastentry].attribute=lastentry;
	this->lastentry++;

	
    return lastentry;
}

Symbol::~Symbol(void)
{
	delete this->Symtable;
}

⌨️ 快捷键说明

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