您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 招聘面试 > 标准实验报告(PL0编译程序)
1[标准实验报告]南昌航空大学实验报告年月日课程名称:编译原理实验名称:扩充的PL/0编译程序班级:姓名:同组人:指导教师评定:签名:一、实验目的进一步熟悉编译程序的整体框架,给出整个编译程序的流程结构,用C或vc++语言编写程序;并将编制的编译程序进行调试、实现PL/0编译程序。二、实验要求(1)根据所选的程序设计语言,修改并调试。(2)举出例子程序,在程序中进行编译。(3)用此编译程序对有关语句进行编译,并输出目标指令。(4)书写出合格的实验报告。三、实验步骤1.输入文件中变量的声明形如:vara,b,c;以var保留字开始,不同变量以”,”分隔,最后以”;”结束。2.read语句格式为read(a)或者read(a,b);3.write语句格式为write(a),括号里面没有字符串常量,注意与书后的形如write(‘a=’,a)是不相同的。4.的声明形如:”procedureproname;”不含参数表。5.一维数组形如:变量类型array数组名[数组下标]。6.条件语句形如:if条件then语句{;else,语句}7.扩充的记录型数据类型形如:for循环语句及带参数的过程。四、参考源代码#includestdio.h/*#includeplo.h*/#includestring.h#definestacksize500typedefenum{false,true}bool;2enumobject{procedur,constant,variable};#definenorw13#definetxmax100#definenmax14#defineal10#definelevmax3#definecxmax200#defineamax2047#definesymnum32enumsymbol{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,};enumfct{lit,opr,lod,sto,cal,inte,jmp,jpc,};#definefctnum8structinstruction{enumfctf;intl;inta;};FILE*fas;FILE*fa;FILE*fal;FILE*fa2;boollistswitch;booltableswitch;3charch;enumsymbolsym;charid[al+1];intnum;intcc,ll;intcx;charline[81];chara[al+1];intnum;intcc,ll;intcx;charline[81];chara[al+1];structinstructioncode[cxmax];charword[norw][al];structinstructioncode[cxmax];charword[norw][al];enumsymbolwsym[norw];enumsymbolssym[256];charmnemonic[fctnum][5];booldeclbegsys[symnum];boolstatbegsys[symnum];boolfacbegsys[symnum];boolfacstatbegsys[symnum];structtablestruct{charname[al];enumobjectkind;intval;intlevel;intadr;intsize;};structtablestructtable[txmax];FILE*fin;FILE*fout;charfname[al];interr;4#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*sl,bool*s2,intn);intinset(inte,bool*s);intaddset(bool*sr,bool*sl,bool*s2,intn);intsubset(bool*sr,bool*sl,bool*s2,intn);intmulset(bool*sr,bool*sl,bool*s2,intn);intblock(intlev,inttx,bool*fsys);voidinterpret();intfactor(bool*fays,int*ptx,intlev);intterm(bool*fays,int*ptx,intlev);intcondition(bool*fays,int*ptx,intlev);intexpression(bool*fays,int*ptx,intlev);intstatement(bool*fays,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);intmain(){boolnxtlev[symnum];printf(inputpl/ofile?);5scanf(%s,fname);fin=fopen(fname,r);if(fin){printf(listobjeckcode?(Y/N));scanf(%s,fname);/*listwitch=(fname[0]=='y'||fname[0]=='Y');*/printf(listsymboltable?(Y/N));scanf(%s,fname);tableswitch=(fname[0]=='y'||fname[0]=='Y');fal=fopen(fal.tmp,w);fprintf(fal,inputpl/ofile?);fprintf(fal,%s,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(fal);fclose(fas);fclose(fin);printf(\n);return0;}fclose(fa);fclose(fal);fclose(fas);if(sym!=period){error(9);}if(err==0){fa2=fopen(fa2.tmp,w);6interpret();fclose(fa2);}else{printf(errorinpl/oprogram);}}fclose(fin);}else{printf(can'topenfile!\n);}printf(\n);return0;}/*chushihua*/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);7strcpy(&(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]),inte);strcpy(&(mnemonic[jmp][0]),jmp);strcpy(&(mnemonic[jpc][0]),jpc);for(i=0;isymnum;i++){declbegsys[i]=false;statbegsys[i]=false;8facbegsys[i]=false;}declbegsys[constsym]=true;declbegsys[varsym]=true;declbegsys[procsym]=true;statbegsys[beginsym]=true;
本文标题:标准实验报告(PL0编译程序)
链接地址:https://www.777doc.com/doc-6018264 .html