在 Bison 中没有 assoc 的情况下难以解决悬挂的其他问题
Difficulty fixing dangling else problem without assoc in Bison
我已尝试遵循有关修复作业语法的悬空 else 问题的建议,但我不太明白。我还有一个 shift/reduce 冲突。我无法对 Bison 文件使用 assoc 或 left 或 right。
我试图用尽 Whosebug 上的所有帖子并浏览 Google 无济于事。
ConditionStmt
: ClosedStatement { $$ = ;}
| OpenStatement {$$ = ; }
;
OpenStatement
: "if" SimpleExpression "then" Statement {$$ = ast::action::MakeStatement<ast::Branch>({, }, @1, ast::BranchType::kIf); }
| "if" SimpleExpression "then" ClosedStatement "else" OpenStatement {$$ = ast::action::MakeStatement<ast::Branch>({, , }, @1, ast::BranchType::kIf); }
| "while" SimpleExpression "do" OpenStatement { $$ = ast::action::MakeStatement<ast::Iterator>({, }, @1, ast::IteratorType::kWhile); }
;
ClosedStatement
: "if" SimpleExpression "then" ClosedStatement "else" ClosedStatement { \
$$ = ast::action::MakeStatement<ast::Branch>({, , }, @1, ast::BranchType::kIf); \
}
| "while" SimpleExpression "do" ClosedStatement { $$ = ast::action::MakeStatement<ast::Iterator>({, }, @1, ast::IteratorType::kWhile); }
| NonIfStatement
;
这是野牛输出
State 148
42 ConditionStmt: ClosedStatement .
45 OpenStatement: "if" SimpleExpression "then" ClosedStatement . "else" OpenStatement
47 ClosedStatement: "if" SimpleExpression "then" ClosedStatement . "else" ClosedStatement
"else" shift, and go to state 158
"else" [reduce using rule 42 (ConditionStmt)]
$default reduce using rule 42 (ConditionStmt)
State 158
45 OpenStatement: "if" SimpleExpression "then" ClosedStatement "else" . OpenStatement
47 ClosedStatement: "if" SimpleExpression "then" ClosedStatement "else" . ClosedStatement
"if" shift, and go to state 61
"return" shift, and go to state 62
"for" shift, and go to state 63
"while" shift, and go to state 64
"break" shift, and go to state 65
"not" shift, and go to state 36
";" shift, and go to state 66
"{" shift, and go to state 67
"(" shift, and go to state 37
"-" shift, and go to state 38
"*" shift, and go to state 39
"?" shift, and go to state 40
kNumericConst shift, and go to state 41
kCharConst shift, and go to state 42
kString shift, and go to state 43
kIdentifier shift, and go to state 44
kBoolConst shift, and go to state 45
NonIfStatement go to state 69
ExpressionStmt go to state 70
CompoundStmt go to state 71
OpenStatement go to state 163
ClosedStatement go to state 164
IteratorStmt go to state 75
ReturnStmt go to state 76
BreakStmt go to state 77
Expression go to state 78
SimpleExpression go to state 79
AndExpr go to state 47
UnaryReletiveExpr go to state 48
ReletiveExpr go to state 49
SumExpr go to state 50
MulExp go to state 51
UnaryExpr go to state 52
UnaryOper go to state 53
Factor go to state 54
Mutable go to state 80
Immutable go to state 56
Call go to state 57
Constant go to state 58
bison .output 文件中的这两行
"else" shift, and go to state 158
"else" [reduce using rule 42 (ConditionStmt)]
告诉你,shift-reduce 冲突是在移位 else
和归约到非终结符 ConditionStmt 之间。这告诉您问题出在某个地方,您在规则的右侧有 ConditionStmt —— 关于该上下文的某些内容允许 ConditionStmt 后跟一个 else
。但是在你展示的语法中,从未使用过ConditionStmt,所以我们不能说那个问题是什么。
我最好的猜测是你在 NonIfStatement
的规则中有一些东西(你也没有显示)(直接或间接地)以 rhs 上的 ConditionStmt 结束,这导致了这个问题。
我已尝试遵循有关修复作业语法的悬空 else 问题的建议,但我不太明白。我还有一个 shift/reduce 冲突。我无法对 Bison 文件使用 assoc 或 left 或 right。
我试图用尽 Whosebug 上的所有帖子并浏览 Google 无济于事。
ConditionStmt
: ClosedStatement { $$ = ;}
| OpenStatement {$$ = ; }
;
OpenStatement
: "if" SimpleExpression "then" Statement {$$ = ast::action::MakeStatement<ast::Branch>({, }, @1, ast::BranchType::kIf); }
| "if" SimpleExpression "then" ClosedStatement "else" OpenStatement {$$ = ast::action::MakeStatement<ast::Branch>({, , }, @1, ast::BranchType::kIf); }
| "while" SimpleExpression "do" OpenStatement { $$ = ast::action::MakeStatement<ast::Iterator>({, }, @1, ast::IteratorType::kWhile); }
;
ClosedStatement
: "if" SimpleExpression "then" ClosedStatement "else" ClosedStatement { \
$$ = ast::action::MakeStatement<ast::Branch>({, , }, @1, ast::BranchType::kIf); \
}
| "while" SimpleExpression "do" ClosedStatement { $$ = ast::action::MakeStatement<ast::Iterator>({, }, @1, ast::IteratorType::kWhile); }
| NonIfStatement
;
这是野牛输出
State 148
42 ConditionStmt: ClosedStatement .
45 OpenStatement: "if" SimpleExpression "then" ClosedStatement . "else" OpenStatement
47 ClosedStatement: "if" SimpleExpression "then" ClosedStatement . "else" ClosedStatement
"else" shift, and go to state 158
"else" [reduce using rule 42 (ConditionStmt)]
$default reduce using rule 42 (ConditionStmt)
State 158
45 OpenStatement: "if" SimpleExpression "then" ClosedStatement "else" . OpenStatement
47 ClosedStatement: "if" SimpleExpression "then" ClosedStatement "else" . ClosedStatement
"if" shift, and go to state 61
"return" shift, and go to state 62
"for" shift, and go to state 63
"while" shift, and go to state 64
"break" shift, and go to state 65
"not" shift, and go to state 36
";" shift, and go to state 66
"{" shift, and go to state 67
"(" shift, and go to state 37
"-" shift, and go to state 38
"*" shift, and go to state 39
"?" shift, and go to state 40
kNumericConst shift, and go to state 41
kCharConst shift, and go to state 42
kString shift, and go to state 43
kIdentifier shift, and go to state 44
kBoolConst shift, and go to state 45
NonIfStatement go to state 69
ExpressionStmt go to state 70
CompoundStmt go to state 71
OpenStatement go to state 163
ClosedStatement go to state 164
IteratorStmt go to state 75
ReturnStmt go to state 76
BreakStmt go to state 77
Expression go to state 78
SimpleExpression go to state 79
AndExpr go to state 47
UnaryReletiveExpr go to state 48
ReletiveExpr go to state 49
SumExpr go to state 50
MulExp go to state 51
UnaryExpr go to state 52
UnaryOper go to state 53
Factor go to state 54
Mutable go to state 80
Immutable go to state 56
Call go to state 57
Constant go to state 58
bison .output 文件中的这两行
"else" shift, and go to state 158
"else" [reduce using rule 42 (ConditionStmt)]
告诉你,shift-reduce 冲突是在移位 else
和归约到非终结符 ConditionStmt 之间。这告诉您问题出在某个地方,您在规则的右侧有 ConditionStmt —— 关于该上下文的某些内容允许 ConditionStmt 后跟一个 else
。但是在你展示的语法中,从未使用过ConditionStmt,所以我们不能说那个问题是什么。
我最好的猜测是你在 NonIfStatement
的规则中有一些东西(你也没有显示)(直接或间接地)以 rhs 上的 ConditionStmt 结束,这导致了这个问题。