SLR(1)和LALR(1)语法冲突

发布于 2025-01-29 09:27:49 字数 168 浏览 2 评论 0原文

假设您有一个语法G,我们为其找到了LR(1)自动机。我们可以通过执行状态和转换规则,但可能会出现冲突来将其转换为LALR(1)或SLR(1)解析器。

我的问题是:所有问题都必须出现在合并状态?在LALR(1)或SLR(1)自动机中遇到冲突的非冲突LR(1)状态是否可能合并?

Suppose you have a grammar G and we find an LR(1) automaton for it. We can transform it into a LALR(1) or SLR(1) parser by doing state-merging and transforming rules but conflicts may appear.

My question is the following: must all problems appear in merged states? Is it possible for a non-conflict LR(1) state that wasn't merged to have a conflict either in LALR(1) or in SLR(1) automaton?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜还是长夜 2025-02-05 09:27:49

有趣的问题!答案是

  • ,如果语法是lr(1),则必须在合并的州发生任何冲突(1)解析器,但是
  • 如果语法是LR(1),则可能会在LR(1)状态中出现冲突。

对于第一点,假设您有一个语法是LR(1),因此您可以形成其LR(1)解析器。我们可以通过将所有状态与相同的作品融合在一起,忽略LookAheads,将其转换为LALR(1)解析器。如果您的LR(1)状态没有与任何东西合并,则LR(1)状态在LALR(1)解析器中逐字化。而且,由于LR(1)状态没有班次/减少或减少冲突,因此相应的LALR(1)解析器状态不会有任何冲突。

在SLR(1)方面,您可以最终获得不会发生LR(1)状态合并的状态,但会减少/减少冲突。其背后的直觉是,您可以拥有一个没有减少/减少LR(1)解析器冲突的状态,因为LookAheads具有足够的细节来解决冲突,但是当从LR(1)切换到SLR并扩展时LookAhead集合我们不小心引入了减少/减少冲突。这是语法发生的示例:

  • S→ATB | ar | CT
  • T→D
  • R→D

这是LR(1)配置集:

(1)
   S' -> .S    [$]
   S  -> .aTb  [$]
   S  -> .aR   [$]
   S  -> .cT   [$]

(2)
   S' -> S.    [$]

(3)
   S -> a.Tb   [$]
   S -> a.R    [$]
   T -> .d     [b]
   R -> .d     [$]

(4)
   T -> d.     [b]
   R -> d.     [$]

(5)
   S -> aT.b   [$]

(6)
   S -> aTb.   [$]

(7)
   S -> aR.    [$]

(8)
   S -> c.T    [$]
   T -> .d     [$]

(9)
   T -> d.     [$]
   
(10)
   S -> cT.    [$]

这些是您在SLR(1)解析器中所具有的相同项目集。请注意,此外,以下是(t)= {$,b}。这意味着LR(1)状态

(4)
   T -> d.     [b]
   R -> d.     [$]

被转换为SLR(1)状态,

(4)
   T -> d.     [b, $]
   R -> d.     [$]

该状态具有减少/减少$的冲突。

Interesting question! The answer is

  • if a grammar is LR(1), any conflicts in the LALR(1) parser must occur in merged states, but
  • if a grammar is LR(1), conflicts may appear in LR(1) states that were not merged.

For the first point, suppose you have a grammar that’s LR(1), so you can form its LR(1) parser. We can convert that to an LALR(1) parser by merging together all states with the same productions, ignoring lookaheads. If you have an LR(1) state that doesn’t get merged with anything, then that LR(1) state is present verbatim in the LALR(1) parser. And since the LR(1) state has no shift/reduce or reduce/reduce conflicts, the corresponding LALR(1) parser state won’t have any conflicts.

On the SLR(1) front, you can end up with states where no LR(1) state merging would occur, yet there's a reduce/reduce conflict. The intuition behind this is that you can have a state with no reduce/reduce conflicts in the LR(1) parser because the lookaheads have enough detail to resolve the conflict, yet when switching from LR(1) to SLR(1) and expanding the lookahead sets we accidentally introduce a reduce/reduce conflict. Here's an example of a grammar where this happens:

  • S → aTb | aR | cT
  • T → d
  • R → d

Here's the LR(1) configurating sets:

(1)
   S' -> .S    [$]
   S  -> .aTb  [$]
   S  -> .aR   [$]
   S  -> .cT   [$]

(2)
   S' -> S.    [$]

(3)
   S -> a.Tb   [$]
   S -> a.R    [$]
   T -> .d     [b]
   R -> .d     [$]

(4)
   T -> d.     [b]
   R -> d.     [$]

(5)
   S -> aT.b   [$]

(6)
   S -> aTb.   [$]

(7)
   S -> aR.    [$]

(8)
   S -> c.T    [$]
   T -> .d     [$]

(9)
   T -> d.     [$]
   
(10)
   S -> cT.    [$]

These are the same item sets that you'd have in the SLR(1) parser. Notice, also, that FOLLOW(T) = {$, b}. This means that the LR(1) state

(4)
   T -> d.     [b]
   R -> d.     [$]

is converted to the SLR(1) state

(4)
   T -> d.     [b, $]
   R -> d.     [$]

which has a reduce/reduce conflict on $.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文