[英]LR(1) BNF grammar for function parameters with trailing elipsis
I would like to write an LR(1) grammar in BNF form for the language described by these two rules from The Complete Syntax of Lua : 我想以BNF形式针对《 Lua的完整语法》中这两个规则描述的语言编写LR(1)语法:
parlist ::= namelist [`,´ `...´] | `...´
namelist ::= Name {`,´ Name}
I have tried the following grammars, but according to the tool I am using, both are "not LR(1) due to shift-reduce conflict": 我已经尝试了以下语法,但是根据我使用的工具,由于“ shift-reduce冲突”,两者都是“不是LR(1)”:
parlist ::= namelist
parlist ::= namelist , ...
parlist ::= ...
namelist ::= Name namelist1
namelist1 ::= , Name namelist1
namelist1 ::= <epsilon>
parlist ::= namelist
parlist ::= namelist , ...
parlist ::= ...
namelist ::= namelist1 Name
namelist1 ::= namelist1 Name ,
namelist1 ::= <epsilon>
Is there an LR(1) grammar, in BNF form, for this language? 该语言是否有BNF形式的LR(1)语法?
Here's a simple one: 这是一个简单的例子:
parlist ::= Name
parlist ::= ...
parlist ::= Name , parlist
Here's a slightly less simple one which has the advantage of being left-recursive: 这是一个不太简单的方法,它具有左递归的优点:
parlist ::= namelist
parlist ::= namelist , ...
parlist ::= ...
namelist ::= Name
namelist ::= namelist , Name
The rather artificial distortion of the grammar using an artificial namelist1
non-terminal, which looks like it is taken out of an LL grammar, is just causing problems. 使用人为的
namelist1
非终结namelist1
对人为的语法进行相当人为的变形,这似乎是从LL语法中提取出来的,这只会引起问题。 (It's not making the grammar LL(1) either, although that could easily be done by left factoring the first alternative above.) (尽管也可以通过左因子分解上述第一种选择来轻松完成语法LL(1)的编写。)
Here's a bison grammar with no conflicts. 这是没有冲突的野牛语法。 It's LALR(1), which makes it also LR(1):
是LALR(1),这也使其成为LR(1):
%token ELIPSES COMMA NAME
%%
parlist : namelist parlistsuffix
| ELIPSES
;
parlistsuffix: COMMA ELIPSES
| /* epsilon */
;
namelist: namelist COMMA NAME
| NAME
;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.