[英]When using yacc, how do you tell yyparse() that you want to stop parsing?
Still learning yacc and flex, and ran across a scenario that the how-to's and tutorials that I have do not cover. 仍在学习yacc和flex,并遇到了我没有涉及的操作方法和教程的场景。 I am trying to parse a file, and as I'm going along, I'm doing some secondary error checking in the code I've placed in my
parser.y
file. 我正在尝试解析一个文件,并且随着我的进展,我正在对我放在
parser.y
文件中的代码中进行一些二次错误检查。 When I come across something that is lexicographically correct (that is, the parse matches properly) but logically incorrect (unexpected value or inappropriate value), how do I get yyparse
to exit? 当我遇到词典上正确的东西(即,解析正确匹配)但逻辑上不正确(意外值或不适当的值)时,如何让
yyparse
退出? Also, can I have it return an error code back to me that I can check for in my calling code? 另外,我可以将它返回给我的错误代码,我可以在我的调用代码中检查吗?
/* Sample */
my_file_format:
header body_lines footer
;
header:
OBRACE INT CBRACE
|
OBRACE STRING CBRACE {
if ( strcmp ( $1, "Contrived_Example" ) != 0 ) { /* I want to exit here */ }
}
;
/* etc ... */
I realize that in my example I can simply look for "Contrived_Example" using a rule, but my point is in the if
-block -- can I tell yyparse
that I want to stop parsing here? 我意识到在我的例子中我可以简单地使用规则查找“Contrived_Example”,但我的观点是在
if
-block中 - 我可以告诉yyparse
我想在这里停止解析吗?
You can use the macros YYERROR
or YYABORT
depending on what exactly you want. 您可以根据需要使用宏
YYERROR
或YYABORT
。 YYABORT
causes yyparse to immediately return with a failure, while YYERROR
causes it to act as if there's been an error and try to recover (which will return failure if it can't recover). YYABORT
导致yyparse立即返回失败,而YYERROR
导致它表现为出现错误并尝试恢复(如果无法恢复将返回失败)。
You can also use YYACCEPT
to cause yyparse to immediately return success. 您还可以使用
YYACCEPT
使yyparse立即返回成功。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.