编译原理上机实验程序(LR分析).docx
《编译原理上机实验程序(LR分析).docx》由会员分享,可在线阅读,更多相关《编译原理上机实验程序(LR分析).docx(20页珍藏版)》请在课桌文档上搜索。
1、小型编译程序:高级语言到四元式的编译ttincludestdio.h/*如果使用TC的话,需要配置头文件路径*/#includestring.h/*如果使用VC的话,可以配置头文件conio.h*/ftdefineACC-2/*/ftdefinesy_if0ftdefinesythen1#definesyeIse2#definesywhile3#definesybegin4ftdefinesydo5ftdefinesyend6ftdefinea7ftdefinesemicolon8ttdefinee9#definejinghao10ftdefineS11ftdefineL12#definete
2、mpsy15ftdefineEA18*Eand*/ftdefineEO19*Eor*/#defineplus34#definetimes36ftdefinebecomes38ftdefineopand39ftdefineopor40ftdefineopnot41ftdefinerop42#define!parent48ftdefinerparent49ftdefineident56ftdefineintconst57/*/charch-0,;/*从字符缓冲区读取当前字符*/intcount=0;/*词法分析结果缓冲区计数器*/staticcharSPening10=;*存放识别的单词符号*/s
3、taticcharIine81=;*一行字符缓冲区,最多80个字符*/charSpline;/*字符缓冲区指针*/staticcharntabl10010;/*变量名表,共100项,每项长度10*/structntabintinttc;fc;ntab2200;intIabel=O;/*真值*/*假值*/*在布尔表达式E中保存有关布尔变量的真、假值*/*指向ntab2的指针*/*存放临时变量的表的定义*/structrwordscharsp10;intsy;);*保存字表)匹配表的结构,用来与输入缓冲区中的单词进行匹配*/*匹配表初始化,大小为10*/structrwordsreswords10
4、-zzifzz,sy_if),do,sy_do,z,elsezz,sy_else,whilez,sy_while,thenzz,sy_then),zbegin,z,sy_begin,end,sy_end,and,opand,zzorzz,opor,structaaintsyl;intpos;buf1000,n,nl,E,sstack100,ibuf100,stack1000;structaaoth;not,opnot);/*存放单词符号的种别编码*/*存放单词符号自身的值*/*词法分析结果缓冲区,保存识别出来的单词符号*/*读取二元式的当前符号*/*当前表达式中的符号*/*非终结符*/*算术或
5、布尔表达式加工处理使用的符号栈*/*算术或布尔表达式使用的缓冲区*/*语法分析加工处理使用的符号栈*/*四元式中空白位置*/structfourexpcharop10;structaaargl;structaaarg2;intresult;fexp200;*四元式的结构定义*/intssp=O;/*指向SStaCk栈指针*/structaa*pbuf=buf;/*指向词法分析缓冲区的指针*/intnlength=O;intInum=O;/*词法分析中记录单词的长度*/*源程序行数记数,源程序长度*/intttl=O;FILEdefile;Afile*mfiie;*/*变量名表指针*/*源程序文
6、件,为结束符*/intnewt=0;intnxq=100;/*临时变量计数器*/*nxq指向下一个形成的四元式的地址*/*每次执行gen)时,地址自动增1*/i11tIr;/*扫描LR分析表1过程中保存的当前状态值*/intIrl;intsp=O;/*扫描LR分析表2或表3所保存的当前状态值*/*查找LR分析表时状态栈的栈顶指针*/intStackl100;intspl=O;intnum=0;struct11intnxql;inttcl;intfcl;labelmark10;/*状态栈1的定义*/*状态栈1的栈顶指针*/*算术或布尔表达式缓冲区指针*/*记录下一条四元式的地址*/*真值链*/*
7、假值链*/*记录语句嵌套层次的数组,*/*即记录嵌套中每层的布尔表达式E的首地址*/intIabeltempElO;/*记录语句嵌套层次的数组,*/*即记录每层else之前的四元式地址*/intpointmark=-l,pointtemp=-l;/5IcIabelmark数组指针*/*labeltemp数组指针*/intsign=O;Asign=I,为赋值语句;sign=2,为布尔表达式。/*程序语句LR分析表*/staticintaction1913=/*0*/2,-1,-1,3,4,-1,-1,5,-1,-1,-1,-1,-1),/*1*/-l,-1,-1,-1,-1,-1,-1,-1,-
8、1,-1,ACC,-1,-1),/*2*/-l,-l,-1,-1,-1,-1,-1,-1,-1,6,-1,-1,-1,/*3*/-l,-l,-1,-1,-1,-1,-1,-1,-1,7,-l,-l,-l,/*4*/2,-l,-l,3,4,-1,-1,5,-1,-1,-1,9,8),/*5*/-l,-1,104,-1,-1,-1,104,-1,104,-1,104,-1,-1,/*6*/1,10,十-1,1,1,1,1,1,1,1,1,/*7*/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*T,-1,-1,-1,-
9、1,-1,105,-1,13,-1,-1,-1,T,/*10*/2,-l,-l,3,4,-1,-1,5,-1,-1,-1,14,-1),/*11*/2,-l,-l,3,4,-1,-1,5,-1,-1,-1,15,-1),*12*/-l,-1,103,-1,-1,-1,103,-1,103,-1,103,-1,-1,/*13*/2,-l,-l,3,4,-1,-1,5,-1,-1,-1,9,16),/*14*/-l,-1,17,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1),/*15*/-l,-1,102,-1,-1,-1,102,-1,102,-1,102,-1,-1),/*16*
10、/-l,-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*/-l,-1,101,-1,-1,-1,101,-1,101,-1,101,-1,-1;/*算术表示式的LR分析表*/staticintactionl107=/*0*/3,-l,-l,2,-l,-l,1,/*1*/-l,4,5,-l,-l,ACC,-l,/*2*/3,-l,-l,2,-l,-l,6,*3*-l,104,104,-1,104,104,-1,/*4*/3,-l,-l,2,-l,-l,7,/*5*/3,-l,
11、-l,2,-l,-l,8,/*6*/-l,4,5,-l,9,-l,-l,/*7*/-l,101,5,-1,101,101,-l,/*8*/-l,102,102,-1,102,102,-1,/*9*/-l,103,103,-1,103,103,-1;/*布尔表示式的LR分析表*/staticintaction21611=/*0*/1,-1,4,-1,5,-1,-1,-1,13,7,8,/*1*/-l,2,-l,101,-1,101,101,o,-,-,-,/*2*/3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1),*3*-l,-l,-l,102,-1,102,102,102,-1
12、,-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*/-l,-l,-l,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,-1,-1,-1,-1),/*10*/107,-1,107,-1,107,-1,-1,-1,-1,-1,-1),/*11*/-l,-l,-l,12,-1,9,10,-l,-l,
13、-l,-l,*12*/-l,-l,-l,103,-1,103,103,103,-1,-1,-1),/*13*/-l,-l,-1,-1,-1,9,10,ACC,-1,-1,-1),/*14*/-l,-l,-l,106,-1,9,10,106,-1,-1,-1),/*15*/-l,-l,-l,108,-1,9,10,108,-1,-1,-1;/*从文件读一4亍至U缓冲区*/voidreadline()charchi;pline=line;chl=fgetc(cfile);while(chi!=,n&!feof(cfile)line=chl;pline+;chl=fgetc(cfile);*plin
14、e=0,;pline=line;/*从缓冲区读取一*个字符*/voidreadch()(if(Ch=0)readline();Inum+;)Ch=*pline;pline+;)/*初利白勺另*/find(charspel)intssl=O;intii=0;while(SSI=O)&(ii=,a,)&(Ch=z,)(ch=,0)&(Ch二9,);pline一一;SPeningk=0,;while(SS=O)&(iii10)if(!strcmp(spelling,reswordsiii.sp)ss=l;iii+;*关键字匹配*/if(ss=l)bufcount.syl=reswordsiii-l.
15、sy;)elsebufcount.syl=ident;j=find(spelling);if(j=-l)bufcount.pos=ttl;strcpy(ntablttl,spelling);ttl+;nlength+;elsebufcount.pos=j;)count+;for(k=0;k三,0)&(Ch=9,);bufcount.syl=intconst;bufcount.pos=ivalue;count+;pline一;/*扫描主函数*/voidscan()while(Ch!=)switch(ch)casebreak;casea,:caseb,:case,c,:cased,:casee,:
16、case,f,:caseg,:caseh,:casei,:casej,:casek,:case:casem,:case,n,:caseo,:casep,:caseq,:caser,:cases,:caset,:caseu,:casev,:case,w,:case,:casey,:casez,:identifier();break;case0,:case:case2,:case3,:case4,:case,5,:case65:case7,:case,8,:case95:number();break;case,)bufcount.pos=4;elsebufcount.pos=l;pline一;)b
17、ufcount.syl=rop;count+;break;case,:readch();if(ch=,=,)(bufcount.pos=2;)elsebufcount.pos=3;pline一;)bufcount,syl=rop;count+;break;case,(,:bufcount.Syl=Iparent;count+;break;case):bufcount.syl-rparent;count+;break;case#:bufcount.syl=jinghao;count+;break;case+:bufcount.syl=plus;count+;break;case*:bufcoun
18、t.syl=times;count+;break;case:readch();if(ch=,=,)bufcount.syl=becomes;count+;break;case-:bufcount.syl=rop;bufcount.pos=5;count+;break;case:bufcount.syl=semicolon;count+;break;)readch();)bufcount.syl=l;voidreadnu()(if(pbuf-syl=O)n.syl=pbuf-syl;n.pos=pbuf-pos;pbuf+;)/*中间变量的生成*/newtemp()newt+;returnnew
19、t;)/*生成四元式*/gen(charopl,structaaargil,structaaarg22,intresultl)strcpy(fexpnxq.op,opl);fexpnxq.argl.syl=argll.syl;fexpnxq.argl.pos=argll.pos;fexpnxq.arg2.syl=arg22.syl;fexpnxq.arg2.pos=arg22.pos;fexpnxq.result=resultl;nxq+;returnnxq-1;/*布尔表示式的匹酉己*/merg(intpl,intp2)intp;if(p2=0)returnpl;elseP=p2;while
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 编译 原理 上机 实验 程序 LR 分析

链接地址:https://www.desk33.com/p-1868956.html