help-bison
[Top][All Lists]
Advanced

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

Reusing a sub-grammar (by setting %start at runtime?)


From: Marcus Holland-Moritz
Subject: Reusing a sub-grammar (by setting %start at runtime?)
Date: Sat, 25 Mar 2006 18:14:34 +0100

Hi,

I'm using bison for a C parser [1], for which I'm currently rewriting
the part that parses expressions.

While being at it, I wondered if it's possible to reuse a certain part
of a bison generated parser, e.g. by setting a different %start token
at runtime.

Currently, the %start token in my grammar is 'source_file', as it is
used to process whole C source files. However, I'd also like to be able
to evaluate constant C expressions, which are simply a part of the whole
grammar.

So, I'd like to be able to parse both

  /* a complete C source file */
  int foo(int a)
  {
    return (a + 2) / 3;
  }

as well as something like

  /* a constant expression */
  (5 + 2) / 3

with the same parser, depeding on its runtime configuration.

If I could just set the %start token to 'constant_expression' instead
of 'source_file' at runtime, that would solve my problem, but I haven't
found if anything like that is possible in the documentation.

Another way would obviously be to have a second parser that simply
replicates all rules required to parse constant expressions. But this
would mean code duplication, as the code would be exactly the same for
both parsers.

Is it possible to avoid the code duplication here?

I could of course generate the two grammars from a single source file
by preprocessing, but then I'd still have redundancy in the generated
code for both grammars.

If there's no support for runtime setting of the %start token in bison,
is there a reason why, or would it be difficult to implement?

Or would there be another way to solve this problem without adding
redundancy?

Thanks,
Marcus

[1] http://search.cpan.org/src/MHX/Convert-Binary-C-0.64_02/ctlib/parser.y




reply via email to

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