【问题标题】:Bison Shift Reduce ConflictsBison Shift 减少冲突
【发布时间】:2017-01-29 22:40:14
【问题描述】:

我正在使用 Bison,而我的 CFG 有一个 Shift 减少冲突,这搞砸了我的优先级。

这是我的代码:

Decl      :  vartype T_Identifier T_Semicolon {
                                             // replace it with your implementation
                                             Identifier *id = new Identifier(@2, $2);
                                             $$ = new VarDecl(id, $1);
                                          }
      |  vartype T_Identifier T_Equal primaryExpr T_Semicolon {
                                                    Identifier *id = new Identifier(@2, $2);
                                                    $$ = new VarDecl(id, $1, $4);
                            }

      |  function_prototype T_Semicolon {$$ = $1;}
      ; 

我有一个针对此特定规则的轮班减少冲突。我希望最后一行(function_prototype ...)具有最高优先级,但冲突转移并将我发送到另一个状态。仅供参考,“function_prototype”是具有“vartype T_Identifier T_LeftParenth”规则的非终结符。这是野牛的输出文件:

State 28 conflicts: 1 shift/reduce
...
state 28

    4 Decl: vartype . T_Identifier T_Semicolon
    5     | vartype . T_Identifier T_Equal primaryExpr T_Semicolon
   11 fully_specified_type: vartype .

    T_Identifier  shift, and go to state 34

    T_Identifier  [reduce using rule 11 (fully_specified_type)]
...
state 34

    4 Decl: vartype T_Identifier . T_Semicolon
    5     | vartype T_Identifier . T_Equal primaryExpr T_Semicolon

    T_Equal      shift, and go to state 36
    T_Semicolon  shift, and go to state 37 

State 34 跳过了我的“函数原型”规则!如何解决此冲突和优先级问题?

【问题讨论】:

    标签: bison


    【解决方案1】:

    冲突在Decl: vartype...规则和fully_specified_type: vartype规则之间——看到vartype后,当lookahead为T_Identifier时,不知道这是不是fully_specified type或不。因此它会发生变化(默认分辨率),将其视为简单的vartype 或Decl。

    一般来说,这类事情通常是一个问题,需要多个令牌前瞻才能知道如何解析事物,但由于您没有显示与 fully_specified_type 规则相关的任何内容,因此很难说如何要解决这个问题。很可能有一种方法可以重构您的语法(也许只需摆脱 fully_qualified_type 并直接使用 vartype 在任何使用它的地方)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多