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

📄 syntax.cpp

📁 本程序包括详细的词法分析
💻 CPP
字号:
//该模块用第归下降完成while循环语法分析
//while循环文法定义为
//   S->while E do P
//   P->id=E
//   E->A
//   E->YA
//   E->EYA
//   A->B
//   A->AZB
//   B->id
//   B->(E)
//   Y->+
//   Y->-
//   Z->*
//   Z->/
//
// 上面的文法经过提左因子后变为
//  S->while E do P
//  P->id=E
//  E->AO
//  E->YAO
//  O->YAO|e
//  A->BQ
//  Q->ZBQ|e
//  B->id
//  B->(E)
//  Y->+
//  Y->-
//  Z->*
//  Z->/ 
//
//  非终结符号S E P A B M N Z 终结符号id = ( ) + - * / e  其中e表示空字
// 
//每个终结符对应的编号 1  2  3  4  5  6  7       id
//                     =  +  -  *  /  (  )   1001~2999
//词法分析器输出结果,引用自Lex.cpp

#include "consts.h"
#include <iostream.h>
#include <string.h>


extern int LexResult[];
extern char Op[];
extern char Identifier[][16];
extern long lIdentifierIDMax;

int  stack[10];
int  stop=0;
int  nNewSymbolID=0;

int nCurIndex=0;   //当前输入符号的索引

int process_S();
int process_E();
int process_P();
int process_A();
int process_B();
int process_Y();
int process_Z();
int process_O();
int process_Q();
int error(char * inf);


int generateNewSymbol()
{   
	char tmp[3]={0};
	int n;
	lIdentifierIDMax++;
	nNewSymbolID++;
    tmp[0]='T';tmp[1]=nNewSymbolID+0x30;
	n=lIdentifierIDMax-IDENTIFIER_ID_BASE;
	strcpy(Identifier[n],tmp);
	return n;
}
int process_S()
{
	if(LexResult[nCurIndex]==KEYWORD_WHILE)
	{
		nCurIndex++;
		cout<<"S0:"<<endl;
		process_E();
		cout<<"  if "<<Identifier[stack[stop]-IDENTIFIER_ID_BASE]<<"!=0 then S1 else S2"<<endl;
		if(LexResult[nCurIndex]==KEYWORD_DO)
		{
			nCurIndex++;
			cout<<"S1:"<<endl;
			nNewSymbolID=0;
			stop=0;
			process_P();
			cout<<"S2:"<<endl;
			cout<<"  ......"<<endl;
		}
		else error("缺少do");

	}
	else error("缺少while");
	return 0;
}

int process_E()
{
	int i;
	int op=LexResult[nCurIndex];
	if(1001<=LexResult[nCurIndex] && LexResult[nCurIndex]<=2999 || LexResult[nCurIndex]==6)  //id
	{
		process_A();
		process_O();
	}
	else if(LexResult[nCurIndex]==2 ||LexResult[nCurIndex]==3)  //+ -
	{
		
		process_Y();
		process_A();
		i=generateNewSymbol();
        cout<<"  "<<Identifier[i]<<"="<<Op[op]<<Identifier[stack[stop]-IDENTIFIER_ID_BASE]<<endl;
		stack[stop]=i+IDENTIFIER_ID_BASE;
		process_O();
		
	}
	else error("表达式错误");
	return 0;
}

int process_P()
{
	int id;
	if(1001<=LexResult[nCurIndex] && LexResult[nCurIndex]<=2999)  //id
	{
		id=LexResult[nCurIndex];
		nCurIndex++;
		if(LexResult[nCurIndex]==1)  //=
		{
			nCurIndex++;
			process_E();
			cout<<"  "<<Identifier[id-IDENTIFIER_ID_BASE]<<"="<<Identifier[stack[stop]-IDENTIFIER_ID_BASE]<<endl;
			cout<<"  goto S0"<<endl;
		}
		else error("缺少=");
	}
	else error("赋值语句错误");
	return 0;
}

int process_A()
{
 	process_B();
	process_Q();
	return 0;
}

int process_B()
{
	if(1001<=LexResult[nCurIndex] && LexResult[nCurIndex]<=2999) //id
	{
	    stack[++stop]=LexResult[nCurIndex];
		nCurIndex++;
	}
	else if(LexResult[nCurIndex]==6) //(
	{
		nCurIndex++;
		process_E();
		if(LexResult[nCurIndex]==7) //)
		{
			nCurIndex++;
		}
		else error("缺少)");

	}
	else error("表达式错误");
	return 0;
}

int process_Y()
{
	if(LexResult[nCurIndex]==2 ||LexResult[nCurIndex]==3)//+ -
	{
		nCurIndex++;
	}
	else error("表达式错误");
	return 0;
}

int process_Z()
{
	if(LexResult[nCurIndex]==4 ||LexResult[nCurIndex]==5)//* / 
	{
		nCurIndex++;
	}
	else error("表达式错误");
	return 0;
}

int process_O()
{
	int i;
	int op=LexResult[nCurIndex];
	if(LexResult[nCurIndex]==2 ||LexResult[nCurIndex]==3)
	{
		process_Y();
	
		process_A();
		i=generateNewSymbol();
		cout<<"  "<<Identifier[i]<<"="<<Identifier[stack[stop-1]-IDENTIFIER_ID_BASE]<<Op[op]<<Identifier[stack[stop]-IDENTIFIER_ID_BASE]<<endl;
		stop--;
		stack[stop]=i+IDENTIFIER_ID_BASE;
		process_O();
		
	}
	return 0;

}

int process_Q()
{
	int i;
	int op=LexResult[nCurIndex];
	if(LexResult[nCurIndex]==4 ||LexResult[nCurIndex]==5)
	{
		process_Z();

		process_B();
		i=generateNewSymbol();
		cout<<"  "<<Identifier[i]<<"="<<Identifier[stack[stop-1]-IDENTIFIER_ID_BASE]<<Op[op]<<Identifier[stack[stop]-IDENTIFIER_ID_BASE]<<endl;
		stop--;
		stack[stop]=i+IDENTIFIER_ID_BASE;
		process_Q();
	
	}
	return 0;
}

int error(char * inf)
{
	cout<<"syntax error!---"<<inf<<endl;
	return 0;
}

⌨️ 快捷键说明

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