简体   繁体   English

以下设置过程

[英]The follow set procedure

I know there are a lot of questions on this but I still haven't been able to find what I'm looking for.我知道对此有很多问题,但我仍然无法找到我想要的东西。

As far as the procedure for finding the Follow sets for a given grammar, I've seen a lot of versions of it, but let's stick to the one given here since it's the one I've seen the most.至于为给定语法查找跟随集的过程,我已经看过很多版本,但让我们坚持这里给出的那个,因为它是我见过最多的那个。 I'll copy it here so that you don't have to have it open:我将它复制在这里,这样你就不必打开它:

  1. First put $ (the end of input marker) in Follow(S) (S is the start symbol)首先将 $(输入标记的结尾)放在 Follow(S) 中(S 是开始符号)
  2. If there is a production A → aBb, (where a can be a whole string) then everything in FIRST(b) except for ε is placed in FOLLOW(B).如果存在产生式 A → aBb,(其中 a 可以是整个字符串),则 FIRST(b) 中除 ε 之外的所有内容都放在 FOLLOW(B) 中。
  3. If there is a production A → aB, then everything in FOLLOW(A) is in FOLLOW(B)如果存在产生式 A → aB,则 FOLLOW(A) 中的所有内容都在 FOLLOW(B) 中
  4. If there is a production A → aBb, where FIRST(b) contains ε, then everything in FOLLOW(A) is in FOLLOW(B)如果存在产生式 A → aBb,其中 FIRST(b) 包含 ε,则 FOLLOW(A) 中的所有内容都在 FOLLOW(B) 中

Question 1 : Are the rules 2 and 4 mutually exclusive or they can both be applied in the same "iteration" (writing it like this because that's actually going to be one of my questions)?问题 1 :规则 2 和 4 是否互斥,或者它们都可以在同一个“迭代”中应用(这样写是因为这实际上是我的问题之一)? What I mean here is that they match in the first part, that is they're both applied "If there is a production A -> aBb".我的意思是它们在第一部分匹配,即它们都被应用“如果有一个生产 A -> aBb”。

Does this mean that if I encounter a production A -> aBb such that ε is in First(b) I should apply both the second and the fourth rule, or only the fourth?这是否意味着如果我遇到产生式 A -> aBb 使得 ε 在 First(b) 中,我应该同时应用第二条和第四条规则,还是只应用第四条?

Question 2 : What exactly are 'a' and 'b'?问题2 :'a'和'b'到底是什么? It's not formally specified anywhere other then partly in the rule 2, where it says that "a can be a whole string".除了在规则 2 中的部分内容之外,它没有在任何地方正式指定,它说“a 可以是整个字符串”。 What about rules 3 and 4?规则 3 和 4 呢? Let me actually generalize this question even further.让我更进一步地概括这个问题。 If I have a production of the form: A -> abcd....efgBxyzw..... can any of these rules be applied?如果我有以下形式的产品: A -> abcd....efgBxyzw..... 可以应用这些规则中的任何一个吗? That is, do these rules require the production to literally contain only three elements on their right hand side?也就是说,这些规则是否要求产生式的右侧只包含三个元素? Or can it be interpreted like this:或者可以这样解释:

A -> abcd...ef [gBx] yzw.... , where the gBx part would now correspond to the aBb part within the rules 2 and 4. Or does this only work for the rule 2 and only the 'a' part, as in: A -> abcd...ef [gBx] yzw.... ,其中 gBx 部分现在对应于规则 2 和 4 中的 aBb 部分。或者这仅适用于规则 2 并且仅适用于“a”部分,如:

A -> abcd....ef[gBx], where the left part can be a whole string while the right part has to be exactly one symbol? A -> abcd....ef[gBx],其中左边部分可以是一个完整的字符串,而右边部分必须是一个符号?

Note: The square brackets are not part of the syntax, I just use them to separate stuff so I can explain what I mean.注意:方括号不是语法的一部分,我只是用它们来分隔东西,所以我可以解释我的意思。

Question 3 : Is this procedure even deterministic?问题 3 :这个过程甚至是确定性的吗? The thing is, they don't mention how long we're supposed to do this and in which order.问题是,他们没有提到我们应该这样做多长时间以及按什么顺序。 Well, actually, I've seen some sources say 'as long as there is anything left to add'.好吧,实际上,我看到一些消息来源说“只要还有什么要补充的”。 What about the order in which we're supposed to do this?我们应该按照什么顺序来做这件事? I suppose we just take productions at random and apply them as long as we can.我想我们只是随机制作并尽可能长时间地应用它们。 Are we supposed to try and derive further productions from the grammar rules that we have?我们是否应该尝试从现有的语法规则中推导出更多的产品? Or is the procedure designed to 'catch' those stuff indirectly?还是该程序旨在间接“捕捉”这些东西? Another very confusing thing here.这里还有一个非常令人困惑的事情。 Let's look at the following scenario:让我们看一下以下场景:

I'm looking at one production and, by applying the rules, I determine that everything from Follow(A) should go into Follow(B).我正在查看一个产品,通过应用规则,我确定从 Follow(A) 的所有内容都应该 go 到 Follow(B)。 Let's informally write it as:让我们非正式地写成:

Follow(B) += Follow(A)关注(B) += 关注(A)

And let Follow(A), for now (based on my latest calculations), contain some elements, say {x, y, z}.现在让Follow(A)(根据我最近的计算)包含一些元素,比如{x, y, z}。 Should I immediately write Follow(B) = {..., x, y, z} or should I wait til the very end?我应该立即写 Follow(B) = {..., x, y, z} 还是应该等到最后? Why?为什么? Well what if, a few productions later, I encounter a production that modifies Follow(A), in any way, let's say it adds 'w' to it, so that now Follow(A) = {x, y, z, w}.好吧,如果在几个产生式之后,我遇到一个修改了 Follow(A) 的产生式,无论如何,假设它添加了“w”,所以现在 Follow(A) = {x, y, z, w }。 Does this mean that, assuming I've written Follow(B) = {..., x, y, z} right away instead of waiting, I now need to go back there and add 'w' to it, or is the procedure supposed to 'catch' that 'w' in some of the later iterations?这是否意味着,假设我已经写了 Follow(B) = {..., x, y, z} 而不是等待,我现在需要 go 回到那里并添加“w”,或者是程序应该在以后的一些迭代中“捕捉”那个“w”?

Thanks in advance.提前致谢。

Question 1: No, They both cannot be applied in the same iteration.问题 1:不,它们不能在同一个迭代中应用。 That is because the except for ε part in it means that if there is no ε.那是因为其中的except for ε部分意味着如果没有ε。

Question 2: 'a' and 'b' are α (alpha) and β (beta).问题2: 'a'和'b'是α (α)和β (β)。 They both stand for multiple symbols.它们都代表多个符号。 (like A → Z b B nm could be A → α B β ; Z b was shortened to α and nm to β ). (如A → Z b B nm可以是A → α B βZ b缩短为αnm缩短为β )。 So no, it isn't limited to only 3 symbols on the rhs.所以不,它不仅限于 rhs 上的 3 个符号。

Question 3: Yes, this is detrministic.问题 3:是的,这是确定性的。 What you do, is you have a map (like std::unordered_map to store the follow set), and then you start a while loop-- the condition being while changes were made during the current iteration to the follow set.你所做的是,你有一个 map (像std::unordered_map来存储跟随集),然后你开始一个 while 循环——条件是在当前迭代期间对跟随集进行了更改。 Inside the the loop body, you put the rules.在循环体内,您放置了规则。 An example:一个例子:

follow_sets = {} # map
changes = True
while changes:
    changes = False
    # follow set rules
    # once a rule is applied, and things are added to the follow sets map, set changes to True

Now, your rule set is good, but not descriptive enough.现在,您的规则集很好,但描述性不够。 This is the rule set that I use to build follow sets:这是我用来构建跟随集的规则集:

Follow(START) has $

If A → α B β (Provided β ≠ ε):
Follow(B) → First(β)

If A → α B:
Follow(B) → Follow(A)

If A → α B β (provided β → ε):
Follow(B) → { First(β) - ε } ∪ First(A)

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

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