首页 > 解决方案 > 如何检测野牛中的移位/减少冲突?

问题描述

我是野牛的新手。我已经制作了 .y 文件,但在编译中出现此错误:

警告:42 移位/减少冲突 [-Wconflicts-sr]

我打开了生成的 .output 文件,我看到了以下内容,但无法理解,也无法弄清楚问题出在哪里。

这是我的 .output 文件的一部分:

    State 63

   56 expr_decl: KW_NOT expr_decl .  [KW_END, KW_OR, OP_OR, KW_AND, OP_AND, '>', OP_LESSOREQUAL, OP_GREATEROREQUAL, '=', OP_NOTEQUAL, '<', '*', '/', KW_DIV, KW_MOD, ';', ')', '+', '-']
   59          | expr_decl . '*' expr_decl
   60          | expr_decl . '/' expr_decl
   61          | expr_decl . KW_DIV expr_decl
   62          | expr_decl . KW_MOD expr_decl
   63          | expr_decl . '+' expr_decl
   64          | expr_decl . '-' expr_decl
   65          | expr_decl . '=' expr_decl
   66          | expr_decl . '<' expr_decl
   67          | expr_decl . '>' expr_decl
   68          | expr_decl . OP_NOTEQUAL expr_decl
   69          | expr_decl . OP_LESSOREQUAL expr_decl
   70          | expr_decl . OP_GREATEROREQUAL expr_decl
   71          | expr_decl . OP_AND expr_decl
   72          | expr_decl . KW_AND expr_decl
   73          | expr_decl . KW_OR expr_decl
   74          | expr_decl . OP_OR expr_decl

    '+'  shift, and go to state 87
    '-'  shift, and go to state 88

    '+'       [reduce using rule 56 (expr_decl)]
    '-'       [reduce using rule 56 (expr_decl)]
    $default  reduce using rule 56 (expr_decl)

    Conflict between rule 56 and token KW_OR resolved as reduce (KW_OR < KW_NOT).
    Conflict between rule 56 and token OP_OR resolved as reduce (OP_OR < KW_NOT).
    Conflict between rule 56 and token KW_AND resolved as reduce (KW_AND < KW_NOT).
    Conflict between rule 56 and token OP_AND resolved as reduce (OP_AND < KW_NOT).
    Conflict between rule 56 and token '>' resolved as reduce ('>' < KW_NOT).
    Conflict between rule 56 and token OP_LESSOREQUAL resolved as reduce (OP_LESSOREQUAL < KW_NOT).
    Conflict between rule 56 and token OP_GREATEROREQUAL resolved as reduce (OP_GREATEROREQUAL < KW_NOT).
    Conflict between rule 56 and token '=' resolved as reduce ('=' < KW_NOT).
    Conflict between rule 56 and token OP_NOTEQUAL resolved as reduce (OP_NOTEQUAL < KW_NOT).
    Conflict between rule 56 and token '<' resolved as reduce ('<' < KW_NOT).
    Conflict between rule 56 and token '*' resolved as reduce ('*' < KW_NOT).
    Conflict between rule 56 and token '/' resolved as reduce ('/' < KW_NOT).
    Conflict between rule 56 and token KW_DIV resolved as reduce (KW_DIV < KW_NOT).
    Conflict between rule 56 and token KW_MOD resolved as reduce (KW_MOD < KW_NOT).

请问有什么帮助吗?我一直在寻找几个小时,但仍然一无所获..

这是我的声明部分:

%left KW_OR OP_OR
%left KW_AND OP_AND
%left '>' OP_LESSOREQUAL OP_GREATEROREQUAL
%left '=' OP_NOTEQUAL '<'
%left OP_PLUS OP_MINUS
%left '*' '/' KW_DIV KW_MOD
%left cast
%right POSITIVE NEGATIVE
%right KW_NOT '!'

这是我在野牛中的规则:

expr_decl : 
        | REAL
        | POSINT
        | '!' expr_decl { $$ = template("!(%s)", $2); }
        | KW_NOT expr_decl { $$ = template("not(%s)", $2); }
        | '+' expr_decl %prec POSITIVE { $$ = template("+(%s)", $2); }
        | '-' expr_decl %prec NEGATIVE { $$ = template("-(%s)", $2); }
        | expr_decl '*' expr_decl { $$ = template("%s * %s", $1, $3); }
        | expr_decl '/' expr_decl { $$ = template("%s / %s", $1, $3); }
        | expr_decl KW_DIV expr_decl { $$ = template("%s div %s", $1, $3); }
        | expr_decl KW_MOD expr_decl { $$ = template("%s mod %s", $1, $3); }
        | expr_decl '+' expr_decl %prec OP_PLUS{ $$ = template("%s + %s", $1, $3); }
        | expr_decl '-' expr_decl %prec OP_MINUS{ $$ = template("%s + %s", $1, $3); }
        | expr_decl '=' expr_decl { $$ = template("%s = %s", $1, $3); }
        | expr_decl '<' expr_decl { $$ = template("%s < %s", $1, $3); }
        | expr_decl '>' expr_decl { $$ = template("%s > %s", $1, $3); }
        | expr_decl OP_NOTEQUAL expr_decl { $$ = template("%s <> %s", $1, $3); }
        | expr_decl OP_LESSOREQUAL expr_decl { $$ = template("%s <= %s", $1, $3); }
        | expr_decl OP_GREATEROREQUAL expr_decl { $$ = template("%s >= %s", $1, $3); }
        | expr_decl OP_AND expr_decl { $$ = template("%s && %s", $1, $3); }
        | expr_decl KW_AND expr_decl { $$ = template("%s and %s", $1, $3); }
            | expr_decl KW_OR expr_decl { $$ = template("%s || %s", $1, $3); }
            | expr_decl OP_OR expr_decl { $$ = template("%s or %s", $1, $3); }
                | '(' expr_decl ')' { $$ = template("(%s)", $2); }
;

标签: parsingcompilationbisonshift-reduce-conflict

解决方案


这部分输出文件:

'+'  shift, and go to state 87
'-'  shift, and go to state 88

'+'       [reduce using rule 56 (expr_decl)]
'-'       [reduce using rule 56 (expr_decl)]

告诉您,在此状态下,前瞻符号'+''-'. reduce 操作周围的[..]表示该操作已被删除(解决冲突以支持转移)。

消息:

Conflict between rule 56 and token KW_OR resolved as reduce (KW_OR < KW_NOT).
Conflict between rule 56 and token OP_OR resolved as reduce (OP_OR < KW_NOT).
Conflict between rule 56 and token KW_AND resolved as reduce (KW_AND < KW_NOT).
       :

告诉您通过语法中的优先规则解决的移位/减少冲突(因此不包括在警告中的 42 个移位/减少冲突中)。

优先级规则不能解决冲突'+''-'因为您没有为这些令牌设置优先级。


推荐阅读