《编译原理实验报告--PL-0编译程序的修改.docx》由会员分享,可在线阅读,更多相关《编译原理实验报告--PL-0编译程序的修改.docx(11页珍藏版)》请在优知文库上搜索。
1、实验报告课程名称_编译原理题目名称PL/O编译程序的修改一学牛学院一计算机学院.专W班级一J2级软件4班一、基本内容(成绩范围:“中”、“及格”或“不及格”)对PL/O作以下修改扩充:(1)增力口单词:保留字ELSE,FOR,STEP,UNTIL,DO,RETURN运算符*=,/=,&,9!(2)修改单词:不等号#改为(3)增加条件语句的ELSE子句,要求:写出相关文法,语法图,语义规则。二、设计思路1.扩充单词1)修改变量定义a)先修改保留字的个数ConStNORW=19;b)修改枚举变量SYMBOL的个数为43typedefenumNUL,IDENT,NUMBER,PLUS,MINUS,T
2、IMES,Timesequal,slashequal,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,PROGSYM,ELSESYM,FORSYM,STEPSYM,UNTILSYM,RETURNSYM,AND,OR,NOTSYMBOL;c)修改SYMoUT数组的个数为43char*SYMOUT=
3、nNUL,1DENT,nNUMBERn,PLUSu,MINUS;TIMESVtTIMESEQUAL,nSLASHEQUALn,nSLASH,ODDSYM,EQL,uNEQ,nLSS,LEQ,GTR,hGEQm,nLPARENn,RPAREN,nC0MMA,SEMICOLONm,PER10Dh,“BECOMES“,BEGlNSYM“,ENDSYM“,IFSYMTTHENSYM”,nWHILESYMu,WRITESYM,nREADSYMn,DOSYM,nCALLSYM,CONSTSYMh,mVARSYMm,PROCSYM11,mPROGSYM11,elsesymhmFORSYMh,STEPSYM,
4、mUNTILSYMm,mRETURNSYMm,andm,11orm,notm1;d)增加关键字和保留字strcpy(KWORDll,BEGlN);strcpy(KWORD2,CALL);strcpy(KWORD3,“CONST);strcpy(KWORD4,DO);strcpy(KWORD5,ELSE);strcpy(KWORD6J,END);StrCPy(KWoRD7JFOR);strcpy(KWORD8,IF);StrcPy(KWoRD9,“ODD”);strcpy(KWORDl0,PROCEDURE);strcpy(KWORD11,PR0GRAMu);StrCPy(KWORDH2,“RE
5、AD”);StrCPy(KWORD13JRETURN);strcpy(KWORD14,STEP);strcpy(KWORD15,THENu);StrCPy(KWoRD16JUNTlL);strcpy(KWORD17,VAR);strcpy(KWORD18,WHILE);strcpy(KWORD19,WRITE);WSYM1=BEGINSYM;WSYM2=CALLSYM;WSYM3=C0NSTSYM;WSYM4=D0SYM;WSYM5=ELSESYM;WSYM6=ENDSYM;WSYM7=FORSYM;WSYM8=IFSYM;WSYM9=ODDSYM;WSYM10=PROCSYM;WSYM11=
6、PROGSYM;WSYM12=READSYM;WSYM13J=RETURNSYM;WSYM14=STEPSYM;WSYM15=THENSYM;WSYM16=UNTILSYM;WSYM17=VARSYM;WSYM18=WHILESYM;WSYM19=WRITESYM;e.在STATEMENT方法中添加caseELSESYM:FOrmlprintfs(读取到ELSE*);break;caseFORSYM:FOrmIprintfsr读取到FOR,);break;caseSTEPSYM:FQrmlprintfs(”读取到STEPh);break;caseUNTILSYM:FormIpring(读取到U
7、NTILm);break;caseRETURNSYM:FOrmlpri11fs(读取到RETURN”);break;caseTIMESEQUAL:FormLprintfs(读取到*=”);break;caseSLASHEQUAL:Formlprintfs读取到/=);break;caseAND:FormIprintfs(“读取到&”);break;caseOR:FOrmlprintfsC读取到r);break;caseNOT:FOrmlprintfsC读取到NOT);break;2)修改GetSym()方法voidGetSym()inti,J,K;ALFAA;while(CH=,A&CH=,Z
8、,)*IDORRESERVEDWORD*/K=O;doif(K=,A,&CH=,O&CH=9);AK=,O,;strcpy(ID,A);i=l;J=NORW;doK=(i+J)/2;if(strcmp(ID,KWORDK)=0)i=K+1;while(iJ)SYM=WSYMK;if(SYM=ELSESYM)Forml-printfs(,ELSEM);If(SYM=FORSYM)Forml-printfs(,FORM);If(SYM=STEPSYM)Forml-printfs(MSTEPM);If(SYM=UNTILSYM)Forml-printfs(MUNTILM);if(SYM=RETURN
9、SYM)FOnnlpriItfSrRETURN);)elseSYM=IDENT;elseif(CH=,O,&CH=,O&CHNMAX)Error(30);elseif(CH=:)GetCh();if(CH=)SYM=BECOMES;GetCh();elseSYM=NUL;)else*THEFOLLOWINGTWOCHECKWEREADDEDBECAUSEASCIIDOESNOTHAVEASINGLECHARACTERFOR=*/if(CH=,)SYM=NEQ;GetCh();elseSYM=LSS;elseif(CH=,)GetCh();if(CH=1)SYM=GEQ;GetCh();else
10、SYM=GTR;if(CH=,*,)添加运算符*=GetCh();if(CH=,=t)SYM=TIMESEQUAL;GetCh();elseSYM=TIMES;if(CH=7,)GetCh();添加运算符/=if(CH=,=t)SYM=SLASHEQUAL,GetCh();)elseSYM=SLASH;if(CH=,)SYM=AND,GetCh();添加运算符&if(CH=T)GetCh();if(CH=T)SYM=OR,GetCh();添加运算符elseSYM=NUL;添加运算符!if(CH=,!t)SYM=NOT,GetCh();elseSYM=SSYMCH;GetCh();/*GetSy
11、m()*/2 .修改单词:不等号#改为a.在文件中的设置单字符符号的部分,将SSym=NEQ删除。b.在GetSym()方法中elseif(CH=:)GetCh();if(CH=,三,)(SYM=BECOMES;GetCh();JelseSYM=NUL;1else*THEFOLLOWINGTWOCHECKWEREADDEDBECAUSEASCIIDOESNOTHAVEASINGLECHARACTERFOR=if(CH=,)SYM=NEQ;GetCh();elseSYM=LSS;)elseif(CH=)GetCh();if(CH=,=)SYM=GEQ;GetCh();)elseSYM=GTR;3
12、 .增加条件语句的ELSE子句1)语法图2)修改STATEMENTO方法caseIFSYM:GetSyniO;CoNDmoN(SymSetUni。Il(SymSetNeW(THENSYM,DOSYM),FSYS),LEVTX);If(SYM=THENSYM)GetSym();elseError(16);CXl=CX;GEN(JPC,0,0);STATEMENT(SymSetUni。II(SymSetNeW(ELSESYM),FSYS),LEV,TX);if(SYM!=ELSESYM)COI)ECX1J.A=CX;e!se!GetSym();CX2=CX;GEN(JMH0,0);CODECX1.
13、A=CX;STATEMENT(SymSetlJnion(SymSetNew(ELSESYM),FSYS),LEV,TX);CODECX2.A=CX;)/STATEMENT(FSYS,LEV,TX);COI)ECX1J.A=CX;break;三、运行测试1.扩充单词的测试(贴运行结果截图)PROGRAMEXOlBEGINELSE;FOR;STEP;UNTIL;RETURN;*h/=;&;II;END.=COMPILEPLO=0PROGRAMEXOl;0BEGIN2ELSE;ELSE读取到ELSEl192FOR;FOR读取到FoR人192STEP;STEP读取到STEP2UNTIL;UNTIL读取到UNT工La192RETURN;RETURN读取到RETURN