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

📄 tables.h

📁 编译原理实践教程PL0语言编译程序源代码。参考书:清华大学出版社的《编译原理》作者吕映芝、张素琴等。 实现主要功能有:对使用PL0语言编写的程序进行词法分析
💻 H
字号:
//常量项
typedef class DefConstantItem  
{
public:
	int con;
	DefConstantItem *next;
public:
	DefConstantItem(int a)
	{
		con=a;
		next=0;
	};
	virtual ~DefConstantItem(){};
}*DC;


//常量
class DefConstant  
{
public:
	DC head,tail;
public:
	void Empty()
	{
		DC temp;
		while(head!=NULL){
			temp=head;
			head=head->next;
			delete temp;
		}
		tail=head;
	};
	int OutputDefConstant(int i)
	{
		DC temp=head;
		for(int m=1;temp!=NULL && m<i;m++) temp=temp->next;
		if(temp==NULL) return 0;
		return temp->con;
	};
	CString OutputDefConstant()
	{
		int i=1;
		CString a,b,c,n;
		n+=(char)13;
		n+=(char)10;
		DC temp=head;
		a.Empty();
		while(temp!=NULL){
			b.Empty();
			c.Empty();
			b.Format("%d",i);
			c.Format("%d",temp->con);
			a+=(b+","+c+n);
			temp=temp->next;
			i++;
		}
		return a;
	};
	int Insert(int a)
	{
		DC temp=head;
		int i=1;
		if(head==NULL){
			head=new DefConstantItem(a);
			tail=head;
			return i;
		}
		while(temp!=NULL){
			if(temp->con==a) return i;
			temp=temp->next;
			i++;
		}
		temp=new DefConstantItem(a);
		if(tail==NULL) tail=temp;
		else{
			tail->next=temp;
			tail=temp;
		}return i;
	};
	DefConstant()
	{
		head=NULL;
		tail=NULL;
	};
	virtual ~DefConstant()
	{
		Empty();
	};

};


//变量项
typedef class DefSymbolItem  
{
public:
	CString str;
	DefSymbolItem *next;
public:
	DefSymbolItem(CString a)
	{
		str=a;
		next=NULL;
	};
	virtual ~DefSymbolItem(){};

} *DSI;
//变量
class DefSymbol  
{
public:
	DSI head,tail;
public:
	void Empty()
	{
		DSI temp;
		while(head!=NULL){
			temp=head;
			head=head->next;
			delete temp;
		}
		tail=head;
	};
	CString OutputDefSymbol()
	{
		int i=1;
		CString a,b,n;
		n+=(char)13;
		n+=(char)10;
		DSI temp=head;
		a.Empty();
		while(temp!=NULL){
			b.Empty();
			b.Format("%d",i);
			a+=(b+","+temp->str+n);
			temp=temp->next;
			i++;
		}
		return a;
	};
	CString OutputDefSymbol(int adr)
	{
		int i;
		DSI temp;
		for(i=1,temp=head;i<adr && temp!=NULL;i++) temp=temp->next;
		if(i==adr && temp!=NULL) return temp->str;
		else return "";
	};
	int Insert(CString a)
	{
		DSI temp=head;
		int i=1;
		if(head==NULL){
			head=new DefSymbolItem(a);
			tail=head;
			return i;
		}
		while(temp!=NULL){
			if((temp->str).Compare(a)==0) return i;
			temp=temp->next;
			i++;
		}
		temp=new DefSymbolItem(a);
		if(tail==NULL) tail=temp;
		else{
			tail->next=temp;
			tail=temp;
		}return i;
	};
	DefSymbol()
	{
		head=NULL;
		tail=NULL;
	};
	virtual ~DefSymbol()
	{
		Empty();
	};
};


//二元组项
typedef class TwoElementItem
{
public:
	int sym;
	CString Name;
	int val;
	int line;//在文本的第line行
	int column;//在文本中的column列
	TwoElementItem *next;
public:
	TwoElementItem(int a,CString name,int b,int l,int col )//添加列的信息
	{
		sym=a;
		Name=name;
		val=b;
		line=l;
        column = col;
		next=NULL;
	};
	virtual ~TwoElementItem(){};
}*TEI;//定义的一个二元组的指针
//二元组
class TwoElement  
{
public:
	TEI head,tail;//二元组链表的头尾
public:
	void Empty()//判断链表是否为空
	{
		TEI temp;
		while(head!=NULL){
			temp=head;
			head=head->next;
			delete temp;
		}
		tail=head;
	};
	//我已经在这里添加了一个参数,用来显示单词所在的列
	void Insert(int a,CString name,int b,int l,int col)//在链表中插入一个二元组项,加在表尾
	{
		if(tail==NULL)
		{
			tail=new TwoElementItem(a,name,b,l,col);
			head=tail;
		}
		else
		{
			tail->next=new TwoElementItem(a,name,b,l,col);
			tail=tail->next;
		}
	};
	//我已经在这里添加了一个参数,用来显示单词所在的列
	void Insert(int a,CString name,int l,int col)
	{
		Insert(a,name,0,l,col);
	};
	CString OutputTwoElement()
	{
		int i=1;
		CString a,b,c,d,e,f,n;		
		n+=(char)13;
		n+=(char)10;
		TEI temp=head;
		a.Empty();
		while(temp!=NULL){
			b.Empty();
			c.Empty();
			d.Empty();
			f.Empty();
			switch(temp->sym)
			{
			case 1:b="开始";break;
			case 2:b="结束";break;
			case 3:b="如果";break;
			case 4:b="那么";break;
			case 5:b="变量";break;
			case 6:b="常量";break;
			case 7:b="过程";break;
			case 8:b="调用";break;
			case 9:b="写出";break;
			case 10:b="读入";break;
			case 11:b="循环";break;
			case 12:b="执行";break;
			case 13:b="加上";break;
			case 14:b="减去";break;
			case 15:b="乘上";break;
			case 16:b="除以";break;
			case 17:b="赋值";break;
			case 18:b="小于";break;
			case 19:b="小于等于";break;
			case 20:b="等于";break;
			case 21:b="大于";break;
			case 22:b="大于等于";break;
			case 23:b="程序终止";break;
			case 24:b="左括号";break;
			case 25:b="右括号";break;
			case 26:b="分号";break;
			case 27:b="逗号";break;
			case 28:b="不等于";break;
			case 29:b="odd";break;
			case 888:b="标识符";break;
			case 999:b="常量";break;
			case 1111:b="数字";break;
			}

		//	b.Format("%d",temp->sym);
			c.Format("%d",temp->val);
			d.Format("%d",temp->line);
			e.Format("%d",temp->column);//我在这里添加了代码,用来输出单词所在的列
			f.Format("%d",i++);
			a+=(f+ ":         " + b+ " ----- "+temp->Name + " ----- " + c+ " ----- "+ d+"行"+ " ----- "+ e +"列" +"  " + n );
			
			temp=temp->next;
		}	
		return a;
	};
	TwoElement()
	{
		head=NULL;
		tail=NULL;
	};
	virtual ~TwoElement()
	{
		Empty();
	};
};

//添加的关于单词所在位置的信息
class  MyPoint
{
private:
	int Row;
	int Column;
public:
	MyPoint(int x=0,int y=0)
	{
		Row = x;
		Column = y;
	}
	int GetRow()
	{
		return Row;
	}
	bool SetRow(int x)
	{
		Row = x;
		return true;
	}
	int GetColumn()
	{
		return Column;
	}
	bool SetColumn(int y)
	{
		Column = y;
		return true;
	}

	
};
enum KIND{CON,VAR,PRO};



//语法分析符号表项
struct TABLE{
	CString name;
	KIND kind;
	int level;
	int value;
	int address;
	int size;

	//int row; 
	//int column;//我在这里添加了“点”
};

#define KeywordTotal 29

enum  Functions{lit=1,opr,lod,sto,cal,init,jmp,jpc};

struct Instruction
{
	Functions function;
	int levdiff;
	int Addr;
};

#define MaxCodeLine   2000

⌨️ 快捷键说明

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