构建跟随集
在为给定语法创建第一组时,我注意到算法参考中未描述的场景。
也就是说,如何使用这样的规则计算非终结符的跟随集。
<exp-list_tail> --> COMMA <exp> <exp-list_tail>
由 <...> 包围的表达式是非终结符,COMMA 是终结符。
我最好的猜测是我应该将空字符串添加到后续集合中,但我不确定。
通常,对于非终结符位于产生式规则末尾的情况,您只需计算左侧非终结符的后续列表,但您可以看到这将是一个问题。
While creating a first set for a given grammar, I noticed a scenario not described in my reference for the algorithm.
Namely, how does one calculate the follow set for a nonterminal with a rule such as this.
<exp-list_tail> --> COMMA <exp> <exp-list_tail>
Expressions surrounded by <..> are nonterminals, COMMA is a terminal.
My best guess is I should just add the empty string to the follow set, but I'm not sure.
Normally, for the case of a nonterminal being at the end of a production rule, you would just compute the follow list for the left nonterminal, but you can see how this would be a problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要正确回答这个问题,了解整个语法会很有帮助。然而,这里是对一般答案的尝试:
这是计算关注组的算法:
将所有关注组初始化为 {},除了 S 初始化为 {$}。
虽然有变化,但对于每个 A∈V 做:
对于每个 Y → αAβ 执行:
跟随(A) = 跟随(A) ∪ 首先(β)
如果 β ⇒* ε,也做: follow(A) = follow(A) ∪ follow(Y)
请注意,这是一个确定性算法,它会给你一个单一的答案,仅取决于你的(整个)语法。
具体来说,我认为这个特定的规则不会影响
的跟随集(它可以,但可能不会)。To answer this properly, it would be helpful to know your entire grammar. However, here is an attempt for a general answer:
Here is the algorithm for calculating follow groups:
Init all follow groups to {}, except S which is init to {$}.
While there are changes, for each A∈V do:
For each Y → αAβ do:
follow(A) = follow(A) ∪ first(β)
If β ⇒* ε, also do: follow(A) = follow(A) ∪ follow(Y)
Note that this is a deterministic algorithm, it will give you a single answer, depending only on your (entire) grammar.
Specifically, I don't think that this particular rule will affect
<exp-list_tail>
's follow set (it could, but probably wouldn't).