北京工业大学 编译原理 实验报告

上传人:第*** 文档编号:57338533 上传时间:2018-10-21 格式:PDF 页数:22 大小:864.59KB
返回 下载 相关 举报
北京工业大学 编译原理  实验报告_第1页
第1页 / 共22页
北京工业大学 编译原理  实验报告_第2页
第2页 / 共22页
北京工业大学 编译原理  实验报告_第3页
第3页 / 共22页
北京工业大学 编译原理  实验报告_第4页
第4页 / 共22页
北京工业大学 编译原理  实验报告_第5页
第5页 / 共22页
点击查看更多>>
资源描述

《北京工业大学 编译原理 实验报告》由会员分享,可在线阅读,更多相关《北京工业大学 编译原理 实验报告(22页珍藏版)》请在金锄头文库上搜索。

1、计 算 机 学 院 实 验 报 告 课程名称:编译原理 实验人学号:110703xx 姓名:xxx 实验完成日期:2014 年 5 月 20 日 报告完成日期:2014 年 5 月 20 日 目录 实验一实验一词法分析程序的设计与实现词法分析程序的设计与实现3 词法的正规式描述:.3 状态图:.4 词法分析程序数据结构与算法: 4 词法分析算法:.5 实验结果:.7 实验中遇到的问题及其解决: 8 1、保留字的检测问题: 8 2、关于 0 为首位的数字是 int8、int10 和 int16 的判断问题:.8 3、关于回退的问题: 8 实验二实验二自顶向下的语法分析自顶向下的语法分析递归子程序

2、法递归子程序法9 改写后的产生式集合:.9 化简后的语法图:.9 递归子程序算法.10 实验结果:.13 实验中遇到的问题及其解决: 14 1、消除左递归,提取左因子之后的 E、 T 对应的子程序的编写问题:14 2、缩进的控制: 14 实验三实验三 语法制导的三地址代码生成程序语法制导的三地址代码生成程序15 语法制导定义:.15 三地址代码生成器的数据结构 16 三地址生成器算法:.17 实验结果:.21 实验中遇到的问题及其解决: 22 1、根据化简后的产生式修改语法制导定义:22 2、使用真假出口法和继承属性来确定 goto 的标号: 22 实验一实验一词法分析程序的设计与实现词法分析

3、程序的设计与实现 词法的正规式描述词法的正规式描述: 标识符(|)* 十进制整数0|(1|2|3|4|5|6|7|8|9)(0|1|2|3|4|5|6|7|8|9)* 八进制整数0(0|1|2|3|4|5|6|7)(0|1|2|3|4|5|6|7)* 十六进制整数 0(x|X)(0|1|2|3|4|5|6|7|8|9|a|b|c|d|e|f)(0|1|2|3|4|5|6|7|8|9|a|b|c|d|e |f)* 运算符和分隔符+-*/= 0 case id = E; S - if C then S; S - while C do S; C - E E; C - E T (+ T) *; E -

4、 T (- T) *; T - F (* F) *; T - F (/ F) *; F - (E); F - id; F - int8; F - int10; F - int16; 化简后的语法图:化简后的语法图: 递归子程序算法递归子程序算法 int ProcedureS(ifstream couttype = IF) coutnametype = THEN) coutnametype = WHILE) coutnametype = DO) coutnametype = IDN) coutnametype = EQU) Indent(); coutnametype = MORE) coutn

5、ametype = LESS) coutnametype = ADD) Indent(); coutnametype = MINUS) Indent(); coutnamename.length(); i+) from_file.unget();/回退 indentation -= 4;/子程序结束 return 0; indentation -= 4;/子程序结束 return 0; int ProcedureT(ifstream couttype = MUL) Indent(); coutnametype = DIC) Indent(); coutnamename.length(); i+

6、) from_file.unget();/回退 indentation -= 4;/子程序结束 return 0; indentation -= 4;/子程序结束 return 0; int ProcedureF(ifstream couttype = LBRAC) coutnamenametype = IDN) coutnametype = INT8) coutvalue)type = INT10) coutvaluetype = INT16) coutvalue)T(+T)*类似的产生式 没有写循环调用控制,后来在(+T)*的最外层加了一个 while(true)循环,然后 在 while

7、(true)的首行加入了不是+就 return 的判定,成功解决了问题。 2、缩进的控制缩进的控制: 这个实验中碰到的第二个问题就是语法树缩进的控制问题,最终通过一个全 局变量 indentation 来控制缩进的字符数量, 一个 Indent()函数来输出缩进 (其实就是空格),控制缩进数量的关键点有两个:一为进入子程序的时候 indentation+=4, 二为结束子程序的时候 indentation-=4。剩下的就是根 据调试来选择在哪里输出缩进空格的问题了。 实验三实验三 语法制导的三地址代码生成程序语法制导的三地址代码生成程序 语法制导定义语法制导定义: 产生式语义规则 S id =

8、 E ;S.code = E.code | gen(id.place := E.place) S if C then SC.true = newlabel; C.false = S.next; S1.next = S.next; S.code = C.code | gen(C.true :) | S1.code S while C do SS.begin = newlabel; C.true = newlabel; C.false = S.next; S1.next = S.begin; S.code = gen(S.begin :) | C.code | gen(C.true :) | S1

9、.code | gen(goto S.begin); C E1 E2C.code = E1.code | E2.code | gen(ifE1.place E2.place goto C.true) | gen(gotoC.false) C E1 T1(+ T2)*E.place = newtemp; (E.code = T1.code|T2.code| gen(E.place := T1.place + T2.place); T1.place = E.place; T1.code = E.code;)+ E T1(- T2)*E.place = newtemp; (E.code = T1.c

10、ode|T2.code| gen(E.place := T1.place - T2.place); T1.place = E.place; T1.code = E.code;)+ E TE.place = T.place; E.code = T.code T FT.place = F.place; T.code = F.code T F1 (* F2)*T.place = newtemp; (T.code = F1.code | F2.code | gen(T.place := F1.place * F2.place); F1.place = T.place; F1.code = T.code

11、;)+ T F1 (/ F2)*T.place = newtemp; (T.code = F1.code | F2.code | gen(T.place := F1.place / F2.place); F1.place = T.place; F1.code = T.code;)+ F ( E )F.place = E.place; F.code = E.code F idF.place = id.name; F.code = F int8F.place = int8.value; F.code = F int10F.place = int10.value; F.code = F int16F

12、.place = int16.value; F.code = 三地址代码生成器的数据结构三地址代码生成器的数据结构 typedef struct /*S 的属性定义*/ char codeCODESIZE; intbegin; intnext; AttrS; typedef struct /*E 的属性定义*/ char codeCODESIZE;/CodeSize = 500 char placeBUFSIZE;/BufSize = 200 AttrE; typedef struct /*C 的属性定义*/ char codeCODESIZE; intc_false;/用来标记入口 intc

13、_true;/用来标记入口 AttrC; typedef struct /*T 的属性定义*/ char codeCODESIZE;/CodeSize = 500 char placeBUFSIZE;/BufSize = 200 AttrT; typedef struct /*F 的属性定义*/ char codeCODESIZE;/CodeSize = 500 char placeBUFSIZE;/BufSize = 200 AttrF; typedef struct /*IDN 的属性定义*/ char idnameBUFSIZE; intentry; AttrIDN; 三地址生成器算法:

14、三地址生成器算法: int ProcedureS(ifstream/C 的属性 AttrS s1;/s1 的属性 AttrE e;/e 的属性 char temp_idn_name50;/用来暂存当下一个是 IDN 时,s-id:=E 的 id 的 name Token* token = TokenScan(from_file); /s- if C then S1/ / if (token-type = IF) c.c_true = NewLabel();/c.c_true 出口有了新标签 s1.begin = c.c_true;/ C 真 则往 S1 走 s1.next = c.c_fals

15、e = s.next; / c 假 则 走 s 的下一步为 L0 标签, 在前面预置了 ProcedureC(from_file, c); token = TokenScan(from_file); if (token-type = THEN) ProcedureS(from_file, s1); sprintf_s(s.code, “nt%snL%d:t%s“, c.code, c.c_true, s1. code);/将中间代码输出到 s.code 中 else exit(-1); /s- while C do S1/ / else if (token-type = WHILE) s1.next = s.begin = NewLabel(); c.c_true = s1.begin=NewLabel();/C 真 则往 S1 走 c.c_f

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号