简体   繁体   English

这个语法LR(1)怎么样而不是SLR(1)?

[英]How is this grammar LR(1) but not SLR(1)?

I have the following grammar, which I'm told is LR(1) but not SLR(1): 我有以下语法,我被告知是LR(1)而不是SLR(1):

S ::= a A | S :: = a A | b A c | b A c | dc | 直流| bda BDA

A ::= d A :: = d

I don't understand why this is. 我不明白为什么会这样。 How would you prove this? 你会怎么证明这一点?

I don't have enough reputation to comment on the above answer, and I'm a bit late to this question, but... 我没有足够的声誉评论上述答案,我对这个问题有点迟,但......

I've seen this grammar as an example elsewhere and the OP actually made a typo. 我在其他地方看过这个语法就是一个例子,OP实际上是一个错字。 It should be: 它应该是:

S ::= A a | S :: = A a | b A c | b A c | dc | 直流| bda BDA

A ::= d A :: = d

ie, the very first clause of S is ' A a', not 'a A '. 即, S的第一个条款是' A a',而不是'a A '。

In this case the FOLLOWS set for A is { $, a, c} and there is an SLR conflict in state 8. 这种情况下,为A设置的FOLLOWS是{$,a,c},并且状态8中存在SLR冲突。

One way to figure this out would be to try to construct LR(1) and SLR(1) parsers for the grammar. 解决这个问题的一种方法是尝试为语法构造LR(1)和SLR(1)解析器。 If we find any shift/reduce or reduce/reduce conflicts in the course of doing so, we can show that the grammar must not belong to one of those classes of languages. 如果我们在这样做的过程中发现任何转移/减少或减少/减少冲突,我们可以证明语法不得属于这些语言类别之一。

Let's start off with the SLR(1) parser. 让我们从SLR(1)解析器开始吧。 First, we need to compute the LR(0) configurating sets for the grammar. 首先,我们需要为语法计算LR(0)配置集。 These are seen here: 这些可以在这里看到:

(1)
S -> .aA
S -> .bAc
S -> .dc
S -> .bda

(2)
S -> a.A
A -> .d

(3)
S -> aA.

(4)
A -> d.

(5)
S -> b.Ac
S -> b.da
A -> .d

(6)
S -> bA.c

(7)
S -> bAc.

(8)
S -> bd.a
A -> d.

(9)
S -> bda.

(10)
S -> d.c

(11)
S -> dc.

Next, we need to get the FOLLOW sets for all the nonterminals. 接下来,我们需要获取所有非终结符的FOLLOW集。 This is shown here: 这显示在这里:

FOLLOW(S) = { $ }
FOLLOW(A) = { $, c }

Given this, we can go back and upgrade the LR(0) configurating sets into SLR(1) configurating sets: 鉴于此,我们可以返回并将LR(0)配置集升级为SLR(1)配置集:

(1)
S -> .aA    [ $ ]
S -> .bAc   [ $ ]
S -> .dc    [ $ ]
S -> .bda   [ $ ]

(2)
S -> a.A    [ $ ]
A -> .d     [ $, c ]

(3)
S -> aA.    [ $ ]

(4)
A -> d.     [ $, c ]

(5)
S -> b.Ac   [ $ ]
S -> b.da   [ $ ]
A -> .d     [ $, c ]

(6)
S -> bA.c   [ $ ]

(7)
S -> bAc.   [ $ ]

(8)
S -> bd.a   [ $ ]
A -> d.     [ $, c ]

(9)
S -> bda.   [ $ ]

(10)
S -> d.c    [ $ ]

(11)
S -> dc.    [ $ ]

Interestingly enough, there are no conflicts here! 有趣的是,这里没有冲突! The only possible shift/reduce conflict is in state (8), but there is no conflict here because the shift action is on a and the reduce is on $ . 唯一可能的转移/减少冲突是在状态(8),但是这里没有冲突,因为转移动作在a ,而reduce在$ Consequently, this grammar actually is SLR(1). 因此,这个语法实际上 SLR(1)。 Since any SLR(1) grammar is also LR(1), this means that the grammar is both SLR(1) and LR(1). 由于任何SLR(1)语法也是LR(1),这意味着语法是SLR(1)和LR(1)。

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

I thought about writing a web-app to determine first- and follow-sets for CFGs and also LR(0), SLR(1) and LR(1) tables. 我考虑过编写一个web-app来确定CFG的第一组和后续组以及LR(0),SLR(1)和LR(1)表。 This would have allowed you to try it out easily. 这样可以让你轻松地尝试一下。

But luckily I googled first and found that such a tool already exists (I didn't necessarily expect this to be the case). 但幸运的是我先搜索了一下,发现这样的工具已经存在(我不一定认为这是这种情况)。 You can find the tool here: 你可以在这里找到这个工具:

http://smlweb.cpsc.ucalgary.ca/ http://smlweb.cpsc.ucalgary.ca/

It expects the input in the following format: 它期望输入格式如下:

S -> a A | b A c | d c | b d a.
A -> d.

Using this tool, I have verified what others have already stated: the grammar in question is SLR(1). 使用这个工具,我已经验证了其他人已经说过的内容:所讨论的语法 SLR(1)。 (I give -1 to the question for this). (我给出了问题的-1)。

After the modification suggested by Toby Hutton, it isn't SLR(1) anymore, but still LR(1). 在Toby Hutton建议的修改之后,它不再是SLR(1),但仍然是LR(1)。

1)The given grammar is LL(1) in top down parsing, and LALR(1) in bottom up parsing. 1)给定语法在自顶向下解析中为LL(1),在自下而上解析中为LALR(1)。

2)while you are creating the parsing table, and the parsing table has No multiple entries, then the grammar tends to attend LALR(1). 2)当您创建解析表,并且解析表没有多个条目时,语法倾向于参加LALR(1)。

3)If your parsing table has multiple entries(i mean the conflict occurrence), then the grammar is said to be SLR(1). 3)如果你的解析表有多个条目(我的意思是冲突发生),那么语法就是SLR(1)。

4)A grammar is said to be LR(1) since it's parsing table or ACTION/GOTO table has no conflict, and we all know that during a conflict occurrence in LR(1) we merge the data and we get the LALR. 4)语法被称为LR(1),因为它的解析表或ACTION / GOTO表没有冲突,我们都知道在LR(1)中发生冲突期间我们合并数据并获得LALR。

LR(0) OR SLR OR SLR(1) are same LR(0)或SLR或SLR(1)相同

LR(1) OR CLR are same LR(1)或CLR是相同的

LALR OR LALR(1) are same LALR或LALR(1)是相同的

the (1) parameter defines the inner build efficiency type of the syntax analyzer. (1)参数定义语法分析器的内部构建效率类型。

thank you. 谢谢。

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

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