0

我正在用 bison 编写 SQL 编译器,但无法解释状态机 bison 的生成。下面是两个状态,每个状态都会导致 1 个reduce/reduce错误。

我猜not_qm是导致这些reduce/recude's in like_condand in_cond(见下面的代码)。

我希望有人能指出我正确的方向。如果需要更多信息,请告诉我。

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
4

1 回答 1

0

问题在于这 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_exp并且下一个标记是会发生什么IN:它可以将一个空字符串归not_qm约为 使堆栈变为scalar_exp, not_qm,也可以scalar_exp归约为row_constructor。发生这种情况是因为 bison 生成了一个 LALR(1) 解析器,因此它仅根据堆栈的顶部元素和下一个标记做出决定。这就是为什么它在这一点上无法区分1)2)规则,即使它们不同。所以你需要改变你的语法,使它变成 LALR(1) 可解析的。

于 2014-10-16T19:12:35.313 回答