这个Bison代码中的转移/减少冲突来自哪里?

yeg*_*256 3 bison

我正在尝试解析这种语法:

34 + 1 ? 8, 32 * 87 + 6 / 4, 34 / 8
Run Code Online (Sandbox Code Playgroud)

我期待它像这样:

(, (- (+ 34 1) 8) (/ (+ (* 32 87) 6) 4) (/ 34 8))
Run Code Online (Sandbox Code Playgroud)

这是BISON的代码:

%token NUMBER
%token COMMA
%token OPERATOR
%left OPERATOR
%left COMMA
%%

term: NUMBER | term op term ;
op: OPERATOR | COMMA;
%%
Run Code Online (Sandbox Code Playgroud)

这儿存在一个问题:

test.y: conflicts: 2 shift/reduce
Run Code Online (Sandbox Code Playgroud)

我该如何解决?

小智 11

要查找冲突的位置,请使用该--verbose选项并查看example.output输入文件所在的文件example.y.这是我从你的输入中得到的文件:

State 7 conflicts: 2 shift/reduce
Run Code Online (Sandbox Code Playgroud)

(略)

state 7

    2 term: term . op term
    2     | term op term .

    COMMA     shift, and go to state 4
    OPERATOR  shift, and go to state 5

    COMMA     [reduce using rule 2 (term)]
    OPERATOR  [reduce using rule 2 (term)]
    $default  reduce using rule 2 (term)

    op  go to state 6
Run Code Online (Sandbox Code Playgroud)