野牛:奇怪的转变,减少冲突 [英] Bison: strange shift-reduce conflict
问题描述
我尝试在自定义语法中实现函数调用(加上类似的数组访问运算符):
I try to implement a function call in a custom grammar (plus a similar array-access operator):
expression
: ....OTHER EXPRESSION RULES....
| expression PARENTHESIS_OPEN expressions PARENTHESIS_CLOSE { } %prec DOT
| expression SQUARE_OPEN expressions SQUARE_CLOSE { } %prec DOT
;
这是我所有的运算符优先级:
Here is my all my operator precedences:
%right ASSIGN ASSIGN_MOD ASSIGN_XOR ASSIGN_AND ASSIGN_STAR ASSIGN_MINUS ASSIGN_PLUS ASSIGN_OR ASSIGN_DIV ASSIGN_LSHIFT ASSIGN_RSHIFT
%right QUESTION COLON
%left OR
%left AND
%left BIN_OR
%left XOR
%left BIN_AND
%left NOT_EQUALS NOT_SAME EQUALS SAME
%left LESS LESS_EQUALS MORE MORE_EQUALS
%left LSHIFT RSHIFT
%left PLUS MINUS
%left PERCENT STAR SLASH
%right TILDE NOT DECREASE INCREASE
%left DOT
请注意,DOT的优先级最高.因此,我尝试将其赋予我的函数调用规则.尽管如此,我仍然收到74个移位/减少警告,所有警告都遵循此模式:
Please note that DOT is has the highest precedence. So, I try to give this to my function-call rules. Still, I get 74 shift/reduce warnings, that all follow this pattern:
State 25
15 expression: expression . PLUS expression
16 | expression . MINUS expression
17 | expression . NOT_EQUALS expression
18 | expression . NOT_SAME expression
19 | expression . PERCENT expression
20 | expression . ASSIGN_MOD expression
21 | expression . XOR expression
22 | expression . ASSIGN_XOR expression
23 | expression . BIN_AND expression
24 | expression . AND expression
25 | expression . ASSIGN_AND expression
26 | expression . STAR expression
27 | expression . ASSIGN_STAR expression
28 | expression . ASSIGN_MINUS expression
29 | expression . ASSIGN expression
30 | expression . EQUALS expression
31 | expression . SAME expression
32 | expression . ASSIGN_PLUS expression
33 | expression . BIN_OR expression
34 | expression . OR expression
35 | expression . ASSIGN_OR expression
36 | expression . SLASH expression
37 | expression . ASSIGN_DIV expression
38 | expression . DOT expression
39 | expression . LESS expression
40 | expression . LESS_EQUALS expression
41 | expression . LSHIFT expression
42 | expression . ASSIGN_LSHIFT expression
43 | expression . MORE expression
44 | expression . MORE_EQUALS expression
45 | expression . RSHIFT expression
46 | expression . ASSIGN_RSHIFT expression
48 | expression . PARENTHESIS_OPEN expressions PARENTHESIS_CLOSE
49 | expression . SQUARE_OPEN expressions SQUARE_CLOSE
53 | DECREASE expression .
55 | expression . DECREASE
56 | expression . INCREASE
PARENTHESIS_OPEN shift, and go to state 46
DECREASE shift, and go to state 47
INCREASE shift, and go to state 52
SQUARE_OPEN shift, and go to state 54
DOT shift, and go to state 61
PARENTHESIS_OPEN [reduce using rule 53 (expression)]
SQUARE_OPEN [reduce using rule 53 (expression)]
$default reduce using rule 53 (expression)
状态46表示发生冲突的移位,表示以下内容:
State 46, that the conflicted shift indicates, says the following:
State 46
48 expression: expression PARENTHESIS_OPEN . expressions PARENTHESIS_CLOSE
MINUS shift, and go to state 5
TILDE shift, and go to state 6
NOT shift, and go to state 7
PARENTHESIS_OPEN shift, and go to state 8
DECREASE shift, and go to state 9
INCREASE shift, and go to state 10
INT shift, and go to state 11
FLOAT shift, and go to state 12
STRING shift, and go to state 13
CHAR shift, and go to state 14
ID shift, and go to state 15
$default reduce using rule 59 (expressions)
expression go to state 87
expressions go to state 88
我真的不明白为什么野牛选择减少.由于我给了函数调用规则最高的优先级,所以野牛应该尝试转移直到它匹配那个规则.前缀DECREASE运算符仍然看起来像是野牛的选择,即使它的优先级较低.
I really do not get why bison chooses to reduce. Since I gave the function-call rule the highest possible precedence, bison should try to shift until it matches that one. Still, the prefix DECREASE operator looks like been the choice of bison, even if it has lower precedence.
为什么野牛会这么做?我如何对野牛清楚地说,函数调用规则应具有更高的优先级,从而避免冲突?
Why bison does that? How can I say clearly to bison that the function-call rule should have the higher precedence, and thus, avoid the conflicts?