C++ 野牛:减少/减少冲突
我正在用bison编写SQL编译器,在解释状态机bison的生成代码时遇到了问题。以下是导致1C++ 野牛:减少/减少冲突,c++,bison,bisonc++,C++,Bison,Bisonc++,我正在用bison编写SQL编译器,在解释状态机bison的生成代码时遇到了问题。以下是导致1reduce/reduce错误的两种状态 我猜是非
reduce/reduce
错误的两种状态
我猜是非
导致了中的减少/重现,比如和中的(参见下面的代码)
我希望有人能给我指出正确的方向。如果需要更多信息,请告诉我
like_cond : scalar_exp not_qm LIKE scalar_exp escape_scalar_exp_qm
;
in_cond : row_constructor not_qm IN LPAREN table_exp RPAREN
| scalar_exp not_qm IN LPAREN scalar_exp_list RPAREN
;
not_qm : /* empty */
| NOT
;
### EDITTED SECTION
row_constructor : scalar_exp
| LPAREN scalar_exp_list RPAREN
;
scalar_exp : un_op_qm scalar_primary
| scalar_exp bin_op scalar_primary
;
###
State 193
35 like_cond: scalar_exp . not_qm LIKE scalar_exp escape_scalar_exp_qm
37 in_cond: scalar_exp . not_qm IN LPAREN scalar_exp_list RPAREN
75 row_constructor: scalar_exp .
78 scalar_exp: scalar_exp . bin_op scalar_primary
STAR shift, and go to state 59
NOT shift, and go to state 218
PLUS shift, and go to state 60
MINUS shift, and go to state 61
DIV shift, and go to state 62
CONCAT shift, and go to state 63
NOT [reduce using rule 75 (row_constructor)]
LIKE reduce using rule 148 (not_qm)
IN reduce using rule 75 (row_constructor)
IN [reduce using rule 148 (not_qm)]
$default reduce using rule 75 (row_constructor)
bin_op go to state 64
not_qm go to state 228
State 211
35 like_cond: scalar_exp . not_qm LIKE scalar_exp escape_scalar_exp_qm
37 in_cond: scalar_exp . not_qm IN LPAREN scalar_exp_list RPAREN
75 row_constructor: scalar_exp .
78 scalar_exp: scalar_exp . bin_op scalar_primary
123 scalar_exp_list: scalar_exp . scalar_exp_list_star
STAR shift, and go to state 59
NOT shift, and go to state 218
PLUS shift, and go to state 60
MINUS shift, and go to state 61
DIV shift, and go to state 62
CONCAT shift, and go to state 63
COMMA shift, and go to state 109
RPAREN reduce using rule 124 (scalar_exp_list_star)
NOT [reduce using rule 75 (row_constructor)]
LIKE reduce using rule 148 (not_qm)
IN reduce using rule 75 (row_constructor)
IN [reduce using rule 148 (not_qm)]
$default reduce using rule 75 (row_constructor)
bin_op go to state 64
scalar_exp_list_star go to state 110
not_qm go to state 228
问题在于这3条规则:
1)row_constructor not_qm IN LPAREN table_exp RPAREN
2)scalar_exp not_qm IN LPAREN scalar_exp_list RPAREN
3)row_constructor : scalar_exp
看看如果堆栈上的最后一个元素是scalar\u exp
,下一个标记是IN
,会发生什么情况:
它可以将空字符串缩减为not\u qm
,使堆栈成为scalar\u exp,not\u qm
,也可以将scalar\u exp
缩减为row\u构造函数。这是因为bison生成了一个LALR(1)解析器,所以它只根据堆栈的顶部元素和下一个标记做出决定。这就是为什么在这一点上它不能区分1)
和2)
规则,尽管它们是不同的。因此,您需要更改语法,使其成为LALR(1)-可解析的。您能展示一下您用于scalar\u exp
和row\u构造函数的规则吗?
?我将它们添加到了我的开始博文中感谢您的帮助。那么问题是bison构建了一个解析器,它使用了大小为1的前瞻?我将语法编辑为LALR(1)有效,并且reduce/reduce冲突消失。@w是的,它使用LALR(1)解析,因为这种类型的解析器在时间和内存使用方面更有效。