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

📄 jun.cpp

📁 写的是一个语法分析器 是用LR分析法分析一个布尔表达式
💻 CPP
字号:
#include <iostream>
using namespace std;
#include <vector>
inline bool __isdigital(char value)
{
	return value<='9'&&value>='0';
}
inline bool __isalpha(char value)
{
	return (value>='a'&&value<='z')||(value>='A'&&value<='Z');
}
struct oitem
{
	char VN;//非终结符标志
	char value[30];//综合属性
	oitem(char lhs,char *rhs)
	{
		memset(value,0,30);
		VN=lhs;
		if(rhs)
			strcpy(value,rhs);
	}
};
bool __islegalopr(char opr)
{
	char legal[]="<=>()&|!";
	for(int i=0;i<strlen(legal);i++)
	{
		if(opr==legal[i])
			return true;
	}
	return false;
}
oitem	*git;
void factor_induce();
void bool_induce();
vector<oitem> midtable;
void express_induce()
{
	if(git==midtable.end())
		return;
	bool not=false;
	if(git->VN=='!')
	{	not=true;
		git++;
	}
	bool_induce();
	if(not) cout<<'!';
	while(git!=midtable.end()&&git->VN=='&'||git->VN=='|')
	{
		char opr=git->VN;
		git++;
		bool_induce();
		cout<<opr;
	}
}
void bool_induce()
{
	char temp1,temp2=' ';
	factor_induce();
	while(git!=midtable.end()&&(git->VN=='<'||git->VN=='>'||git->VN=='='))
	{
		temp1=git->VN;
		git++;
		if(git!=midtable.end()&&git->VN=='=')
		{
			temp2=git->VN;
			git++;
		}
		factor_induce();
		cout<<temp1;
		if(temp2!=' ')
			cout<<temp2;
	}
}
void factor_induce()
{
	if(git->VN=='(')
	{
		git++;
		express_induce();
		if(git->VN!=')')
		{
			cout<<"缺少)"<<endl;
			int m;
			cin>>m;
			exit(0);
		}
		git++;
		return;
	}
	else
		cout<<git->value<<' ';
	git++;
}
void print_otable()
{
	oitem *it=midtable.begin();
	for(;it!=midtable.end();it++)
	{
		cout<<it->VN<<' '<<it->value;
	}
}
char input[1024];
int main()
{
	memset(input,0,1024);
	gets(input);
	char temp;
	int firstindex=0;
	int nextindex=0;
	char tmp[30];
	for(;;nextindex++)
	{
		temp=input[nextindex];
		if(__isdigital(temp)==false&&__isalpha(temp)==false)
		{
			if(firstindex==nextindex)
			{
				if(temp!=' '&&temp!='#')
				{
					if(__islegalopr(temp))
						midtable.push_back(oitem(temp,""));
					else 
					{
						cout<<"error express"<<endl;
						exit(0);
					}
				}
				if(temp=='#')
				{
					break;
				}
				firstindex++;
				continue;
			}
			memset(tmp,0,30);
			memcpy(tmp,input+firstindex,nextindex-firstindex);
			midtable.push_back(oitem('i',tmp));
			if(temp=='#')
				break;
			else if(__islegalopr(temp))
				midtable.push_back(oitem(temp,""));
			else if(temp!=' ')
			{
				cout<<"error"<<endl;
				exit(0);
			}
			firstindex=nextindex+1;
			
		}
		else if(__isalpha(temp))
		{
			if(firstindex!=nextindex&&!__isalpha(input[firstindex]))
			{
				cout<<"error express"<<endl;
				return 1;
			}
		}
	}
//	print_otable();
	git=midtable.begin();
	express_induce();
	cin>>temp;
	return ;
	
}

⌨️ 快捷键说明

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