I am writing a small expression analyser parser for a project at the company where I work. The parser is supposed to check, for example, a division by zero or an undefined identifier, report the error and stop. What is the best way to do this? Assuming that my own code has no memory leaks, can I 开发者_Go百科simply do this:
if ($3 == 0) {
yyerror("Division by zero");
return 1;
}
should I rather do:
if ($3 == 0) {
yyerror("Division by zero");
YYERROR;
}
is there a third better alternative?
There is a third option indeed: YYACCEPT
If you are trying to stop the parsing because the input was valid and without errors(Since you are making a parser to detect errors, then it's not a parser error, the input did fit in the grammar's rules).
This leaves YYERROR for grammatical errors (like receiving a string from another language)
If you call yyerror
explicitly, how about using YYABORT
?
As far as I see, bison
seems to perform some cleanups at exit.
So, YYABORT
or YYERROR
will be more preferable to return 1
.
精彩评论