Reporting malloc failure in actions

Joe Nelson <[email protected]> Mon, 28 Dec 2020 22:37:02 -0600
Newsgroups gmane.comp.parsers.bison.general
Message-ID <[email protected]>
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?