扩展PL/0编译程序功能,通过阅读、研究PL/0编译程序源文件,有选择地补充、完善其中词法分析、语法分析、语义分析、目标代码生成、目标代码解释执行等部分的功能。以语法分析部分为例,则可以增加处理更多语法成分的功能,可处理++、--、+=、-=、*=、/=、%(取余)、!(取反)、for、else、处理注释、错误提示。
/*PL/0 编译系统C版本头文件 pl0.h*/
# define norw 20 /*关键字个数*/
# define txmax 100 /*名字表容量*/
# define nmax 14 /*number的最大位数*/
# define al 10 /*符号的最大长度*/
# define amax 2047 /*地址上界*/
# define levmax 3 /*最大允许过程嵌套声明层数[0,lexmax]*/
# define cxmax 200 /*最多的虚拟机代码数*/
/*符号*/
enum symbol{
nul, ident, number, plus, minus,
times, slash, oddsym, eql, neq,
lss, leq, gtr, geq, lparen,
rparen, comma, semicolon,period, becomes,
beginsym, endsym, ifsym, thensym, whilesym,
writesym, readsym, dosym, callsym, constsym,
varsym, procsym, elsesym, forsym, downtosym,
tosym, returnsym, repeatsym, untilsym, pluseq,
inc, minueq, dec, arrayp,timeseq,slasheq,
};
#define symnum 46
/*-------------*/
enum object{
constant,
variable,
procedur,
array,
};
/*--------------*/
enum fct{
lit, opr, lod, sto, cal, inte, jmp, jpc,
};
#define fctnum 8
/*--------------*/
struct instruction
{
enum fct f;
int l;
int a;
};
FILE * fas;
FILE * fa;
FILE * fa1;
FILE * fa2;
bool tableswitch;
bool listswitch;
char ch;
enum symbol sym;
char id[al+1];
int num;
int cc,ll;
int cx;
int sum;
char line[81];
char a[al+1];
struct instruction code[cxmax];
char word[norw][al];
enum symbol wsym[norw];
enum symbol ssym[256];
char mnemonic[fctnum][5];
bool declbegsys[symnum];
bool statbegsys[symnum];
bool facbegsys[symnum];
/*------------------------------*/
struct tablestruct
{
char name[al]; /*名字*/
enum object kind; /*类型:const,var,array or procedure*/
int val; /*数值,仅const使用*/
int level; /*所处层,仅const不使用*/
int adr; /*地址,仅const不使用*/
int size; /*需要分配的数据区空间,仅procedure使用*/
};
struct tablestruct table[txmax]; /*名字表*/
FILE * fin;
FILE* fout;
char fname[al];
int err; /*错误计数器*/
/*当函数中会发生fatal error时,返回-1告知调用它的函数,最终退出程序*/
#define getsymdo if(-1==getsym())return -1
#define getchdo if(-1==getch())return -1
#define testdo(a,b,c) if(-1==test(a,b,c))return -1
#define gendo(a,b,c) if(-1==gen(a,b,c))return -1
#define expressiondo(a,b,c) if(-1==expression(a,b,c))return -1
#define factordo(a,b,c) if(-1==factor(a,b,c))return -1
#define termdo(a,b,c) if(-1==term(a,b,c))return -1
#define conditiondo(a,b,c) if(-1==condition(a,b,c))return -1
#define statementdo(a,b,c) if(-1==statement(a,b,c))return -1
#define constdeclarationdo(a,b,c) if(-1==constdeclaration(a,b,c))return -1
#define vardeclarationdo(a,b,c) if(-1==vardeclaration(a,b,c))return -1
void error(int n);
int getsym();
int getch();
void init();
int gen(enum fct x,int y,int z);
int test(bool*s1,bool*s2,int n);
int inset(int e,bool*s);
int addset(bool*sr,bool*s1,bool*s2,int n);
int subset(bool*sr,bool*s1,bool*s2,int n);
int mulset(bool*sr,bool*s1,bool*s2,int n);
int block(int lev,int tx,bool* fsys);
void interpret();
int factor(bool* fsys,int* ptx,int lev);
int term(bool*fsys,int*ptx,int lev);
int condition(bool*fsys,int*ptx,int lev);
int expression(bool*fsys,int*ptx,int lev);
int statement(bool*fsys,int*ptx,int lev);
void listcode(int cx0);
int vardeclaration(int* ptx,int lev, int* pdx);
int constdeclaration(int* ptx,int lev, int* pdx);
int position(char* idt,int tx);
void enter(enum object k,int* ptx,int lev,int* pdx);
int base(int l,int* s,int b);
*使用方法:
*运行后输入PL/0 源程序文件名
*回答是否输出虚拟机代码
*回答是否输出名字表
*fa.tmp 输出虚拟机代码
*fa1.tmp 输出源文件及其各行对应的首地gen(lodmode[jk],lev-table[i].vp.level,table[i].vp.ADR); //读取此变量的数,放入栈顶址
*fa2.tmp 输出结果
*fas.tmp 输出名字表
*/
#include<stdio.h>
#include"pl0.h"
#include"string.h"
#include <stdlib.h>
/*解释执行时使用的栈*/
#define stacksize 500
int main()
{
bool nxtlev[symnum];
printf("Input pl/0 file ?");
scanf("%s",fname); /*输入文件名*/
sum=0;
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,"Iput pl/0 file ?");
fprintf(fa1,"%s\n", fname);
init(); /*初始化*/
err=0;
cc=cx=ll=0;
ch=' ';
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)
{
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");
system("pause");
return 0;
}
/*
*初始化
*/
void init()
{
int i;
for(i=0;i<=255;i++)
{
ssym[i]=nul;
}
ssym['+']=plus;
ssym['-']=minus;
ssym['*']=times;
ssym['/']=slash;
ssym['(']=lparen;
ssym[')']=rparen;
ssym['=']=eql;
ssym[',']=comma;
ssym['.']=period;//
ssym['#']=neq;
ssym[';']=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]),"downto");//增加downto
strcpy(&(word[5][0]),"else");//增加else
strcpy(&(word[6][0]),"end");
strcpy(&(word[7][0]),"for");//增加for
strcpy(&(word[8][0]),"if");
strcpy(&(word[9][0]),"odd");
strcpy(&(word[10][0]),"procedure");
strcpy(&(word[11][0]),"read");
strcpy(&(word[12][0]),"repeat");//增加repeat
strcpy(&(word[13][0]),"return");//增加return
strcpy(&(word[14][0]),"then");
strcpy(&(word[15][0]),"to");//增加to
strcpy(&(word[16][0]),"until");//增加until
strcpy(&(word[17][0]),"var");
strcpy(&(word[18][0]),"while");
strcpy(&(word[19][0]),"write");
/*设置保留字符号*/
wsym[0]=beginsym;
wsym[1]=callsym;
wsym[2]=constsym;
wsym[3]=dosym;
wsym[4]=downtosym;
wsym[5]=elsesym;
wsym[6]=endsym;
wsym[7]=forsym;
wsym[8]=ifsym;
wsym[9]=oddsym;
wsym[10]=procsym;
wsym[11]=readsym;
wsym[12]=repeatsym;
wsym[13]=returnsym;
wsym[14]=thensym;
wsym[15]=tosym;
wsym[16]=untilsym;
wsym[17]=varsym;
wsym[18]=whilesym;
wsym[19]=writesym;//
/*设置指令名称*/
strcpy(&(mnemonic[lit][0]),"lit");
strcpy(&(mnemonic[opr][0]),"opr");
strcpy(&(mnemonic[lod][0]),"lod");
strcpy(&(mnemonic[sto][0]),"sto");
strcpy(&(mnemonic[cal][0]),"cal");
strcpy(&(mnemonic[inte][0]),"int");
strcpy(&(mnemonic[jmp][0]),"jmp");
strcpy(&(mnemonic[jpc][0]),"jpc");
/*设置符号集*/
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[forsym]=true;
statbegsys[repeatsym]=true;
statbegsys[whilesym]=true;
/*设置因子开始符号集*/
facbegsys[ident]=true;
facbegsys[number]=true;
facbegsys[lparen]=true;
facbegsys[inc]=true;
facbegsys[dec]=true;
}
/*
*用数组实现集合的集合运算
*/
int inset(int e,bool* s)
{
return s[e];
}
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;
}
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;
}
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;
}
/*
*出错处理,打印出错位置和错误编码
*/
void error(int n)
{
char space[81];
memset(space,32,81); printf("-------%c\n",ch);
space[cc-1]=0;//出错时当前符号已经读完,所以cc-1
printf("****%s!%d\n",space,n);
err++;
}
/*
* 漏掉空格,读取一个字符
*
* 每次读一行,存入line缓冲区,line被getsym取空后再读一行
*
* 被函数getsym调用
*/
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=' ';
while(ch!=10)
{
//fscanf(fin,"%c",&ch)
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;
}
/*词法分析,获取一个符号
*/
int getsym()
{
int i,j,k;
while( ch==' '||ch==10||ch==9)
{
getchdo;
}
//......................................................................
if(ch=='[')//如果是‘[’则认为是数组]
{
getchdo;//getch()是一个读单词的函数,如果能在缓冲区里读入一个单词,那么返回值不是-1,当读入失败,返回-1,getchdo被直接定义,用它能直接调用getch(),并且知道结果。
getsymdo;
sum=num;//把getsym取出的数字赋给sum,作为数组的容量
if(ch==']')//维数后的应该是‘]’
{
getchdo;
sym=arrayp;
}
else
{
error(33);//如果没有‘]’报错
}
if(sum<1) error(34);//数组容量不能为0
}
else
//......................................................