首页 > 解决方案 > 在 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

标签: parsingcompiler-constructionbisonambiguous-grammar

解决方案


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(直接或间接)结束,这会导致此问题。


推荐阅读