help-bison
[Top][All Lists]
Advanced

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

Re: Deallocations upon parse errors?


From: Hans Aberg
Subject: Re: Deallocations upon parse errors?
Date: Wed, 17 Jan 2007 00:16:52 +0100

There is a parser stack, which in each entry contains a state, a semantic value, plus some more, like location value, if called for. So no values are confused: they are being built bottom-up in the parse tree. Just build $$ from the $k. For details on how a LALR(1) parser works, see for example Aho, Sethi & Ullman, "Compilers..." (the "dragon book"), or perhaps the FAQ of the Usenet newsgroup comp.compilers, posted there monthly, or perhaps <http:// en.wikipedia.org/wiki/LALR_parser>.

  Hans Aberg


On 16 Jan 2007, at 23:05, Roland Nilsson wrote:

Ok, I'll try to find an more recent version. Thanks.

But in any case, does the yylval variable carry the value of the most
recent $$ variable? Or is this perhaps overwritten with each new token
scanned?

// Roland

2007/1/16, Hans Aberg <address@hidden>:
Pick down some later version of Bison (2.3+); then, if you use the C
parser, there is a %destructor command, that can be used to handle
clean-up during error recovery. Another way is to use a C++ parser,
and a semantic type that does cleanup via the C++ language, but one
cannot then use %union.

  Hans Aberg


On 15 Jan 2007, at 23:12, Roland Nilsson wrote:

> Hello,
>
> [apologies if this is a common question, I didn't find any FAQ for
> this list]
>
> I am using Bison (v1.24, a windows port I found on the web; I'm not on
> linux) for a simple command-line interpreter of my own experimental
> language. The parser actions are used to recursively build up a kind > of syntax tree, so they allocate various objects along the way. This > works fine, except when there are parse errors; in which case I need
> to intercept and deallocate whatever objects have been allocated so
> far, to avoid memory leaks.
>
> To do this cleanup I need to find the thing produced by the last
> reduction before the error. I first thought I could use yylval inside > the yyerror() routine for this, but I constantly find that yylval is > empty (null) when entering yyerror(). So how do I access the value of
> the last reduction (i.e. the last thing assigned to $$) ??
>
> By the way, yylval is null also after successful parsing. I thought
> this was strange too, from what I gathered from the Bison docs it
> should contain the $$ result at all the time? I got around that by
> inserting a "top level" rule that simply copies $$ to a different
> variable prior to accepting, but it seems like a weird work- around ...
> ?
>
> Thanks for any help!
>
> Best,
> Roland Nilsson
> Linköping, Sweden
>
>
> _______________________________________________
> address@hidden http://lists.gnu.org/mailman/listinfo/help-bison







reply via email to

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