完美WORD格式
} {2,3,5,7,10} {2,5,7,8,10} {2,3,5,7,9,10} {2,4,5,7,8,10} {11} {6} {2,4,5,7,8,10} {2,5,7,8,10} {2,4,5,7,8,10} {2,5,7,8,10} φ φ } {2,3,5,7,10} {2,3,5,7,9,10} {2,3,5,7,10} {2,3,5,7,9,10} φ φ {11} {11} {11} {11} φ φ {6} {6} {6} {6} φ φ
A S
5:A?S?A 6:A?SA? 3:S??S? S??AS S?A?S S A a A?S?A S??AS S??b
A??SA A??SA S??b b A??a A??a A??SA
A??a S??AS
S??b
S a A S b S A b a A
4:S?A?S 7:S?AS? 0:S???S
S??AS S??AS A?S?A A S S??AS S??b S??b
A??SA A??SA S??b b A??a A??a A??SA A??a
a a b b a
1:A?a? 2:S?b?
DFA
构造LR(0)项目集规范族也可以用GO函数来计算得到。所得到的项目集规范族与上图中的项目集一样:
I0={S???S,S??AS,S??b,A??SA,A??a} GO(I0,a)={ A?a?}=I1 GO(I0,b)={ S?b?}=I2
GO(I0,S)={ S??S?,A?S?A,A??SA,A??a,S??AS,S??b}=I3
专业整理 知识分享
完美WORD格式
GO(I0,A)={ S?A?S,S??AS,S??b,A??SA,A??a}=I4 GO(I3,a)={ A?a?}=I1 GO(I3,b)={ S?b?}=I2
GO(I3,S)={ A?S?A,S??AS,S??b,A??SA,A??a}=I5
GO(I3,A)={ A?SA?,S?A?S,S??AS,S??b,A??SA,A??a}=I6 GO(I4,a)={ A?a?}=I1 GO(I4,b)={ S?b?}=I2
GO(I4,S)={ S?AS?,A?S?A,S??AS,S??b,A??SA,A??a}=I7 GO(I4,A)={ S?A?S,S??AS,S??b,A??SA,A??a}=I4 GO(I5,a)={ A?a?}=I1 GO(I5,b)={ S?b?}=I2
GO(I5,S)={ A?S?A,S??AS,S??b,A??SA,A??a}=I5
GO(I5,A)={ A?SA?,S?A?S,S??AS,S??b,A??SA,A??a}=I6 GO(I6,a)={ A?a?}=I1 GO(I6,b)={ S?b?}=I2
GO(I6,S)={ S?AS?,A?S?A,S??AS,S??b,A??SA,A??a}=I7 GO(I6,A)={ S?A?S,S??AS,S??b,A??SA,A??a}=I4 GO(I7,a)={ A?a?}=I1 GO(I7,b)={ S?b?}=I2
GO(I7,S)={ A?S?A,S??AS,S??b,A??SA,A??a}=I5
GO(I7,A)={ A?SA?,S?A?S,S??AS,S??b,A??SA,A??a}=I6 项目集规范族为C={I1,I2,I3,I4,I5,I6,I7}
(3)不是SLR文法
状态3,6,7有移进归约冲突
状态3:FOLLOW(S’)={#}不包含a,b
状态6:FOLLOW(S)={#,a,b}包含a,b,;移进归约冲突无法消解 状态7:FOLLOW(A)={a,b}包含a,b;移进归约冲突消解 所以不是SLR文法。
(4) 构造例如LR(1)项目集规范族 见下图: 对于状态5,因为包含项目[A?AS? a/b],所以遇到搜索符号a或b时,应该用A?AS归约。又因为状态5包含项目[A??a a/b],所以遇到搜索符号a时,应该移进。因此存在“移进-归约”矛盾,所以这个文法不是LR(1)文法。
专业整理 知识分享
完美WORD格式
b b b 1: 5: 8: S??S? #A?SA? a/bS?A?S a/b A A?S?A a/bS?A?S a/bS??AS a/b A A A??SA a/bS??AS a/bS??b a/b A??a a/bS??b a/bA??SA a/b S??AS a/bA??SA a/bA??a a/b S S??b a/bA??a a/b a a S S a S 3: 0: 3: a a A a A?a? a/b A S???S # S??AS #/a/b9: 6: S??b #/a/b S?AS? a/bA?S?A a/b S A??SA a/bA?S?A a/bA??SA a/b b 4: A??SA a/b A??a a/bS?b? #/a/bA??a a/b A??a a/b S??AS a/b S S??AS a/bS??b a/b A b S??b a/b a a S b b 2: 7: S?AS? #/a/bS?A?S #/a/b A?S?A a/bS??AS #/a/b A??SA a/bS??b #/a/b10: S b A??a a/bS?b? a/b A??SA a/b S??AS a/bA??a a/b S??b a/b A A 5: 专业整理 知识分享
完美WORD格式
第六章
/********************第六章会有点难
P164–5
(1)
E?E1+T {if (E1.type = int) and (T.type = int ) then E.type := int else E.type := real} E?T {E.type := T.type} T?num.num {T.type := real} T?num {T.type := int} (2)
P164–7
S?L1|L2
{S.val:=L1.val+(L2.val/2
L2.length)}
S?L {S.val:=L.val}
L?L1B {L.val:=2*L1.val + B.val; L.length:=L1.length+1} L?B {L.val:=B.c;
L.length :=1} B?0 {B.c:=0} B?1 {B.c:=1} ***********************/
第七章
P217–1
a*(-b+c) a+b*(c+d/e) -a+b*(-c+d)
ab@c+* abcde/+*+ a@bc@d+*+
?A??(C??D) A?CD????
(A?B)?(?C?D) AB?C@D?? AB?CD@E???
(A?B)?(C??D?E)
if (x+y)*z =0 then (a+b)↑c else a↑b↑c xy+z*0= ab+c↑abc↑↑ ¥ 或 xy+z*0= P1 jez ab+c↑ P2 jump abc↑↑
P1 P2
专业整理 知识分享
完美WORD格式
P217–3
-(a+b)*(c+d)-(a+b+c)的 三元式序列: (1) +, a, b (2) @, (1), - (3) +, c, d (4) *, (2), (3) (5) +, a, b (6) +, (5), c (7) -, (4), (6) 间接三元式序列: 三元式表: (1) +, a, b (2) @, (1), - (3) +, c, d (4) *, (2), (3) (5) +, (1), c (6) -, (4), (5) 间接码表: (1) (2) (3) (4) (1) (5) (6)
四元式序列: (1) (2) (3) (4) (5) (6) (7)
+, a, b, T1 @, T1, -, T2 +, c, d, T3 *, T2, T3, T4 +, a, b, T5 +, T5, c, T6 -, T4, T6, T7
P218–4
自下而上分析过程中把赋值句翻译成四元式的步骤:A:=B*(-C+D) 步骤 输入串 栈 PLACE 四元式 (1) A:=B*(-C+D)
(2) :=B*(-C+D) i A (3) B*(-C+D) i:= A- (4) *(-C+D) i:=i A-B
(5) *(-C+D) i:=E A-B
专业整理 知识分享