📄 compile.cpp
字号:
//一遍编译器的实现文件
#include "compile.h"
//将 DIV,MOD两个记号插入
void translate::init()
{
entry keywords[] =
{
"div", DIV,
"mod", MOD,
0, 0
};
entry *p;
for( p = keywords; p->token; p++)
insert(p->lexptr,p->token);
}
//-----------------------------------------------------
//查找标志符函数定义
int translate::lookup(char s[])
{
int p;
for(p = lastentry; p>0; p = p-1)
if(strcmp(symtable[p].lexptr, s) == 0)
return p;
return 0;
}
//------------------------------------------------------
//插入标志符函数的定义
int translate::insert(char s[],int tok)
{
int len;
len = strlen(s); //strlen 计算s的长度
if(lastentry + 1 >= SYMMAX)
error("lexemes table full");
if(lastchar + len + 1 >= STRMAX)
error("lexemes array full");
lastentry = lastentry + 1;
symtable[lastentry].token = tok;
symtable[lastentry].lexptr = &lexemes[lastchar +1];
lastchar = lastchar + len + 1;
strcpy(symtable[lastentry].lexptr, s);
return lastentry;
}
//------------------------------------------------------
//错误处理函数定义,输出错误显示的字符串
void translate::error(char *m)
{
fprintf(stderr, "line %d: %s\n", lineno, m);
return; //非正常终止
}
//-------------------------------------------------------
// 后缀形式的成员函数输出
void translate::emit(int t, int tval)
{
switch(t)
{
case '+':
case '-':
case '*':
case '/':
printf("%c\n",t);
break;
case DIV:
printf("DIV\n");
break;
case MOD:
printf("MOD\n");
break;
case NUM:
printf("%d\n",tval);
break;
case ID:
printf("%s\n",symtable[tval].lexptr);
break;
default:
printf("token %d,tokenval %d\n", t, tval);
}
}
//-----------------------------------------------------
//语法分析成员函数定义
int translate::lexan()
{
int t;
while(1)
{
t = getchar();
if( t==' ' || t=='\t')
; //删去空白符
else if(t == '\n')
lineno = lineno + 1;
else if(isdigit(t))
{
ungetc(t,stdin);
scanf("%d",&tokenval);
return NUM;
}
else if(isalpha(t)) //t是字母
{
int p, b = 0;
while(isalnum(t)) //t是字母或数字
{
lexbuf[b] = t;
t = getchar();
b = b + 1;
if (b >= BSIZE)
error(" compiler error");
}
lexbuf[b] = EOS;
if (t != EOF)
ungetc(t, stdin);
p = lookup(lexbuf);
if (p == 0)
p = insert(lexbuf, ID);
tokenval = p;
return symtable[p].token;
}
else if (t == EOF)
return DONE;
else
{
tokenval = NONE;
return t;
}
}
}
//----------------------------------------------------
void translate::parse() //分析并翻译表达式列表
{
lookahead = lexan();
while (lookahead != DONE)
{
expr();
match(';');
}
}
//--------------------------------------------------------
//
void translate::expr()
{
int t;
term();
while(1)
{
switch(lookahead)
{
case '+':
case '-':
t = lookahead;
match(lookahead);
term();
emit(t,NONE);
continue;
default:
return;
}
}
}
//-----------------------------------------------------
//
void translate::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 translate::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("systax error");
}
}
//------------------------------------------------------
//
void translate::match(int t)
{
if (lookahead == t)
lookahead = lexan();
else
error("syntax error");
}
//-------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -