在yacc中为可选数据建模的最佳方式是什么?我有以下声明:如何构建yacc代码以支持可选的非终端
StmtBlock : '{' VariableDeclList StmtList '}' { $$ = new StmtBlock($2, $3); }
;
两者VariableDeclList和StmtList是可选的(ε)所以我模仿他们如下:
VariableDeclList : VariableDeclList VariableDecl { ($$=$1)->Append($2); }
| { $$ = new List<VarDecl*>; }
和
StmtList : StmtList Stmt { ($$=$1)->Append($2); }
| { $$ = new List<Stmt*>; }
;
唯一的问题是,当我认为这会导致转变/减少冲突。当我尝试编译我的代码时,我的y.ouput文件包含以下内容:
State 74 conflicts: 1 shift/reduce
...
state 74
38 StmtBlock: '{' VariableDeclList . StmtList '}'
39 VariableDeclList: VariableDeclList . VariableDecl
T_Bool shift, and go to state 2
T_Int shift, and go to state 3
T_Double shift, and go to state 4
T_String shift, and go to state 5
T_Identifier shift, and go to state 8
T_Identifier [reduce using rule 18 (Epsilon)]
$default reduce using rule 18 (Epsilon)
VariableDecl go to state 80
Variable go to state 13
Type go to state 34
Epsilon go to state 81
StmtList go to state 82
...
是否有更合适的方法来对此进行建模?
感谢克里斯的建议。你能否指出我在正确的方向上对你如何在你的第一个建议中进行后检查? – blcArmadillo 2011-02-06 20:39:14