【发布时间】: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