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

📄 parser.cpp

📁 编译原理实验将简单中缀算术表达式变换成后缀形式
💻 CPP
字号:
//parser.c
#include "stdafx.h"
#include "global.h"

int lookahead;
int lhead,lend;
int fp1_lhead;/////////////
void parse()
{
	int temp;
	lookahead=lexan();
	while(lookahead!=DONE)
	{
		isID=0;
		if(!Empty(S)) Pop(S,temp);
		lhead=ftell(fp2);//
        fp1_lhead=ftell(fp1);
		expr(); 
		if(lookahead!=DONE) {match('\n'); lineno++;}
		lend=ftell(fp2);//
		if(isID==0&&mode)
		{
			fseek(fp2,lhead,0);
			while(ftell(fp2)<lend)
				fputc(' ',fp2);
			fseek(fp2,lhead,0);
            fprintf(fp2,"%d",GetTop(S));
		}
		fprintf(fp2,"\n"); 
	}
}

void expr()
{
	int t;
	term();
	while(1)
	{
		switch(lookahead)
		{
		case'+':
		case'-':
			t=lookahead;
			match(lookahead);
			term();
			emit(t,NONE);
			continue;
		default:
			return;
		}
	}
}

void term()
{
 int t;
 factor();
 while(1)
	 switch(lookahead)
 {
	 case'*':
	 case'/':
	 case DIV:
	 case MOD:
		 t=lookahead;
		 match(lookahead);
		 factor();
		 emit(t,NONE);
		 continue;
	 default:
		 return;
 }
}

void factor()
{
	switch(lookahead)
	{
	case'(':
		match('(');
		expr();
		match(')');
		break;
	case NUM:
		emit(NUM,tokenval);
		match(NUM);
		break;
	case ID:
		emit(ID,tokenval);
		match(ID);
		break;
	default:
		error("syntax error");
	}
}

void match(int t)
{
	if(lookahead==t)
		lookahead=lexan();
	else error("syntax error");
}

⌨️ 快捷键说明

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