最新用C语言写的PL0编译器《编译原理第二版》清华大学出版社.docx
《最新用C语言写的PL0编译器《编译原理第二版》清华大学出版社.docx》由会员分享,可在线阅读,更多相关《最新用C语言写的PL0编译器《编译原理第二版》清华大学出版社.docx(48页珍藏版)》请在冰豆网上搜索。
![最新用C语言写的PL0编译器《编译原理第二版》清华大学出版社.docx](https://file1.bdocx.com/fileroot1/2023-2/17/7aae4b36-9fcb-4e2c-80f8-f6d00a33e975/7aae4b36-9fcb-4e2c-80f8-f6d00a33e9751.gif)
最新用C语言写的PL0编译器《编译原理第二版》清华大学出版社
/*
测试用的程序:
consta=10;
varb,c;
procedurep;
begin
c:
=b+a
end;
begin
read(b);
whileb#0do
begin
callp;write(2*c);read(b);
end
end.
*/
#include
/*#include"pl0.h"*/
#include"string.h"
/*PL/0编译系统C版本头文件pl0.h*/
/*typedefenum{false,true}bool;*/
#definenorw13
#definetxmax100
#definenmax14
#defineal10
#defineamax2047
#definelevmax3
#definecxmax200
enumsymbol{
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,
};
#definesymnum32
enumobject{
constant,
variable,
procedur,
};
enumfct{
lit,opr,lod,sto,cal,inte,jmp,jpc,
};
#definefctnum8
structinstruction
{
enumfctf;
intl;
inta;
};
FILE*fas;
FILE*fa;
FILE*fa1;
FILE*fa2;
boollistswitch;
booltableswitch;
charch;
enumsymbolsym;
charid[al+1];
intnum;
intcc,ll;
intcx;
charline[81];
chara[al+1];
structinstructioncode[cxmax];
charword[norw][al];
enumsymbolwsym[norw];
enumsymbolssym[256];
charmnemonic[fctnum][5];
booldeclbegsys[symnum];
boolstatbegsys[symnum];
boolfacbegsys[symnum];
structtablestruct
{
charname[al];
enumobjectkind;
intval;
intlevel;
intadr;
intsize;
};
structtablestructtable[txmax];
FILE*fin;
FILE*fout;
charfname[al];
interr;
#definegetsymdoif(-1==getsym())return-1
#definegetchdoif(-1==getch())return-1
#definetestdo(a,b,c)if(-1==test(a,b,c))return-1
#definegendo(a,b,c)if(-1==gen(a,b,c))return-1
#defineexpressiondo(a,b,c)if(-1==expression(a,b,c))return-1
#definefactordo(a,b,c)if(-1==factor(a,b,c))return-1
#definetermdo(a,b,c)if(-1==term(a,b,c))return-1
#defineconditiondo(a,b,c)if(-1==condition(a,b,c))return-1
#definestatementdo(a,b,c)if(-1==statement(a,b,c))return-1
#defineconstdeclarationdo(a,b,c)if(-1==constdeclaration(a,b,c))return-1
#definevardeclarationdo(a,b,c)if(-1==vardeclaration(a,b,c))return-1
voiderror(intn);
intgetsym();
intgetch();
voidinit();
intgen(enumfctx,inty,intz);
inttest(bool*s1,bool*s2,intn);
intinset(inte,bool*s);
intaddset(bool*sr,bool*s1,bool*s2,intn);
intsubset(bool*sr,bool*s1,bool*s2,intn);
intmulset(bool*sr,bool*s1,bool*s2,intn);
intblock(intlev,inttx,bool*fsys);
voidinterpret();
intfactor(bool*fsys,int*ptx,intlev);
intterm(bool*fsys,int*ptx,intlev);
intcondition(bool*fsys,int*ptx,intlev);
intexpression(bool*fsys,int*ptx,intlev);
intstatement(bool*fsys,int*ptx,intlev);
voidlistcode(intcx0);
intvardeclaration(int*ptx,intlev,int*pdx);
intconstdeclaration(int*ptx,intlev,int*pdx);
intposition(char*idt,inttx);
voidenter(enumobjectk,int*ptx,intlev,int*pdx);
intbase(intl,int*s,intb);
#definestacksize500
intmain()
{
boolnxtlev[symnum];
printf("Inputpl/0file?
");
scanf("%s",fname);
fin=fopen(fname,"r");
if(fin)
{
printf("Listobjectcode?
(Y/N)");
scanf("%s",fname);
listswitch=(fname[0]=='y'||fname[0]=='Y');
printf("Listsymboltable?
(Y/N)");
scanf("%s",fname);
tableswitch=(fname[0]=='y'||fname[0]=='Y');
fa1=fopen("fa1.tmp","w");
fprintf(fa1,"Inputpl/0file?
");
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");
return0;
}
fclose(fa);
fclose(fa1);
fclose(fas);
if(sym!
=period)
{
error(9);
}
if(err==0)
{
fa2=fopen("fa2.tmp","w");
interpret();
fclose(fa2);
}
else
{
printf("Errorsinpl/0program");
}
}
fclose(fin);
}
else
{
printf("Can'topenfile!
\n");
}
printf("\n");
return0;
}
voidinit()
{
inti;
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]),"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]=beginsym;
wsym[1]=callsym;
wsym[2]=constsym;
wsym[3]=dosym;
wsym[4]=endsym;
wsym[5]=ifsym;
wsym[6]=oddsym;
wsym[7]=procsym;
wsym[8]=readsym;
wsym[9]=thensym;
wsym[10]=varsym;
wsym[11]=whilesym;
wsym[12]=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{
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;
}
intinset(inte,bool*s)
{
returns[e];
}
intaddset(bool*sr,bool*s1,bool*s2,intn)
{
inti;
for(i=0;i{
sr[i]=s1[i]||s2[i];
}
return0;
}
intsubset(bool*sr,bool*s1,bool*s2,intn)
{
inti;
for(i=0;i{
sr[i]=s1[i]&&(!
s2[i]);
}
return0;
}
intmulset(bool*sr,bool*s1,bool*s2,intn)
{
inti;
for(i=0;i{
sr[i]=s1[i]&&s2[i];
}
return0;
}
voiderror(intn)
{
charspace[81];
memset(space,32,81);
space[cc-1]=0;
printf("****%s!
%d\n",space,n);
fprintf(fa1,"****%s!
%d\n",space,n);
err++;
}
intgetch()
{
if(cc==ll)
{
if(feof(fin))
{
printf("programincomplete");
return-1;
}
ll=0;
cc=0;
printf("%d",cx);
fprintf(fa1,"%d",cx);
ch='';
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++;
return0;
}
intgetsym()
{
inti,j,k;
while(ch==''||ch==10||ch==9)
{
getchdo;
}
if(ch>='a'&&ch<='z')
{
k=0;
do{
if(k{
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=ident;
}
}
else
{
if(ch>='0'&&ch<='9')
{
k=0;
num=0;
sym=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=becomes;
getchdo;
}
else
{
sym=nul;
}
}
else
{
if(ch=='<')
{
getchdo;
if(ch=='=')
{
sym=leq;
getchdo;
}
else
{
sym=lss;
}
}
else
{
if(ch=='>')
{
getchdo;
if(ch=='=')
{
sym=geq;
getchdo;
}
else
{
sym=gtr;
}
}
else
{
sym=ssym[ch];
if(sym!
=period)
{
getchdo;
}
}
}
}
}
}
return0;
}
intgen(enumfctx,inty,intz)
{
if(cx>=cxmax)
{
printf("Programtoolong");
return-1;
}
code[cx].f=x;
code[cx].l=y;
code[cx].a=z;
cx++;
return0;
}
inttest(bool*s1,bool*s2,intn)
{
if(!
inset(sym,s1))
{
error(n);
while((!
inset(sym,s1))&&(!
inset(sym,s2)))
{
getsymdo;
}
}
return0;
}
intblock(intlev,inttx,bool*fsys)
{
inti;
intdx;
inttx0;
intcx0;
boolnxtlev[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);
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;
vardeclarationdo(&tx,lev,&dx);
}
if(sym==semicolon)
{
getsymdo;
}
else
{
error(5);
}
}while(sym==ident);
}
while(sym==procsym)
{
getsymdo;
if(sym==ident)
{
enter(procedur,&tx,lev,&dx);
getsymdo;
}
else
{
error(4);
}
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,dec