bison-patches
[Top][All Lists]
Advanced

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

bison.simple patch for destruction of semantic values during error recov


From: Cris Bailiff
Subject: bison.simple patch for destruction of semantic values during error recovery
Date: Tue, 11 Jun 2002 19:28:21 +1000

Hi,

Please find attached a suggested patch for bison.simple (against current CVS 
HEAD), which defines a couple of optional hooks where the generated parser 
can call user-define code to deal with the contents of a semantic value just 
before the value is discarded during error recovery.

We have a lexer which mallocs a structure and puts in useful semantic 
information, to return as the value for yylvalp alongside the token number. 
The grammar actions can take care of  managing this storage for conforming 
input, but during error recovery, bison can pop off  an arbitrary number of 
semantic values from its stack, or discard them during lookahead, and these 
are effectively leaked until the caller terminates. (We could add some form 
of pool allocator, or other 'double accounting' for these chunks, but's it's 
not a general solution).

With this patch, user-defined code can get at the value just before it would 
be lost, and free it up to prevent a leak, or otherwise process it. (In our 
application we actually link the value onto an error recovery list in the 
YYPARSE_PARAM structure, to get at the contents again later).

There are 2 basic points in bison.simple where recovery happens, so the patch 
provides 2 different hooks. YYERROR_POP is called when the top of the stack 
is about to disappear, and YYERROR_DISCARD is called when throwing away 
lookahead tokens in error recovery. There is an additional case at YYEOF, 
when the current stack is basically thrown away before calling YYABORT. This 
patch deals with popping off all the remaining stack content at this point as 
well (using YYERROR_POP) to make sure EOF needn't cause a leak.

(Our application requires a long-lived, robust parser that needs to preserve 
as much of the input as possible, whilst not leaking, even for gratuitously 
incorrect or short input.)

I have seen quite a few questions in the bison-help archives regarding 
destruction of semantic values during error recovery, but never found a 
useful answer. Hopefully this patch can form the basis (or at least prompt 
discussion) for providing this faciltiy.

We developed and tested the changes with bison 1.35, but this patch is 
against CVS HEAD for convenience - there didn't seem to be any conflicting 
changes in bison.simple which would be affected.  

Comments or further suggestions/improvements welcome - especially any 
comments regarding additional code paths which might exit the parser without 
getting a chance to clean up...

Cheers,
Cris Bailiff,
address@hidden - http://www.awayweb.com






Attachment: bison.simple.free-semantic-values.patch
Description: hooks during error recovery


reply via email to

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