When using yacc, how do you tell yyparse () that you want to stop parsing?

Continue to learn yacc and flex, and run through a script that is not suitable for the instructions and manuals that I have. I am trying to parse a file, and as I go along, I do additional error checking in the code that I posted in my parser.y file. When I come across something that is lexicographically correct (i.e. Parsing matches correctly) but logically incorrect (unexpected value or inappropriate value), how do I get yyparse to exit? Also, can I return an error code to me, which I can check in my code 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 understand that in my example I can just search for “Contrived_Example” using the rule, but my point is in if -block - can I tell yyparse that I want to stop parsing here?

+4
source share
1 answer

You can use YYERROR or YYABORT macros depending on what exactly you want. YYABORT forces yyparse to immediately return with a failure, and YYERROR forces it to act as if there was an error, and try to recover (which will return a failure if it cannot recover).

You can also use YYACCEPT to force yyparse to immediately return success.

+4
source

Source: https://habr.com/ru/post/1341892/


All Articles