0
votes

I am using Flex/Bison for a script parser which needs to break out of the parser and return a nonzero status from yyparse() for ALL ERRORS. Every bit of documentation for Bison I can find is about recovery -- how can I write a rule set which bails (i.e., with YYABORT) rather than trying to recover?

Thanks for your collective wisdom.

2

2 Answers

0
votes

If you refer to semantic errors, i.e., checks that you are doing in the semantic part of the syntax rules, then you can just invoke the exit() function to immediately exit the parser's executable. The parameter to exit() is the error code to return to the shell environment.

You may also implement yyerror() that would invoke exit() if you wish (to exit on syntax errors). This is documented here.

0
votes

If there is no applicable error rule, bison will not attempt to recover and will immediately return when a syntax error is detected. So unless you explicitly attempt error recovery, the bison parser will act as you wish.

If you do attempt error recovery, you can still invoke YYABORT in an action to cause yyparse to return.

If your parser is not behaving in this way, please post more details.

If you want to force an error from the scanner, just return a token value which is not used in any production. That is guaranteed to create an error in the parser because the token cannot be shifted.