Bison Shift 减少冲突
Bison Shift Reduce Conflicts
我正在使用 Bison,我的 CFG 有一个 Shift reduce 冲突,这搞砸了我的优先级。
这是我的代码:
Decl : vartype T_Identifier T_Semicolon {
// replace it with your implementation
Identifier *id = new Identifier(@2, );
$$ = new VarDecl(id, );
}
| vartype T_Identifier T_Equal primaryExpr T_Semicolon {
Identifier *id = new Identifier(@2, );
$$ = new VarDecl(id, , );
}
| function_prototype T_Semicolon {$$ = ;}
;
我有一个减少此特定规则冲突的班次。我希望最后一行 (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
状态 34 跳过了我的 "function prototype" 规则!我该如何解决这个冲突和优先问题?
冲突发生在 Decl: vartype
... 规则和 fully_specified_type: vartype
规则之间 -- 在看到 vartype
之后,当前瞻是 T_Identifier
时,它不会不知道这是不是fully_specified type
。所以它移动(默认分辨率),将其视为 Decl
.
的简单 vartype
一般来说,这种事情通常是一个问题,需要不止一个标记先行才能知道如何解析事物,但是由于您没有显示与 fully_specified_type
规则相关的任何内容,因此它很难说如何解决。很可能有一种方法可以重构你的语法(也许只是摆脱 fully_qualified_type
并直接在任何使用的地方使用 vartype
)。
我正在使用 Bison,我的 CFG 有一个 Shift reduce 冲突,这搞砸了我的优先级。
这是我的代码:
Decl : vartype T_Identifier T_Semicolon {
// replace it with your implementation
Identifier *id = new Identifier(@2, );
$$ = new VarDecl(id, );
}
| vartype T_Identifier T_Equal primaryExpr T_Semicolon {
Identifier *id = new Identifier(@2, );
$$ = new VarDecl(id, , );
}
| function_prototype T_Semicolon {$$ = ;}
;
我有一个减少此特定规则冲突的班次。我希望最后一行 (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
状态 34 跳过了我的 "function prototype" 规则!我该如何解决这个冲突和优先问题?
冲突发生在 Decl: vartype
... 规则和 fully_specified_type: vartype
规则之间 -- 在看到 vartype
之后,当前瞻是 T_Identifier
时,它不会不知道这是不是fully_specified type
。所以它移动(默认分辨率),将其视为 Decl
.
vartype
一般来说,这种事情通常是一个问题,需要不止一个标记先行才能知道如何解析事物,但是由于您没有显示与 fully_specified_type
规则相关的任何内容,因此它很难说如何解决。很可能有一种方法可以重构你的语法(也许只是摆脱 fully_qualified_type
并直接在任何使用的地方使用 vartype
)。