用C语言写的PL0编译器(《编译原理第二版》清华大学出版社)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

/*测试用的程序:consta=10;varb,c;procedurep;beginc:=b+aend;beginread(b);whileb#0dobegincallp;write(2*c);read(b);endend.*/#includestdio.h/*#includepl0.h*/#includestring.h/*PL/0编译系统C版本头文件pl0.h*//*typedefenum{false,true}bool;*/#definenorw13#definetxmax100#definenmax14#defineal10#defineamax2047#definelevmax3#definecxmax200enumsymbol{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,};#definesymnum32enumobject{constant,variable,procedur,};enumfct{lit,opr,lod,sto,cal,inte,jmp,jpc,};#definefctnum8structinstruction{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-1voiderror(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);#definestacksize500intmain(){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;isymnum;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;in;i++){sr[i]=s1[i]||s2[i];}return0;}intsubset(bool*sr,bool*s1,bool*s2,intn){inti;for(i=0;in;i++){sr[i]=s1[i]&&(!s2[i]);}return0;}intmulset(bool*sr,bool*s1,bool*s2,intn){inti;for(i=0;in;i++){sr[i]=s1[i]&&s2[i];}return0;}voiderror(intn){charspace[81];memset(space,32,81);space[c

1 / 32
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功