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

📄 lr0.cpp

📁 在vs2005环境下用c++代码写的编译原理LR0算法
💻 CPP
字号:
// LR0.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<string>
#include<conio.h>
using namespace std;
//定义链式栈的节点类
class link
{
public:
	char  element;
	link *next;
	link(const char element1,link* nextval = NULL)
	{
		element=element1;
		next=nextval;
	}

};
//定义链式栈类
class stack
{
public:
	link* top;
	int size;
public:
	stack()
	{
		top=NULL;size=0;
	}
	~stack()
	{
		clear();
	}
	void clear()
	{
		while (top!=NULL)
		{
		link* itemp=top;
		top=top->next;
		size=0;
		delete itemp;
		}
	}
	bool push(char item)
	{
	top=new link (item,top);
	size++;
	return true;
	}
	char pop()
	{
		char it,a;
		if(size==0) return a;
		it=top->element;
		link * itemp=top->next;
		delete top;
		top=itemp;
		size--;
		return it;
	}
	bool topValue(char it)const
	{
		if(size==0)return false;
		it=top->element;
		return true;
	}
	int length()const
	{
		return size;
	}


};
bool ISZJ(char x)//判断是否是终结符
{  int i;
	string str1="ab+*$";
	i=str1.find(x,0);
	if(i>=0)
return true;
 else 
	 return false;
}
void error()//出错时报错
{
	cout<<"error"<<endl;

}


 int main()
{
	string action[11][6]={{" ","a ","b ","+ ","* ","$  "},
	                      {"0","s4","s5","  ","  ","   "},
	                      {"1","  ","s6","  ","  ","acc"},
	                      {"2","s4","s5","r2","  ","r2 "},
	                      {"3","r4","r4","r4","s8","r4 "},
	                      {"4","r6","r6","r6","r6","r6 "},
	                      {"5","r7","r7","r7","r7","r7 "},
	                      {"6","s4","s5","  ","  ","   "},
	                      {"7","r3","r3","r3","s8","r3 "},
	                      {"8","r5","r5","r5","r5","r5 "},
	                      {"9","  ","  ","r1","s8","r1"}};
	string a,b,c,d;
	stack S;
	int n=0;
	char x;//栈中的当前字符
	cout<<"文法的分析表的action表为:"<<endl;
	for(int i=0;i<11;i++)
	{
		for(int j=0;j<6;j++)
		cout<<action[i][j]<<"   ";
	cout<<endl;
	}
	char gto[11][4]={{' ','E','T','F'},
	{'0','1','2','3'},
	{'1',' ',' ',' '},
	{'2',' ',' ','7'},
	{'3',' ',' ',' '},
	{'4',' ',' ',' '},
	{'5',' ',' ',' '},
	{'6',' ',' ',' '},
	{'7',' ',' ','7'},
	{'8',' ',' ',' '},
	{'9',' ',' ',' '}};

	cout<<"文法的分析表的GOTO表为:"<<endl;
	for(int i=0;i<11;i++)
	{
		for(int j=0;j<4;j++)
		cout<<gto[i][j]<<"   ";
	cout<<endl;
	}
	string str[7]={"E->E+T","E->T","T->TF","T->F","F->F*","F->a","F->b"};
	cout<<"请输入要识别的串"<<endl;
	cin>>a;
	S.push('0');
	while(1)
	{
	getch();
	
	return 0;
}

⌨️ 快捷键说明

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