help-bison
[Top][All Lists]
Advanced

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

Re: initial-action question


From: lfinsto1
Subject: Re: initial-action question
Date: Mon, 11 Feb 2008 11:03:52 +0100 (CET)
User-agent: SquirrelMail/1.4.9a

> I would like to declare some variables that are local to yyparse.

To the best of my knowledge, you would need to modify the skeleton file. 
When I wanted to do this, I determined that it was not possible otherwise.
I may be wrong, but I don't think this has changed with more recent
versions.

> I
> am building a linked list of structures, and I need to make sure I
> have values for all the structure members before I add a new node to
> the list.  Since I wrote the parser to be a pure parser, global
> variables, are not an option.

Why not?  I'm not recommending this, but you can certainly declare global
variables and make them accessible to `yyparse' in the ordinary way (i.e.,
by using `extern' declarations, if necessary).  If you're using threads,
you will, of course, have to protect these variables with mutexes, which
would reduce concurrency.  That's why I recommend against doing this.

> Something like %initial-action {int x;
> int y; int z;} doesn't work, since the initial action code is
> enclosed in braces and the variables x,y,z,etc. do not exist outside
> the braces.  If I use %parse-param{variables_t variables;}, where
> variables_t is a structure containing x,y,z, I get what I want, but
> since I don't need x,y,z after yyparse returns, this seems like an
> inefficient way to do things.  Is there a better way to do this?
> There must be a "standard" way this is done---temporarily saving all
> the previously parsed values until all the required values are
> available.  Any help would be very appreciated.  Thanks.

I recommend declaring a `struct' or `class' type for use as the parameter
to `yyparse'.  You can put anything you want into it, not just these
variables.    If you're worried about the memory, you can declare this
object in such a way that it will be on the stack and disappear in the
fullness of time. Otherwise, you could allocate memory for it on the heap
and free it when you're done with it.  However, the cost of doing this
with respect to time might outweigh the real or imaginary advantage of not
declaring it statically.

Unless you're working on a system where every clock cycle and every byte
of memory counts, I wouldn't worry about a few integers, or even a few
larger objects.

I allocate the (very large) object that I pass as the parameter to
`yyparse' (and `yylex') on the heap and free the memory when I'm done with
it.

Laurence Finston








reply via email to

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