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

📄 gxn.cpp

📁 编译一个简单的记法分析报告 挺好的哦
💻 CPP
字号:
#include<ctype.h>
#include<stdlib.h>
#include<iomanip>
#include<string>
#include<iostream.h>
struct danci
  {
	  int id;			//单词种别
	  char name[10];	//单词属性值
  };
int fout[20];
int index=1;
int G_table[9][6];
int k[20];
int n=0;
danci word[20];
int boo(int);
int compare(int);
int not(int);
int forid(int);
typedef struct node{
	int data; 
   	node* next;
 }* link;
link insert(link stack,int n)//向前插入链表
{
	link newnode;
	newnode=new node;
	if(!newnode)
	{
		cout<<"error!"<<endl;
		exit(0);
	}
	newnode->data=n;
	newnode->next=stack;
	stack=newnode;
	return stack;
}
link del(link stack,int &value)//删除链表元素
{
	link top;
	if(stack)
	{
		top=stack;
		stack=stack->next;
		value=top->data;
		delete top;
		return stack;
	}
	value=-1;
	return NULL;
}
void error()
{
	cout<<"词法错误,标志符不能以数字开头"<<endl;
	exit(0);
}
void error1()
{
	cout<<"语法错误,输入正确句子"<<endl;
	exit(0);
}
int not(int i)
{
	int ii;
	i=forid(i);
	while(word[i].id==3)
	{
		ii=i;
		i++;
		i=forid(i);
		fout[index]=ii;
		index++;
	}
	return i;
}
void init()//初始化文法矩阵
{
	for(int i=0;i<9;i++)               	
	for(int j=0;j<6;j++)
		G_table[i][j]=-1;
	G_table[0][0]=0;G_table[0][1]=1; //初始化文法表
	G_table[1][0]=1;G_table[1][1]=1;G_table[1][2]=5;G_table[1][3]=2;
	G_table[2][0]=1;G_table[2][1]=2;
	G_table[3][0]=1;G_table[3][1]=1;G_table[3][2]=3;
	G_table[4][0]=2;G_table[4][1]=3;
	G_table[5][0]=3;G_table[5][1]=6;G_table[5][2]=3;
	G_table[6][0]=3;G_table[6][1]=9;G_table[6][2]=1;G_table[6][3]=10;
	G_table[7][0]=3;G_table[7][1]=7;
	G_table[8][0]=3;G_table[8][1]=9;G_table[8][2]=7;G_table[8][3]=8;
	G_table[8][4]=7;G_table[8][5]=10;
}
void print()//打印文法
{   
char a0[]="E";char a1[]="S";
char a2[]="B";char a3[]="C";
char a4[]="and";char a5[]="or";
char a6[]="not";char a7[]="id";
char  a9[]="(";
char a10[]=")";
cout<<"本实验的文法为:"<<endl;
cout<<a0<<"->"<<a1<<endl;
cout<<a1<<"->"<<a1<<' '<<a5<<' '<<a2<<endl;
cout<<a1<<"->"<<a2<<endl;
cout<<a2<<"->"<<a2<<' '<<a4<<' '<<a3<<endl;
cout<<a2<<"->"<<a3<<endl;
cout<<a3<<"->"<<a6<<' '<<a3<<endl;
cout<<a3<<"->"<<a9<<a1<<a10<<endl;
cout<<a3<<"->"<<a7<<endl;
}
int  lex(char * buf)//词法分析
{
	int i=0,j=0;
	int n=0;
	char Temp_name[10];
	while(buf[i]!='\0')
	{
		if(buf[i]==' '||buf[i]=='\n')
			i++;
		else if(isalpha(buf[i])||buf[i]=='_')//标志符判断
		{
			Temp_name[j]=buf[i];
			i++;
			while(isalnum(buf[i]))
			{
				j++;
				Temp_name[j]=buf[i];
				i++;
			}
		   // if
			memcpy(word[n].name,Temp_name,j+1);
			word[n].name[j+1]='\0';
			if(strcmp(word[n].name,"and")==0)
				word[n].id=1;
			else if(strcmp(word[n].name,"or")==0)
				word[n].id=2;
			else if(strcmp(word[n].name,"not")==0)
				word[n].id=3;
			else 
				word[n].id=0;
			n++;
			j=0;
		}
		else if(isdigit(buf[i]))//数字判断
		{
			while(isdigit(buf[i]))
			{
			Temp_name[j]=buf[i];
			i++;
			j++;
			}
				if(isalpha(buf[i]))
                  error();
			memcpy(word[n].name,Temp_name,j+1);
			word[n].name[j]='\0';
			n++;
			j=0;
		}
		else//运算符号判断
		{
			switch(buf[i])
			{
				case '>':if(buf[i+1]=='=')
						 {
							  i++;
						   	  word[n].id=8;
							  strcpy(word[n].name,">=");
						 }
						 else
						 {
							 word[n].id=4;
							 strcpy(word[n].name,">");
						 }
						 n++;break;
				case '<':if(buf[i+1]=='=')
						 {
							  i++;
						   	  word[n].id=9;
							  strcpy(word[n].name,"<=");
						 }
						 else 
						 {
							 word[n].id=5;
							 strcpy(word[n].name,"<");
						 }
						 n++;break;
				case '=': word[n].id=6;strcpy(word[n].name,"=");n++;break;
				case '!': if(buf[i+1]=='=')
						  {
							  i++;
						   	  word[n].id=7;strcpy(word[n].name,"!=");
							  n++;
						  }
						   break;
				case '(': word[n].id=10;strcpy(word[n].name,"(");n++;break;
				case ')': word[n].id=11;strcpy(word[n].name,")");n++;break;
			}
			i++;
		}
	}
	strcpy(word[n].name,"#");
		word[n].id=999;
	return n;//单词符号个数
}
 
int boo (int i)
{
	int ii;
	i=compare(i);
	while (word[i].id==1||word[i].id==2)
	{
		ii=i;
		i++;				
		i=compare(i);	
		fout[index]=ii;//
		index++;
	}
	return i;
}

int forid(int i)
{
	if (word[i].id==0)		
	{
		fout[index]=i;
		index++;
		i++;
	}
    else
	{
		if(word[i].id==10)
		{
			i++;				
			i=boo(i);
			if (word[i].id==11)
				i++;			
			else
				word[i].id=99;
		}
		else if(word[i].id==3)
		{;}
		else
		{
            error1();
			word[i].id=99;
		}
	}
	return i;
}
void boland()
{
	for(int i=1;i<index;i++)
	{
		if(word[fout[i]].id==0)
			cout<<word[fout[i]].name<<" ";
		else
		{
			switch (word[fout[i]].id)
			{
			case 1: cout<<"and ";break;
			case 2: cout<<"or ";break;
			case 3: cout<<"not ";break;
			case 4: cout<<"> ";break;
			case 5: cout<<"< ";break;
			case 6: cout<<"= ";break;
			case 7: cout<<"!= ";break;
			case 8: cout<<">= ";break;
			case 9: cout<<"<= ";break;
			}
		}
	}
	index=0;
 }
int compare(int i)
{
	int ii;
	i=not(i);
	while(word[i].id==4||word[i].id==5||word[i].id==6
		||word[i].id==7||word[i].id==8||word[i].id==9)
	{
		ii=i;
		i++;				
		i=not(i);
		fout[index]=ii;
		index++;
	}
	return i;
}
void main()
{
	char buffer[30];
	int i=0;
	int length;
	char *inword;
	init();
	print();
	cout<<"输入句型,以#结束 :"<<endl;
	cin>>buffer[i];
	while(buffer[i]!='#')
	{
		i++;
		cin.get(buffer[i]);
	}
	buffer[i]='\0';
	length=i+1;
	inword=new char[length];
	memcpy(inword,buffer,length);
	length=lex(inword);
	cout<<"词法分析结果如下:"<<endl;
	cout<<'('<<"属性"<<' '<<"值"<<')';
	for( i=0;i<length;i++)
	{	cout<<'('<<word[i].id<<' '<<word[i].name<<')'; cout<<' ';}
	cout<<endl;


	boland();
	cout<<endl;
}
  
  
 

⌨️ 快捷键说明

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