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

📄 parser.c

📁 编译原理的词法分析器
💻 C
字号:
#include"global.h"
int lookahead;
parse()  //分析并翻译表达式列表
{
	lookahead = lexan();
	while(lookahead !=DONE){
		expr();/*emit(DONE,DONE);*/match(';');
	}
}
expr()
{
	int t;
	term();
	while(1)
		switch(lookahead){
		case '+':case'-':
			t = lookahead;
			match(lookahead);term();emit(t,NONE);
			continue;
		default:
			return;
	}
}
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;
	}
}
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");
	}
}
match(t)
    int t;
{
	if(lookahead == t)
		lookahead = lexan();
	else error("syntax error");
}

⌨️ 快捷键说明

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