编译原理 有文法G(S): S->aSS->bSS->a 1)构造识别文法活缀的DFA 2)写出该文法的SLR(1)分析表
来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/17 16:04:48
编译原理 有文法G(S): S->aSS->bSS->a 1)构造识别文法活缀的DFA 2)写出该文法的SLR(1)分析表
编译原理
有文法G(S):
S->aS
S->bS
S->a
1)构造识别文法活缀的DFA
2)写出该文法的SLR(1)分析表
编译原理 有文法G(S): S->aSS->bSS->a 1)构造识别文法活缀的DFA 2)写出该文法的SLR(1)分析表
首先扩展文法为:
\x05\x05\x051) S1->S
\x05\x05\x052) S->aS
\x05\x05\x053) S->bS
\x05\x05\x054) S->a
\x05则:
\x05I0 = Closure({S1->.S})={S1->.S,S->.aS,S->.bS,S->.a}
\x05go(I0,S) = Closure({S1->S.})={S1->S.} = I1
\x05go(I0,a) = Closure({S->a.S,S->a.})={S->a.S,S->.aS,S->.bS,S->.a,S->a.} = I2
\x05go(I0,b) = Closure({S->b.S})={S->b.S,S->.aS,S->.bS,S->.a}=I3
\x05go(I2,S) = closure({S->aS.})={S->aS.}=I4
\x05go(I2,a) = Closure({S->a.S,S->a.}) = I2
\x05go(I2,b) = Closure({S->b.S}) =I3
\x05go(I3,S) = Closure({S->bS.}) = {S->bS.} = I5
\x05go(I3,a) = Closure({S->a.S,S->a.}) = I2
\x05go(I3,b) = Closure({S->b.S}) = I3
\x05
\x05由图所示,状态I2,既有归约项目(S->a.)又有移近项目(S->.aS,S->.bS,S->.a),产生冲突.当用SRL分析法时,需向前看一步,即求出:
\x05\x05Follow(S) = Follow(S1) = {#}
\x05\x05则,Follow(S)∩{a,b} =∮
\x05故而Action(I2,a) = s2
\x05\x05Action(I2,b) = s3
\x05\x05Action(I2,#) = r4
\x05\x05
则构造出srl分析表如下所示:
\x05 Action Goto
\x05 a b #\x05\x05\x05\x05S
I0\x05 s2 s3 \x05\x05\x05\x051
I1\x05\x05acc\x05
I2 s2 s3\x05r4\x05\x05\x05\x054
I3 s2 s3\x05\x05\x05\x05\x055
I4\x05 r2 r2 r2\x05\x05\x05\x05\x05\x05\x05
I5\x05 r3 r3\x05r3