北京印刷学院编译原理课程设计实验报告.pdf
黑龙江大学黑龙江大学编译课程设计文档编译课程设计文档学号 20092612 姓名 张冰一 编译器的实现方式编译器的实现方式Microsoft Visual C+ 6.0利用 SLR(1)的分析方式进行语法分析二 编译器功能介绍编译器功能介绍首先将非终结符输入(以#结束输入) 。然后输入终结符(以#结束输入) 。输入文法,例如 E (i)(用空格代替 - ,以#结束输入文法) 。语法分析中可识别:while 语句,for 语句,if、else 语句。可识别数组,赋值运算,逻辑和赋值判断,+-*/运算。可将 while 语句和 if、else 语句翻译成四元式。可将源程序翻译为汇编程序。三 测试用例与实验结果测试用例与实验结果测试用的源程序:void main()int i,j;if( i 100 & i 20)a = b + (c - 3*q) / h;elsewhile(i10)a = a + b;语法分析所用的语法:/*c=chart=intf=floatd=doubleo=ifu=elseh=forw=while*/生成的 FIRST 集:FIRST(A)(ieFIRST(B)!&|FIRST(E)(ieFIRST(F)(ieFIRST(K)iohwctfd(eFIRST(M)vFIRST(P)vFIRST(Q)ctfdFIRST(T)(ieFIRST(U)ctfdFIRST(V)iFIRST(W)=FIRST(Z)*生成的 FOLLOW 集:FOLLOW(A);,)!&|(ie+-*/FOLLOW(B)(ieFOLLOW(E);,)!&|(ie+-FOLLOW(F);,)!&|(ie+-*/FOLLOW(K)FOLLOW(M)#FOLLOW(P)#FOLLOW(Q)iohwctfd(eFOLLOW(T);FOLLOW(U)*iFOLLOW(V);FOLLOW(W);,FOLLOW(Z)iLR(0)项目规范族如下:I0M - PP - vm()QKI1M - PI2P - vm()QKI3P - vm()QKI4P - vm()QKI5P - vm()QKI6P - vm()QKQ - UZV;U - cU - tU - fU - dI7P - vm()QKK - i=E;KK - EK - o(T)KuKKK - h(i=E;T;i=E)KKK - w(T)KKK - QKK - E - E+AE - E-AE - AQ - UZV;A- A*FA- A/FA- FU - cU - tU - fU - dF - (E)F - iF - eI8Q - UZV;Z - *Z - I9U - cI10U - tI11U - fI12U - dI13P - vm()QKI14K - i=E;KF - iI15K - EE - E+AE - E-AI16K - o(T)KuKKI17K - h(i=E;T;i=E)KKI18K - w(T)KKI19K - QKK - i=E;KK - EK - o(T)KuKKK - h(i=E;T;i=E)KKK - w(T)KKK - QKK - E - E+AE - E-AE - AQ - UZV;A- A*FA- A/FA- FU - cU - tU - fU - dF - (E)F - iF - eI20E -AA-A*FA-A/FI21A- FI22F - (E)E - E+AE - E-AE - AA- A*FA- A/FA- FF - (E)F - iF - eI23F - eI24Q - UZV;V - iWV - iW,ZVI25Z - *I26P - vm()QKI27K - i=E;KE - E+AE - E-AE - AA- A*FA- A/FA- FF - (E)F - iF - eI28E - E+AA- A*FA- A/FA- FF - (E)F - iF - eI29E - E-AA- A*FA- A/FA- FF - (E)F - iF - eI30K - o(T)KuKKT - EBTT - EE - E+AE - E-AE - AA- A*FA- A/FA- FF - (E)F - iF - eI31K - h(i=E;T;i=E)KKI32K - w(T)KKT - EBTT - EE - E+AE - E-AE - AA- A*FA- A/FA- FF - (E)F - iF - eI33K - QKI34A-A*FF - (E)F - iF - eI35A-A/FF - (E)F - iF - eI36F - (E)E - E+AE - E-AI37F - iI38Q - UZV;I39V - iWV - iW,ZVW - EW - =EW - I40K - i=E;KE - E+AE - E-AI41E - E+AA-A*FA-A/FI42E - E-AA-A*FA-A/FI43K - o(T)KuKKI44T - EBTT - EE - E+AE - E-AB - B - B - =B - !=B - &B - |B - I45K - h(i=E;T;i=E)KKI46K - w(T)KKI47A-A*FI48A-A/FI49F - (E)I50Q - UZV;I51V - iWV - iW,ZVI52W - EE - E+AE - E-AE - AA- A*FA- A/FA- FF - (E)F - iF - eI53W - =EE - E+AE - E-AE - AA- A*FA- A/FA- FF - (E)F - iF - eI54K - i=E;KK - i=E;KK - EK - o(T)KuKKK - h(i=E;T;i=E)KKK - w(T)KKK - QKK - E - E+AE - E-AE - AQ - UZV;A- A*FA- A/FA- FU - cU - tU - fU - dF - (E)F - iF - eI55K - o(T)KuKKI56T - EBTT - EBTT - EE - E+AE - E-AE - AA- A*FA- A/FA- FF - (E)F - iF - eI57B - B - =I59B - I60B - !=I61B - &I62B - |I63K - h(i=E;T;i=E)KKE - E+AE - E-AE - AA- A*FA- A/FA- FF - (E)F - iF - eI64K - w(T)KKI65V - iW,ZVZ - *Z - I66W - EE - E+AE - E-AI67W - =EE - E+AE - E-AI68K - i=E;KI69K - o(T)KuKKK - i=E;KK - EK - o(T)KuKKK - h(i=E;T;i=E)KKK - w(T)KKK - QKK - E - E+AE - E-AE - AQ - UZV;A- A*FA- A/FA- FU - cU - tU - fU - dF - (E)F - iF - eI70T - EBTI71B - =I73B - !=I74B - &I75B - |I76K - h(i=E;T;i=E)KKE - E+AE - E-AI77K - w(T)KKK - i=E;KK - EK - o(T)KuKKK - h(i=E;T;i=E)KKK - w(T)KKK - QKK - E - E+AE - E-AE - AQ - UZV;A- A*FA- A/FA- FU - cU - tU - fU - dF - (E)F - iF - eI78V - iW,ZVV - iWV - iW,ZVI79W - EI80K - o(T)KuKKI81K - h(i=E;T;i=E)KKT - EBTT - EE - E+AE - E-AE - AA- A*FA- A/FA- FF - (E)F - iF - eI82K - w(T)KKI83V - iW,ZVI84K - o(T)KuKKI85K - h(i=E;T;i=E)KKI86K - w(T)KKK - i=E;KK - EK - o(T)KuKKK - h(i=E;T;i=E)KKK - w(T)KKK - QKK - E - E+AE - E-AE - AQ - UZV;A- A*FA- A/FA- FU - cU - tU - fU - dF - (E)F - iF - eI87K - o(T)KuKKI88K - h(i=E;T;i=E)KKI89K - w(T)KKI90K - o(T)KuKKK - i=E;KK - EK - o(T)KuKKK - h(i=E;T;i=E)KKK - w(T)KKK - QKK - E - E+AE - E-AE - AQ - UZV;A- A*FA- A/FA- FU - cU - tU - fU - dF - (E)F - iF - eI91K - h(i=E;T;i=E)KKI92K - o(T)KuKKI93K - h(i=E;T;i=E)KKE - E+AE - E-AE - AA- A*FA- A/FA- FF - (E)F - iF - eI94K - o(T)KuKKK - i=E;KK - EK - o(T)KuKKK - h(i=E;T;i=E)KKK - w(T)KKK - QKK - E - E+AE - E-AE - AQ - UZV;A- A*FA- A/FA- FU - cU - tU - fU - dF - (E)F - iF - eI95K - h(i=E;T;i=E)KKE - E+AE - E-AI96K - o(T)KuKKI97K - h(i=E;T;i=E)KKI98K - h(i=E;T;i=E)KKK - i=E;KK - EK - o(T)KuKKK - h(i=E;T;i=E)KKK - w(T)KKK - QKK - E - E+AE - E-AE - AQ - UZV;A- A*FA- A/FA- FU - cU - tU - fU - dF - (E)F - iF - eI99K - h(i=E;T;i=E)KKI100K - h(i=E;T;i=E)KKK - i=E;KK - EK - o(T)KuKKK - h(i=E;T;i=E)KKK - w(T)KKK - QKK - E - E+AE - E-AE - AQ - UZV;A- A*FA- A/FA- FU - cU - tU - fU - dF - (E)F - iF - eI101K - h(i=E;T;i=E)KKGO 函数:GO(I0,P)=I1GO(I0,v)=I2GO(I2,m)=I3GO(I3,()=I4GO(I4,)=I5GO(I5,)=I6GO(I6,Q)=I7GO(I6,U)=I8GO(I6,c)=I9GO(I6,t)=I10GO(I6,f)=I11GO(I6,d)=I12GO(I7,U)=I8GO(I7,c)=I9GO(I7,t)=I10GO(I7,f)=I11GO(I7,d)=I12GO(I7,K)=I13GO(I7,i)=I14GO(I7,E)=I15GO(I7,o)=I16GO(I7,h)=I17GO(I7,w)=I18GO(I7,Q)=I19GO(I7,A)=I20GO(I7,F)=I21GO(I7,()=I22GO(I7,e)=I23GO(I8,Z)=I24GO(I8,*)=I25GO(I13,)=I26GO(I14,=)=I27GO(I15,+)=I28GO(I15,-)=I29GO(I16,()=I30GO(I17,()=I31GO(I18,()=I32GO(I19,U)=I8GO(I19,c)=I9GO(I19,t)=I10GO(I19,f)=I11GO(I19,d)=I12GO(I19,i)=I14GO(I19,E)=I15GO(I19,o)=I16GO(I19,h)=I17GO(I19,w)=I18GO(I19,Q)=I19GO(I19,A)=I20GO(I19,F)=I21GO(I19,()=I22GO(I19,e)=I23GO(I19,K)=I33GO(I20,*)=I34GO(I20,/)=I35GO(I22,A)=I20GO(I22,F)=I21GO(I22,()=I22GO(I22,e)=I23GO(I22,E)=I36GO(I22,i)=I37GO(I24,V)=I38GO(I24,i)=I39GO(I27,A)=I20GO(I27,F)=I21GO(I27,()=I22GO(I27,e)=I23GO(I27,i)=I37GO(I27,E)=I40GO(I28,F)=I21GO(I28,()=I22GO(I28,e)=I23GO(I28,i)=I37GO(I28,A)=I41GO(I29,F)=I21GO(I29,()=I22GO(I29,e)=I23GO(I29,i)=I37GO(I29,A)=I42GO(I30,A)=I20GO(I30,F)=I21GO(I30,()=I22GO(I30,e)=I23GO(I30,i)=I37GO(I30,T)=I43GO(I30,E)=I44GO(I31,i)=I45GO(I32,A)=I20GO(I32,F)=I21GO(I32,()=I22GO(I32,e)=I23GO(I32,i)=I37GO(I32,E)=I44GO(I32,T)=I46GO(I34,()=I22GO(I34,e)=I23GO(I34,i)=I37GO(I34,F)=I47GO(I35,()=I22GO(I35,e)=I23GO(I35,i)=I37GO(I35,F)=I48GO(I36,+)=I28GO(I36,-)=I29GO(I36,)=I49GO(I38,;)=I50GO(I39,W)=I51GO(I39,)=I52GO(I39,=)=I53GO(I40,+)=I28GO(I40,-)=I29GO(I40,;)=I54GO(I41,*)=I34GO(I41,/)=I35GO(I42,*)=I34GO(I42,/)=I35GO(I43,)=I55GO(I44,+)=I28GO(I44,-)=I29GO(I44,B)=I56GO(I44,)=I58GO(I44,)=I59GO(I44,!)=I60GO(I44,&)=I61GO(I44,|)=I62GO(I45,=)=I63GO(I46,)=I64GO(I51,)=I65GO(I52,A)=I20GO(I52,F)=I21GO(I52,()=I22GO(I52,e)=I23GO(I52,i)=I37GO(I52,E)=I66GO(I53,A)=I20GO(I53,F)=I21GO(I53,()=I22GO(I53,e)=I23GO(I53,i)=I37GO(I53,E)=I67GO(I54,U)=I8GO(I54,c)=I9GO(I54,t)=I10GO(I54,f)=I11GO(I54,d)=I12GO(I54,i)=I14GO(I54,E)=I15GO(I54,o)=I16GO(I54,h)=I17GO(I54,w)=I18GO(I54,Q)=I19GO(I54,A)=I20GO(I54,F)=I21GO(I54,()=I22GO(I54,e)=I23GO(I54,K)=I68GO(I55,)=I69GO(I56,A)=I20GO(I56,F)=I21GO(I56,()=I22GO(I56,e)=I23GO(I56,i)=I37GO(I56,E)=I44GO(I56,T)=I70GO(I57,=)=I71GO(I58,=)=I72GO(I60,=)=I73GO(I61,&)=I74GO(I62,|)=I75GO(I63,A)=I20GO(I63,F)=I21GO(I63,()=I22GO(I63,e)=I23GO(I63,i)=I37GO(I63,E)=I76GO(I64,)=I77GO(I65,*)=I25GO(I65,Z)=I78GO(I66,+)=I28GO(I66,-)=I29GO(I66,)=I79GO(I67,+)=I28GO(I67,-)=I29GO(I69,U)=I8GO(I69,c)=I9GO(I69,t)=I10GO(I69,f)=I11GO(I69,d)=I12GO(I69,i)=I14GO(I69,E)=I15GO(I69,o)=I16GO(I69,h)=I17GO(I69,w)=I18GO(I69,Q)=I19GO(I69,A)=I20GO(I69,F)=I21GO(I69,()=I22GO(I69,e)=I23GO(I69,K)=I80GO(I76,+)=I28GO(I76,-)=I29GO(I76,;)=I81GO(I77,U)=I8GO(I77,c)=I9GO(I77,t)=I10GO(I77,f)=I11GO(I77,d)=I12GO(I77,i)=I14GO(I77,E)=I15GO(I77,o)=I16GO(I77,h)=I17GO(I77,w)=I18GO(I77,Q)=I19GO(I77,A)=I20GO(I77,F)=I21GO(I77,()=I22GO(I77,e)=I23GO(I77,K)=I82GO(I78,i)=I39GO(I78,V)=I83GO(I80,)=I84GO(I81,A)=I20GO(I81,F)=I21GO(I81,()=I22GO(I81,e)=I23GO(I81,i)=I37GO(I81,E)=I44GO(I81,T)=I85GO(I82,)=I86GO(I84,u)=I87GO(I85,;)=I88GO(I86,U)=I8GO(I86,c)=I9GO(I86,t)=I10GO(I86,f)=I11GO(I86,d)=I12GO(I86,i)=I14GO(I86,E)=I15GO(I86,o)=I16GO(I86,h)=I17GO(I86,w)=I18GO(I86,Q)=I19GO(I86,A)=I20GO(I86,F)=I21GO(I86,()=I22GO(I86,e)=I23GO(I86,K)=I89GO(I87,)=I90GO(I88,i)=I91GO(I90,U)=I8GO(I90,c)=I9GO(I90,t)=I10GO(I90,f)=I11GO(I90,d)=I12GO(I90,i)=I14GO(I90,E)=I15GO(I90,o)=I16GO(I90,h)=I17GO(I90,w)=I18GO(I90,Q)=I19GO(I90,A)=I20GO(I90,F)=I21GO(I90,()=I22GO(I90,e)=I23GO(I90,K)=I92GO(I91,=)=I93GO(I92,)=I94GO(I93,A)=I20GO(I93,F)=I21GO(I93,()=I22GO(I93,e)=I23GO(I93,i)=I37GO(I93,E)=I95GO(I94,U)=I8GO(I94,c)=I9GO(I94,t)=I10GO(I94,f)=I11GO(I94,d)=I12GO(I94,i)=I14GO(I94,E)=I15GO(I94,o)=I16GO(I94,h)=I17GO(I94,w)=I18GO(I94,Q)=I19GO(I94,A)=I20GO(I94,F)=I21GO(I94,()=I22GO(I94,e)=I23GO(I94,K)=I96GO(I95,+)=I28GO(I95,-)=I29GO(I95,)=I97GO(I97,)=I98GO(I98,U)=I8GO(I98,c)=I9GO(I98,t)=I10GO(I98,f)=I11GO(I98,d)=I12GO(I98,i)=I14GO(I98,E)=I15GO(I98,o)=I16GO(I98,h)=I17GO(I98,w)=I18GO(I98,Q)=I19GO(I98,A)=I20GO(I98,F)=I21GO(I98,()=I22GO(I98,e)=I23GO(I98,K)=I99GO(I99,)=I100GO(I100,U)=I8GO(I100,c)=I9GO(I100,t)=I10GO(I100,f)=I11GO(I100,d)=I12GO(I100,i)=I14GO(I100,E)=I15GO(I100,o)=I16GO(I100,h)=I17GO(I100,w)=I18GO(I100,Q)=I19GO(I100,A)=I20GO(I100,F)=I21GO(I100,()=I22GO(I100,e)=I23GO(I100,K)=I101四 编译器算法实现编译器算法实现正文(小四)分析表及说明,每个模块的流程图。词法分析流程图:五 关键技术关键技术cifa.hanalysis()函数是词法分析的函数,通过将“源文件.c”中的字符逐一读入进行分析,将分析结果分别写入三个文件当中:编码文件.txt、常数.txt、标识符.txt 。cal.hChange()函数是将源程序中需要进行运算的表达式转换为逆波兰式。Cal()函数是对转换完成的逆波兰式进行计算。实验 8.cppGrammer 中存放分析输入流所用的文法。getFIRST()函数用于计算每个非终结符的 FIRST 集合。getFOLLOW()函数用于计算每个非终结符的 FOLLOW 集合。getSELECT()函数用于计算每个文法的 SELECT 集合。getCLOSURE()函数用于生成项目集规范族,先将 I0 计算出,然后根据 I0逐步扩展项目集。生成四元式是通过递归回填的方式将四元式第四项的地址填入。huibian()函数是用于生成汇编程序的函数。六 总结总结本程序的语法分析中可对数组、指针进行识别,可对 for、if else、while 语句进行分析、可对逻辑判断语句进程分析。所用文法覆盖的范围比较广。但在生成四元式中无法对 for 语句进行分析判断。七 参考文献参考文献编译原理(第 2 版)