收藏 分享(赏)

北邮 编译原理 自底向上语法分析实验报告.docx

上传人:HR专家 文档编号:4916268 上传时间:2019-01-22 格式:DOCX 页数:13 大小:244.48KB
下载 相关 举报
北邮 编译原理 自底向上语法分析实验报告.docx_第1页
第1页 / 共13页
北邮 编译原理 自底向上语法分析实验报告.docx_第2页
第2页 / 共13页
北邮 编译原理 自底向上语法分析实验报告.docx_第3页
第3页 / 共13页
北邮 编译原理 自底向上语法分析实验报告.docx_第4页
第4页 / 共13页
北邮 编译原理 自底向上语法分析实验报告.docx_第5页
第5页 / 共13页
点击查看更多>>
资源描述

1、自底向上语法分析器实验报告一问题描述编写语法分析程序,实现对算术表达式的语法分析。要求所分析算术表达式由如下的文法产生。E - E+T | E-T | TT - T*F | T/F | FF - id | (E) | num实验要求:在对输入表达式进行分析的过程中,输出所采用的产生式。编写语法分析程序实现自底向上的分析,要求如下:(1) 构造识别所有活前缀的 DFA。(2) 构造 LR 分析表。(3) 编程实现算法 4.3,构造 LR 分析程序。二算法思想1.大体步骤:(1)根据题目所给出的文法构造相应的拓广文法,并求出该文法各非终结符的FIRST、 FOLLOW 集合;(2).构造拓广文法的

2、项目集规范族,并构造出识别所有前缀的 DFA;(3)构造文法的 LR 分析表;(4)由此构造 LR 分析程序。2.数据结构:1.输入缓冲区为一个字符型数组,读入输入的算术表达式并保存在此,以$结束;2.构建一个相对应的整型数组,将输入缓冲区中的字符转换为相应的代号并保存;3.构造一个结构体,以保存文法的某个产生式,该结构包括三个元素:整形变量,保存产生式左部非终结符代号。整型数组,保存产生式右部字符串的代号。整型变量,保存产生式右部长度;4.定义该结构的数组,保存文法的所有产生式;5.定义两个二维整形数组,goto 和 action,其值大于零代表移进操作,小于零代表规约操作,引进的状态或规约

3、用到的产生式又绝对值表示。等于零代表出现错误。等于特殊值 999 代表 acc.状态。3.计算过程:文法对应的拓广文法为:1 S - E2 E - E+T3 E - E-T4 E - T5 T - T*F6 T - T/F7 T - F8 F - (E)9 F - id10 F - num求的各个非终结符的 FIRST、FOLLOW 集合为:FIRST(S) = id, num, ( FOLLOW (S) = $ FIRST(E) = id, num, ( FOLLOW (E) = $ , + , - , ) FIRST(T) = id, num, ( FOLLOW (T) = $ , + ,

4、 - , * , / , ) FIRST(F) = id, num, ( FOLLOW (F) = $ , + , - , * , / , ) 构造项目集规范族:I0 = closure(S-E) = S-E, E-E+T, E-E-T, E-T, T-T*F, T-T/F, T-F, F-id, F-(E), F-num;从 I0 出发:I1 = go(I0, E) = closure(S-E, E-E+T, E-E-T) = S-E, E-E+T, E-E-T;I2 = go(I0, T) = closure(E-T, T-T*F, T-T/F) = E-T, T-T*F, T-T/F;I

5、3 = go(I0, F) = closure(T-F) = T-F;I4 = go(I0, id) = closure(F-id) = F-id;I5 = go(I0, () = closure(F-(E) = F-(E), E-E+T, E-E-T, E-T, T-T*F, T-T/F, T-F, F-id, F-(E), F-num;I6 = go(I0, num) = closure(F-num) = F-num;从 I1 出发:I7 = go(I1, +) = closure(E-E+T) = E-E+T, T-T*F, T-T/F, T-F, F-id, F-(E), F-num;

6、I8 = go(I1, -) = closure(E-E-T) = E-E-T, T-T*F, T-T/F, T-F, F-id, F-(E), F-num;从 I2 出发:I9 = go(I2, *) = closure(T-T*F) = T-T*F, F-id, F-(E), F-num;I10 = go(I2, /) = closure(T-T/F) = T-T/F, F-id, F-(E), F-num;从 I5 出发:I11 = go(I5, E) = closure(F-(E), E-E+T, E-E-T) = F-(E), E-E+T, E-E-T;从 I7 出发:I12 = g

7、o(I7, T) = closure(E-E+T, T-T*F, T-T/F) = E-E+T, T-T*F, T-T/F;从 I8 出发:I13 = go(I8, T) = closure(E-E-T, T-T*F, T-T/F) = E-E-T, T-T*F, T-T/F;从 I9 出发:I14 = go(I9, F) = closure(T-T*F) = T-T*F;从 I10 出发:I15 = go(I10, F) = closure(T-T/F) = T-T/F;从 I11 出发:I16 = go(I11, ) = closure(F-(E) = F-(E);下面构造文法的 LR 分

8、析表:goto0,E = 1; goto0,T = 2; goto0,F = 3;action0,id = S4; action0,( = S5; action0,num = S6;action1,$ = ACC.; action1,+ = S7; action1,- = S8;action2,) = action2,+ = action2,- = action2,$ = R4;action2,* = S9; action2,/ = S10;action3,) = action3,+ = action3,- = action3,* = action3,/ = action3,$ = R7;ac

9、tion4,) = action4,+ = action4,- = action4,* = action4,/ = action4,$ = R8;goto5,E = 11; goto5,T = 2; goto5,F = 3;action5,id = S4; action5,( = S5; action5,num = S6;action6,) = action6,+ = action6,- = action6,* = action6,/ = action6,$ = R10;goto7,T = 12; goto7,F = 3;action7,id = S4; action7,( = S5; act

10、ion7,num = S6;goto8,T = 13; goto8,F = 3;action8,id = S4; action8,( = S5; action8,num = S6;goto9,F = 14;action9,id = S4; action9,( = S5; action9,num = S6;goto10,F = 15;action10,id = S4; action10,( = S5; action10,num = S6;action11,) = S16; action11,+ = S7; action11,- = S8;action12,) = action12,+ = act

11、ion12,- = action12,$ = R2; action12,* = S9; action12,/ = S10;action13,) = action13,+ = action13,- = action13,$ = R3; action13,* = S9; action13,/ = S10;action14,) = action14,+ = action14,- = action14,* = action14,/ = action14,$ = R5;action15,) = action15,+ = action15,- = action15,* = action15,/ = act

12、ion15,$ = R6;action16,) = action16,+ = action16,- = action16,* = action16,/ = action16,$ = R9;4.SLR(1)分析表为:E1: 缺少运算对象。认为加入一个 Id 入栈并转移至相应状态E2: 括号不匹配删掉右括号,则删去右括号并转移至相应状态E3: 缺少运算符,添加运算符并转移至相应状态action goto( ) + - * / id num $ E T F0 s5 E2 E1 E1 E1 E1 s4 s6 E1 1 2 31 E3 E2 s7 s8 E3 E3 Acc.2 E3 r4 r4 r4 s

13、9 s10 E3 E3 r43 r7 r7 r7 r7 r7 r74 r8 r8 r8 r8 r8 r85 s5 E2 E1 E1 E1 E1 s4 s6 11 2 36 E3 r10 r10 r10 r10 r10 r107 s5 E2 E1 E1 E1 E1 s4 s6 12 38 s5 E2 E1 E1 E1 E1 s4 s6 13 39 s5 E2 E1 E1 E1 E1 s4 s6 1410 s5 E2 E1 E1 E1 E1 s4 s6 1511 E3 s16 s7 s8 E3 E312 E3 r2 r2 r2 s2 s2 E3 E3 r213 E3 r3 r3 r3 s9 s10

14、 E3 E3 r314 r5 r5 r5 r5 r5 r515 r6 r6 r6 r6 r6 r616 r9 r9 r9 r9 r9 r9三、实验程序设计说明1.主要函数说明void TRANS();/*将读入的 buffer 数组内容按字符转换为相应代号存入 code 数组*/void GetFromCode();/*取得当前输入符号串的元素*/void PUSH(int A,int S);/*入栈操作 */void POP();/*出栈操作*/void SHITF();/*移入操作*/void REDUCE();/*规约操作*/void ACC();/*接受操作*/ void ERROR(

15、);/*错误处理*/void E1();/*错误处理 */void E2();void E3();2.源程序代码#include#include#includeint symbol,status;/*移入或规约时压入分析栈的符号、移入或规约操作后转而进入的状态*/int analysis_stack 50;/*定义分析栈*/int ip = -1;/*ip 为栈顶元素下标*/char buffer30;/*将输入符号串 w 保存于数组中*/int code30;/*将读入的字符串转换为代号形式 */int position = 0;/*position 为 code 数组的下标*/int X;

16、/*X 为当前获取的输入符号的代号 */int flag = 1;/*循环标志*/typedef struct/*定义文法产生式的结构*/int Vn;/*文法产生式的左部非终结符的代码*/int Str4;/*文法产生式右部的代码串*/int size;/*文法产生式右部的长度*/G;G production11 = /*该文法的所有产生式*/0,101,0,102,1,/*S - E*/102,0,102,3,103,3,/*E - E+T*/102,0,102,4,103,3,/*E - E-T*/102,0,103,1,/*E - T*/103,0,103,5,104,3,/*T -

17、T*F*/103,0,103,6,104,3,/*T - T/F*/103,0,104,1,/*T - F*/104,0,7,1,/*F - id*/104,0,1,101,2,3,/*F - (E)*/104,0,8,1,/*F - num*/;int GOTO175 = /*LR 分析表 goto*/0,0,1,2,3,/*0*/0,0,0,0,0,/*1*/0,0,0,0,0,/*2*/0,0,0,0,0,/*3*/0,0,0,0,0,/*4*/0,0,11,2,3,/*5*/0,0,0,0,0,/*6*/0,0,0,12,3,/*7*/0,0,0,13,3,/*8*/0,0,0,0,1

18、4,/*9*/0,0,0,0,15,/*10*/0,0,0,0,0,/*11*/0,0,0,0,0,/*12*/0,0,0,0,0,/*13*/0,0,0,0,0,/*14*/0,0,0,0,0,/*15*/0,0,0,0,0/*16*/;int ACTION179 = /*LR 分析表 action*/51,5,52,51,51,51,51,4,6,/0999,53,52,7,8,0,0,53,53,/1-4,53,-4,-4,-4,9,10,53,53,/2-7,0,-7,-7,-7,-7,-7,0,0,/3-8,0,-8,-8,-8,-8,-8,0,0,/40,5,52,51,51,51

19、,51,4,6,/5-10,0,-10,-10,-10,-10,-10,0,0,/651,5,52,51,51,51,51,4,6,/751,5,52,51,51,51,51,4,6,/851,5,52,51,51,51,51,4,6,/951,5,52,51,51,51,51,4,6,/100,53,16,7,8,0,0,53,53,/11-2,53,-2,-2,-2,9,10,53,53,/12-3,53,-3,-3,-3,9,10,53,53,/13-5,0,-5,-5,-5,-5,-5,0,0,/14-6,0,-6,-6,-6,-6,-6,0,0,/15-9,0,-9,-9,-9,-9

20、,-9,0,0/16;void TRANS();/*将读入的 buffer 数组内容按字符转换为相应代号存入 code 数组中*/void GetFromCode();/*取得当前输入符号串的元素 */void PUSH(int A,int S);/*入栈操作*/void POP();/*出栈操作*/void SHITF();/*移入操作*/void REDUCE();/*规约操作*/void ACC();/*接受操作*/ void E1();/*错误处理 */void E2();void E3();main()int c=1; printf(“请输入表达式,并以 $结尾。例如:a+b$n“)

21、;scanf(“%s“,buffer);/*读入算术表达式,保存至 buffer 中*/printf(“n“);TRANS();/*将字符型的 buffer 数组内容以字符为单位,转换为相应代号,保存至整型数组 w 中*/PUSH(0,0);/*状态 S0 入栈*/GetFromCode();/从输入缓冲区读取一个字符 while(flag)if( ACTIONanalysis_stackipX 0 break;case 102: printf(“E - “);break;case 103: printf(“T - “);break;case 104: printf(“F - “);break;for(int i = 1; i =3 printf(“e3ttt 缺少运算符, 添加运算符至栈“);break;3.程序测试与运行结果:测试用例:a$测试用例:35$测试用例:x+y$测试用例:40+6*(3-sum/x)$测试用例:10*3+4$ (错误的事例)测试用例:7+6)+6$(错误的事例)测试用例:6*7(+4$(错误的事例)四心得体会以及完成的功能

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 实用文档 > 工作总结

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报