pascal.cpp

来自「请认真阅读您的文件包然后写出其具体功能(至少要20个字)。尽量不要让站长把时间都」· C++ 代码 · 共 1,033 行 · 第 1/2 页

CPP
1,033
字号
		if (g_nExpCurChar.type!=tempsy)
		{
			ssp++;
			num++;
			sstack[ssp].type=g_nExpCurChar.type;
			sstack[ssp].pos=g_nExpCurChar.pos;
		}
		
		g_nExpCurChar.type=ibuf[num].type;
		g_nExpCurChar.pos=ibuf[num].pos;
		lrparse1(num);
	}
	if((lr1>=100)&&(lr1<105))
	{
		switch(lr1)
		{
		case 100:
			break;
		case 101:
			E.pos=newtemp( );
			gen("+",sstack[ssp-2],sstack[ssp],E.pos+100);
			ssp=ssp-2;
			sstack[ssp].type=tempsy;
			sstack[ssp].pos=E.pos;
			sp1=sp1-3;
			break;
			/*E->E+E*/			
		case 102:
			E.pos=newtemp( );
			gen("*",sstack[ssp-2],sstack[ssp],E.pos+100);
			ssp=ssp-2;
			sstack[ssp].type=tempsy;
			sstack[ssp].pos=E.pos;
			sp1=sp1-3;
			break;
			/*E->E*E*/
		case 103:
			E.pos=sstack[ssp-1].pos;
			ssp=ssp-2;
			sstack[ssp].type=tempsy;
			sstack[ssp].pos=E.pos;
			sp1=sp1-3;
			break;
			/*E->(E)*/
		case 104:
			E.pos=sstack[ssp].pos;
			sp1--;
			break;
			/*E->i*/
		}
		g_nExpCurChar.type=tempsy;     /*规约后为非终结符*/
		g_nExpCurChar.pos=E.pos;
		lrparse1(num);
	}
	if ((lr1==ACC)&&(stack1[sp1]==1))
	{
		/*归约A->i:=E*/
		gen(":= ",sstack[ssp],oth,ibuf[0].pos);
		ssp=ssp-3;
		sp1=sp1-3;
	}
}
/*********************布尔表达式的分析**************************/
int lrparse2(int num)
{
	int templabel;
	lr1=action2[stack1[sp1]][change2(g_nExpCurChar.type)];
	if (lr1==-1) 
	{	
		if (sign==2) printf("\nwhile语句出错!\n");
		if (sign==3) printf("\nif语句出错!\n");
		getch( );
		exit(0);
	}
	if ((lr1<16)&&(lr1>=0))
	{
		sp1++;
		stack1[sp1]=lr1;
		ssp++;
		sstack[ssp].type=g_nExpCurChar.type;
		sstack[ssp].pos=g_nExpCurChar.pos;
		if ((g_nExpCurChar.type!=tempsy)&&(g_nExpCurChar.type!=EA)&&(g_nExpCurChar.type!=EO)) num++;
		g_nExpCurChar.type=ibuf[num].type;
		g_nExpCurChar.pos=ibuf[num].pos;
		lrparse2(num);
	}
	if((lr1>=100)&&(lr1<109))
	{
		switch(lr1)
		{
		case 100:
			break;
		case 101:
			ntab2[label].tc=nxq;
			ntab2[label].fc=nxq+1;
			gen("jnz",sstack[ssp],oth,0);
			gen("j",oth,oth,0);
			sp1--;
			ssp--;
			label++;
			g_nExpCurChar.type=tempsy;
			break;
			/*E->i*/
		case 102:
			ntab2[label].tc=nxq;
			ntab2[label].fc=nxq+1;
			switch (sstack[ssp-1].pos)
			{
			case 0:
				gen("j<=",sstack[ssp-2],sstack[ssp],0);
				break;
			case 1:
				gen("j<",sstack[ssp-2],sstack[ssp],0);
				break;
			case 2:
				gen("j>=",sstack[ssp-2],sstack[ssp],0);
				break;
			case 3:
				gen("j>",sstack[ssp-2],sstack[ssp],0);
				break;
			case 4:
				gen("j<>",sstack[ssp-2],sstack[ssp],0);
				break;
			case 5:
				gen("j=",sstack[ssp-2],sstack[ssp],0);
				break;
			}
			gen("j",oth,oth,0);
			ssp=ssp-3;
			sp1=sp1-3;
			label++;
			g_nExpCurChar.type=tempsy;
			break;
			/*E->i rop i*/
			case 103:
				label=label-1;
				ssp=ssp-3;
				sp1=sp1-3;
				label++;
				g_nExpCurChar.type=tempsy;
				break;
				/*E->(E)*/
			case 104:
				label=label-1;
				templabel=ntab2[label].tc;
				ntab2[label].tc=ntab2[label].fc;
				ntab2[label].fc=templabel;
				ssp=ssp-2;
				sp1=sp1-2;
				label++;
				g_nExpCurChar.type=tempsy;
				break;
				/*E->not E*/
			case 105:
				backpatch(ntab2[label-1].tc,nxq);
				label=label-1;
				ssp=ssp-2;
				sp1=sp1-2;
				label++;
				g_nExpCurChar.type=EA;
				break;
				/*EA->E(1)and*/
			case 106:
				label=label-2;
				ntab2[label].tc=ntab2[label+1].tc;
				ntab2[label].fc=merg(ntab2[label].fc,ntab2[label+1].fc);
				ssp=ssp-2;
				sp1=sp1-2;
				label++;
				g_nExpCurChar.type=tempsy;
				break;
				/*E->EA E(2)*/
			case 107:
				backpatch(ntab2[label-1].fc,nxq);
				label=label-1;
				ssp=ssp-2;
				sp1=sp1-2;
				label++;
				g_nExpCurChar.type=EO;
				break;
			case 108:
				label=label-2;
				ntab2[label].fc=ntab2[label+1].fc;
				ntab2[label].tc=merg(ntab2[label].tc,ntab2[label+1].tc);
				ssp=ssp-2;
				sp1=sp1-2;
				label++;
				g_nExpCurChar.type=tempsy;
				break;
		}
		lrparse2(num);
	}
	if (lr1==ACC)return 1;
	return -1;
}
/***************测试字符是否为表达式中的值(不包括"; ")**************/
bool test(int value)
{
	switch (value)
	{
	case intconst:
	case ident:
	case plus:
	case times:
	case becomes:
	case lparent:
	case rparent:
	case rop:
	case op_and:
	case op_or:
	case op_not:
		return 1;
	default:
		return 0;
	}
} 
/*******************************************/
int lrparse( )
{
	int i1=0;
	int num=0;
	/*指向表达式缓冲区*/
	if (test(g_nCurChar.type))
	{
		if(stack[sp].type==sy_while) 
		{
			sign=2;
		}
		else 
		{
			if (stack[sp].type==sy_if) sign=3;
			else sign=1;
		}
		do
		{
			ibuf[i1].type=g_nCurChar.type;
			ibuf[i1].pos=g_nCurChar.pos;
			readnu( );
			i1++;
		}while(test(g_nCurChar.type));
		/*把表达式放入缓冲区*/
		ibuf[i1].type=sharp;
		pbuf--;
		/*指针后退1,需要吗?*/
		sstack[0].type=sharp;
		ssp=0;
		/*符号栈底的初始化*/
		if (sign==1)
		{
			sp1=0;
			stack1[sp1]=0;
			/*状态栈1的栈底初始化*/
			num=2;
			/*指向:=*/
			g_nExpCurChar.type=ibuf[num].type;
			g_nExpCurChar.pos=ibuf[num].pos;
			lrparse1(num);
			/*处理赋值语句*/
			g_nCurChar.type=a;
			/*当前文法符号置为a(赋值语句)*/
		}
		if ((sign==2)||(sign==3))
		{
			pointmark++;
			labelmark[pointmark].nxq1=nxq;
			sp1=0;
			stack1[sp1]=0;
			num=0;
			g_nExpCurChar.type=ibuf[num].type;
			g_nExpCurChar.pos=ibuf[num].pos;
			lrparse2(num);
			labelmark[pointmark].tc1=ntab2[label-1].tc;
			labelmark[pointmark].fc1=ntab2[label-1].fc;
			/*处理布尔语句*/
			/*在处理完E,要回填真值链*/
			backpatch(labelmark[pointmark].tc1,nxq);
			g_nCurChar.type=e;
			/*当前文法符号置为e(赋值语句)*/
		}
	}
	lr=action[stack[sp].pos][g_nCurChar.type];
	printf("stack[%d]=%d\t\tn=%d\t\tlr=%d\n",sp,stack[sp].pos,g_nCurChar.type,lr);
	
	
	
	if ((lr<19)&&(lr>=0))
	{
		sp++;
		stack[sp].pos=lr;
		stack[sp].type=g_nCurChar.type;
		readnu( );
		lrparse( );
	}
	if ((lr<=106)&&(lr>=100))
	{
		switch (lr)
		{
		case 100:break;
			/*S'->S*/
		case 101:
			printf("S->if e then S then S else S 归约\n");
			sp=sp-6;
			g_nCurChar.type=S;
			/*归约完if后,填then后面的无条件转移语句*/
			fexp[labeltemp[pointtemp]].result=nxq;
			pointtemp--;
			if(stack[sp].type==sy_then)
			{
				gen("j",oth,oth,0);
				backpatch(labelmark[pointmark].fc1,nxq);
				pointtemp++;
				labeltemp[pointtemp]=nxq-1;
			}
			pointmark--;
			if(stack[sp].type==sy_do)
			{
				gen("j",oth,oth,labelmark[pointmark].nxq1);
				backpatch(labelmark[pointmark].fc1,nxq);
			}
			break;
			/*S->if e then S then S else S*/
		case 102:
			printf("S->while e do S 归约\n");
			sp=sp-4;
			g_nCurChar.type=S;
			pointmark--;
			if(stack[sp].type==sy_do)
			{
				gen("j",oth,oth,labelmark[pointmark].nxq1);
				backpatch(labelmark[pointmark].fc1,nxq);
			}
			
			if(stack[sp].type==sy_then)
			{
				gen("j",oth,oth,0);
				fexp[labelmark[pointmark].fc1].result=nxq;
				pointtemp++;
				labeltemp[pointtemp]=nxq-1;
			}
			break;
			/*S->while e do S*/
		case 103:
			printf("S->begin L end 归约\n");
			sp=sp-3;
			g_nCurChar.type=S;
			if(stack[sp].type==sy_then)
			{
				gen("j",oth,oth,0);
				backpatch(labelmark[pointmark].fc1,nxq);
				pointtemp++;
				labeltemp[pointtemp]=nxq-1;
			}
			if(stack[sp].type==sy_do)
			{
				gen("j",oth,oth,labelmark[pointmark].nxq1);
				backpatch(labelmark[pointmark].fc1,nxq);
			}
			getch();
			break;
			/*S->begin L end*/
		case 104:
			printf("S->a 归约\n");
			sp=sp-1;
			g_nCurChar.type=S;
			if(stack[sp].type==sy_then)
			{
				gen("j",oth,oth,0);
				backpatch(labelmark[pointmark].fc1,nxq);
				pointtemp++;
				labeltemp[pointtemp]=nxq-1;
			}
			if(stack[sp].type==sy_do)
			{
				gen("j",oth,oth,labelmark[pointmark].nxq1);
				backpatch(labelmark[pointmark].fc1,nxq);
			}
			break;
			/*S->a*/	
		case 105:
			printf("归约\n");
			sp=sp-1;
			g_nCurChar.type=L;
			break;
			/*L->S*/
		case 106:
			printf("L->S;L 归约\n");
			sp=sp-3;
			g_nCurChar.type=L;
			break;
			/*L->S;L*/
		}
		getch( );
		pbuf--;
		lrparse();
	}
	if (lr==ACC) return ACC;
	return -1;
}
/*****************************disp1*************************/
void disp1( )
{
	int temp1=0;
	printf("\n********************词法分析结果***********************\n");
	for(temp1=0;temp1<count;temp1++)
	{
		
		printf("%d\t%d\n",buf[temp1].type,buf[temp1].pos);
		if (temp1==20)
		{
			printf("Press any key to continue......\n");
			getch( );
		}
	}
	getch ( );
}
/******************************************************/
void disp2()
{
	int temp1=100;
	printf("\n********************四元式分析结果***********************\n");
	
	for(temp1=100;temp1<nxq;temp1++)
	{
		if(temp1>100)
			fprintf(mfile, "\n");
		fprintf(mfile, "%d\t",temp1);
		fprintf(mfile, "(%s\t, ",fexp[temp1].op);
		if (fexp[temp1].arg1.type==ident)
			fprintf(mfile, "%s\t, ",varnametable[fexp[temp1].arg1.pos]);
		
		else 
		{
			if (fexp[temp1].arg1.type==tempsy)
				fprintf(mfile, "T%d\t, ",fexp[temp1].arg1.pos);
			else
			{
				if(fexp[temp1].arg1.type==intconst)
					fprintf(mfile, "%d\t, ",fexp[temp1].arg1.pos);
				else fprintf(mfile, "\t, ");
			}
		}
		if (fexp[temp1].arg2.type==ident)
			fprintf(mfile, "%s\t, ",varnametable[fexp[temp1].arg2.pos]);
		else
		{
			if (fexp[temp1].arg2.type==tempsy)
				fprintf(mfile, "T%d\t, ",fexp[temp1].arg2.pos);
			else 
				
			{
				if(fexp[temp1].arg2.type==intconst)
					fprintf(mfile, "%d\t, ",fexp[temp1].arg2.pos);
				else fprintf(mfile, "\t, ");
			}
		}
		
		
		if (fexp[temp1].op[0]!= 'j')
		{
			if(fexp[temp1].result>=100)
				fprintf(mfile, "T%d\t) ",fexp[temp1].result-100);
			else fprintf(mfile, "%s\t) ",varnametable[fexp[temp1].result]);
		}
		
		else fprintf(mfile, "%d\t) ",fexp[temp1].result);
		if (temp1==20)
		{
			printf("\nPress any key to continue......\n");
			getch( );
		}
	}
	
	getch( );
}
void disp3( )
{
	int tttt;
	printf("\n\n程序总共%d行,产生了%d个二元式!\n",lnum,count);
	getch( );
	printf("\n*******************变量表*******************\n");
	for(tttt=0;tttt<tt1;tttt++)
		printf("%d\t%s\n",tttt,varnametable[tttt]);
	getch();
}
/*****************主程序**********************/
int main()
{
	cfile=fopen("pas.dat", "r");
	mfile=fopen("pas.med", "w");
	/*打开c语言源文件*/
	readch( );
	/*从源文件读一个字符*/
	scan( );
	/*词法分析*/
	disp1( );
	disp3( );
	stack[sp].pos=0;
	stack[sp].type=-1;
	/*初始化状态栈栈底*/
	stack1[sp1]=0;
	/*初始化状态栈栈底*/
	oth.type=-1;
	printf("\n*********状态栈变化过程及规约顺序************\n");
	readnu();
	/*从二元式读一个字符*/
	lrparse();
	getch( );
	/*四元式的分析*/
	disp2();
	fclose(cfile);
	printf("\n程序结束。谢谢使用!\n");
	printf("COPYRIGHT BY Z.Y.P 2001.6.7");
	getch();
	return 0; 
}

⌨️ 快捷键说明

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