编译原理实验二;语法分析Word文件下载.doc
《编译原理实验二;语法分析Word文件下载.doc》由会员分享,可在线阅读,更多相关《编译原理实验二;语法分析Word文件下载.doc(18页珍藏版)》请在冰豆网上搜索。
block>
<
const-decl>
<
var-decl>
proc-decl>
statement>
<
=const<
const-assignment-list>
;
|ε
ident>
=<
number>
|<
<
=var<
ident-list>
|ε
|<
procedure<
expression>
|call<
id>
|begin<
statement-list>
end
|if<
condition>
then<
|while<
do<
|ε
=odd<
relation>
==|<
>
|>
=|>
=
term>
adding-operator>
=+|-
factor>
multiplying-operator>
=*|/
|(<
)
18
文法缩略版:
P->
S
S->
BD
B->
varH;
H->
id|H,id
D->
i=E|call(k)|if(k)DelseD|while(K)doD
K->
ERE
R->
|>
=|<
|==
E->
EOT|T
T->
TQF|F
O->
+|-
Q->
*|/
F->
id|nunber|(E)
programe>
=P
black>
=S
=B
=H
=D
=K
=E
=T
=F
实验环境:
windowsmicrosoftvisualc++
实验原理:
构造项集
Setofltemsclosure(I)
{
repeatforeachA®
a.Bb,ainJandeachprodutcion
B®
gofGsuchthatB®
.g,eachoffirst(ba)isnotinJ:
ADDB®
.g,btoI
until…nomoreitemscanbeaddedtoI
returnI
}
Setofltemsgoto(I,X):
{
IfIisasetofLR
(1)itemsandXisagrammarsymbol(terminalornon-terminal),thengoto(I,X)isdefinedasfollows:
IfA.X,ainI
theneveryiteminclosure({AX.,a})willbeingoto(I,X).
Voiditems()
Cis{closure({S’®
.S,$})}
repeatthefollowingsuntilnomoresetofLR
(1)itemscanbeaddedtoC.
foreachIinCandeachgrammarsymbolX
ifgoto(I,X)isnotemptyandnotinC
addgoto(I,X)toC
简略分析表:
1
2
3
4
5
6
7
8
9
10
11
12
20
21
22
23
24
25
id
num
call
if
else
while
do
*
(
)
@
D
H
K
E
T
F
r4
acc
S5
S6
S7
S8
…
S20
S21
S22
17
19
S23
13
30
34
14
15
S16
16
r7
r12
S39
r15
r16
r17
..
..
.
38
r13
39
42
40
S41
41
r18
r14
31
43
实验流程图:
实验结果:
输入:
输出:
输入的字符串以结束@
bi,i,i;
f(i>
i)i=n*n+new(i>
n)di=i+i@
栈符号输入动作
0bi,i,i;
n)di=i+i@移入
03bi,i,i;
0310bi,i,i;
n)di=i+i@根据H->
i规约
039bH,i,i;
03915bH,i,i;
0391516bH,i,i;
H,i规约
039bH,i;
03915bH,i;
0391516bH,i;
039bH;
0392