Home > database >  Difficulty fixing dangling else problem without assoc in Bison
Difficulty fixing dangling else problem without assoc in Bison

Time:09-27

I have tried to follow the advice on fixing dangling else problem for an assignments grammar but I can't quite get it. I still have the a single shift/reduce conflict. I can't use assoc or left nor right for the Bison file.

I have tried to exhaust all of the posts on StackOverflow and gone through Google to no avail.

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
  ;

This is the bison output

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

CodePudding user response:

These two lines in the bison .output file

"else"  shift, and go to state 158

"else"    [reduce using rule 42 (ConditionStmt)]

tell you that the shift-reduce conflict is between shifting an else and reducing to the non-terminal ConditionStmt. This tells you that the problem is somewhere you have ConditionStmt on the right-hand-side of a rule -- something about that context allows the ConditionStmt to be followed by an else. But in the grammar you show, ConditionStmt is never used, so we can't say what that problem is.

My best guess is that you have something in the rule for NonIfStatement (which you also don't show) that ends (directly or indirectly) with ConditionStmt on the rhs, which causes this problem.

  • Related