简体   繁体   English

是否可以将此文法转换为 LR(1)?

[英]Is it possible to transform this grammar to be LR(1)?

The following grammar generates the sentences a, a , a, b , b, b , ..., h, b .以下语法生成句子a, a , a, b , b, b , ..., h, b Unfortunately it is not LR(1) so cannot be used with tools such as "yacc".不幸的是,它不是 LR(1),因此不能与诸如“yacc”之类的工具一起使用。

S -> a comma a.
S -> C comma b.
C -> a | b | c | d | e | f | g | h.

Is it possible to transform this grammar to be LR(1) (or even LALR(1), LL(k) or LL(1)) without the need to expand the nonterminal C and thus significantly increase the number of productions?是否可以将此文法转换为 LR(1)(甚至 LALR(1)、LL(k) 或 LL(1))而不需要扩展非终结符C从而显着增加产生式的数量?

Not as long as you have the nonterminal C unchanged preceding comma in some rule.只要您在某些规则中使非终结符 C 前面的逗号保持不变即可。

In that case it is clear that a parser cannot decide, having seen an "a", and having lookahead "comma", whether to reduce or shift.在这种情况下,很明显解析器无法决定,在看到“a”和向前看“逗号”后,是减少还是移位。 So with C unchanged, this grammar is not LR(1), as you have said.所以在 C 不变的情况下,这个语法不是 LR(1),正如你所说的。

But the solution lies in the two phrases, "having seen an 'a'" and "C unchanged".但解决方案在于两个短语,“已经看到一个'a'”和“C 不变”。 You asked if there's fix that doesn't expand C. There isn't, but you could expand C "a little bit" by removing "a" from C, since that's the source of the problem:您询问是否有不扩展 C 的修复程序。没有,但是您可以通过从 C 中删除“a”来“稍微”扩展 C,因为这是问题的根源:

S -> a comma a .
S -> a comma b .
S -> C comma b .
C -> b | c | d | e | f | g | h .

So, we did not "significantly" increase the number of productions.因此,我们并没有“显着”增加制作数量。

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

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