parsing - 在 Bison 中没有关联的情况下难以解决悬空问题
问题描述
我曾尝试遵循有关解决作业语法的悬空问题的建议,但我不太明白。我仍然有一个单一的转变/减少冲突。我不能对 Bison 文件使用 assoc 或 left 或 right 。
我试图用尽 StackOverflow 上的所有帖子,并通过谷歌搜索无济于事。
ConditionStmt
: ClosedStatement { $$ = $1;}
| OpenStatement {$$ = $1; }
;
OpenStatement
: "if" SimpleExpression "then" Statement {$$ = ast::action::MakeStatement<ast::Branch>({$2, $4}, @1, ast::BranchType::kIf); }
| "if" SimpleExpression "then" ClosedStatement "else" OpenStatement {$$ = ast::action::MakeStatement<ast::Branch>({$2, $4, $6}, @1, ast::BranchType::kIf); }
| "while" SimpleExpression "do" OpenStatement { $$ = ast::action::MakeStatement<ast::Iterator>({$2, $4}, @1, ast::IteratorType::kWhile); }
;
ClosedStatement
: "if" SimpleExpression "then" ClosedStatement "else" ClosedStatement { \
$$ = ast::action::MakeStatement<ast::Branch>({$2, $4, $6}, @1, ast::BranchType::kIf); \
}
| "while" SimpleExpression "do" ClosedStatement { $$ = ast::action::MakeStatement<ast::Iterator>({$2, $4}, @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 冲突是在将 anelse
和归约到非终结 ConditionStmt 之间。这告诉您问题出在规则右侧有 ConditionStmt 的某个地方——关于该上下文的某些内容允许 ConditionStmt 后跟一个else
. 但是在你展示的语法中,从未使用过 ConditionStmt,所以我们不能说那个问题是什么。
我最好的猜测是,您的规则中有某些NonIfStatement
内容(您也没有显示)以 rhs 上的 ConditionStmt(直接或间接)结束,这会导致此问题。
推荐阅读
- java - 对字节数组的 ArrayList 进行排序
- .net - 带有单个任务的异步 OnNext
- facebook-graph-api - Facebook Graph API - 实时观看者峰值指标?
- docker - 无法访问使用 docker 工具箱运行的 docker 容器内的 postgres 数据库
- ansible - 在 playbook 中设置密码变量时,Ansible “成为”插件不起作用
- python - 从 .ply 转换为 .pcd 格式
- airflow-scheduler - 气流 | 多天批量调度 URL
- c# - 无法识别查询源
- java - 如何从 Java 中的 POST 响应中提取标头值?
- gnuplot - 调用脚本时未打开 Gnuplot 控制台窗口