第十一章__小型編譯程序_第1頁
第十一章__小型編譯程序_第2頁
第十一章__小型編譯程序_第3頁
第十一章__小型編譯程序_第4頁
第十一章__小型編譯程序_第5頁
已閱讀5頁,還剩242頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

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第十一章第十一章

2、小型編譯程序小型編譯程序 #define sy_while3#define sy_begin4#define sy_do5#define sy_end6#define a7#define semicolon8#define e9第十一章第十一章 小型編譯程序小型編譯程序 #define jinghao10#define S11#define L12 #define tempsy 15#define EA18 /*E and*/#define EO19 /*E or*/第十一章第十一章 小型編譯程序小型編譯程序 #define plus34#define times36#define becom

3、es38#define op_and39#define op_or40#define op_not41#define rop42#define lparent4#define rparent49#define ident 56#define intconst 57第十一章第十一章 小型編譯程序小型編譯程序 /*/charch=0 ; /*當前字符*/int count=0;staticcharspelling10= ; /*存放識別的字*/staticcharline81= ; /*一行字符緩沖區(qū)*/char*pline ; /*字符緩沖區(qū)指針*/第十一章第十一章 小型編譯程序小型編譯程序 s

4、taticcharntab110010;struct ntabint tc;int fc;ntab2200;int label=0;/*存放臨時變量的表的定義 */第十一章第十一章 小型編譯程序小型編譯程序 struct rwordscharsp10;intsy;/*存放文件的結(jié)構(gòu)*/struct rwords reswords10= if,sy_if,do,sy_do,else,sy_else,while,sy_while,then,sy_then,第十一章第十一章 小型編譯程序小型編譯程序 begin,sy_begin,end,sy_end,and,op_and,or,op_or,not,

5、op_not;struct aaint sy1;int pos;buf1000;/*詞法分析結(jié)果緩沖區(qū)*/第十一章第十一章 小型編譯程序小型編譯程序 n;/*當前字符*/n1;/*當前表達式中的字符*/E;/*非終結(jié)符*/sstack100;/*符號棧*/ibuf100,stack1000;struct aa oth;第十一章第十一章 小型編譯程序小型編譯程序 struct fourexpchar op10;struct aa arg1;struct aa arg2;intresult;fexp200;/*四元式的結(jié)構(gòu)*/int ssp=0;/*指向sstack100*/第十一章第十一章 小型

6、編譯程序小型編譯程序 structaa*pbuf=buf;/*指向詞法分析緩沖區(qū)*/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;第十一章第十一章 小型編譯程序小型編譯程序 /* 狀態(tài)棧定義*/int stack1100;int sp1=0;/*狀態(tài)棧1的定義*/int num=0;struct llint

7、 nxq1;int tc1;int fc1;labelmark10;第十一章第十一章 小型編譯程序小型編譯程序 int labeltemp10;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,第十

8、一章第十一章 小型編譯程序小型編譯程序 /*3*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,7,-1,-1,-1,/*4*/ 2,-1,-1,3,4,-1,-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,-

9、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,-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,-

10、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,-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

11、,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,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,

12、-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,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,1

13、05,-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,10,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;/*從文件讀一行到緩沖區(qū)

14、*/第十一章第十一章 小型編譯程序小型編譯程序 readline( )char ch1;pline=line;ch1=getc(cfile);while (ch1!= n)*pline=ch1;pline+;ch1=getc(cfile);第十一章第十一章 小型編譯程序小型編譯程序 *pline=0;pline=line;/*從緩沖區(qū)讀一個字符*/readch( )if (ch=0) readline( );lnum+;第十一章第十一章 小型編譯程序小型編譯程序 ch=*pline;pline+;/*標志符和關(guān)鍵字的識別*/find(char spel )int ss1=0;int ii=0;

15、while(ss1=0)&(ii=a)&(ch=0)&(ch=9);第十一章第十一章 小型編譯程序小型編譯程序 pline-;spellingk= 0;while(ss=0)&(iii10)if (!strcmp(spelling,reswordsiii.sp) ss=1;iii+;第十一章第十一章 小型編譯程序小型編譯程序 /*關(guān)鍵字匹配*/if(ss=1)bufcount.sy1=reswordsiii-1.sy;elsebufcount.sy1=ident;j=find(spelling);if (j=-1)第十一章第十一章 小型編譯程序小型編譯程序 bu

16、fcount.pos=tt1;strcpy(ntab1tt1,spelling);tt1+;nlength+;else bufcount.pos=j;count+;for(k=0;k=0)&(ch=9);第十一章第十一章 小型編譯程序小型編譯程序 bufcount.sy1=intconst;bufcount.pos=ivalue;count+;pline-;/ * * * * * * * * * * * * * * 掃 描 主 函 數(shù)*/scan( )第十一章第十一章 小型編譯程序小型編譯程序 int i;while(ch!= )switch (ch)case :break;case

17、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:case 2:case 3:case 4:ca

18、se 5:case 6:case 7:case 8:case 9:number( );第十一章第十一章 小型編譯程序小型編譯程序 break;case ) 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 (:bufcou

19、nt.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;第十一章第十一章

20、 小型編譯程序小型編譯程序 case =:bufcount.sy1=rop;bufcount.pos=5;count+;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+;第十一章第十一章 小型編譯程序小型編譯程序 / * * * * * * * * * * * * * * * * *

21、 * 中 間 變 量 的 生 成*/newtemp( )newt+;return newt;/*生成四元式*/第十一章第十一章 小型編譯程序小型編譯程序 gen(char op1 ,struct aa arg11,struct aa arg22,int result1)strcpy(fexpnxq.op,op1);fexpnxq.arg1.sy1=arg11.sy1;fexpnxq.arg1.pos=arg11.pos;fexpnxq.arg2.sy1=arg22.sy1;fexpnxq.arg2.pos=arg22.pos;fexpnxq.result=result1;nxq+;return

22、 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(q!=0)tempq=fexpq.result;fexpq.result=t;q=tempq;第十一章第十一章 小型編譯程序小型編譯程序 /*/

23、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:return 0;ca

24、se 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;第十一章第十一章 小型編譯程序小型編譯程序 / * * * * * * * * * * * * 賦 值 語 句

25、的 分 析*/lrparse1(int num)lr1=action1stack1sp1change1(n1.sy1);if (lr1=-1)printf(n算術(shù)表達式或賦值語句出錯!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;lrpars

26、e1(num);if(lr1=100)&(lr1E+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;

27、sp1=sp1-3;break;/*E-(E)*/第十一章第十一章 小型編譯程序小型編譯程序 case 104:E.pos=sstackssp.pos;sp1-;break;/*E-i*/n1.sy1=tempsy; /*規(guī)約后為非終結(jié)符*/n1.pos=E.pos;lrparse1(num);第十一章第十一章 小型編譯程序小型編譯程序 if (lr1=ACC)&(stack1sp1=1)/*歸約A-i:=E*/gen(:= ,sstackssp,oth,ibuf0.pos);ssp=ssp-3;sp1=sp1-3;第十一章第十一章 小型編譯程序小型編譯程序 /*布爾表達式的分析*/l

28、rparse2(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.sy1;sstackssp.pos=n1.pos;if (n1.sy1!=tempsy)&(n1.sy1!=EA)&(n1

29、.sy1!=EO) num+;n1.sy1=ibufnum.sy1;n1.pos=ibufnum.pos;lrparse2(num);第十一章第十一章 小型編譯程序小型編譯程序 if(lr1=100)&(lr1i*/第十一章第十一章 小型編譯程序小型編譯程序 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 = , s s t a c k s s p -2,sstackssp,0)

30、;break;case 3:gen(j,sstackssp-2,sstackssp,0);break;case 4:gen(j,sstackssp-2,sstackssp,0);break;case 5:gen(j=,sstackssp-2,sstackssp,0);break;第十一章第十一章 小型編譯程序小型編譯程序 gen(j,oth,oth,0);ssp=ssp-3;sp1=sp1-3;label+;n1.sy1=tempsy;break;/*E-i rop i*/第十一章第十一章 小型編譯程序小型編譯程序 case 103:label=label-1;ssp=ssp-3;sp1=sp

31、1-3;label+;n1.sy1=tempsy;break;/*E-(E)*/第十一章第十一章 小型編譯程序小型編譯程序 case 104:label=label-1;templabel=ntab2label.tc;ntab2label.tc=ntab2label.fc;ntab2label.fc=templabel;ssp=ssp-2;sp1=sp1-2;label+;n1.sy1=tempsy;break;/*E-not E*/第十一章第十一章 小型編譯程序小型編譯程序 case 105:backpatch(ntab2label-1.tc,nxq);label=label-1;ssp=s

32、sp-2;sp1=sp1-2;label+;n1.sy1=EA;break;/*EA-E(1)and*/第十一章第十一章 小型編譯程序小型編譯程序 case 106:label=label-2;ntab2label.tc=ntab2label+1.tc;ntab2label.fc=merg(ntab2label.fc,ntab2label+1.fc);ssp=ssp-2;sp1=sp1-2;label+;n1.sy1=tempsy;break;/*E-EA E(2)*/第十一章第十一章 小型編譯程序小型編譯程序 case 107:backpatch(ntab2label-1.fc,nxq);l

33、abel=label-1;ssp=ssp-2;sp1=sp1-2;label+;n1.sy1=EO;break;第十一章第十一章 小型編譯程序小型編譯程序 case 108:label=label-2;ntab2label.fc=ntab2label+1.fc;ntab2label.tc=merg(ntab2label.tc,ntab2label+1.tc);ssp=ssp-2;sp1=sp1-2;label+;n1.sy1=tempsy;break;第十一章第十一章 小型編譯程序小型編譯程序 lrparse2(num);if (lr1=ACC) return 1;/*測試字符是否為表達式中的

34、值(不包括; )*/test(int value)switch (value)case intconst:case ident:case plus:case times:第十一章第十一章 小型編譯程序小型編譯程序 case becomes:case lparent:case rparent:case rop:case op_and:case op_or:case op_not:return 1;default:return 0; 第十一章第十一章 小型編譯程序小型編譯程序 /*/lrparse( )int i1=0;int num=0;/*指向表達式緩沖區(qū)*/if (test(n.sy1)if

35、(stacksp.sy1=sy_while) sign=2;else if (stacksp.sy1=sy_if) sign=3;else sign=1;第十一章第十一章 小型編譯程序小型編譯程序 doibufi1.sy1=n.sy1;ibufi1.pos=n.pos;readnu( );i1+;while(test(n.sy1);第十一章第十一章 小型編譯程序小型編譯程序 /*把表達式放入緩沖區(qū)*/ibufi1.sy1=jinghao;pbuf-;/*指針后退1,需要嗎?*/sstack0.sy1=jinghao;ssp=0;/*符號棧底的初始化*/if (sign=1)第十一章第十一章 小

36、型編譯程序小型編譯程序 sp1=0;stack1sp1=0;/*狀態(tài)棧1的棧底初始化*/num=2;/*指向:=*/n1.sy1=ibufnum.sy1;n1.pos=ibufnum.pos;lrparse1(num);/*處理賦值語句*/n.sy1=a;/*當前文法符號置為a(賦值語句)*/第十一章第十一章 小型編譯程序小型編譯程序 if (sign=2)|(sign=3)pointmark+;labelmarkpointmark.nxq1=nxq;sp1=0;stack1sp1=0;num=0;n1.sy1=ibufnum.sy1;n1.pos=ibufnum.pos;lrparse2(n

37、um);第十一章第十一章 小型編譯程序小型編譯程序 labelmarkpointmark.tc1=ntab2label-1.tc;labelmarkpointmark.fc1=ntab2label-1.fc;/*處理布爾語句*/*在處理完E,要回填真值鏈*/backpatch(labelmarkpointmark.tc1,nxq);n.sy1=e;/*當前文法符號置為e(賦值語句)*/第十一章第十一章 小型編譯程序小型編譯程序 l r = a c t i o n s t a c k s p . p o s n . s y 1 ;printf(stack%d=%dttn=%dttlr=%dn,s

38、p,stacksp.pos,n.sy1,lr);if (lr=0)sp+;stacksp.pos=lr;stacksp.sy1=n.sy1;readnu( );lrparse( );第十一章第十一章 小型編譯程序小型編譯程序 if (lr=100)switch (lr)case 100:break;/*S-S*/case 101:printf(S-if e then S then S else S 歸約n);sp=sp-6;n.sy1=S;第十一章第十一章 小型編譯程序小型編譯程序 /*歸約完if后,填then后面的無條件轉(zhuǎn)移語句*/fexplabeltemppointtemp.result=

39、nxq;pointtemp-;if(stacksp.sy1=sy_then)gen(j,oth,oth,0);backpatch(labelmarkpointmark.fc1,nxq);pointtemp+;labeltemppointtemp=nxq-1;第十一章第十一章 小型編譯程序小型編譯程序 pointmark-;if(stacksp.sy1=sy_do)gen(j,oth,oth,labelmarkpointmark.nxq1);backpatch(labelmarkpointmark.fc1,nxq);break;第十一章第十一章 小型編譯程序小型編譯程序 /*S-if e the

40、n S then S else S*/case 102:printf(S-while e do S 歸約n);sp=sp-4;n.sy1=S;pointmark-;if(stacksp.sy1=sy_do)gen(j,oth,oth,labelmarkpointmark.nxq1);backpatch(labelmarkpointmark.fc1,nxq);第十一章第十一章 小型編譯程序小型編譯程序 if(stacksp.sy1=sy_then)gen(j,oth,oth,0);fexplabelmarkpointmark.fc1.result=nxq;pointtemp+;labeltemp

41、pointtemp=nxq-1;break;第十一章第十一章 小型編譯程序小型編譯程序 /*S-while e do S*/case 103:printf(S-begin L end 歸約n);sp=sp-3;n.sy1=S;if(stacksp.sy1=sy_then)gen(j,oth,oth,0);backpatch(labelmarkpointmark.fc1,nxq);pointtemp+;labeltemppointtemp=nxq-1;第十一章第十一章 小型編譯程序小型編譯程序 if(stacksp.sy1=sy_do)gen(j,oth,oth,labelmarkpointma

42、rk.nxq1);backpatch(labelmarkpointmark.fc1,nxq);getch( );break;第十一章第十一章 小型編譯程序小型編譯程序 /*S-begin L end*/case 104:printf(S-a 歸約n);sp=sp-1;n.sy1=S;if(stacksp.sy1=sy_then)gen(j,oth,oth,0);backpatch(labelmarkpointmark.fc1,nxq);pointtemp+;labeltemppointtemp=nxq-1;第十一章第十一章 小型編譯程序小型編譯程序 if(stacksp.sy1=sy_do)g

43、en(j,oth,oth,labelmarkpointmark.nxq1);backpatch(labelmarkpointmark.fc1,nxq);break;/*S-a*/第十一章第十一章 小型編譯程序小型編譯程序 case 105:printf(歸約n);sp=sp-1;n.sy1=L;break;/*L-S*/第十一章第十一章 小型編譯程序小型編譯程序 case 106:printf(L-S;L 歸約n);sp=sp-3;n.sy1=L;break;/*L-S;L*/第十一章第十一章 小型編譯程序小型編譯程序 getch( );pbuf-;lrparse( );if (lr=ACC)

44、 return ACC;/*disp1*/第十一章第十一章 小型編譯程序小型編譯程序 disp1( )int temp1=0;printf(n*詞法分析結(jié)果*n);for(temp1=0;temp1count;temp1+)第十一章第十一章 小型編譯程序小型編譯程序 printf(%dt%dn,buftemp1.sy1,buftemp1.pos);if (temp1=20)printf(Press any key to continue.n);getch( );getch ( );第十一章第十一章 小型編譯程序小型編譯程序 /*/disp2()int temp1=100;printf(n*四元

45、式分析結(jié)果*n);第十一章第十一章 小型編譯程序小型編譯程序 for(temp1=100;temp1100) fprintf(mfile, n);fprintf(mfile, %dt,temp1);fprintf(mfile, (%st, ,fexptemp1.op);if (fexptemp1.arg1.sy1=ident)fprintf(mfile, %st, ,ntab1fexptemp1.arg1.pos);第十一章第十一章 小型編譯程序小型編譯程序 else if (fexptemp1.arg1.sy1=tempsy)f p r i n t f ( m f i l e , T % d

46、 t , ,fexptemp1.arg1.pos);elseif(fexptemp1.arg1.sy1=intconst)fprintf(mfile, %dt, ,fexptemp1.arg1.pos);else fprintf(mfile, t, );第十一章第十一章 小型編譯程序小型編譯程序 if (fexptemp1.arg2.sy1=ident)f p r i n t f ( m f i l e , % s t , ,ntab1fexptemp1.arg2.pos);elseif (fexptemp1.arg2.sy1=tempsy)f p r i n t f ( m f i l e

47、, T % d t , ,fexptemp1.arg2.pos);else 第十一章第十一章 小型編譯程序小型編譯程序 if(fexptemp1.arg2.sy1=intconst)fprintf(mfile, %dt, ,fexptemp1.arg2.pos);else fprintf(mfile, t, );第十一章第十一章 小型編譯程序小型編譯程序 if (fexptemp1.op0!= j)if(fexptemp1.result=100)fprintf(mfile, T%dt) ,fexptemp1.result-100);e l s e f p r i n t f ( m f i l

48、 e , % s t ) ,ntab1fexptemp1.result);第十一章第十一章 小型編譯程序小型編譯程序 else fprintf(mfile, %dt) ,fexptemp1.result);if (temp1=20)printf(nPress any key to continue.n);getch( );第十一章第十一章 小型編譯程序小型編譯程序 getch( );disp3( )int tttt;printf(nn程序總共%d行,產(chǎn)生了%d個二元式!n,lnum,count);getch( );printf(n*變量表*n);for(tttt=0;tttttt1;tttt+

49、) printf(%dt%sn,tttt,ntab1tttt);getch();第十一章第十一章 小型編譯程序小型編譯程序 / * * * * * * * * * * * * * * * * * 主 程 序*/main()cfile=fopen(pas.dat, r); mfile=fopen(pas.med, w);/*打開c語言源文件*/readch( );/*從源文件讀一個字符*/scan( );第十一章第十一章 小型編譯程序小型編譯程序 /*詞法分析*/disp1( );disp3( );stacksp.pos=0;stacksp.sy1=-1;/*初始化狀態(tài)棧棧底*/stack1sp

50、1=0;/*初始化狀態(tài)棧棧底*/第十一章第十一章 小型編譯程序小型編譯程序 oth.sy1=-1;printf(n*狀態(tài)棧變化過程及規(guī)約順序*n);readnu( );/*從二元式讀一個字符*/lrparse( );getch( );/*四元式的分析*/disp2( );printf(n程序結(jié)束。謝謝使用!n);printf(COPYRIGHT BY Z.Y.P 2001.6.7);getch( );第十一章第十一章 小型編譯程序小型編譯程序 11.2 11.2 四元式到匯編語言程序的編譯程序四元式到匯編語言程序的編譯程序COMPILERCOMPILER1. COMPILER的組成 編譯程序C

51、OMPILER由1個工程文件,5個 *.h 文件,6個 *.C 文件組成。 (1) 1個工程文件為 compiler.prj 第十一章第十一章 小型編譯程序小型編譯程序 (2) 5個 *.h 文件為 globals.h read.h symtab.h block.h cgen.h第十一章第十一章 小型編譯程序小型編譯程序 (3) 6個*.C 文件為 main.c read.c symtab.c block.c code.c cgen.c第十一章第十一章 小型編譯程序小型編譯程序 2. 工程文件/*/* compiler.prj */* 四元式到匯編工程文件 */*/第十一章第十一章 小型編譯程

52、序小型編譯程序 D:tc源程序main.cD:tc源程序read.cD:tc源程序symtab.cD:tc源程序block.cD:tc源程序code.cD:tc源程序cgen.c第十一章第十一章 小型編譯程序小型編譯程序 3. *.h文件/*/* 文件: globals.h */*/第十一章第十一章 小型編譯程序小型編譯程序 #ifndef _GLOBALS_H_#define _GLOBALS_H_#include #include #include #include #ifndef FALSE#define FALSE 0#endif第十一章第十一章 小型編譯程序小型編譯程序 #ifnde

53、f TRUE#define TRUE 1#endif/*四元式的結(jié)構(gòu)類型*/typedef struct MLanguage unsigned int line; char op4; char opv110; char opv210; char result10; struct MLanguage * next; *ml;第十一章第十一章 小型編譯程序小型編譯程序 /*符號表每個元素的結(jié)構(gòu)*/typedef struct VariableRec char name10; int regstats; int regnum; struct variableRec * next; * vr; 第十一

54、章第十一章 小型編譯程序小型編譯程序 extern FILE* source; /* 中間代碼的源文件 */extern FILE* code; /* 匯編語言的目標文件 */ #endif /*/* 文件: read.h */* 讀取中間語言源文件的接口。 */*/第十一章第十一章 小型編譯程序小型編譯程序 #ifndef _READ_H_#define _READ_H_/*過程read_mlanguage讀取源文件以指定格式輸入到內(nèi)存。*/void read_mlanguage( ); #endif/*/* 文件:symtab.h */* 訪問符號表的接口。 */*/第十一章第十一章 小型

55、編譯程序小型編譯程序 #ifndef _SYMTAB_H_#define _SYMTAB_H_/*過程st_insert把程序變量插入符號表。*/void st_insert( char name ,vr Table ); /*過程insert根據(jù)變量是變量還是臨時變量,然后執(zhí)行st_insert過程 把不同的變量插入相應(yīng)的符號表。*/void insert( char name );第十一章第十一章 小型編譯程序小型編譯程序 /*建立符號表的函數(shù),變量和臨時變量保存在不同的符號表中。*/void buildsymtab( ); /*在符號表中查找變量,返回所需變量在符號表中的地址。*/vr

56、st_lookup(char name ,vr Table ); /*判斷是變量還是臨時變量,然后在不同的符號表中查找*/vr lookup( char name );第十一章第十一章 小型編譯程序小型編譯程序 /*建立數(shù)據(jù)段的主程序,臨時變量不存入數(shù)據(jù)段。*/void create_dataseg( ); #endif /*/* 文件: block.h */* 建立基本塊的接口。 */*/第十一章第十一章 小型編譯程序小型編譯程序 #ifndef _BLOCK_H_#define _BLOCK_H_ /*過程create_basicblock為中間語言建立基本塊,并按行號從小到大的順序排列。

57、*/void create_basicblock( );#endif/*/* 文件: cgen.h */* 執(zhí)行中間語言的分析,調(diào)用相應(yīng)的子程序 */* 來完成匯編代碼的產(chǎn)生的接口。 */*/第十一章第十一章 小型編譯程序小型編譯程序 #ifndef _CGEN_H_#define _CGEN_H_ /*過程codegen產(chǎn)生匯編目標語言的初始化程序段,*/*并調(diào)用相應(yīng)的函數(shù)產(chǎn)生程序的數(shù)據(jù)段和代碼段,*/*字符串變量codefile為目標文件的文件名,*/*用于在編譯過程中作為注釋在屏幕上打印出文件名。*/void codegen( char * codefile );#endif第十一章第十

58、一章 小型編譯程序小型編譯程序 4. *.c文件/*/* 文件: main.c */* 中間語言編譯的主程序。 */*/#include globals.h#include read.h#include symtab.h#include block.h#include cgen.h第十一章第十一章 小型編譯程序小型編譯程序 /* allocate global variables */FILE * source;FILE * code; int Error = FALSE;main( ) char filename20,* codefilename,ch ; /* 中間代碼源文件名 */ pr

59、intf(是否從鍵盤輸入四元式(y/n)?: ); ch = getch(); if( ch != y )第十一章第十一章 小型編譯程序小型編譯程序 printf(n請輸入源文件的文件名:); scanf(%s,filename); if( strchr (filename, . ) = NULL ) strcat(filename, .med); source = fopen(filename, r); if( source = NULL ) fprintf(stderr, n文件 %s 沒有找到!n,filename); exit(1);第十一章第十一章 小型編譯程序小型編譯程序 if (

60、! Error) int fnlen = strcspn(filename, . ); codefilename= (char *) calloc(fnlen+5, sizeof(char); strncpy(codefilename,filename,fnlen); strcat(codefilename, .asm); code = fopen(codefilename, w); if( code = NULL ) printf(不能打開 %s 文件!n,codefilename); exit(1); 第十一章第十一章 小型編譯程序小型編譯程序 read_file( filename ); buildsymtab ( ); create_basicblock( ); code

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論