编译原理课后答案——第十一章小型编译程序ppt课件.ppt
《编译原理课后答案——第十一章小型编译程序ppt课件.ppt》由会员分享,可在线阅读,更多相关《编译原理课后答案——第十一章小型编译程序ppt课件.ppt(247页珍藏版)》请在三一办公上搜索。
1、第十一章 小型编译程序,11.1 高级语言到四元式的编译程序PAS11.2 四元式到汇编语言程序的编译程序COMPILER,11.1 高级语言到四元式的编译程序PAS,/*/* pas.c */* 高级语言到四元式 */*/,#includetdio.h#include string.h#defineACC-2/*/#define sy_if0#define sy_then1#define sy_else2,#define sy_while3#define sy_begin4#define sy_do5#define sy_end6#define a7#define semicolon8#de
2、fine e9,#define jinghao10#define S11#define L12#define tempsy 15#define EA18 /*E and*/#define EO19 /*E or*/,#define plus34#define times36#define becomes38#define op_and39#define op_or40#define op_not41#define rop42#define lparent4#define rparent49#define ident 56#define intconst 57,/*/charch=0 ; /*当
3、前字符*/int count=0;staticcharspelling10= ; /*存放识别的字*/staticcharline81= ; /*一行字符缓冲区*/char*pline ; /*字符缓冲区指针*/,staticcharntab110010;structntabint tc;int fc;ntab2200;int label=0;/*存放临时变量的表的定义 */,structrwordscharsp10;intsy;/*存放文件的结构*/struct rwords reswords10= if,sy_if,do,sy_do,else,sy_else,while,sy_while,
4、then,sy_then,begin,sy_begin,end,sy_end,and,op_and,or,op_or,not,op_not;struct aaint sy1;int pos;buf1000;/*词法分析结果缓冲区*/,n;/*当前字符*/n1;/*当前表达式中的字符*/E;/*非终结符*/sstack100;/*符号栈*/ibuf100,stack1000;struct aa oth;,struct fourexpchar op10;struct aa arg1;struct aa arg2;intresult;fexp200;/*四元式的结构*/int ssp=0;/*指向s
5、stack100*/,structaa*pbuf=buf;/*指向词法分析缓冲区*/int nlength=0;int lnum=0;/*源程序长度*/int tt1=0;FILE *cfile;FILE *mfile;,/*/int newt=0;/*临时变量*/int nxq=100;/*nxq指向下一个形成的四元式的地址*/int lr;int lr1;int sp=0;,/* 状态栈定义*/int stack1100;int sp1=0;/*状态栈1的定义*/int num=0;struct llint nxq1;int tc1;int fc1;labelmark10;,int lab
6、eltemp10;int pointmark=-1,pointtemp=-1;int sign=0;/*sign1,表达式为赋值语句;sign2,表达式为布尔表达式。*/*/static intaction1913=/*0*/2,-1,-1,3,4,-1,-1,5,-1,-1,-1,1,-1,/*1*/-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,ACC,-1,-1,/*2*/-1,-1,-1,-1,-1,-1,-1,-1,-1,6,-1,-1,-1,/*3*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,7,-1,-1,-1,/*4*/ 2,-1,-1,3,4,-1,
7、-1,5,-1,-1,-1,9,8,/*5*/ -1,-1,104,-1,-1,-1,104,-1,104,-1,104,-1,-1,/*6*/ -1,10,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,/*7*/ -1,-1,-1,-1,-1,11,-1,-1,-1,-1,-1,-1,-1,/*8*/ -1,-1,-1,-1,-1,-1,12,-1,-1,-1,-1,-1,-1,/*9*/ -1,-1,-1,-1,-1,-1,105,-1,13,-1,-1,-1,-1,/*10*/ 2,-1,-1,3,4,-1,-1,5,-1,-1,-1,14,-1,/*11*/ 2,-
8、1,-1,3,4,-1,-1,5,-1,-1,-1,15,-1,/*12*/ -1,-1,103,-1,-1,-1,103,-1,103,-1,103,-1,-1,/*13*/ 2,-1,-1,3,4,-1,-1,5,-1,-1,-1,9,16,/*14*/ -1,-1,17,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,/*15*/ -1,-1,102,-1,-1,-1,102,-1,102,-1,102,-1,-1,/*16*/ -1,-1,-1,-1,-1,-1,106,-1,-1,-1,-1,-1,-1,/*17*/ 2,-1,-1,3,4,-1,-1,5,-1,-1,-
9、1,18,-1,/*18*/ -1,-1,101,-1,-1,-1,101,-1,101,-1,101,-1,-1;,static int action1107=/*0*/ 3,-1,-1,2,-1,-1,1,/*1*/ -1,4,5,-1,-1,ACC,-1,/*2*/ 3,-1,-1,2,-1,-1,6,/*3*/ 104,104,104,104,104,104,-1,/*4*/ 3,-1,-1,2,-1,-1,7,/*5*/ 3,-1,-1,2,-1,-1,8,/*6*/ -1,4,5,-1,9,-1,-1,/*7*/ 101,101,5,101,101,101,-1,/*8*/ 102
10、,102,102,102,102,102,-1,/*9*/ 103,103,103,103,103,103,-1;,static int action21611=/*0*/ 1,-1,4,-1,5,-1,-1,-1,13,7,8,/*1*/ -1,2,-1,101,-1,101,101,101,-1,-1,-1,/*2*/ 3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,/*3*/ -1,-1,-1,102,-1,102,102,102,-1,-1,-1,/*4*/ 1,-1,4,-1,5,-1,-1,-1,11,7,8,/*5*/1,-1,4,-1,5,-1,-1,-1,6,
11、7,8,/*6*/-1,-1,-1,104,-1,9,10,104,-1,-1,-1,/*7*/1,-1,4,-1,5,-1,-1,-1,14,7,8,/*8*/1,-1,4,-1,5,-1,-1,-1,15,7,8,/*9*/105,-1,105,-1,105,-1,-1,105,-1,-1,-1,/*10*/107,-1,107,-1,107,-1,-1,107,-1,-1,-1,/*11*/-1,-1,-1,12,-1,9,10,-1,-1,-1,-1,/*12*/-1,103,-1,103,-1,103,103,103,-1,-1,-1,/*13*/-1,-1,-1,-1,-1,9,1
12、0,ACC,-1,-1,-1,/*14*/-1,-1,-1,106,-1,9,10,106,-1,-1,-1,/*15*/-1,-1,-1,108,-1,9,10,108,-1,-1,-1;/*从文件读一行到缓冲区*/,readline( )char ch1;pline=line;ch1=getc(cfile);while (ch1!= n)*pline=ch1;pline+;ch1=getc(cfile);,*pline=0;pline=line;/*从缓冲区读一个字符*/readch( )if (ch=0) readline( );lnum+;,ch=*pline;pline+;/*标志符
13、和关键字的识别*/find(char spel )int ss1=0;int ii=0;while(ss1=0)&(iinlength),if (!strcmp(spel,ntab1ii) ss1=1;ii+;if (ss1=1) return ii-1;else return -1;,identifier( )int iii=0,j,k;int ss=0;k=0;dospellingk=ch;k+;readch( ); while(ch=a),pline-;spellingk= 0;while(ss=0),/*关键字匹配*/if(ss=1)bufcount.sy1=reswordsiii-1
14、.sy;elsebufcount.sy1=ident;j=find(spelling);if (j=-1),bufcount.pos=tt1;strcpy(ntab1tt1,spelling);tt1+;nlength+;else bufcount.pos=j;count+;for(k=0;k10;k+) spellingk= ;,/*数字的识别*/number( )int ivalue=0;int digit;dodigit=ch-0;ivalue=ivalue*10+digit;readch( );while(ch=0),bufcount.sy1=intconst;bufcount.pos
15、=ivalue;count+;pline-;/*扫描主函数*/scan( ),int i;while(ch!= )switch (ch)case :break;case a:case b:case c:,case d:case e:case f:case g:case h:case i:case j:case k:case l:case m:case n:case o:,case p:case q:case r:case s:case t:case u:case v:case w:case x:case y:case z:,identifier( );break;case 0:case 1:c
16、ase 2:case 3:case 4:case 5:case 6:case 7:case 8:case 9:number( );,break;case :readch( );if(ch=)bufcount.pos=0;else,if(ch=) bufcount.pos=4;elsebufcount.pos=1;pline-;bufcount.sy1=rop;count+;break;,case :readch( );if(ch=)bufcount.pos=2;elsebufcount.pos=3;pline-;,bufcount.sy1=rop;count+;break;case (:buf
17、count.sy1=lparent;count+;break;case ) :bufcount.sy1=rparent;count+;break;,case #:bufcount.sy1=jinghao;count+;break;case +:bufcount.sy1=plus;count+;break;,case *:bufcount.sy1=times;count+;break;case : :readch( );if (ch=)bufcount.sy1=becomes;count+;break;,case =:bufcount.sy1=rop;bufcount.pos=5;count+;
18、break;case ; :bufcount.sy1=semicolon;count+;break;,readch( );bufcount.sy1=-1;/*/,readnu( )if (pbuf-sy1=0)n.sy1=pbuf-sy1;n.pos=pbuf-pos;pbuf+;,/*中间变量的生成*/newtemp( )newt+;return newt;/*生成四元式*/,gen(char op1 ,struct aa arg11,struct aa arg22,int result1)strcpy(fexpnxq.op,op1);fexpnxq.arg1.sy1=arg11.sy1;f
19、expnxq.arg1.pos=arg11.pos;fexpnxq.arg2.sy1=arg22.sy1;fexpnxq.arg2.pos=arg22.pos;fexpnxq.result=result1;nxq+;return nxq-1;,/*布尔表达式的匹配*/merg(int p1,int p2)int p;if(p2=0) return p1;elsep=p2;while(fexpp.result!=0) p=fexpp.result;fexpp.result=p1;return p2;,backpatch(int p,int t)int tempq;int q;q=p;while(
20、q!=0)tempq=fexpq.result;fexpq.result=t;q=tempq;,/*/change1(int chan)switch (chan)case ident:case intconst:return 0;case plus:return 1;case times:return 2;,case lparent:return 3;case rparent:return 4;case jinghao:return 5;case tempsy:return 6;,change2(int chan)switch (chan)case ident:case intconst:re
21、turn 0;case rop:return 1;case lparent:return 2;,case rparent:return 3;case op_not:return 4;case op_and:return 5;case op_or:return 6;case jinghao:return 7;,case tempsy:return 8;case EA:return 9;case EO:return 10;,/*赋值语句的分析*/lrparse1(int num)lr1=action1stack1sp1change1(n1.sy1);if (lr1=-1)printf(n算术表达式
22、或赋值语句出错!n);getch( );exit(0);,if (lr1=0)sp1+;stack1sp1=lr1;if (n1.sy1!=tempsy)ssp+;num+;sstackssp.sy1=n1.sy1;sstackssp.pos=n1.pos;,n1.sy1=ibufnum.sy1;n1.pos=ibufnum.pos;lrparse1(num);if(lr1=100),gen(+,sstackssp-2,sstackssp,E.pos+100);ssp=ssp-2;sstackssp.sy1=tempsy;sstackssp.pos=E.pos;sp1=sp1-3;break;
23、/*E-E+E*/,case 102:E.pos=newtemp( );gen(*,sstackssp-2,sstackssp,E.pos+100);ssp=ssp-2;sstackssp.sy1=tempsy;sstackssp.pos=E.pos;sp1=sp1-3;break;/*E-E*E*/,case 103:E.pos=sstackssp-1.pos;ssp=ssp-2;sstackssp.sy1=tempsy;sstackssp.pos=E.pos;sp1=sp1-3;break;/*E-(E)*/,case 104:E.pos=sstackssp.pos;sp1-;break;
24、/*E-i*/n1.sy1=tempsy; /*规约后为非终结符*/n1.pos=E.pos;lrparse1(num);,if (lr1=ACC),/*布尔表达式的分析*/lrparse2(int num)int templabel;lr1=action2stack1sp1change2(n1.sy1);if (lr1=-1) if (sign=2) printf(nwhile语句出错!n);if (sign=3) printf(nif语句出错!n);getch( );exit(0);,if (lr1=0)sp1+;stack1sp1=lr1;ssp+;sstackssp.sy1=n1.sy
25、1;sstackssp.pos=n1.pos;if (n1.sy1!=tempsy),if(lr1=100),gen(jnz,sstackssp,oth,0);gen(j,oth,oth,0);sp1-;ssp-;label+;n1.sy1=tempsy;break;/*E-i*/,case 102:ntab2label.tc=nxq;ntab2label.fc=nxq+1;switch (sstackssp-1.pos)case 0:gen(j=,sstackssp-2,sstackssp,0);break;case 1:gen(j,sstackssp-2,sstackssp,0);brea
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 编译 原理 课后 答案 第十一 小型 编译程序 ppt 课件

链接地址:https://www.31ppt.com/p-1626966.html