B → ε
解答
FIRST集的计算
- A → b B
∴ FIRST(A) = {b}
- B → ε
∴ FIRST(B) = {ε}
- S → A a A
应用FIRST集的规则(4)
即,将S → A a A与X → Y1Y2Y3进行比较
∴ FIRST(S) = FIRST(A a A) = FIRST(A) = {b}
∴ FIRST(S) = {b}
- S → B b B
∵ FIRST(B)包含ε 或 B 可推导出 ε ∴ 应用规则(4c)
∴ FIRST(S) = FIRST(B b B)
∴ FIRST(S) = FIRST(B) - {ε} ∪ FIRST(bB)
∴ FIRST(S) = FIRST(B) - {ε} ∪ {b} = {ε} - {ε} ∪ {b} = {b}
∴ FIRST(A) = {b}
FIRST(B) = {ε}
FIRST(S) = {b}
FOLLOW集的计算
应用规则(1) FOLLOW(S) = {$}
- S → A a A
应用规则(2) FOLLOW
将S → A a A与A → αBβ进行比较
S → | ε | A | a | A |
A → | α | B | β |
∵ FIRST(β) = FIRST(aA) = {a}
不包含ε
∴ 应用FOLLOW规则2(a)
FOLLOW(A) = {FIRST(aA)} = {a}
∴ FOLLOW(A) = {a}
应用FOLLOW规则(3)
将S → A a A与A → αBβ进行比较
S → | A | a | A |
A → | α | B |
∴ FOLLOW(A) = {FOLLOW(S)}
- S → B b B
应用规则(2) FOLLOW
将S → B b B与A → αBβ进行比较
S → | ε | B | b | B |
A → | α | B | β |
A = S,
α = ε
B = B
β = bB
∵ FIRST(β) = FIRST(bB) = {b}
不包含ε
∴ 应用FOLLOW规则2(a)
FOLLOW(B) = {FIRST(bB)}
∴ FOLLOW(B) = {b}
应用FOLLOW规则(3)
将S → B a B与A → αB进行比较
S → | B | a | B |
A → | α | B |
∴ FOLLOW(B) = {FOLLOW(S)}
- A → bB
无法应用规则(2)。因为无法将A → b B与A → αBβ匹配。
如果比较α = b, B = B, β = ε。
这里,β将变为ε,这是不可能的。
应用FOLLOW规则(3)
将A → b B与A → αB进行比较
A → | b | B |
A → | α | B |
∴ FOLLOW(B) = {FOLLOW(A)}
根据(1)到(6)
FOLLOW(S) = {$}
FOLLOW(A) = {a}
FOLLOW(A) = {FOLLOW(S)}
FOLLOW(B) = {b}
FOLLOW(B) = {FOLLOW(S)}
FOLLOW(B) = {FOLLOW(A)}
∴ FOLLOW(S) = {$}
根据(1),(2),(3)
FOLLOW(A) = {$, a}
根据(4),(5),(6)
FOLLOW(B) = {$, a, b}