简体   繁体   English

计算语法的 FIRST 和 FOLLOW 集?

[英]Computing FIRST and FOLLOW sets for a grammar?

I have to compute the FIRST and FOLLOW sets of the following grammar:我必须计算以下语法的 FIRST 和 FOLLOW 集:

S -> ABC
A -> a | Cb | 1
B -> c | dA | 1
C -> e | f

However, I am not entirely sure if I understand how to do it.但是,我不完全确定我是否理解如何做到这一点。 According to my understanding I get the following computation:根据我的理解,我得到以下计算:

FIRST(S)= {a, e, f, b, c, d}
FIRST(A)= {a, e, f, 1}
FIRST(B)= {c, d, a, e, f, b, 1}
FIRST(C)= {e, f}

FOLLOW(S)= {$}
FOLLOW(A)= {c, d, a, e, f, b}
FOLLOW(B)= {e, f}
FOLLOW(C)= {$}

Is this correct?这样对吗? Did I miss something?我错过了什么? If so, could someone please explain what I missed and how I would go about computing the correct FIRST and FOLLOW sets?如果是这样,请有人解释我错过了什么以及我将如何计算正确的 FIRST 和 FOLLOW 集? Also, by looking at the sets produced, how can I tell if its an LL(1) grammar?另外,通过查看生成的集合,我如何判断它是否是 LL(1) 语法?

When S (the goal nonterminal symbol) expands, the first terminal in its derivation must come from A only, since that is exactly what the grammar indicates in S->ABC.当 S(目标非终结符)展开时,其派生中的第一个终结符必须仅来自 A,因为这正是 S->ABC 中语法所指示的。 (Unless "1" in your grammar means epsilon, an empty right-hand side; in this case, we move on to B and find B's FIRST set.) A can expand to a and 1 directly, and via C to e and f. (除非你的语法中的“1”表示 epsilon,一个空的右边;在这种情况下,我们继续到 B 并找到 B 的第一个集合。)A 可以直接扩展到 a 和 1,并通过 C 扩展到 e 和 f . So the FIRST set of S is a,1,e,f (the union of all these terminals).所以第一组 S 是 a,1,e,f(所有这些终端的并集)。 The grammar looks to be LL(1), since each actual token appears to determine which right-hand-side to use at each step in the derivation.语法看起来是 LL(1),因为每个实际标记似乎决定了在推导的每个步骤中使用哪个右侧。 Calculate all the FIRST sets to confirm this guess.计算所有 FIRST 集以确认此猜测。 If the same terminal is in the FIRST set for two RHSs of the same nonterminal LHS, then the grammar is not LL(1) since that terminal would cause an ambiguity in the choice of which production to use in the derivation.如果同一个终结符在同一个非终结符 LHS 的两个 RHS 的 FIRST 集合中,则文法不是 LL(1),因为该终结符会导致在推导中选择使用哪个产生式时产生歧义。 LL(1) grammars must be unambiguous and have a nonempty FIRST entry for each nonterminal, meaning that the grammar has no left-recursion. LL(1) 文法必须是明确的,并且每个非终结符都有一个非空的 FIRST 条目,这意味着文法没有左递归。

Note that the FOLLOW set is not needed since there are no productions with empty right-hand-sides (such productions can be used to model LL(2) languages in LL(1) grammars at the cost of an increase in parser complexity).请注意,不需要 FOLLOW 集,因为没有右侧为空的产生式(此类产生式可用于在 LL(1) 文法中对 LL(2) 语言进行建模,但会增加解析器的复杂性)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM