[英]Shift/reduce conflict with args and kwargs - PLY
I'm writing parser for Python-like language, which allows to pass two types of arguments (positional and named) to functions. 我正在为类似Python的语言编写解析器,该解析器允许将两种类型的参数(位置和命名)传递给函数。 And, like in Python, named argument must be passed after positional.
而且,就像在Python中一样,必须在位置之后传递命名参数。 I wrote a grammar for it, but it has shift/reduce conflicts, and I can't even imagine how to write it another way.
我为此编写了一个语法,但是它具有移位/减少冲突的功能,而且我什至无法想象如何用另一种方式编写它。
Here's my grammar: 这是我的语法:
optionalcomma : COMMA
| empty
arguments : posargs
| posargs COMMA kwargs
| kwargs
| empty
posargs : optionalnl languageitem
| optionalnl languageitem COMMA posargs
kwargs : optionalnl varassign optionalcomma
| optionalnl varassign COMMA kwargs
Here, optionalnl
is optional newline, languageitem
is basic object that can be passed as positional argument, and varassign
is variable assignment rule, which is equal to passing a named argument to function. 在这里,
optionalnl
是可选的换行符, languageitem
是可以作为位置参数传递的基本对象,而varassign
是变量赋值规则,它等于将命名参数传递给函数。 The parser goes through arguments to first comma, and doesn't know which item (kwarg or posarg) will follow the comma; 解析器将参数传递给第一个逗号,并且不知道哪个项目(kwarg或posarg)将跟在逗号后面; that's the problem.
那就是问题所在。 And I completely stuck here, can't write the correct grammar.
而且我完全呆在这里,无法编写正确的语法。
I'm using LALR(1) parser PLY, so advice to use GLR parsing won't help me much. 我正在使用LALR(1)解析器PLY,因此使用GLR解析的建议对我没有多大帮助。
Unless there is something unusual in the rest of your grammar, writing posargs
in the usual left-recursive fashion will not produce a shift/reduce conflict: 除非语法的其余部分中有异常,
posargs
以通常的左递归方式编写posargs
不会产生移位/减少冲突:
posargs : optionalnl languageitem
| posargs ',' optionalnl languageitem
Personally, I'd write kwargs
left-recursively as well. 就个人而言,我也会以递归方式写
kwargs
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.