从语法生成第一个集合
Generating First Set from the grammar
Algorithm 查找第一组:
Given a grammar with the rules A1 → w1, ..., An → wn, we can compute the Fi(wi) and Fi(Ai) for every rule as follows:
initialize every Fi(Ai) with the empty set
set Fi(wi) to Fi(wi) for every rule Ai → wi, where Fi is defined as follows:
Fi(a w' ) = { a } for every terminal a
Fi(A w' ) = Fi(A) for every nonterminal A with ε not in Fi(A)
Fi(A w' ) = Fi(A) \ { ε } ∪ Fi(w' ) for every nonterminal A with ε in Fi(A)
Fi(ε) = { ε }
add Fi(wi) to Fi(Ai) for every rule Ai → wi
do steps 2 and 3 until all Fi sets stay the same.
语法:
A -> B C c
A -> g D B
B -> EPSILON | b C D E
C -> D a B | c a
D -> EPSILON | d D
E -> g A f | c
这website生成第一个集合如下:
Non-Terminal Symbol First Set
A g, ε, b, a, c, d
B ε, b
C a, c, ε, d
D ε, d
E g, c
但是算法说 Fi(A w' ) = Fi(A) for every nonterminal A with ε not in Fi(A)
所以根据这个算法的 First(A) 不应该包含 ε
。 First(A) = {g, b, a, c, d}
.
问:上述语法的第一个(A)是= First(B) - ε U First(C) U {g}
?
这个video也是按照上面的算法,不选ε。
First(B) = {ε, b}
First(D) = {ε, d}
First(E) = {g, c}
First(C) = {c, d, a}
First(A) = {b, g, c, d, a}
示例:
X -> Y a | b
Y -> c | ε
First(X) = {c, a, b}
First(Y) = {c, ε}
First(X) 没有 ε,因为如果用 ε 替换 Y,则 First(Y a) 等于 First(ε a) = {a}
首先在我的 github 上设置实施。
编辑:已更新link
https://github.com/amirbawab/EasyCC-CPP/blob/master/src/syntax/grammar/Grammar.cpp#L229
计算第一个和后续集都可以在上面的新 link 上使用。
Algorithm 查找第一组:
Given a grammar with the rules A1 → w1, ..., An → wn, we can compute the Fi(wi) and Fi(Ai) for every rule as follows:
initialize every Fi(Ai) with the empty set
set Fi(wi) to Fi(wi) for every rule Ai → wi, where Fi is defined as follows:
Fi(a w' ) = { a } for every terminal a
Fi(A w' ) = Fi(A) for every nonterminal A with ε not in Fi(A)
Fi(A w' ) = Fi(A) \ { ε } ∪ Fi(w' ) for every nonterminal A with ε in Fi(A)
Fi(ε) = { ε }
add Fi(wi) to Fi(Ai) for every rule Ai → wi
do steps 2 and 3 until all Fi sets stay the same.
语法:
A -> B C c
A -> g D B
B -> EPSILON | b C D E
C -> D a B | c a
D -> EPSILON | d D
E -> g A f | c
这website生成第一个集合如下:
Non-Terminal Symbol First Set
A g, ε, b, a, c, d
B ε, b
C a, c, ε, d
D ε, d
E g, c
但是算法说 Fi(A w' ) = Fi(A) for every nonterminal A with ε not in Fi(A)
所以根据这个算法的 First(A) 不应该包含 ε
。 First(A) = {g, b, a, c, d}
.
问:上述语法的第一个(A)是= First(B) - ε U First(C) U {g}
?
这个video也是按照上面的算法,不选ε。
First(B) = {ε, b}
First(D) = {ε, d}
First(E) = {g, c}
First(C) = {c, d, a}
First(A) = {b, g, c, d, a}
示例:
X -> Y a | b
Y -> c | ε
First(X) = {c, a, b}
First(Y) = {c, ε}
First(X) 没有 ε,因为如果用 ε 替换 Y,则 First(Y a) 等于 First(ε a) = {a}
首先在我的 github 上设置实施。
编辑:已更新link
https://github.com/amirbawab/EasyCC-CPP/blob/master/src/syntax/grammar/Grammar.cpp#L229
计算第一个和后续集都可以在上面的新 link 上使用。