📄 compiler.cpp
字号:
/*
FileName: compiler.cpp
Author: LiWen
Create: 2005-11-23
Last Modified: 2005-12-28
Discription: 编译器主函数,处理入口
*/
#include "compiler.h"
FILE *fin,*ft,*fl; //输入文件指针
char FileName[STRMAX]; //文件名
char WResult[WANYOUTSIZE]; //存放词法分析结果
bool wordswitch,tableswitch,codeswitch; //是否显示词法分析,是否显示符号表,是否显示中间代码
char str[STRMAX]; //字符串,中间变量,词法分析显示使用
extern int errp; //错误表指针
extern ERRT errlist[]; //错误存储表
extern char errinfo[ERRMAXN][ERRMAXL]; //错误提示信息表
extern symbol sym; //当前符号类型,临时
extern char symtype[][20]; //符号名字符串
extern TABLE table[]; //table表
extern int tx; //table表指针
extern INSTRUCTION codelist[]; //中间代码表
extern char strcode[8][4]; //中间代码操作名字符串
extern int cx; //中间代码表指针
void init(){
errorinit();
symbolinit();
tableinit();
fseek(fin,0,SEEK_SET);
WResult[0] = 0;
return;
}
int main(){
int i;
char command;
printf("请输入PL/0源程序文件名:");
scanf("%s",FileName);
fin = fopen(FileName,"r");
if(!fin){
printf("Error:打开源文件失败,请检查路径是否正确!\n");
return 0;
}
printf("是否显示词法分析结果?");
fflush(stdin);
scanf("%c",&command);
if(command == 'Y' || command =='y'){
wordswitch = true;
}
printf("是否显示中间代码列表?");
fflush(stdin);
scanf("%c",&command);
if(command == 'Y' || command =='y'){
codeswitch = true;
}
init();
printf("Compiling...\n");
if(wordswitch){
while(getsym() != -1){
if(sym != procsym){
sprintf(str,"\t\tType:%s\n",symtype[sym]);
}else{
sprintf(str,"\tType:%s\n",symtype[sym]);
}
strcat(WResult,str);
if(sym == period)
break;
}
for(i=0;i<errp;i++){
sprintf(str,errinfo[errlist[i].type],errlist[i].str);
printf("Error %d:In Row %d:%s\n",i+1,errlist[i].row,str);
}
if(errp != 0){
fclose(fin);
return 0;
}
printf("词法分析结果如下:\n%s",WResult);
init();
}
printf("Syntaxing...\n");
if(getsym() != TMNT){
program();
}
for(i=0;i<errp;i++){
sprintf(str,errinfo[errlist[i].type],errlist[i].str);
printf("Error %d:In Row %d:%s\n",i+1,errlist[i].row,str);
}
if(codeswitch && errp == 0){
printf("中间代码如下:\n");
for(i=0;i<cx;i++){
printf("%2d: %s %d %d\n",i,strcode[codelist[i].f],codelist[i].l,codelist[i].a);
}
}
for(i = strlen(FileName)-1;i>=0;i--){
if(FileName[i] == '.'){
FileName[i] = 0;
break;
}
}
strcat(FileName,".lst");
fl = fopen(FileName,"w");
if(!fl){
printf("存储代码列表文件失败!");
}else{
for(i=0;i<cx;i++){
fprintf(fl,"%2d:%s %d %d\n",i,strcode[codelist[i].f],codelist[i].l,codelist[i].a);
}
}
fclose(fl);
if(errp == 0){
printf("Running....\n");
execute();
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -