




已阅读5页,还剩29页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
课 程 设 计 报 告课程设计名称: 编译技术 系 别: 三 系 学生姓名: 班 级: 软件班 学 号: 成 绩: 指导教师: 开课时间: 学年 学期一.设计题目1二.主要内容1三.具体要求1四.进度安排1五.成绩评定2六.设计思路26.1 单词符号及种别表26.2 SLR的分析表36.21 算术表达式的SLR(1)分析表36.22 布尔表达式的LR分析表46.23 程序语句的LR分析表5七.程序运行结果截图6八.设计体会10一.设计题目设计和实现一个简单的小型高级程序设计语言的编译器二.主要内容自定义一种简单的小型高级程序设计语言,采用任意一种编程语言和编程工具设计并实现其编译器,展示编译系统中词法分析和语法分析(LR分析方法)的全过程。三.具体要求3.1对单词的构词规则有明确的定义; 3.2编写的词法分析程序能够正确识别源程序中的单词符号;3.3识别出的单词以的二元式形式保存并输出; 3.4构造出程序设计语言各语法单位的SLR(1)分析表(其他分析表亦可);3.5能够对输入的源程序做出正确的语法分析并输出清晰的结果; 3.6高级语言的规模不易过大,注重编译器内核的设计,对于界面无要求;3.7课程设计报告中应对所自定义高级语言进行简单描述,对编译器的设计进行简单的需求 分析,给出设计说明和程序结构框架,阐明设计思路、用到的原理和方法。3.8不可更改课程设计报告中已有的字体和格式,课程设计报告中新加入内容,汉字采用宋 体五号字,英文字符和数字采用TimesNewRoman五号字,行距全部采用单倍行距。 3.9课程设计报告中的运行结果应以程序运行截图形式出现。 3.10程序源代码以附件形式发送至指定邮箱:。附件名称为:“完整学号+姓名”4. 进度安排序号内容时间(天)1设计题目、查找资料12概要设计13详细设计34调试分析并测试结果15总结并完成报告1总计8天五.成绩评定5.1考核方法:通过现场考勤、检查源程序和批改课程设计报告相结合的方式考察学生的动手能力,独立分析解决问题的能力和创新精神,并根据学生的学习态度综合考评。5.2成绩评定:成绩分优、良、中、及格和不及格五等。6. 设计思路6.1 单词符号及种别表符号种别编码说明sy_if0保留字 ifsy_then1保留字 thensy_else2保留字 elsesy_while3保留字 whilesy_begin4保留字 beginsy_do5保留字 dosy_end6保留字 enda7赋值语句semicolon8“;”e9布尔表达式jinghao10“#”S11语句L12复合语句tempsy15临时变量EA18B and(即布尔表达式中”B”)EO19B and(即布尔表达式中”B”)plus34“+”times36“*”become38“:=”op_and39“and”op_or40“or”op_not41“not”rop42关系运算符lparent48“(”rparent49“)”ident56变量intconst57整常量6.2 SLR的分析表6.21 算术表达式的SLR(1)分析表: 算数表达式文法GE如下:E-E+E|E*E|(E)|i 将文法GE拓广为文法GE:(0)SE (1)EE+E (2)EE*E (3)E(E) (4)Ei由此得到算数表达式的SLR(1)分析表如下:状态ACTIONGOTOi+*()#E0s3s211s4s5acc2s3s263r4r4r44s3s275s3s286s4s5s97r1s5r1r18r2r2r2r29r3r3r3r36.22 布尔表达式的LR分析表布尔表达式的文法如下:B-BB|BB|B|i rop i|i 为了便于语法分析时加工处理,我们将上述文法改为文法GS: B-BAB|B0B|B|(B)|Iropi|i BA-B B0-B 将文法GS拓广为文法GS:(0) S-B (1)B-i (2)B-iropi(3)B-(B)(4)B-NOTB(5)A-BAND(6)B-AB(7)O-BOR(8)B-OB 由此得到布尔表达式的SLR(1)分析表如下:状态ACTIONGOTOirop()NOTANDOR#BAO0s1s4s513781s2r1r1r1r12s33r2r2r2r24s1s4s511785s1s4s56786r4s9s10r47s1s4s514788s1s4s515789r5r5r510r7r7r711s12s9s1012r3r3r3r313s9s10acc14r6s9s10r615R8s9s10r86.23 程序语句的LR分析表:程序语句的文法GS如下S-ifethenSelseS|whileedoS|beginLend|aL-S;L|S由于在编译程序设计与实现中,我们是将赋值语句与算数表达式归为一类处理的,故在此将赋值语句仅看作是程序语句文法中一个终结符a,将布尔表达式B也看作为终结符e。将文法GS拓广为文法GS:(0)S-S(1)S-ifethenSelseS(2)S-whileedoS(3)S-beginLend(4)S-a(5)L-S(6)L-S;L由此得到程序语句的SLR(1)分析表如下:状态ACTIONGOTOifthenelsewhilebegindoenda;e#SL0s2s3s4s511acc2s63s74s2s3s4s5985r4r4r4r46s107s118s129r5s1310s2s3s4s51411s2s3s4s51512r3r3r3r313s2s3s4s591614s1715r2r2r2r216r617s2s3s4s51818r1r1r1r1词法分析:首先,扫描主函数,对构成源程序的字符串进行扫描和分解,识别出一个个单词符号。在扫描到6种关系运算符时,要特别的注意,例如扫描到“”时,要通过readch()函数再读入一个字符,如果是“=”则为“=”关系运算符,则赋予种别编码值。在扫描到终结运算符时,按照种别表赋予对应的sy1。接着,通过for循环,将扫描到的单词符号的种别编码和具体的输出。最后,输出变量名表。语法分析:函数从二元式读入一个字符串,然后进行程序语句处理。最后通过一些的处理输出四元式分析结果。七.程序运行结果截图7.1 词法分析结果7.2 变量名表7.3 状态栈加工过程及规约顺序7.4 四元式分析结果8. 设计体会附源码:#include stdio.h#include string.h#include stdlib.h#define ACC -2/*/#define sy_if 0#define sy_then 1#define sy_else 2#define sy_while 3#define sy_begin 4#define sy_do 5#define sy_end 6#define a 7#define semicolon 8#define e 9#define jinghao 10#define S 11#define L 12#define tempsy 15#define EA 18#define E0 19#define plus 34#define sub 35#define times 36#define div 37#define becomes 38#define op_and 39#define op_or 40#define op_not 41#define rop 42#define lparent 48#define rparent 49#define ident 56#define intconst 57/*/char ch=0;int count=0;static char spelling10=;static char line81=;char *pline;static char ntab110010;struct ntab int tc; int fc; ntab2200;int label=0;struct rwords char sp10; int sy; ;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,op_not;struct aa int sy1; int pos; buf1000, n, n1, E, sstack100, ibuf100, stack1000;struct aa oth;struct fourexp char op10; struct aa arg1; struct aa arg2; int result; fexp200;int ssp=0;struct aa *pbuf=buf;int nlength=0;int lnum=0;int tt1=0;FILE *cfile;/*/int newt=0;int nxq=100;int lr;int lr1;int sp=0;int stack1100;int sp1=0;int num=0;struct ll int nxq1; int tc1; int fc1; labelmark10;int labeltemp10;int pointmark=-1,pointtemp=-1;int sign=0;/*程序语句的LR分析表*/static int action1913= 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分析表*/static int action1149= 3,-1,-1,-1,-1,2,-1,-1,1, -1,4,10,5,11,-1,-1,ACC,-1, 3,-1,-1,-1,-1,2,-1,-1,6, -1,104,104,104,104,-1,104,104,-1, 3,-1,-1,-1,-1,2,-1,-1,7, 3,-1,-1,-1,-1,2,-1,-1,8, -1,4,10,5,11,-1,9,-1,-1, -1,101,101,5,11,-1,101,101,-1, -1,102,102,102,102,-1,102,102,-1, -1,103,103,103,103,-1,103,103,-1, 3,-1,-1,-1,-1,2,-1,-1,12, 3,-1,-1,-1,-1,2,-1,-1,13, -1,105,105,5,11,-1,105,105,-1,-1,106,106,106,106,-1,106,106,-1,;/*布尔表达式的LR分析表*/static int action21611= 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() char ch1; pline=line; ch1=getc(cfile); while(ch1!=n)&(ch1!=EOF) *pline=ch1; pline+; ch1=getc(cfile); *pline=0; pline=line;/*从缓冲区读取一个字符*/readch() if(ch=0) readline(); lnum+; ch=*pline; pline+;/*标识符和关键字的识别*/find(char spel) int ss1=0; int ii=0; while(ss1=0)&(ii=a)&(ch=0)&(ch=9); pline-; spellingk=0; while(ss=0)&(iii10) if(!strcmp(spelling,reswordsiii.sp) ss=1; iii+; /*关键字匹配*/ if(ss=1) bufcount.sy1=reswordsiii-1.sy; else bufcount.sy1=ident; j=find(spelling); if(j=-1) bufcount.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-;/*扫描主程序*/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: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9:number();break; case ) bufcount.pos=4; else bufcount.pos=1; pline-; bufcount.sy1=rop; count+; break; case : readch(); if(ch=) bufcount.pos=2; else bufcount.pos=3; pline-; bufcount.sy1=rop; count+; break; case (: bufcount.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=sub; count+; break; case *: bufcount.sy1=times; count+; break; case /: bufcount.sy1=div; count+; break; case : readch(); if(ch=) bufcount.sy1=becomes; count+; break; 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+; /*中间变量的生成*/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 nxq-1;/*布尔表达式的匹配*/merg(int p1,int p2) int p; if(p2=0) return p1; else p=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; /*/change1(int chan) switch(chan) case ident: case intconst:return 0; case plus:return 1; case sub:return 2; case times:return 3; case div:return 4; case lparent:return 5; case rparent:return 6; case jinghao:return 7; case tempsy:return 8; change2(int chan) switch(chan) case ident: case intconst:return 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 E0:return 10;/*赋值语句的分析*/lrparse1(int num) lr1=action1stack1sp1change1(n1.sy1); if(lr1=-1) printf(n算术表达式或赋值语句出错!n); getchar(); 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)&(lr1107) switch(lr1) case 100:break; case 101:E.pos=newtemp(); gen(+,sstackssp-2,sstackssp,E.pos+100); ssp=ssp-2; sstackssp.sy1=tempsy; sstackssp.pos=E.pos; sp1=sp1-3; break; 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; case 103:E.pos=sstackssp-1.pos; ssp=ssp-2; sstackssp.sy1=tempsy; sstackssp.pos=E.pos; sp1=sp1-3; break; case 104:E.pos=sstackssp.pos; sp1-; break; case 105:E.pos=newtemp(); gen(-,sstackssp-2,sstackssp,E.pos+100); ssp=ssp-2; sstackssp.sy1=tempsy; sstackssp.pos=E.pos; sp1=sp1-3; break; case 106:E.pos=newtemp(); gen(/,sstackssp-2,sstackssp,E.pos+100); ssp=ssp-2; sstackssp.sy1=tempsy; sstackssp.pos=E.pos; sp1=sp1-3; break; n1.sy1=tempsy; n1.pos=E.pos; lrparse1(num); if(lr1=ACC)&(stack1sp1=1) gen(:+,sstackssp,oth,ibuf0.pos); ssp=ssp-3; sp1=sp1-3; /*布尔表达式的分析*/lrparse2(int num) int templabel; lr1=action2stack1sp1change2(n1.sy1); if(lr1=-1) if(sign=2) printf(nwhile语句出错n); if(sign=3) printf(nif语句出错n); getchar(); exit(0); if(lr1=0) sp1+; stack1sp1=lr1; ssp+; sstacksp1.sy1=n1.sy1; sstackssp.pos=n1.pos; if(n1.sy1!=tempsy)&(n1.sy1!=EA)&(n1.sy1!=E0) num+; n1.sy1=ibufnum.sy1; n1.pos=ibufnum.pos; lrparse2(num); if(lr1=
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 大学市政工程试卷及答案
- 小白菜素食产品创新创业项目商业计划书
- 丁二酸装置操作工国庆节后复工安全考核试卷含答案
- 山石工国庆节后复工安全考核试卷含答案
- 建房的合同(标准版)
- 商品材料购销合同(标准版)
- 新项目实施方案
- 外来访客安全培训方案课件
- 手绘工国庆节后复工安全考核试卷含答案
- 福州车位租赁合同(标准版)
- (2025.06.12)领导干部任前应知应会党内法规和法律知识考试题库(2025年度)
- 宜宾党校考试试题及答案
- 关于数据安全管理制度
- 华中师范大学第─附属中学2025届高三下五月高考模拟英语试卷
- 小儿烧伤的护理
- 2025年牛羊肉市场分析报告
- 《民营经济促进法》解读与案例分析课件
- 《技术分析核心精要》课件
- 化工安装验收报告
- 爆破钻孔合同协议
- 2024年宁波市北仑区招聘事业单位人员笔试真题
评论
0/150
提交评论