shift/reduce 语法与可选分号冲突
shift/reduce conflict in grammar with optional semicolon
我想做这样的事情:
// match used as a statement, semicolon is optional
match (1) {}
// match used as an expression, semicolon is required
1 + match (2) {};
statement
: expression_without_block T_SEMICOLON
| expression_with_block
;
expression
: expression_without_block
| expression_with_block
;
expression_without_block
: scalar
| expression_without_block T_PLUS expression
| T_PLUS expression
;
expression_with_block
: T_MATCH T_LEFT expression T_RIGHT T_CURLY_LEFT T_CURLY_RIGHT
;
这个想法是 expression_with_block
不能用在语句的开头,使以下内容明确无误:
match (1) {}
+2;
// Can't mean
(match (1) {} + 2);
// because of the line "expression_without_block T_PLUS expression"
语法导致 shift/reduce 冲突,但我不知道为什么。输出如下(但我不确定如何处理):
State 11 conflicts: 1 shift/reduce
...
State 11
5 expression: expression_without_block .
8 expression_without_block: expression_without_block . T_PLUS expression
T_PLUS shift, and go to state 14
T_PLUS [reduce using rule 5 (expression)]
$default reduce using rule 5 (expression)
The full output file can be found here.
我什至不确定这是正确的方法,因为这样的方法是行不通的:
// `match` can't be at the lhs of an expression, even in sub-expressions
func_call(match (10) {} + 20);
有没有办法在 bison 中实现我在这里描述的内容?我不是野牛专家,所以我真的很感激一些帮助。谢谢!
这是左递归规则发生的一种非常经典的冲突,但由于 shift/reduce 冲突的默认解决方案是进行移位,那么
这里一切都会好起来的。
阅读野牛踪迹的方法如下:
T_PLUS shift, and go to state 14 -- when next token is T_PLUS shift action is selected
T_PLUS [reduce using rule 5 (expression)] -- the action between [] was disabled by Bison default conflict resolution
要覆盖 Bison 的默认冲突解决方案(此处不需要),我们可以使用运算符优先级(参见 https://www.gnu.org/software/bison/manual/bison.html#Precedence-Decl) or precedence for non operator (see https://www.gnu.org/software/bison/manual/bison.html#Non-Operators)
我想做这样的事情:
// match used as a statement, semicolon is optional
match (1) {}
// match used as an expression, semicolon is required
1 + match (2) {};
statement
: expression_without_block T_SEMICOLON
| expression_with_block
;
expression
: expression_without_block
| expression_with_block
;
expression_without_block
: scalar
| expression_without_block T_PLUS expression
| T_PLUS expression
;
expression_with_block
: T_MATCH T_LEFT expression T_RIGHT T_CURLY_LEFT T_CURLY_RIGHT
;
这个想法是 expression_with_block
不能用在语句的开头,使以下内容明确无误:
match (1) {}
+2;
// Can't mean
(match (1) {} + 2);
// because of the line "expression_without_block T_PLUS expression"
语法导致 shift/reduce 冲突,但我不知道为什么。输出如下(但我不确定如何处理):
State 11 conflicts: 1 shift/reduce
...
State 11
5 expression: expression_without_block .
8 expression_without_block: expression_without_block . T_PLUS expression
T_PLUS shift, and go to state 14
T_PLUS [reduce using rule 5 (expression)]
$default reduce using rule 5 (expression)
The full output file can be found here.
我什至不确定这是正确的方法,因为这样的方法是行不通的:
// `match` can't be at the lhs of an expression, even in sub-expressions
func_call(match (10) {} + 20);
有没有办法在 bison 中实现我在这里描述的内容?我不是野牛专家,所以我真的很感激一些帮助。谢谢!
这是左递归规则发生的一种非常经典的冲突,但由于 shift/reduce 冲突的默认解决方案是进行移位,那么 这里一切都会好起来的。
阅读野牛踪迹的方法如下:
T_PLUS shift, and go to state 14 -- when next token is T_PLUS shift action is selected
T_PLUS [reduce using rule 5 (expression)] -- the action between [] was disabled by Bison default conflict resolution
要覆盖 Bison 的默认冲突解决方案(此处不需要),我们可以使用运算符优先级(参见 https://www.gnu.org/software/bison/manual/bison.html#Precedence-Decl) or precedence for non operator (see https://www.gnu.org/software/bison/manual/bison.html#Non-Operators)