简体   繁体   English

判断语法是否为LR(0)

[英]deciding whether the grammar is LR(0) or not

I am new to the subject of compilation and have just started an exercise for Bottom -up parsing. 我是编译主题的新手,刚开始进行Bottom -up解析练习。

I have stuck on the following problem. 我坚持以下问题。

build a LR(0) parsing table for following grammar: 为后续语法构建LR(0)解析表:

1) E –> E + T
2) E –> T
3) T –> (E)
4) T –> id


I0 :

   E' –> .E
   E –> .E + T
   E –> .T
   T –> .(E)
   T –> .id

on E the next state in the DFA would be : 在E上,DFA的下一个州将是:

 I1:

    E' -> E.
    E  -> E. + T

from what I have learned so far isn't this a SR conflict? 从我到目前为止所学到的不是SR冲突? because the parser would not know whether to reduce or shift as it has no look-ahead variable? 因为解析器不知道是否减少或移位,因为它没有预见变量? so this should not be LR(0) grammar? 所以这不应该是LR(0)语法?

but the PDF which I am reading have built the LR(0) table. 但我正在阅读的PDF已经构建了LR(0)表。 So is there a mistake in the PDF or have I gone wrong some where understanding the concept? 那么PDF中是否存在错误,或者在理解概念时出错?

You augmented the grammar with E' -> E . 你用E' -> E增加了语法。 Normally, you'd augment with a production like E' -> E $ , where $ is a (terminal) symbol that doesn't otherwise occur in the grammar, and denotes end-of-input. 通常情况下,你会使用像E' -> E $这样的产品进行扩充,其中$是一个(终端)符号,它在语法中不会出现,并表示输入结束。

So I1 would actually be 所以I1实际上是


E' -> E. $
E  -> E. + T

and there isn't a conflict. 而且没有冲突。 (And I believe the grammar is LR(0).) (我相信语法 LR(0)。)

This is indeed a shift/reduce conflict. 这确实是一种转移/减少冲突。 This grammar isn't LR(0). 这个语法不是LR(0)。 You can also see this because it's not prefix-free ; 你也可以看到这个,因为它不是无前缀的 ; the grammar contains multiple strings that are prefixes of one another, so it can't be LR(0). 语法包含多个字符串,这些字符串是彼此的前缀,因此它不能是LR(0)。

That said, you can still construct all the LR(0) configurating sets and make the LR(0) automaton. 也就是说,您仍然可以构造所有LR(0)配置集并生成LR(0)自动机。 It just won't be deterministic because of the shift/reduce conflict. 由于转移/减少冲突,它不会是确定性的。 It's possible, therefore, that you're right and the handout is right. 因此,你可能是正确的,而且讲义是正确的。

Hope this helps! 希望这可以帮助!

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

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