有文法G[S], 求此文法的每一个非终结符号的First集和Follow集。S::=aAcB | Bd A::= AaB | Bc | ε B::= bBc | ABc | ε 写出原因和计算过程
时间: 2023-11-23 09:53:12 浏览: 107
求非终结符的first集和follow集
4星 · 用户满意度95%
该文法的每一个非终结符号的First集和Follow集如下:
First(S) = {a, b}
First(A) = {ε, a, b, c}
First(B) = {ε, b}
Follow(S) = {$}
Follow(A) = {b, c}
Follow(B) = {c}
计算过程如下:
1. 首先计算出所有终结符的First集和所有非终结符的Follow集,即:
First(a) = {a}
First(b) = {b}
First(c) = {c}
First(ε) = {ε}
Follow(S) = {$}
Follow(A) = {}
Follow(B) = {}
2. 计算非终结符A的First集:
First(A) = First(AaB) ∪ First(Bc) ∪ {ε}
= First(AaB) ∪ {c, ε}
= First(AaB) ∪ {c}
计算AaB的First集:
First(AaB) = First(A) ∩ First(aB)
= (First(A) ∖ {ε}) ∩ (First(a) ∪ First(B))
= {a, b, c}
所以,First(A) = {a, b, c}
3. 计算非终结符B的First集:
First(B) = First(bBc) ∪ First(ABc) ∪ {ε}
= First(bBc) ∪ First(A) ∪ {c, ε}
= First(bBc) ∪ {a, b, c, ε}
计算bBc的First集:
First(bBc) = {b}
所以,First(B) = {b, a, c, ε}
4. 计算非终结符S的First集:
First(S) = First(aAcB) ∪ First(Bd) ∪ {ε}
= First(aAcB) ∪ First(B) ∪ {d, ε}
= First(aAcB) ∪ {a, b, c, ε, d}
计算aAcB的First集:
First(aAcB) = First(aA) ∩ First(cB)
= (First(a) ∪ First(A)) ∩ (First(c) ∪ First(B))
= ({a} ∪ {a, b, c, ε}) ∩ ({c} ∪ {b, a, c, ε})
= {a, b, c}
所以,First(S) = {a, b}
阅读全文