【编译原理】第四章作业(2)

编译原理第四章作业(2)

计科 吴政亿

  • 编译原理第四章作业(2)
    • 151220129 计科 吴政亿
      • 4.6.2
      • 4.6.3
      • 4.6.6
      • 4.7.1

2

  1. 增广文法:

    (0) SS
    (1) SSS+
    (2) BSS
    (3) Ba

  2. SLR项集如下:

    • I0:SS,SSS+,SSS,Sa
    • I1:Sa
    • I2:SS,SSS+,SSS,SSS+,SSS,Sa
    • I3:SSS+,SSS,SSS+,
             SSS,SSS+,SSS,Sa
    • I4:SSS+
    • I5:SSS
  3. GOTO 函数如下:
    GOTO(I0,a)=I1 , GOTO(I0,S)=I2
    GOTO(I2,a)=I1 , GOTO(I2,S)=I3 , GOTO(I2, $ )=accept
    GOTO(I3,a)=I1 , GOTO(I3,S)=I3
    GOTO(I3,+)=I4 , GOTO(I3,)=I5

  4. 语法分析表如下:

状态 ACTION GOTO
a + * $ S
0 S1 2
1 r3 r3 r3 r3
2 S1 accept 3
3 S1 S4 S5 3
4 r1 r1 r1 r1
5 r2 r2 r2 r2

无冲突,这显然是一个 SLR 文法

3

序号 符号 输入 动作
(1) 0 aa*a+$ 移入
(2) 01 a a*a+$ 按S->a规约
(3) 02 S a*a+$ 移入
(4) 021 Sa *a+$ 按S->a规约
(5) 023 SS *a+$ 移入
(6) 0235 SS* a+$ 按S->SS*规约
(7) 02 S a+$ 移入
(8) 021 Sa +$ 按S->a规约
(9) 023 SS +$ 移入
(10) 0234 SS+ $ 按S->SS+规约
(11) 02 S $ 接受

6

  • 因为First(SA) = First(A) = {a},所以该文法不是LL(1)的。
  • 下证该文法是SLR(1)的:

    1. 增广文法:
      (0) SS
      (1) SSA
      (2) SA
      (3) Aa
    2. SLR项集:

      • I0:SS,SSA,SA,Aa
      • I1:Aa
      • I2:SA
      • I3:SS,SSA,Aa
      • I4:SSA
    3. 语法分析表:

状态 ACTION GOTO
a $ S A
0 S1 S2 S3
1 r3 r3
2 r2 r2
3 S! acc S4
4 r1 r1

因为没有重复的冲突项,故该文法为SLR(1)的。

1

  1. 规范LR项集族
I0:
    [S' -> ·S  , $] 
    [S' -> ·SS+, $], [S' -> ·SS+, a]
    [S' -> ·SS*, $], [S' -> ·SS*, a]
    [S' -> ·a  , $], [S' -> ·a  , a]
I1:
    [S' -> a·  , $], [S' -> a·  , a]
I2:
    [S' -> S·  , $] 
    [S' -> S·S+, $], [S' -> S·S+, a]
    [S' -> S·S*, $], [S' -> S·S*, a]
    [S' -> ·SS+, a], [S' -> ·SS+, *], [S' -> ·SS+, +]
    [S' -> ·SS*, a], [S' -> ·SS*, *], [S' -> ·SS*, +]
    [S' -> ·a  , a], [S' -> ·a  , *], [S' -> ·a  , +]
I3:
    [S' -> a·  , a], [S' -> a·  , *], [S' -> a·  , +]
I4:
    [S' -> SS·+, $], [S' -> SS·+, a]
    [S' -> SS·*, $], [S' -> SS·*, a]
    [S' -> S·S+, a], [S' -> S·S+, *], [S' -> S·S+, +]
    [S' -> S·S*, a], [S' -> S·S*, *], [S' -> S·S*, +]
    [S' -> ·SS+, a], [S' -> ·SS+, *], [S' -> ·SS+, +]
    [S' -> ·SS*, a], [S' -> ·SS*, *], [S' -> ·SS*, +]
    [S' -> ·a  , a], [S' -> ·a  , *], [S' -> ·a  , +]
I5:
    [S' -> SS+·, $], [S' -> SS+·, a]
I6:
    [S' -> SS*·, $], [S' -> SS*·, a]
I7:
    [S' -> SS·+, a], [S' -> SS·+, *], [S' -> SS·+, +]
    [S' -> SS·*, a], [S' -> SS·*, *], [S' -> SS·*, +]
    [S' -> S·S+, a], [S' -> S·S+, *], [S' -> S·S+, +]
    [S' -> S·S*, a], [S' -> S·S*, *], [S' -> S·S*, +]
    [S' -> ·SS+, a], [S' -> ·SS+, *], [S' -> ·SS+, +]
    [S' -> ·SS*, a], [S' -> ·SS*, *], [S' -> ·SS*, +]
    [S' -> ·a  , a], [S' -> ·a  , *], [S' -> ·a  , +]
I8:
    [S' -> SS+·, a], [S' -> SS+·, *], [S' -> SS+·, +]
I9:
    [S' -> SS*·, a], [S' -> SS*·, *], [S' -> SS*·, +]
  1. LALR项集族:
I0:
    [S' -> ·S  , $] 
    [S' -> ·SS+, $], [S' -> ·SS+, a]
    [S' -> ·SS*, $], [S' -> ·SS*, a]
    [S' -> ·a  , $], [S' -> ·a  , a]
I1:
    [S' -> S·  , $] 
    [S' -> S·S+, $], [S' -> S·S+, a]
    [S' -> S·S*, $], [S' -> S·S*, a]
    [S' -> ·SS+, a], [S' -> ·SS+, *], [S' -> ·SS+, +]
    [S' -> ·SS*, a], [S' -> ·SS*, *], [S' -> ·SS*, +]
    [S' -> ·a  , a], [S' -> ·a  , *], [S' -> ·a  , +]
I2:
    [S' -> SS·+, a], [S' -> SS·+, *], [S' -> SS·+, +], [S' -> SS·+, $]
    [S' -> SS·*, a], [S' -> SS·*, *], [S' -> SS·*, *], [S' -> SS·*, $]
    [S' -> S·S+, a], [S' -> S·S+, *], [S' -> S·S+, +]
    [S' -> S·S*, a], [S' -> S·S*, *], [S' -> S·S*, +]
    [S' -> ·SS+, a], [S' -> ·SS+, *], [S' -> ·SS+, +]
    [S' -> ·SS*, a], [S' -> ·SS*, *], [S' -> ·SS*, +]
    [S' -> ·a  , a], [S' -> ·a  , *], [S' -> ·a  , +]
I3:
    [S' -> a·  , a], [S' -> a·  , *], [S' -> a·  , +], [S' -> a·  , $]
I4:
    [S' -> SS+·, a], [S' -> SS+·, *], [S' -> SS+·, +], [S' -> SS+·, $]
I5:
    [S' -> SS*·, a], [S' -> SS*·, *], [S' -> SS*·, +], [S' -> SS*·, $]

你可能感兴趣的:(编译原理)