首页 编译器源代码

编译器源代码

举报
开通vip

编译器源代码#include"stdio.h"#include"string.h"#defineACC-2/**************************************/#definesy_if0#definesy_then1#definesy_else2#definesy_while3#definesy_begin4#definesy_do5#definesy_end6#definea7#definesemicolon8#definee9#definejinghao10#defineS11#defineL12...

编译器源代码
#include"stdio.h"#include"string.h"#defineACC-2/**************************************/#definesy_if0#definesy_then1#definesy_else2#definesy_while3#definesy_begin4#definesy_do5#definesy_end6#definea7#definesemicolon8#definee9#definejinghao10#defineS11#defineL12#definetempsy15#defineEA18#defineE019#defineplus34#definetimes36#definebecomes38#defineop_and39#defineop_or40#defineop_not41#definerop42#definelparent48#definerparent49#defineident56#defineintconst57/********************************************/charch='\0';intcount=0;staticcharspelling[10]={""};staticcharline[81]={""};char*pline;staticcharntab1[100][10];structntab{inttc;intfc;}ntab2[200];intlabel=0;structrwords{charsp[10];intsy;};structrwordsreswords[10]={{"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",op_not}};structaa{intsy1;intpos;}buf[1000],n,n1,E,sstack[100],ibuf[100],stack[1000];structaaoth;structfourexp{charop[10];structaaarg1;structaaarg2;intresult;}fexp[200];intssp=0;structaa*pbuf=buf;intnlength=0;intlnum=0;inttt1=0;FILE*cfile;/********************************************************/intnewt=0;intnxq=100;intlr;intlr1;intsp=0;intstack1[100];intsp1=0;intnum=0;structll{intnxq1;inttc1;intfc1;}labelmark[10];intlabeltemp[10];intpointmark=-1,pointtemp=-1;intsign=0;/********************³ÌÐòÓï¾äµÄLR·ÖÎö±í********************/staticintaction[19][13]={{2,-1,-1,3,4,-1,-1,5,-1,-1,10,1,-1},{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,ACC,-1,-1},{-1,-1,-1,-1,-1,-1,-1,-1,-1,6,-1,-1,-1},{-1,-1,-1,-1,-1,-1,-1,-1,-1,7,-1,-1,-1},{2,-1,-1,3,4,-1,-1,5,-1,-1,-1,9,8},{-1,-1,104,-1,-1,-1,104,-1,104,-1,104,-1,-1},{-1,10,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},{-1,-1,-1,-1,-1,11,-1,-1,-1,-1,-1,-1,-1},{-1,-1,-1,-1,-1,-1,12,-1,-1,-1,-1,-1,-1},{-1,-1,-1,-1,-1,-1,105,-1,13,-1,-1,-1,-1},{2,-1,-1,3,4,-1,-1,5,-1,-1,-1,14,-1},{2,-1,-1,3,4,-1,-1,5,-1,-1,-1,15,-1},{-1,-1,103,-1,-1,-1,103,-1,103,-1,103,-1,-1},{2,-1,-1,3,4,-1,-1,5,-1,-1,-1,9,16},{-1,-1,17,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},{-1,-1,102,-1,-1,-1,102,-1,102,-1,102,-1,-1},{-1,-1,-1,-1,-1,-1,106,-1,-1,-1,-1,-1,-1},{2,-1,-1,3,4,-1,-1,5,-1,-1,-1,18,-1},{-1,-1,101,-1,-1,-1,101,-1,101,-1,101,-1,-1}};/********************ËãÊõ±í´ïʽµÄLR·ÖÎö±í********************/staticintaction1[10][7]={{3,-1,-1,2,-1,-1,1},{-1,4,5,-1,-1,ACC,-1},{3,-1,-1,2,-1,-1,6},{-1,104,104,-1,104,104,-1},{3,-1,-1,2,-1,-1,7},{3,-1,-1,2,-1,-1,8},{-1,4,5,-1,9,-1,-1},{-1,101,5,-1,101,101,-1},{-1,102,102,-1,102,102,-1},{-1,103,103,-1,103,103,-1}};/********************²¼¶û±í´ïʽµÄLR·ÖÎö±í*********************/staticintaction2[16][11]={{1,-1,4,-1,5,-1,-1,-1,13,7,8},{-1,2,-1,101,-1,101,101,101,-1,-1,-1},{3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},{-1,-1,-1,102,-1,102,102,102,-1,-1,-1},{1,-1,4,-1,5,-1,-1,-1,11,7,8},{1,-1,4,-1,5,-1,-1,-1,6,7,8},{-1,-1,-1,104,-1,9,10,104,-1,-1,-1},{1,-1,4,-1,5,-1,-1,-1,14,7,8},{1,-1,4,-1,5,-1,-1,-1,15,7,8},{105,-1,105,-1,105,-1,-1,-1,-1,-1,-1},{107,-1,107,-1,107,-1,-1,-1,-1,-1,-1},{-1,-1,-1,12,-1,9,10,-1,-1,-1,-1},{-1,-1,-1,103,-1,103,103,103,-1,-1,-1},{-1,-1,-1,-1,-1,9,10,ACC,-1,-1,-1},{-1,-1,-1,106,-1,9,10,106,-1,-1,-1},{-1,-1,-1,108,-1,9,10,108,-1,-1,-1}};/********************´ÓÎļþ¶ÁÒ»Ðе½»º³åÇø**********************/readline(){charch1;pline=line;ch1=fgetc(cfile);while((ch1!='\n')&&(ch1!=EOF)){*pline=ch1;pline++;ch1=fgetc(cfile);}*pline='\0';pline=line;}/**********************´Ó»º³åÇø¶ÁÈ¡Ò»¸ö×Ö·û*********************/readch(){if(ch=='\0'){readline();lnum++;}ch=*pline;pline++;}/***********************±êʶ·ûºÍ¹Ø¼ü×ÖµÄʶ±ð********************/find(charspel[]){intss1=0;intii=0;while((ss1==0)&&(ii='a')&&(ch<='z'))||((ch>='0')&&(ch<='9')));pline--;spelling[k]='\0';while((ss==0)&&(iii<10)){if(!strcmp(spelling,reswords[iii].sp))ss=1;iii++;}/*¹Ø¼ü×ÖÆ¥Åä*/if(ss==1){buf[count].sy1=reswords[iii-1].sy;}else{buf[count].sy1=ident;j=find(spelling);if(j==-1){buf[count].pos=tt1;strcpy(ntab1[tt1],spelling);tt1++;nlength++;}elsebuf[count].pos=j;}count++;for(k=0;k<10;k++)spelling[k]='';}/**********************Êý×Öʶ±ð*************************/number(){intivalue=0;intdigit;do{digit=ch-'0';ivalue=ivalue*10+digit;readch();}while((ch>='0')&&(ch<='9'));buf[count].sy1=intconst;buf[count].pos=ivalue;count++;pline--;}/***********************ɨÃèÖ÷³ÌÐò************************/scan(){inti;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':case'2':case'3':case'4':case'5':case'6':case'7':case'8':case'9':number();break;case'<':readch();if(ch=='=')buf[count].pos=0;else{if(ch=='>')buf[count].pos=4;else{buf[count].pos=1;pline--;}}buf[count].sy1=rop;count++;break;case'>':readch();if(ch=='=')buf[count].pos=2;else{buf[count].pos=3;pline--;}buf[count].sy1=rop;count++;break;case'(':buf[count].sy1=lparent;count++;break;case')':buf[count].sy1=rparent;count++;break;case'#':buf[count].sy1=jinghao;count++;break;case'+':buf[count].sy1=plus;count++;break;case'*':buf[count].sy1=times;count++;break;case':':readch();if(ch=='=')buf[count].sy1=becomes;count++;break;case'=':buf[count].sy1=rop;buf[count].pos=5;count++;break;case';':buf[count].sy1=semicolon;count++;break;}readch();}buf[count].sy1=-1;}/*******************************************************/readnu(){if(pbuf->sy1>=0){n.sy1=pbuf->sy1;n.pos=pbuf->pos;pbuf++;}}/***********************Öмä±äÁ¿µÄÉú³É*********************/newtemp(){newt++;returnnewt;}/***********************Éú³ÉËÄԪʽ**************************/gen(charop1[],structaaarg11,structaaarg22,intresult1){strcpy(fexp[nxq].op,op1);fexp[nxq].arg1.sy1=arg11.sy1;fexp[nxq].arg1.pos=arg11.pos;fexp[nxq].arg2.sy1=arg22.sy1;fexp[nxq].arg2.pos=arg22.pos;fexp[nxq].result=result1;nxq++;returnnxq-1;}/***********************²¼¶û±í´ïʽµÄÆ¥Åä**********************/merg(intp1,intp2){intp;if(p2==0)returnp1;else{p=p2;while(fexp[p].result!=0)p=fexp[p].result;fexp[p].result=p1;returnp2;}}backpatch(intp,intt){inttempq;intq;q=p;while(q!=0){tempq=fexp[q].result;fexp[q].result=t;q=tempq;}}/*********************************************************/change1(intchan){switch(chan){caseident:caseintconst:return0;caseplus:return1;casetimes:return2;caselparent:return3;caserparent:return4;casejinghao:return5;casetempsy:return6;}}change2(intchan){switch(chan){caseident:caseinconst:return0;caserop:return1;caselparent:return2;caserparent:return3;caseop_not:return4;caseop_and:return5;caseop_or:return6;casejinghao:return7;casetempsy:return8;caseEA:return9;caseE0:return10;}}/*********************¸³ÖµÓï¾äµÄ·ÖÎö**********************/lrparse1(intnum){lr1=action1[stack1[sp1]][change1(n1.sy1)];if(lr1==-1){printf("\nËãÊõ±í´ïʽ»ò¸³ÖµÓï¾ä³ö´í!\n");getch();exit(0);}if((lr1<10)&&(lr1>=0)){sp1++;stack1[sp1]=lr1;if(n1.sy1!=tempsy){ssp++;num++;sstack[ssp].sy1=n1.sy1;sstack[ssp].pos=n1.pos;}n1.sy1=ibuf[num].sy1;n1.pos=ibuf[num].pos;lrparse1(num);}if((lr1>=100)&&(lr1<105)){switch(lr1){case100:break;case101:E.pos=newtemp();gen("+",sstack[ssp-2],sstack[ssp],E.pos+100);ssp=ssp-2;sstack[ssp].sy1=tempsy;sstack[ssp].pos=E.pos;sp1=sp1-3;break;case102:E.pos=newtemp();gen("*",sstack[ssp-2],sstack[ssp],E.pos+100);ssp=ssp-2;sstack[ssp].sy1=tempsy;sstack[ssp].pos=E.pos;sp1=sp1-3;break;case103:E.pos=sstack[ssp-1].pos;ssp=ssp-2;sstack[ssp].sy1=tempsy;sstack[ssp].pos=E.pos;sp1=sp1-3;break;case104:E.pos=sstack[ssp].pos;sp1--;break;}n1.sy1=tempsy;n1.pos=E.pos;lrparse1(num);}if((lr1==ACC)&&(stack1[sp1]==1)){gen(":=",sstack[ssp],oth,ibuf[0].pos);ssp=ssp-3;sp1=sp1-3;}}/***********************²¼¶û±í´ïʽµÄ·ÖÎö**********************/lrparse2(intnum){inttemplabel;lr1=action2[stack1[sp1]][change2(n1.sy1)];if(lr1==-1){if(sign==2)printf("\nwhileÓï¾ä³ö´í\n");if(sign==3)printf("\nifÓï¾ä³ö´í\n");getch();exit(0);}if((lr1<16)&&(lr1>=0)){sp1++;stack1[sp1]=lr1;ssp++;sstack[sp1].sy1=n1.sy1;sstack[ssp].pos=n1.pos;if((n1.sy1!=tempsy)&&(n1.sy1!=EA)&&(n1.sy1!=E0))num++;n1.sy1=ibuf[num].sy1;n1.pos=ibuf[num].pos;lrparse2(num);}if((lr1>=100)&&(lr1<109)){switch(lr1){case100:break;case101:ntab2[label].tc=nxq;ntab2[label].fc=nxq+1;gen("jnz",sstack[ssp],oth,0);gen("j",oth,oth,0);sp1--;ssp--;label++;n1.sy1=tempsy;break;case102:ntab2[label].tc=nxq;ntab2[label].fc=nxq+1;switch(sstack[ssp-1].pos){case0:gen("j<=",sstack[ssp-2],sstack[ssp],0);break;case1:gen("j<",sstack[ssp-2],sstack[ssp],0);break;case2:gen("j>=",sstack[ssp-2],sstack[ssp],0);break;case3:gen("j>",sstack[ssp-2],sstack[ssp],0);break;case4:gen("j<>",sstack[ssp-2],sstack[ssp],0);break;case5:gen("j=",sstack[ssp-2],sstack[ssp],0);break;}gen("j",oth,oth,0);sp1=sp1-3;ssp=ssp-3;label++;n1.sy1=tempsy;break;case103:label=label-1;ssp=ssp-3;sp1=sp1-3;label++;n1.sy1=tempsy;break;case104:label=label-1;templabel=ntab2[label].tc;ntab2[label].tc=ntab2[label].fc;ntab2[label].fc=templabel;ssp=ssp-2;sp1=sp1-2;label++;n1.sy1=tempsy;break;case105:backpatch(ntab2[label-1].tc,nxq);label=label-1;ssp=ssp-2;sp1=sp1-2;label++;n1.sy1=EA;break;case106:label=label-2;ntab2[label].tc=ntab2[label+1].tc;ntab2[label].fc=merg(ntab2[label].fc,ntab2[label+1].fc);ssp=ssp-2;sp1=sp1-2;label++;n1.sy1=tempsy;break;case107:backpatch(ntab2[label-1].fc,nxq);label=label-1;ssp=ssp-2;sp1=sp1-2;label++;n1.sy1=E0;break;case108:label=label-2;ntab2[label].fc=ntab2[label+1].fc;ntab2[label].tc=merg(ntab2[label].tc,ntab2[label+1].tc);ssp=ssp-2;sp1=sp1-2;label++;n1.sy1=tempsy;break;}lrparse2(num);}if(lr1==ACC)return1;}/************²âÊÔ×Ö·ûÊÇ·ñΪ±í´ïʽÖеÄÖµ(²»°üÀ¨":")****************/test(intvalue){switch(value){caseintconst:caseident:caseplus:casetimes:casebecomes:caselparent:caserparent:caserop:caseop_and:caseop_or:caseop_not:return1;default:return0;}}/***************************************************************/lrparse(){inti1=0;intnum=0;if(test(n.sy1)){if(stack[sp].sy1==sy_while)sign=2;else{if(stack[sp].sy1==sy_if)sign=3;elsesign=1;}do{ibuf[i1].sy1=n.sy1;ibuf[i1].pos=n.pos;readnu();i1++;}while(test(n.sy1));ibuf[i1].sy1=jinghao;pbuf--;sstack[0].sy1=jinghao;ssp=0;if(sign==1){sp1=0;stack1[sp1]=0;num=2;n1.sy1=ibuf[num].sy1;n1.pos=ibuf[num].pos;lrparse1(num);n.sy1=a;}if((sign==2)||(sign==3)){pointmark++;labelmark[pointmark].nxq1=nxq;sp1=0;stack1[sp1]=0;num=0;n1.sy1=ibuf[num].sy1;n1.pos=ibuf[num].pos;lrparse2(num);labelmark[pointmark].tc1=ntab2[label-1].tc;labelmark[pointmark].fc1=ntab2[label-1].fc;backpatch(labelmark[pointmark].tc1,nxq);n.sy1=e;}}lr=action[stack[sp].pos][n.sy1];printf("stack[%d]=%d\t\tn=%d\t\tlr=%d\n",sp,stack[sp].pos,n.sy1,lr);if((lr<19)&&(lr>=0)){sp++;stack[sp].pos=lr;stack[sp].sy1=n.sy1;readnu();lrparse();}if((lr<=106)&&(lr>=100)){switch(lr){case100:break;case101:printf("S->ifethenselses¹éÔ¼\n");sp=sp-6;n.sy1=S;fexp[labeltemp[pointtemp]].result=nxq;pointtemp--;if(stack[sp].sy1==sy_then){gen("j",oth,oth,0);backpatch(labelmark[pointmark].fc1,nxq);pointtemp++;labeltemp[pointtemp]=nxq-1;}pointmark--;if(stack[sp].sy1==sy_do){gen("j",oth,oth,labelmark[pointmark].nxq1);backpatch(labelmark[pointmark].fc1,nxq);}break;case102:printf("S->whileedos¹éÔ¼\n");sp=sp-4;n.sy1=S;pointmark--;if(stack[sp].sy1==sy_do){gen("j",oth,oth,labelmark[pointmark].nxq1);backpatch(labelmark[pointmark].fc1,nxq);}if(stack[sp].sy1==sy_then){gen("j",oth,oth,0);fexp[labelmark[pointmark].fc1].result=nxq;pointtemp++;labeltemp[pointtemp]=nxq-1;}break;case103:printf("S->beginLend¹éÔ¼\n");sp=sp-3;n.sy1=S;if(stack[sp].sy1==sy_then){gen("j",oth,oth,0);backpatch(labelmark[pointmark].fc1,nxq);pointtemp++;labeltemp[pointtemp]=nxq-1;}if(stack[sp].sy1==sy_do){gen("j",oth,oth,labelmark[pointmark].nxq1);backpatch(labelmark[pointmark].fc1,nxq);}getch();break;case104:printf("S->a¹éÔ¼\n");sp=sp-1;n.sy1=S;if(stack[sp].sy1==sy_then){gen("j",oth,oth,0);backpatch(labelmark[pointmark].fc1,nxq);pointtemp++;labeltemp[pointtemp]=nxq-1;}if(stack[sp].sy1==sy_do){gen("j",oth,oth,labelmark[pointmark].nxq1);backpatch(labelmark[pointmark].fc1,nxq);}break;case105:printf("L->S¹éÔ¼\n");sp=sp-1;n.sy1=L;break;case106:printf("L->S;L¹éÔ¼\n");sp=sp-3;n.sy1=L;break;}getch();pbuf--;lrparse();}if(lr==ACC)returnACC;}/*************************disp1*******************************/disp1(){inttemp1=0;printf("\n*****************´Ê·¨·ÖÎö½á¹û********************\n");for(temp1=0;temp1=100)printf("T%d\t)",fexp[temp1].result-100);elseprintf("%s\t)",ntab1[fexp[temp1].result]);}elseprintf("%d\t)",fexp[temp1].result);if(temp1==20){printf("\npressanykeytocontinue......\n");getch();}printf("\n");}getch();}disp3(){inttttt;printf("\n\n³ÌÐò×ܹ²%dÐУ¬²úÉúÁË%d¸ö¶þԪʽ!\n",lnum,count);getch();printf("\n******************±äÁ¿Ãû±í**********************\n");for(tttt=0;tttt
本文档为【编译器源代码】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
个人认证用户
云匠
暂无简介~
格式:pdf
大小:469KB
软件:PDF阅读器
页数:21
分类:
上传时间:2023-04-30
浏览量:7