Follow set example 不遵循任何规则?

Follow set example doesn't follow any rules?

  1. S → asg
  2. S → 如果 C 则 S E
  3. C → 布尔值
  4. E → 否则 S
  5. E → λ

所有小写和λ都是终结符号

我需要帮助来导出此语法的后续集。我通常不会遇到这些问题,而且我知道规则,但是当我练习书中的这个例子时,这是我唯一能得到的:

Follow(S) = {$} U Follow(E) 
Follow(C) = 
Follow(E) = 

根据https://www.cs.uaf.edu/~cs331/notes/FirstFollow.pdf

To compute FOLLOW(A) for all nonterminals A, apply the following rules until nothing can be added to any FOLLOW set:

  1. Place $ in FOLLOW(S), where S is the start symbol and $ is the input right endmarker.
  2. If there is a production A ⇒ αΒβ, then everything in FIRST(β), except for ε, is placed in FOLLOW(B).
  3. If there is a production A ⇒ αΒ, or a production A ⇒ αΒβ where FIRST(β) contains ε (i.e., β ⇒ε), then everything in FOLLOW(A) is in FOLLOW(B).

假设 S 是语法中的开始符号,而 λ 表示一个空字符串,我们得到:

  • {$} ⊆ Follow(S) 根据规则 1。
  • (First(E) \ {λ}) ⊆ Follow(S) 根据规则 2 / 产生式 2.
  • Follow(E) ⊆ Follow(S) 根据规则 3 / 产生式 4.
  • (First(then S E) \ {λ}) ⊆ Follow(C) 根据规则 2 / 产生式 2.
  • Follow(S) ⊆ Follow(E) 根据规则 3 / 产生式 2.

First(then S E) 只是 then(因为它是终端),所以我们有 {then} ⊆ Follow(C).

这是对Follow(C)的唯一约束,所以满足它的最小集合是:

Follow(C) = {then}

因为我们有 Follow(E) ⊆ Follow(S)Follow(S) ⊆ Follow(E),所以(哈)它们是相等的:

Follow(E) = Follow(S)

终于有了

Follow(S) = {$} ∪ (First(E) \ {λ})

幸好First(E)很容易,因为E只有两个产生式,一个是空的,另一个以终结符开头:

First(E) = {λ, else}

因此

Follow(S) = {$, else}

Follow(E) = {$, else}