📄 pl0.cpp
字号:
/*使用方法:
*运行后输入源程序文件名
*fas.tmp:输出名字表
*fa1.tmp:输出源文件
*fa2.tmp:输出结果
*fa.tmp:输出虚拟机代码
*/
#include<stdio.h>
#include<string.h>
#include"pl0.h"
#define stacksize 500 //解释执行使用的栈大小
int main()
{
bool nxtlev[symnum];
printf("Input pl/0 file?");
scanf("%s",fname);//输入文件名
fin = fopen(fname,"r");
if(fin)
{
printf("List object code?(Y/N)");//是否输出虚拟机代码
scanf("%s",fname);
listswitch = (fname[0]=='y'||fname[0]=='Y');
printf("List symbol table?(Y/N)");
scanf("%s",fname);
tableswitch = (fname[0]=='y'||fname[0]=='Y');//是否输出名字表
fa1 = fopen("fa1.tmp","w");
fprintf(fa1,"Input pl/0 file?");
fprintf(fa1,"%s\n",fname);
init();//初始化
err = 0;
cc = cx = ll = 0;
ch = ' ';
printf("compiler.....\n");
if(-1!=getsym())
{
fa = fopen("fa.tmp","w");
fas = fopen("fas.tmp","w");
addset(nxtlev,declbegsys,statbegsys,symnum);
nxtlev[period]=true;
if(-1==block(0,0,nxtlev))//调用编译程序
{
fclose(fa);
fclose(fa1);
fclose(fas);
fclose(fin);
printf("\n");
return 0;
}
fclose(fa);
fclose(fa1);
fclose(fas);
if(sym!=period)
{
error(9);
}
if(err==0)
{
printf("complier complete and no error!\n");
fa2 = fopen("fa2.tmp","w");
interpret(); //调用解释执行程序
fclose(fa2);
}
else
{
printf("Errors in pl/0 program");
}
}
fclose(fin);
}
else
{
printf("Can't open file!\n");
}
printf("\n");
return 0;
}
/*
* 打印出错位置和错误编码
*/
void error(int n)
{
//char space[81];
//memset(space,32,81);
//space[cc-1]=0;
printf("%d:%s\n",n,errors[n]);
fprintf(fa1,"%d:%s\n",n,errors[n]);
//fprintf(fa1,"***%s!%d\n",space,n);
err++;
}
/*
* 词法分析,获取一个符号
*/
int getsym()
{
int i,j,k;
while(ch==' '||ch==10||ch==9)//忽略空格、换行和制表符
{
getchdo;
}
if(ch>='a'&&ch<='z')//标识符或关键字
{
k=0;
do
{
if(k<al)
{
a[k]=ch;
k++;
}
getchdo;
}while(ch>='a'&&ch<='z'||ch>='0'&&ch<='9');
a[k]=0;
strcpy(id,a);
i=0;
j=norw-1;
do
{ //搜索当前符号是否为关键字
k=(i+j)/2;
if(strcmp(id,word[k])<=0)
{
j=k-1;
}
if(strcmp(id,word[k])>=0)
{
i=k+1;
}
}while(i<=j);
if(i-1>j)
{
sym=wsym[k];
}
else
{
sym=(symbol)ident; //搜索失败,是标识符
}
}
else
{
if(ch>='0'&&ch<='9')//检测是否为数字
{
k=0;
num=0;
sym=(symbol)number;
do
{
num=10*num+ch-'0';
k++;
getchdo;
}while(ch>='0'&&ch<='9');
k--;
if(k>nmax)
{
error(30);
}
}
else
{
if(ch==':')//检测赋值符号
{
getchdo;
if(ch=='=')
{
sym=(symbol)becomes;
getchdo;
}
else
{
sym=(symbol)nul;//不能识别的符号
}
}
else
{
if(ch=='<') //检测<或<=符号
{
getchdo;
if(ch=='=')
{
sym=(symbol)leq;
getchdo;
}
else
{
sym=(symbol)lss;
}
}
else
{
if(ch=='>')//检测>或>=符号
{
getchdo;
if(ch=='=')
{
sym=(symbol)geq;
getchdo;
}
else
{
sym=(symbol)gtr;
}
}
else
{ //当符号不满足上述条件时,全部按照单字符符号处理
sym=ssym[ch];
if(sym!=period)
{
getchdo;
}
}
}
}
}
}
return 0;
}
/*
*读取一个字符
*每次读入一行,存入line缓冲区,line被取空后再读一行
*/
int getch()
{
if(cc==ll)
{
if(feof(fin))
{
printf("program incomplete");
return -1;
}
ll=0;
cc=0;
//printf("%d",cx);
//fprintf(fa1,"%d",cx);
ch=32;
while(ch!=10)
{
if(EOF==fscanf(fin,"%c",&ch))
{
line[ll]=0;
break;
}
printf("%c",ch);
fprintf(fa1,"%c",ch);
line[ll]=ch;
ll++;
}
//printf("\n");
fprintf(fa1,"\n");
}
ch=line[cc];
cc++;
return 0;
}
/*
*初始化
*/
void init()
{
int i;
/*设置单字符符号*/
for(i=0;i<=255;i++)
ssym[i] =(symbol) nul;
ssym['+']=(symbol)plus;
ssym['-']=(symbol)minus;
ssym['*']=(symbol)times;
ssym['/']=(symbol)slash;
ssym['(']=(symbol)lparen;
ssym[')']=(symbol)rparen;
ssym['=']=(symbol)eql;
ssym[',']=(symbol)comma;
ssym['.']=(symbol)period;
ssym['#']=(symbol)neq;
ssym[';']=(symbol)semicolon;
/*设置保留字*/
strcpy(&(word[0][0]),"begin");
strcpy(&(word[1][0]),"call");
strcpy(&(word[2][0]),"const");
strcpy(&(word[3][0]),"do");
strcpy(&(word[4][0]),"end");
strcpy(&(word[5][0]),"if");
strcpy(&(word[6][0]),"odd");
strcpy(&(word[7][0]),"procedure");
strcpy(&(word[8][0]),"read");
strcpy(&(word[9][0]),"then");
strcpy(&(word[10][0]),"var");
strcpy(&(word[11][0]),"while");
strcpy(&(word[12][0]),"write");
/*设置保留字符号*/
wsym[0]=(symbol)beginsym;
wsym[1]=(symbol)callsym;
wsym[2]=(symbol)constsym;
wsym[3]=(symbol)dosym;
wsym[4]=(symbol)endsym;
wsym[5]=(symbol)ifsym;
wsym[6]=(symbol)oddsym;
wsym[7]=(symbol)procsym;
wsym[8]=(symbol)readsym;
wsym[9]=(symbol)thensym;
wsym[10]=(symbol)varsym;
wsym[11]=(symbol)whilesym;
wsym[12]=(symbol)writesym;
/*设置指令名称*/
strcpy(&(mnemonic[lit][0]),"lit");
strcpy(&(mnemonic[opr][0]),"opr");
strcpy(&(mnemonic[cal][0]),"cal");
strcpy(&(mnemonic[sto][0]),"sto");
strcpy(&(mnemonic[lod][0]),"lod");
strcpy(&(mnemonic[jpc][0]),"jpc");
strcpy(&(mnemonic[jmp][0]),"jmp");
strcpy(&(mnemonic[inte][0]),"int");
/*设置符号集*/
for(i=0;i<symnum;i++)
{
declbegsys[i]=false;
statbegsys[i]=false;
facbegsys[i]=false;
}
/*设置声明开始符号集*/
declbegsys[constsym]=true;
declbegsys[varsym]=true;
declbegsys[procsym]=true;
/*设置语句开始符号集*/
statbegsys[beginsym]=true;
statbegsys[callsym]=true;
statbegsys[ifsym]=true;
statbegsys[whilesym]=true;
/*设置因子开始符号集*/
facbegsys[ident]=true;
facbegsys[number]=true;
facbegsys[lparen]=true;
/*设置错误字符串*/
strcpy(&(errors[0][0]),"没有错误");
strcpy(&(errors[1][0]),"常数说明中的'='写成':='");
strcpy(&(errors[2][0]),"常数说明中的=后应是数字");
strcpy(&(errors[3][0]),"常数说明中的标识符应是=");
strcpy(&(errors[4][0]),"const,var,procedure后应为标识符");
strcpy(&(errors[5][0]),"漏掉了,或;");
strcpy(&(errors[6][0]),"过程说明后的符号不正确");
strcpy(&(errors[7][0]),"应是语句开始符");
strcpy(&(errors[8][0]),"程序体内语句部分的后继符号不正确");
strcpy(&(errors[9][0]),"程序结束是掉了.");
strcpy(&(errors[10][0]),"语句之间漏掉了';'");
strcpy(&(errors[11][0]),"标识符未说明");
strcpy(&(errors[12][0]),"赋值语句中,左部标识符应是变量");
strcpy(&(errors[13][0]),"赋值语句左部标识符应该是':='");
strcpy(&(errors[14][0]),"call 后应是标识符");
strcpy(&(errors[15][0]),"call后的标识符应是过程");
strcpy(&(errors[16][0]),"条件语句掉了then");
strcpy(&(errors[17][0]),"掉了end或';'");
strcpy(&(errors[18][0]),"while语句掉了do");
strcpy(&(errors[19][0]),"语句后的符号不正确");
strcpy(&(errors[20][0]),"应是关系运算符号");
strcpy(&(errors[21][0]),"表达式内的标识符不能是过程");
strcpy(&(errors[22][0]),"表达式中漏掉了')'");
strcpy(&(errors[23][0]),"因子后的符号非法");
strcpy(&(errors[24][0]),"表达式的开始符号不正确");
strcpy(&(errors[25][0]),"没有错误");
strcpy(&(errors[26][0]),"没有错误");
strcpy(&(errors[27][0]),"没有错误");
strcpy(&(errors[28][0]),"没有错误");
strcpy(&(errors[29][0]),"没有错误");
strcpy(&(errors[30][0]),"没有错误");
strcpy(&(errors[31][0]),"数越界");
strcpy(&(errors[32][0]),"read语句括号中的标识符不是变量");
}
/*
*生成虚拟机代码
*/
int gen(enum fct x,int y,int z)
{
if(cx>cxmax)
{
printf("program too long!");
return -1;
}
code[cx].f=x;
code[cx].l=y;
code[cx].a=z;
cx++;
return 0;
}
/*
*测试当前符号是否合法
*s1:我们需要的集合
*s2:一个补救用的集合
*n:错误号
*/
int test(bool*s1,bool*s2,int n)
{
if(!inset(sym,s1))
{
error(n);
/*当检测不通过时,不停获取符号,直到它属于需要的集合或补救的集合*/
while((!inset(sym,s1))&&(!inset(sym,s2)))
{
getsymdo;
}
}
return 0;
}
/**********************
*用数组实现集合的运算*
**********************/
/*
* 判断元素e是否在集合s中
*/
int inset(int e,bool*s)
{
return s[e];
}
/*
*集合并:元素i在集合s1或s2中,则元素i在集合sr中
*/
int addset(bool*sr,bool*s1,bool*s2,int n)
{
int i;
for(i=0;i<n;i++)
sr[i] = s1[i]||s2[i];
return 0;
}
/*
*集合减:元素i在集合s1不在s2中,则元素i在集合sr中
*/
int subset(bool*sr,bool*s1,bool*s2,int n)
{
int i;
for(i=0;i<n;i++)
sr[i] = s1[i]&&(!s2[i]);
return 0;
}
/*
*集合交:元素i在集合s1且在s2中,则元素i在集合sr中
*/
int mulset(bool*sr,bool*s1,bool*s2,int n)
{
int i;
for(i=0;i<n;i++)
sr[i] = s1[i]&&s2[i];
return 0;
}
/*
*编译程序主体
*lev:当前分程序所在层
*tx:名字表当前尾指针
*fsys:当前模块的后继符号集合
*/
int block(int lev,int tx,bool*fsys)
{
int i;
int dx; //名字分配到的相对地址
int tx0; //保留初始tx
int cx0; //保留初始cx
/*在下级函数的参数中,符号集合均为值参,但由于使用数组
*实现,传递进来的是指针,为防止下级函数改变上级函数的
*集合,开辟新的空间传递给下级函数
*/
bool nxtlev[symnum];
dx=3;
tx0=tx;//记录本层名字的初始位置
table[tx].adr=cx;
gendo(jmp,0,0);
if(lev>levmax)
{
error(32);
}
do
{
if(sym==constsym)//收到常量声明符号,开始处理常量声明
{
getsymdo;
do
{
constdeclarationdo(&tx,lev,&dx);//dx的值会被constantdeclaration改变,使用指针
while(sym==comma)
{
getsymdo;
constdeclarationdo(&tx,lev,&dx);
}
if(sym==semicolon)
{
getsymdo;
}
else
{
error(5);//漏掉了逗号或分号
}
}while(sym==ident);
}
if(sym==varsym)//收到变量声明的开始符号,开始处理变量声明
{
getsymdo;
do
{
vardeclarationdo(&tx,lev,&dx);
while(sym==comma)
{
getsymdo;
vardeclaration(&tx,lev,&dx);
}
if(sym==semicolon)
{
getsymdo;
}
else
{
error(5);
}
}while(sym==ident);
}
while(sym==procsym)//收到过程声明符号,开始处理过程声明
{
getsymdo;
if(sym==ident)
{
enter(procedure,&tx,lev,&dx);//记录过程名字
getsymdo;
}
else
{
error(4);//procedure后应为标识符
}
if(sym==semicolon)
{
getsymdo;
}
else
{
error(5);//漏掉了分号
}
memcpy(nxtlev,fsys,sizeof(bool)*symnum);
nxtlev[semicolon]=true;
if(-1==block(lev+1,tx,nxtlev))
{
return -1;//递归调用
}
if(sym==semicolon)
{
getsymdo;
memcpy(nxtlev,statbegsys,sizeof(bool)*symnum);
nxtlev[ident]=true;
nxtlev[procsym]=true;
testdo(nxtlev,fsys,6);
}
else
{
error(5);//漏掉了分号
}
}
memcpy(nxtlev,statbegsys,sizeof(bool)*symnum);
nxtlev[ident]=true;
nxtlev[period]=true;
testdo(nxtlev,declbegsys,7);
}while(inset(sym,declbegsys));//直到没有声明符号
code[table[tx0].adr].a=cx;//开始生成过程代码
table[tx0].adr=cx;//当前过程代码的地址
table[tx0].size=dx;/*声明部分中每增加一条声明都会给dx增加1,声明部分
*已经结束,dx就是当前过程数据的size*/
cx0=cx;
gendo(inte,0,dx);//生成分配内存代码
if(tableswitch)//输出名字表
{
printf("TABLE:\n");
if(tx0+1>tx)
{
printf("NULL\n");
}
for(i=tx0+1;i<=tx;i++)
{
switch(table[i].kind)
{
case constant:
printf("%d const %s ",i,table[i].name);
printf("val=%d\n",table[i].val);
fprintf(fas,"%d const %s ",i,table[i].name);
fprintf(fas,"val=%d\n",table[i].val);
break;
case variable:
printf("%d var %s ",i,table[i].name);
printf("lev=%d addr=%d\n",table[i].level,table[i].adr);
fprintf(fas,"%d var %s ",i,table[i].name);
fprintf(fas,"lev=%d addr=%d\n",table[i].level,table[i].adr);
break;
case procedure:
printf("%d proc %s ",i,table[i].name);
printf("lev=%d addr=%d size=%d\n",table[i].level,table[i].adr,table[i].size);
fprintf(fas,"%d proc %s ",i,table[i].name);
fprintf(fas,"lev=%d addr=%d size=%d\n",table[i].level,table[i].adr,table[i].size);
break;
}
}
printf("\n");
}
//语句后继符号为分号或end
memcpy(nxtlev,fsys,sizeof(bool)*symnum);//每个后继符号集合都包含上层后继符号集合
nxtlev[semicolon]=true;
nxtlev[endsym]=true;
statementdo(nxtlev,&tx,lev);
gendo(opr,0,0);//每个过程出口都要使用的释放数据段指令
memset(nxtlev,0,sizeof(bool)*symnum);//分程序没有补救集合
testdo(fsys,nxtlev,8);//检测后继符号的正确性
listcode(cx0);//输出代码
return 0;
}
/*
* 解释程序
*/
void interpret()
{
int b,p,t;//指令指针,指令基址,栈顶指针
struct instruction i;//存放当前指令
int s[stacksize];//栈
printf("start pl0......\n");
t=0;
b=0;
p=0;
s[0]=s[1]=s[2]=0;
do
{
i=code[p];//读当前指令
p++;
switch(i.f)
{
case lit://将a 的值取到栈顶
s[t]=i.a;
t++;
break;
case opr://数学、逻辑运算
switch(i.a)
{
case 0:
t=b;
p=s[t+2];
b=s[t+1];
break;
case 1:
s[t-1]=-s[t-1];
break;
case 2:
t--;
s[t-1]=s[t-1]+s[t];
break;
case 3:
t--;
s[t-1]=s[t-1]-s[t];
break;
case 4:
t--;
s[t-1]=s[t-1]*s[t];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -