help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Reporting malloc failure in actions


From: Joe Nelson
Subject: Reporting malloc failure in actions
Date: Mon, 28 Dec 2020 22:37:02 -0600

Hi, is there a recommended way to fail parsing when an action cannot
allocate memory? I could use YYABORT, but the caller could mistake this
for a problem in the input, when it's really an internal problem.

Looking at the generated foo.tab.c file for my parser, I see these
macros:

        #define YYACCEPT        goto yyacceptlab
        #define YYABORT         goto yyabortlab
        #define YYERROR         goto yyerrorlab

Later in the file I see an interesting label that has no corresponding
documented macro:

        /*-------------------------------------------------.
        | yyexhaustedlab -- memory exhaustion comes here.  |
        `-------------------------------------------------*/
        yyexhaustedlab:
          yyerror (scanner, callback, YY_("memory exhausted"));
          yyresult = 2;
          goto yyreturn;

This seems to be exactly the code I should execute, since it would
inform the caller what went wrong. One option would be for my code to do
something like this:

        if (!(bla = malloc(n)))
                goto yyexhaustedlab;

I don't like relying on undocumented internals like this, since they're
subject to change across versions of Bison. Any advice?



reply via email to

[Prev in Thread] Current Thread [Next in Thread]