编译器的代码生成解析Word格式文档下载.docx

上传人:b****4 文档编号:8065595 上传时间:2023-05-10 格式:DOCX 页数:56 大小:58.71KB
下载 相关 举报
编译器的代码生成解析Word格式文档下载.docx_第1页
第1页 / 共56页
编译器的代码生成解析Word格式文档下载.docx_第2页
第2页 / 共56页
编译器的代码生成解析Word格式文档下载.docx_第3页
第3页 / 共56页
编译器的代码生成解析Word格式文档下载.docx_第4页
第4页 / 共56页
编译器的代码生成解析Word格式文档下载.docx_第5页
第5页 / 共56页
编译器的代码生成解析Word格式文档下载.docx_第6页
第6页 / 共56页
编译器的代码生成解析Word格式文档下载.docx_第7页
第7页 / 共56页
编译器的代码生成解析Word格式文档下载.docx_第8页
第8页 / 共56页
编译器的代码生成解析Word格式文档下载.docx_第9页
第9页 / 共56页
编译器的代码生成解析Word格式文档下载.docx_第10页
第10页 / 共56页
编译器的代码生成解析Word格式文档下载.docx_第11页
第11页 / 共56页
编译器的代码生成解析Word格式文档下载.docx_第12页
第12页 / 共56页
编译器的代码生成解析Word格式文档下载.docx_第13页
第13页 / 共56页
编译器的代码生成解析Word格式文档下载.docx_第14页
第14页 / 共56页
编译器的代码生成解析Word格式文档下载.docx_第15页
第15页 / 共56页
编译器的代码生成解析Word格式文档下载.docx_第16页
第16页 / 共56页
编译器的代码生成解析Word格式文档下载.docx_第17页
第17页 / 共56页
编译器的代码生成解析Word格式文档下载.docx_第18页
第18页 / 共56页
编译器的代码生成解析Word格式文档下载.docx_第19页
第19页 / 共56页
编译器的代码生成解析Word格式文档下载.docx_第20页
第20页 / 共56页
亲,该文档总共56页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

编译器的代码生成解析Word格式文档下载.docx

《编译器的代码生成解析Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《编译器的代码生成解析Word格式文档下载.docx(56页珍藏版)》请在冰点文库上搜索。

编译器的代码生成解析Word格式文档下载.docx

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;

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 高等教育 > 军事

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2