help-bison
[Top][All Lists]
Advanced

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

RE: Conditional generation of Grammar rules


From: David Fang
Subject: RE: Conditional generation of Grammar rules
Date: Tue, 2 May 2006 13:57:01 -0400 (EDT)

> Thanks for the response.
> But I'm not sure I understood what you have mentioned.
> Could you please give an example or a bit more detail?

Sure, (at the risk of being slightly off-topic)...

> > nonterminal X:
> > #ifdef FEATURE
> >  { rule 1 }
> > #else
> >  { rule 1; rule 2 }
> > #endif

Place the above in foo.y.in.

cpp -P -DFEATURE foo.y.in > foo-with-feature.y
        and/or
cpp -P -UFEATURE foo.y.in > foo-without-feature.y
        (the -UFEATURE is optional, since macros are undefined by default)

Of course, this will expand any #includes you have in your .y.in file,
which may be undesirable.  To avoid that, you can split up your input file
into chunks and piece them together with a series of 'cat' commands.  (m4
has commands to do this for you, but I won't go into it.)  The general
idea is to isolate the conditional parts of your .y file and glue them
together.  Although hackish as it may seem, this way works without
depending on any preprocessor-like features of the parser-generator.
You'd probably want to do this in a Makefile to avoid running the commands
manually.

> > I know that ifdefs dont work with the grammar but is there anything
> > else that can be done? Or is it possible to make bison to read define
> > variables from a make file?
> > I want to avoid writing different parsers / recursive parsers to do this.
>
>       You could generate your .y file explicitly using cpp, going from
> .y.in -> .y, and using a separaete header to define your feature sets.
> (I do something similar with autoconf-configure, for example.)  Or you
> could generate it with a more sophisticated preprocessor like m4.

Fang





reply via email to

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