发布时间 : 星期二 文章编译原理 语法分析更新完毕开始阅读
编译原理
——语法分析器
成员: 杨霞 030802107 王日日 030802139
方艳丽 030802102
福州大学 数学与计算机科学(软件)学院
实验二:语法分析实验
一、实验目的
根据给出的文法编制LR(1)分析程序,以便对任意输入的符号串进行分析。本次实验的目的主要是加深对LR(1)分析法的理解。 二、实验内容
对已给语言文法,构造LR(1)分析表,编制语法分析程序,要求将错误信息输出到语法错误文件中,并输出分析句子的过程(显示栈的内容)
LR(1)分析法的功能是利用LR(1)分析表,对输入符号串自下而上的分析过程。 LR(1)分析表的构造及分析过程。 三.实验文法
2
福州大学 数学与计算机科学(软件)学院
实验环境:windows microsoft visual c++
实验原理: 构造项集
Setofltems closure(I) {
repeat for each A ??.B? ,ain J and each produtcion B?? of G such that B?.?, each of first(?a)is not in J: ADD B?.?,b to I until … no more items can be added to I return I }
Setofltems goto (I,X): {
If I is a set of LR(1) items and X is a grammar symbol (terminal or non-terminal), then goto(I,X) is defined as follows: If A .X,a in I
then every item in closure({A X.,a}) will be in goto(I,X). }
Void items() { C is { closure({S’?.S,$}) }
repeat the followings until no more set of LR(1) items can be added to C. for each I in C and each grammar symbol X if goto(I,X) is not empty and not in C add goto(I,X) to C
简略分析表:
0 1 2 3 4 5 6 7 8 9 10 11 12 20 id num 0 1 call if else while do r4 * > ( ) @ D 21 H 22 K 23 E 24 T 25 F acc 2 S5 S6 S7 S8 4 ? 11 12 13 S20 S23 S20 S21 S22 17 18 19 S21 S22 30 34 18 19 3
福州大学 数学与计算机科学(软件)学院
1S2S21 S2 23114 0 2 5 4 8 9 1S1 5 6 1 6 1 r7 r7 7 1 r12 S3r1 r12 r1 8 9 2 2 1 r15 r15 r1 r15 r1 9 5 5 2 r16 r16 r1 r16 r1 0 6 6 2 r17 r17 r1 r17 r1 1 7 7 ? .. .. .. .. ... .. .. . . . .. 3 r13 S3r1 r13 r1 8 9 3 3 3S2S21 S2 49 0 2 2 4 S4 0 1 4 r18 r18 r1r18 r1 1 8 8 4 r14 r14 r1r14 r13 2 4 4 1 4S5 S6 S8 3
实验流程图:
4