help-bison
[Top][All Lists]
Advanced

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

Re: help with parser syntax


From: Cédric Lucantis
Subject: Re: help with parser syntax
Date: Mon, 6 Aug 2007 15:44:25 +0200
User-agent: KMail/1.9.7

Le lundi 06 août 2007, Laurence Finston a écrit :
> On Mon, 6 Aug 2007, cwcaceres wrote:
> > For each command, I had to make a new commandlist. How do I do it
> > so that I make Commandlist comlist; only once, at the start of the
> > program?
>
> There are various ways:
>
> 1.  Declare it globally and put an `extern' declaration into a header
> file that you include in your parser input file.  I don't recommend
> this, especially if you're planning to use threads somewhere down the
> line.
>
> 2.  Declare it somewhere, perhaps in `main', and pass a pointer to it
> as a parameter to `yyparse' (cast to `void*').
>
> 3.  Declare it as a data member of some class or struct and pass a
> pointer to an instance of that type as the parameter to `yyparse'.
> Instead, you might declare a pointer to it as the data member and
> allocated memory for it dynamically.  Or you could use an array of
> `Commandlist' or an array of pointers to `Commandlist'.  If you're
> using C++ in your actions, you could use a `vector', a `queue', or
> whatever. I would most likely use one of these variants of the basic
> idea.
>
> These are just the possibilities that occur to me off the top of my
> head. There may well be others.
>

You can do this with the %parse-param directive ; first create a parser 
struct or class containing the datas you need inside the grammar :

typedef struct _MyParser
{
        Commandlist comlist;
}
MyParser;

then add this to your grammar (%parse-lex is only required if you need 
those datas in yylex) :

%parse-param { MyParser * p }
%parse-lex { MyParser * p }

and finally modify your yyparse and yylex functions to have the 
following prototypes :

int yyparse (MyParser * p);
int yylex (MyParser * p);

Now you just have to create and initialize a MyParser struct and call 
yyparse(parser). You'll find more about this in the bison info page.

-- 
Cédric Lucantis




reply via email to

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