parser.cpp
来自「编译原理作业」· C++ 代码 · 共 75 行
CPP
75 行
#include "global.h"
int lexan();
void expr();
void term();
void factor();
void match(int t);
void emit(int,int tval);
void error(char *m);
extern int tokenval;
int lookahead;
void parse()
{
lookahead = lexan();
while ( lookahead != DONE ) {
expr(); match(';');
}
}
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");
}