简体   繁体   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 集:

A -> B C
B -> A x | x
C -> y C | y

According to my understanding I get the following computation:根据我的理解,我得到以下计算:

Firstly we remove left recursion首先我们去掉左递归

A -> B C
B -> x B'
B' -> C x B' | ε
C -> y C | y

Follow (A) = {$}跟随 (A) = {$}

But in the book, the answer for Follow (A) = {x,$}但是书里的答案是 Follow (A) = {x,$}

Why?为什么? Did they not remove left recursion?他们没有删除左递归吗?

You're right about the contents of FOLLOW(A) for these two grammars, as far as I can see, and to a superficial inspection you didn't change the language of the grammar when you eliminated left recursion.就我所见,您对这两个语法的 FOLLOW(A) 的内容是正确的,并且从表面上看,当您消除左递归时,您并没有改变语法的语言。

Why did you feel the need to eliminate left recursion?为什么你觉得需要消除左递归? Top-down parsing may not work on left-recursive grammars, but FIRST and FOLLOW are still well-defined.自顶向下的解析可能不适用于左递归文法,但 FIRST 和 FOLLOW 仍然是明确定义的。 So you don't need to eliminate left recursion just to compute those sets and I'm guessing they didn't do so in your text.所以你并不需要消除左递归只计算那些集和我猜他们没有在你的文字这样做。

And you probably already realize this, but for completeness I'll add that eliminating left recursion definitely does change the parse trees and sentential forms and, importantly here, FIRST and FOLLOW sets of a grammar without changing L(G).你可能已经意识到这一点,但为了完整起见,我要补充一点,消除左递归肯定会改变解析树和句子形式,而且重要的是,在这里,语法的 FIRST 和 FOLLOW 集不改变 L(G)。 That's actually the point of doing that elimination.这实际上是进行消除的重点。

So bottom-line: I think they probably just didn't eliminate left recursion in your book, even though I can't say for sure from what's here.所以底线:我认为他们可能只是没有消除你书中的左递归,即使我不能从这里确定。

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

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