1
votes

I want to handle error recovery from bison2.4.1.

I referred to a oreilly's book(lex&yacc) and some websites to put the error token in my rule,

but I think it doesn't work. it doesn't help me handling error recovery!

my code is as follows:

PDL:
    DataDesc ComputationDesc    {Build_front_proc($1,$2);}
    ;
DataDesc:
    PartyDecl AccLvDesc     {$$ = echo_dataDesc($1, $2);}
    ;
// Party Description
PartyDecl:
    PARTY ':' ID ',' ID ENDL    {if($3->is_func || $5->is_func)
                        yyerror("it is a reserved word!\n");
                    $$ = echo_partyDecl($3->name,$5->name);}
    |error ENDL         {printf("There is a error");}
    ;

I gave words of "Party: id_a ;" (it should be two id name after "Party".)

and it went straight to yyerror() and showed syntax error...

I have no idea why it doesn't handle this error.

1
possible duplicate of error handling in YACCKaz

1 Answers

1
votes

Error rules in yacc/bison don't PREVENT errors -- they RECOVER from errors. So in this case, you get an error (and it calls yyerror("syntax error")), and THEN looks for error rules to recover. So in this case it will be in a state looking for a ',' to shift, after shifting PARTY, ':', and ID. In this state, a ';' can't be parsed, so it will issue a syntax error. After the error it will start popping states until it finds one with an error production -- in this case 3 states to get to the one that corresponds to the beginning of a PartyDecl. In that state, it will shift the error, putting it in a state expect an ENDL. It will then throw away input symbols (the ; and anything after it) until it finds an ENDL, which it will shift, putting in a state where it can reduce the PartyDecl: error ENDL rule, calling printf("There is a error");

If it never finds an ENDL, it will exit after it gets to an EOF, having never recovered from the error. In addition, it will remain in error recovery mode for 2 more shifts after the ENDL -- if it gets another error before then, it won't call yerror and will instead go directly to error recovery.