简体   繁体   English

为什么这个语法在 LR(0) 中有 Reduce/Reduce 冲突?

[英]Why this grammar has Reduce/Reduce conflict in LR(0)?

I have the following grammar:我有以下语法:

S -> a b D E
S -> A B E F
D -> M x
E -> N y
F -> z
M -> epsilon
N -> epsilon

My textbook says there is a Reduce/Reduce conflict in LR(0).我的教科书说 LR(0) 中存在 Reduce/Reduce 冲突。 I built a diagram and found out that there is a state:我建了一个图,发现有一个state:

S -> a b . D E
S -> A B . E F
D -> . M x
E -> . N y
M -> .
N -> .

The textbook says that it's a Reduce/Reduce conflict.教科书说这是一个减少/减少冲突。 I'm trying to figure out why.我试图找出原因。 If I build the SLR table I get the following row (3 is the state above):如果我构建 SLR 表,我会得到以下行(3 是上面的 state):

在此处输入图像描述

That's because:那是因为:

  • Follow(M)={x} so we can do reduce to rule 6 from state 3. Follow(M)={x} 所以我们可以从 state 3 减少到规则 6。
  • Follow(N)={y} so we can do reduce to rule 7 from state 3. Follow(N)={y} 所以我们可以从 state 3 减少到规则 7。

I was taught that there is a conflict S/R if there is a cell with S/R and conflict R/R if there is a cell with R/R.有人告诉我,如果有一个带有 S/R 的单元格,则存在冲突 S/R,如果有一个带有 R/R 的单元格,则存在冲突 R/R。 But I don't see two Rs in the same cell in the table.但我没有在表格的同一个单元格中看到两个 Rs。 So why is it a reduce/reduce conflict?那么为什么它是一个减少/减少冲突呢?

You show an SLR(1) parsing table, in which the columns correspond to a lookahead of length 1. It's correct, and there is no conflict.您展示了一个 SLR(1) 解析表,其中的列对应于长度为 1 的前瞻。它是正确的,并且没有冲突。

But here we're talking about an LR(0) machine, in which there is no lookahead.但是这里我们讨论的是没有前瞻的 LR(0) 机器。 (That's the 0 in LR(0).) The only decision the machine can make is to shift or reduce, and since it cannot use lookahead, it can only use the state itself. (这是 LR(0) 中的 0。)机器可以做出的唯一决定是移位或减少,由于它不能使用前瞻,它只能使用 state 本身。 A given state must be either a shift state or a reduce state (and, if a reduce state, which production is being reduced).给定的 state 必须是移位 state 或减少 state (并且,如果减少 Z9ED39E25631586B73E,则为减少生产)。

(In case it's confusing, and it often is, the concept of lookahead does not refer to the use of the shifted symbol to decide which state to transition to. The transition is taken based on the shifted symbol, which is at that point no longer part of the lookahead.) (如果它令人困惑,而且通常是,前瞻的概念并不是指使用移位符号来决定要转换到哪个 state。转换是基于移位符号进行的,此时不再前瞻的一部分。)

So in that state, there is no possible shift action;所以在那state中,没有可能的换档动作; in all items in the itemset, either the dot is at the end or the next symbol is a non-terminal (implying a GOTO action after returning from a reduce).在项目集中的所有项目中,点位于末尾或下一个符号是非终结符(暗示从归约返回后执行 GOTO 操作)。

But the state does not have a unique reduction.但是 state 没有独特的减少。 Depending on the lookahead, the parsers needs to choose to reduce M or to reduce N. And since there is no lookahead, the decision cannot be made and hence it's a conflict.根据前瞻,解析器需要选择减少 M 或减少 N。由于没有前瞻,因此无法做出决定,因此这是一个冲突。

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

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