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

📄 语法分析.cpp

📁 他是语义分析的前端
💻 CPP
字号:
#include<iomanip>
#include<stdlib.h>
#include<ctype.h>
#include<iostream>
#include<fstream>
#include<string>
using namespace std;


/**************************************************/
              /*语法分析中的数据结构*/
/*************************语法分析中的Acction表**************************/

#define max 60

int acction[max][max];

/*************************语法分析中的Goto表*****************************/
int acc_goto[max][max];




/*************************语法分析中的栈结构的定义************************/
int stack[max];
int head=0;

                         /*head用于栈顶的控制
                           stack[0]初始化状态0*/





/*************************定义token表的结构******************************/
#define max1 100
struct ss{
	int id;
	int point;
}token[max1];
/************************************************************************/


/************************定义产生式的存放结构****************************/
struct p{
	char p_left;
	//string p_p;
	string p_right;
	int p_length;
}producer[30];
/*******************************************************/


int token_num=1;

//acction表的初始化程序
void initial_acction();

//goto表的初始化程序
void initial_acc_goto();

//producer的初始化程序
void initial_producer();



int main()
{
	cout<<"      //*****************************************************************//"<<endl;
	cout<<"      //****************              语法分析               ************//"<<endl;
	cout<<"      //*****************************************************************//"<<endl;
	
	
	
	/*********************************初始化token表*************************/
	for(int ii=1;ii<max1;ii++)
		token[ii].id=0;
    /**********************************************************************/

	const int maxlength=21;
	char file_token[maxlength];
	
    /***********将token文件中的内容读入到token数组中**********/
	cout<<"      请输入token文件名:";
	cin>>file_token;
	cout<<endl;
    fstream tokenfile(file_token,ios::in);
    if(!tokenfile){
		cout<<"                 文件:"<<file_token<<"打开失败!"<<endl;
		exit(1);
	}
	
	while(tokenfile>>token[token_num].id)
	{
		if(token[token_num].id!=9&&token[token_num].id!=10)
		{
			cout<<"                "<<setiosflags(ios::left)<<setw(6)<<token[token_num].id<<'\n';
			
		}
		else
		{
			tokenfile>>token[token_num].point;
		    cout<<"                "<<setiosflags(ios::left)<<setw(6)<<token[token_num].id<<token[token_num].point<<'\n';
		}
        token_num++;
	}
    /*****************************************************************************/


	
    initial_acction();
	initial_acc_goto();
	initial_producer();
	 
    cout<<endl;
	cout<<"      //*****************************************************************//"<<endl;
	cout<<"                         语法分析中所用的产生式如下:"<<endl;
	cout<<"      //*****************************************************************//"<<endl;


    int goto_m;
	stack[0]=0;
	int i=1;
    while(true)
	{
		int stack_h=stack[head];
		//查询acction表
		int acction_s;
		acction_s=acction[stack_h][token[i].id];
		
		//acction_s为10001时,为接收accp状态
		if(acction_s==10001){
			cout<<endl;
            cout<<"                "<<"语法分析成功!"<<endl;
			cout<<endl;
			return(1);
		}

		//acction_s为10000时, 为错误erro状态
		else if(acction_s==10000)
		{
			cout<<"                "<<"程序出错"<<endl;
			return(0);
		}

		//acctio_s>=0时,进行移入操作
		else if(acction_s>=0)
		{
			head++;
			stack[head]=token[i].id;
			head++;
			stack[head]=acction_s;
			i++;
		}

		//acction_s<=0时,进行归约操作
		else if(acction_s<0)
		{
			//输出归约过程中所需要的产生式
			//acc_s为归约的产生式的标号
			int acc_s=- acction_s;

			//if(acc_s!=9&&acc_s!=21&&acc_s!=22&&acc_s!=28&&acc_s!=29)
				//i++;
			cout<<"                "<<(char)producer[acc_s].p_left<<"->"<<producer[acc_s].p_right<<endl;
            
            for(int step=producer[acc_s].p_length; step>0; step--)
			{
				head--;
				head--;
			}

			
            goto_m=acc_goto[stack[head]][producer[acc_s].p_left-'A'+1];
			head++;
			stack[head]=producer[acc_s].p_left;
			head++;
			stack[head]=goto_m;	

			
			
		}
		
	}
}




void initial_producer()
{
	producer[1].p_left='E'; producer[1].p_right="E + T"; producer[1].p_length=3;
	producer[2].p_left='E'; producer[2].p_right="T"; producer[2].p_length=1;
	producer[3].p_left='T'; producer[3].p_right="T * F"; producer[3].p_length=3;
	producer[4].p_left='T'; producer[4].p_right="F"; producer[4].p_length=1;
    producer[5].p_left='F'; producer[5].p_right="id"; producer[5].p_length=1;
	producer[6].p_left='F'; producer[6].p_right="( E )"; producer[6].p_length=3;
	producer[7].p_left='B'; producer[7].p_right="B and M B"; producer[7].p_length=4;
	producer[8].p_left='B'; producer[8].p_right="id > id"; producer[8].p_length=3;
    //空产生式的标志
	producer[9].p_left='M'; producer[9].p_right="NULL"; producer[9].p_length=0;
    producer[10].p_left='S'; producer[10].p_right="id = P E"; producer[10].p_length=4;
	producer[11].p_left='C'; producer[11].p_right="if B then"; producer[11].p_length=3;
	producer[12].p_left='G'; producer[12].p_right="C S else"; producer[12].p_length=3;
	producer[13].p_left='S'; producer[13].p_right="G S"; producer[13].p_length=2;
	producer[14].p_left='S'; producer[14].p_right="C S"; producer[14].p_length=2;
	producer[15].p_left='W'; producer[15].p_right="while"; producer[15].p_length=1;
	producer[16].p_left='U'; producer[16].p_right="W B do"; producer[16].p_length=3;
	producer[17].p_left='S'; producer[17].p_right="U S"; producer[17].p_length=2;
	producer[18].p_left='S'; producer[18].p_right="{ L }"; producer[18].p_length=3;
	producer[19].p_left='L'; producer[19].p_right="L ; N S"; producer[19].p_length=4;
	producer[20].p_left='L'; producer[20].p_right="S"; producer[20].p_length=1;
	producer[21].p_left='P'; producer[21].p_right="NULL"; producer[21].p_length=0;
	producer[22].p_left='N'; producer[22].p_right="NULL"; producer[22].p_length=0;
	producer[23].p_left='A'; producer[23].p_right="K D"; producer[23].p_length=2;
	producer[24].p_left='D'; producer[24].p_right="D ; D"; producer[24].p_length=3;
	producer[25].p_left='D'; producer[25].p_right="id : H"; producer[25].p_length=3;
	producer[26].p_left='D'; producer[26].p_right="id() Q D;S"; producer[26].p_length=7;
	producer[27].p_left='H'; producer[27].p_right="int"; producer[27].p_length=1;
	producer[28].p_left='K'; producer[28].p_right="NULL"; producer[28].p_length=0;
	producer[29].p_left='Q'; producer[29].p_right="NULL"; producer[29].p_length=0;
}

void initial_acction()
{
	
	for(int acc_linenum=0; acc_linenum<max; acc_linenum++){
		for(int acc_pallnum=0; acc_pallnum<max; acc_pallnum++){
			acction[acc_linenum][acc_pallnum]=10000;
		}
	}
	
	acction[0][10]=-28;
	acction[1][10]=3;
	acction[2][11]=4; acction[2][0]=-23;
	acction[3][12]=5; acction[3][13]=6;
	acction[4][10]=3;
	acction[5][2]=9;
	acction[6][14]=10;
	acction[7][11]=-24; acction[7][0]=-24;
	acction[8][11]=-25; acction[8][0]=-25;
	acction[9][11]=-27; acction[9][0]=-27;
	acction[10][10]=-29;
	acction[11][10]=3;
	acction[12][11]=13;
	acction[13][4]=20; acction[13][7]=22; acction[13][10]=15; acction[13][15]=19;
	acction[14][11]=-26; acction[14][0]=-26;
	acction[15][12]=5; acction[15][13]=6; acction[15][20]=23;
	acction[16][4]=20; acction[16][7]=22; acction[16][10]=25; acction[16][15]=19;
    acction[17][4]=20; acction[17][7]=22; acction[17][10]=25; acction[17][15]=19;
    acction[18][4]=20; acction[18][7]=22; acction[18][10]=25; acction[18][15]=19;
    acction[19][4]=20; acction[19][7]=22; acction[19][10]=25; acction[19][15]=19;
    acction[20][10]=31;
	acction[21][10]=31;
    acction[22][10]=-15;
	acction[23][10]=-21; acction[23][13]=-21;
	acction[24][6]=-13; acction[24][11]=-13; acction[24][16]=-13; acction[24][0]=-13;
	acction[25][20]=23;
	acction[26][6]=34; acction[26][11]=-14; acction[26][16]=-14; acction[26][0]=-14;
	acction[27][6]=-17; acction[27][11]=-17; acction[27][16]=-17; acction[27][0]=-17;
	acction[28][11]=36; acction[28][16]=35;
	acction[29][11]=-20; acction[29][16]=-20;
	acction[30][1]=38; acction[30][5]=37; 
	acction[31][19]=39;
	acction[32][1]=38; acction[32][8]=40; 
	acction[33][10]=44; acction[33][13]=45;
	acction[34][4]=-12; acction[34][7]=-12; acction[34][10]=-12; acction[34][15]=-12;
	acction[35][6]=-18; acction[35][11]=-18; acction[35][16]=-18; acction[35][0]=-18;
	acction[36][4]=-22; acction[36][7]=-22; acction[36][10]=-22; acction[36][15]=-22;
	acction[37][4]=-11; acction[37][7]=-11; acction[37][10]=-11; acction[37][15]=-11;
	acction[38][10]=-9;
	acction[39][10]=48;
	acction[40][4]=-16; acction[40][7]=-16; acction[40][10]=-16; acction[40][15]=-16;
	acction[41][6]=-10; acction[41][11]=-10; acction[41][16]=-10; acction[41][17]=49;acction[41][0]=-10;
	acction[42][6]=-2; acction[42][11]=-2; acction[42][14]=-2; acction[42][16]=-2; acction[42][17]=-2; acction[42][18]=50; acction[42][0]=-2;
	acction[43][6]=-4; acction[43][11]=-4; acction[43][14]=-4; acction[43][16]=-4; acction[43][17]=-4; acction[43][18]=-4; acction[43][0]=-4;
	acction[44][6]=-5; acction[44][11]=-5; acction[44][14]=-5; acction[44][16]=-5; acction[44][17]=-5; acction[44][18]=-5; acction[44][0]=-5;
    acction[45][10]=44; acction[45][13]=45;
	acction[46][4]=20; acction[46][7]=22; acction[46][10]=25; acction[46][15]=19;
	acction[47][10]=31;
	acction[48][1]=-8; acction[48][5]=-8; acction[48][8]=-8;
	acction[49][10]=44; acction[49][13]=45;
	acction[50][10]=44; acction[50][13]=45;
	acction[51][14]=56; acction[51][17]=49;
	acction[52][11]=-19; acction[52][16]=-19;
	acction[53][1]=-7; acction[53][5]=-7; acction[53][8]=-7;
	acction[54][6]=-1; acction[54][11]=-1; acction[54][14]=-1; acction[54][16]=-1; acction[54][17]=-1; acction[54][18]=50; acction[54][0]=-1;
	acction[55][6]=-3; acction[55][11]=-3; acction[55][14]=-3; acction[55][16]=-3; acction[55][17]=-3; acction[55][18]=-3; acction[55][0]=-3;
	acction[56][6]=-6; acction[56][11]=-6; acction[56][14]=-6; acction[56][16]=-6; acction[56][17]=-6; acction[56][18]=-6; acction[56][0]=-6;
	acction[57][0]=10001;
}
    
void initial_acc_goto()
{
	//int acc_goto[max][max];
	acc_goto[0][1]=57; 	acc_goto[0][11]=1;
	acc_goto[1][4]=2;
	acc_goto[4][4]=7;
	acc_goto[5][8]=8;
	acc_goto[10][17]=11;
	acc_goto[11][4]=12;
	acc_goto[13][3]=17; acc_goto[13][4]=7;   acc_goto[13][7]=16; acc_goto[13][19]=14; acc_goto[13][21]=18; acc_goto[13][23]=21;
    acc_goto[16][3]=17;                      acc_goto[16][7]=16; acc_goto[16][19]=24; acc_goto[16][21]=18; acc_goto[16][23]=21;
    acc_goto[17][3]=17;                      acc_goto[17][7]=16; acc_goto[17][19]=26; acc_goto[17][21]=18; acc_goto[17][23]=21;
    acc_goto[18][3]=17;                      acc_goto[18][7]=16; acc_goto[18][19]=27; acc_goto[18][21]=18; acc_goto[18][23]=21;
    acc_goto[19][3]=17; acc_goto[19][12]=28; acc_goto[19][7]=16; acc_goto[19][19]=29; acc_goto[19][21]=18; acc_goto[19][23]=21;
	acc_goto[20][2]=30;
	acc_goto[21][2]=32;
	acc_goto[23][16]=33;
	acc_goto[33][5]=41; acc_goto[33][6]=43; acc_goto[33][20]=42;
	acc_goto[36][14]=46;
	acc_goto[38][13]=47;
	acc_goto[45][5]=51; acc_goto[45][6]=43; acc_goto[45][20]=42;
	acc_goto[46][3]=17;                      acc_goto[46][7]=16; acc_goto[46][19]=52; acc_goto[46][21]=18; acc_goto[46][23]=21;
    acc_goto[47][2]=53; 
	acc_goto[49][6]=43; acc_goto[49][20]=54;
	acc_goto[50][6]=55;
}

⌨️ 快捷键说明

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