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

📄 symset.h

📁 pl0文法编译器
💻 H
字号:
#ifndef __SYMSET_H
#define __SYMSET_H

#include <map>
#include <utility>
#include <string>
#include <vector>
using namespace std;
/************************************************************************/
/*						单词类型值的枚举                                */
/************************************************************************/
enum Symbol{endfile=-1,	nul=0,		ident,		procsy,		funcsy,		cha,		inum,		rnum,		
			plussy,		minussy,	timesy,		divisy,		lparsy,		rparsy,		equsy,		nequsy,		
			lesssy,		leseqsy,	grtsy,		grteqsy,	intsy,		charsy,		realsy,		varsy,		
			constsy,	assignsy,	ifsy,		thensy,		elsesy,		whilesy,	dosy,		beginsy,	
			endsy,		forsy,		tosy,		readsy,		writesy,	commasy,	colonsy,	semicolonsy,
			dot,		squot,		dquot,		chastring,	lastsy};

/************************************************************************/
/*						符号表单词类型枚举                                  */
/************************************************************************/
enum object{constent, variable, procedure, func,para};

/************************************************************************/
/*							指令码枚举									*/
/************************************************************************/
enum OPCode{
				LITCH=1,//取常量字符到运算栈栈顶						LITCH	0,a
				LITI,
				LITR,	//取常量实数到运算栈栈顶						LITR	0,a
				LODCH,
				LODI,	//取整型变量到运算栈栈顶						LODI	l,a (l为层次差, a为相对地址)
				LODR,	//取实型变量到运算栈栈顶						LODR	l,a (l为层次差, a为相对地址)
				STO,
				READI,	//读数据到活动记录中变量						READ	l,a (l为层次差, a为相对地址)
				READR,
				READS,
				WRTCH,	//显示运算栈站定值								WRT		0,0	(为零地址指令)
				WRTI,
				WRTR,
				WRTSTR,	//从stringstack指定位置读取字符串并显示在终端	WRTSTR	0,a (a为在stringstack中的指定位置)
				JMP,	//指令寄存器跳转到代码段指定位置				JMP		0,a (a为跳转位置)
				JPC,	
				RTN,	//函数返回指令
				REV,	//将运算栈栈顶操作数取反						REV		0,0
				ADD,	//将运算栈顶两个操作数相加						ADD		0,0
				MIN,	//将运算栈顶两操作数相减,次栈顶减栈顶			MIN		0,0
				TIM,	//将运算栈顶两操作数相乘						TIM		0,0
				DIV,	//将运算栈顶两操作数相除,栈顶去除次栈顶		DIV		0,0
				DIVI,
				ALOC,	//申请活动记录									ALOC	0,a (活动记录的长度)
				CALL,
				EQU,
				NEQU,
				GRT,
				GRTEQ,
				LES,
				LESEQ
			};

struct Instruction
{
	OPCode code;//指令码
	int	l;		//层次差
	float a;	//值
};

typedef vector<Instruction> CodeStack;//数据栈定义

/************************************************************************/
/*							活动记录定义								*/
/************************************************************************/


/************************************************************************/
/*							类型定义                                    */
/************************************************************************/
typedef map<string,Symbol> resvword_value;//保留字-类别码映射表类型
typedef resvword_value::value_type resvwordValueType;
typedef pair<Symbol,string> token_pair;//单词-类别码映射类型
typedef pair<int, int> Location;//单词的 行-列
typedef pair<int,Location> errorInsert;
typedef vector<errorInsert> errcode_loc;//错误号-位置
typedef multimap<int, Location>::value_type errcodelocValueType;

/************************************************************************/
/*							常量定义                                    */
/************************************************************************/
//const int MAX_NUMBER = 2047;//最大整数
//const int MAX_NUMBERLENGTH = 14;//最大数位
const int MAX_IEDNTLENGTH = 15;//最大标识符字符数
const int MAX_TABLELENGTH = 1024*5;//标识符表最大长度
const int MAX_LEV = 10;//最大允许的嵌套层数
const int MAX_CX = 200;//类PCode代码数组最大长度



/************************************************************************/
/*					SymSet类声明                                        */
/*说明:单词类型值的集合,主要用于test()操作提供合法字符集与停止字符集	*/
/*作者:马永焘															*/
/*时间:2006年2月17日											*/
/*版本:V1.0															*/
/************************************************************************/
class SymSet
{
public:
	SymSet(){for(int i = 0; i < lastsy; i++) set[i] = 0;}

public:
	int inSet(Symbol sym){return set[sym];}
	SymSet& operator=(const SymSet &oldSet);
	SymSet& operator+=(const SymSet &oldSet);
	SymSet& operator+=(const Symbol &oldSymbol);
	SymSet& operator-=(const Symbol &oldSymbol);
	SymSet& reSet(){for(int i = 0; i < lastsy; i++) set[i] = 0; return *this;}
private:
	int set[lastsy];

};

inline SymSet& SymSet::operator=(const SymSet &oldSet)
{
	for(int i = 0; i < lastsy; i++)
	{
		set[i] = oldSet.set[i];
	}
	return *this;
}

inline SymSet& SymSet::operator+=(const SymSet &oldset)
{
	for (int i = 0; i < lastsy; i++)
	{
		set[i] = (set[i] || oldset.set[i]);
	}
	return *this;
}

inline SymSet& SymSet::operator+=(const Symbol &oldSymbol)
{
	set[oldSymbol] = 1;
	return *this;
}

inline SymSet& SymSet::operator-=(const Symbol &oldSymbol)
{
	set[oldSymbol] = 0;
	return *this;
}


#endif//SymSey.h

⌨️ 快捷键说明

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