简体   繁体   English

计算跟随集

[英]Computing the FOLLOW sets

My task is to calculate FIRST and FOLLOW sets for the following grammar: 我的任务是为以下语法计算FIRST和FOLLOW集:

P ::= S CS .
S ::= ( int , int )
CS ::= C CS | epsilon
C ::= left int | right int | low C

I got the following first sets: 我得到了以下第一套:

FIRST(S) = {'('}
FIRST(C) = {left,right,low}
FIRST(CS) = {left,right,low,epsilon}
FIRST(P) = FIRST(S) = {'('}

For the following sets I calculated: 对于以下集合,我计算出:

FOLLOW(P) = $ (or empty)
FOLLOW(C) = {left,right,low,'.'}
FOLLOW(CS) = {'.'}
FOLLOW(S) = {left,right,low}

I tried out my solution using first and follow sets generator and what I got with FOLLOW(S) was: FOLLOW(S) = {'.', left, right, low} . 我尝试使用第一个和跟随集生成器来尝试解决方案,得到的FOLLOW(S)是: FOLLOW(S) = {'.', left, right, low} Is generator's solution correct and why? 发电机的解决方案正确吗?为什么? I calculated my solution using formula: FOLLOW(S) = FIRST({left,right,low} concat FOLLOW(P)) = {left, right, low} . 我使用以下公式计算了解决方案: FOLLOW(S) = FIRST({left,right,low} concat FOLLOW(P)) = {left, right, low} Can someone please explain me why my/ generator's solution is not correct and check whether I got everything else right? 有人可以向我解释为什么我/发电机的解决方案不正确,然后检查我是否做对了所有其他事情? I also want to know why I don't have int in any first or follow set and if this will be okay with building parser later anyway. 我也想知道为什么我没有intint集合,如果以后无论如何构建解析器都可以。 Thank you 谢谢

When you compute FOLLOW sets you have to be careful with​ empty productions. 计算FOLLOW集时,必须注意空生产。

In this case, CS has an empty production, which means that S might be followed by a . 在这种情况下, CS的产量为空,这意味着S后面可能有一个. in P → S CS . P → S CS . . Similarly, the C in C CS might be at the end of the production, so C could also be followed by a . 同样, CC CS可能是在生产结束,所以C也跟着一个.

int can only appear after a left or right token. int只能出现在left标记或right标记之后。 It can never appear at the beginning of a nom-terminal nor immediately following a non-terminal. 它永远不会出现在标称终端的开头或紧接在非终端之后。 So it is entirely expected that it not be in any FIRST or FOLLOW set. 因此,完全可以预期它不在任何FIRST或FOLLOW集合中。

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

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