编译器的代码生成解析Word格式文档下载.docx
《编译器的代码生成解析Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《编译器的代码生成解析Word格式文档下载.docx(56页珍藏版)》请在冰点文库上搜索。
true,
}bool;
*/
#definenorw20/*关键字个数*/
#definetxmax100/*名字表容量*/
#definenmax14/*number的最大位数*/
#defineal10/*符号的最大长度*/
#defineamax2047/*地址上界*/
#definelevmax3/*最大允许过程嵌套声明层数[0,lexmax]*/
#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,elsesym,forsym,tosym,
downtosym,returnsym,pluseql,minuseql,plusplus,
minusminus,repeatsym,dowhilesym,
};
#definesymnum43
/*-------------*/
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;
booltableswitch;
boollistswitch;
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;
/*类型:
const,var,arrayorprocedure*/
intval;
/*数值,仅const使用*/
intlevel;
/*所处层,仅const不使用*/
intadr;
/*地址,仅const不使用*/
intsize;
/*需要分配的数据区空间,仅procedure使用*/
structtablestructtable[txmax];
/*名字表*/
FILE*fin;
FILE*fout;
charfname[al];
interr;
/*错误计数器*/
/*当函数中会发生fatalerror时,返回-1告知调用它的函数,最终退出程序*/
#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);
PL0.cpp:
/*
*使用方法:
*运行后输入PL/0源程序文件名
*回答是否输出虚拟机代码
*回答是否输出名字表
*fa.tmp输出虚拟机代码
*fa1.tmp输出源文件及其各行对应的首地址
*fa2.tmp输出结果
*fas.tmp输出名字表
#include<
stdio.h>
#include"
pl0.h"
string.h"
#include<
cstdio>
/*解释执行时使用的栈*/
#definestacksize500
intmain()
intc;
boolnxtlev[symnum];
printf("
Inputpl/0file?
"
);
scanf("
%s"
fname);
/*输入文件名*/
fin=fopen(fname,"
r"
if(fin)
{
printf("
Listobjectcode?
(Y/N)"
/*是否输出虚拟机代码*/
scanf("
listswitch=(fname[0]=='
y'
||fname[0]=='
Y'
Listsymboltable?
(Y/N)"
/*是否输出名字表*/
tableswitch=(fname[0]=='
fa1=fopen("
fa1.txt"
"
w"
fprintf(fa1,"
Iputpl/0file?
%s\n"
fname);
init();
/*初始化*/
err=0;
cc=cx=ll=0;
ch='
'
;
if(-1!
=getsym())
{
fa=fopen("
fa.txt"
fas=fopen("
fas.txt"
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);
}
fclose(fin);
}
else
Can'
topenfile!
\n"
while((c=getchar())!
='
\n'
&
&
c!
=EOF);
//清空输入流
clearerr(stdin);
//清除流的错误标记
getchar();
//等待用户输入回车
return0;
}
*初始化
voidinit()
inti;
for(i=0;
i<
=255;
i++)
ssym[i]=nul;
ssym['
+'
]=plus;
-'
]=minus;
*'
]=times;
/'
]=slash;
('
]=lparen;
)'
]=rparen;
='
]=eql;
'
]=comma;
.'
]=period;
//ssym['
#'
]=neq;
//注释掉不等号
'
]=semicolon;
/*设置保留字名字,按照字母顺序,便于折半查找*/
strcpy(&
(word[0][0]),"
begin"
(word[1][0]),"
call"
(word[2][0]),"
const"
(word[3][0]),"
do"
(word[4][0]),"
dowhile"
//增加dowhile
(word[5][0]),"
downto"
/*增加downto*/
(word[6][0]),"
else"
/*增加else*/
strcpy(&
(word[7][0]),"
end"
(word[8][0]),"
for"
/*增加for*/
(word[9][0]),"
if"
(word[10][0]),"
odd"
(word[11][0]),"
procedure"
(word[12][0]),"
read"
(word[13][0]),"
repeat"
//增加repeat
(word[14][0]),"
return"
/*增加return*/
(word[15][0]),"
then"
(word[16][0]),"
to"
/*增加to*/
(word[17][0]),"
var"
(word[18][0]),"
while"
(word[19][0]),"
write"
/*设置保留字符号*/
wsym[0]=beginsym;
wsym[1]=callsym;
wsym[2]=constsym;
wsym[3]=dosym;
wsym[4]=dowhilesym;
/*增加dowhilesym*/
wsym[5]=downtosym;
/*增加downtosym*/
wsym[6]=elsesym;
/*增加elsesym*/
wsym[7]=endsym;
wsym[8]=forsym;
/*增加forsym*/
wsym[9]=ifsym;
wsym[10]=oddsym;
wsym[11]=procsym;
wsym[12]=readsym;
wsym[13]=repeatsym;
//增加repeatsym
wsym[14]=returnsym;
/*增加returnsym*/
wsym[15]=thensym;
wsym[16]=tosym;
/*增加tosym*/
wsym[17]=varsym;
wsym[18]=whilesym;
wsym[19]=writesym;
/*设置指令名称*/
(mnemonic[lit][0]),"
lit"
(mnemonic[opr][0]),"
opr"
(mnemonic[lod][0]),"
lod"
(mnemonic[sto][0]),"
sto"
(mnemonic[cal][0]),"
cal"
(mnemonic[inte][0]),"
int"
(mnemonic[jmp][0]),"
jmp"
(mnemonic[jpc][0]),"
jpc"
/*设置符号集*/
symnum;
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;
statbegsys[repeatsym]=true;
//增加repeat为语句开始符号
/*设置因子开始符号集*/
facbegsys[ident]=true;
facbegsys[number]=true;
facbegsys[lparen]=true;
/*
*用数组实现集合的集合运算
*/
intinset(inte,bool*s)
returns[e];
intaddset(bool*sr,bool*s1,bool*s2,intn)
n;
sr[i]=s1[i]||s2[i];
intsubset(bool*sr,bool*s1,bool*s2,intn)
sr[i]=s1[i]&
(!
s2[i]);
intmulset(bool*sr,bool*s1,bool*s2,intn)
s2[i];
}
*出错处理,打印出错位置和错误编码
voiderror(intn)
charspace[81];
memset(space,32,81);
-------%c\n"
ch);
space[cc-1]=0;
//出错时当前符号已经读完,所以cc-1
****%s!
%d\n"
space,n);
err++;
*漏掉空格,读取一个字符
*
*每次读一行,存入line缓冲区,line被getsym取空后再读一行
*被函数getsym调用
intgetch()
if(cc==ll)
if(feof(fin))
printf("
programincomplete"
return-1;
ll=0;
cc=0;
%d"
cx);
cx);
while(ch!
=10)
//fscanf(fin,"
%c"
&
ch)
if(EOF==fscanf(fin,"
ch))
line[ll]=0;
break;
fprintf(fa1,"
line[ll]=ch;
ll++;
ch=line[cc];
cc++;
/*词法分析,获取一个符号
intgetsym()
inti,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>
||ch>
0'
9'
a[k]=0;
strcpy(id,a);
i=0;
j=norw-1;
k=(i+j)/2;
if(strcmp(id,word[k])<
=0)
j=k-1;
if(strcmp(id,word[k])>
i=k+1;
}while(i<
=j);
if(i-1>
j)
sym=wsym[k];
else
sym=ident;
if(ch>
k=0;
num=0;
sym=number;
do{
num=10*num+ch-'
getchdo;
}while(ch>
/*获取数字的值*/
k--;
if(k>
nmax)
error(30);
if(ch=='
:
)/*检测赋值符号*/
if(ch=='
{
sym=becomes;