[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Reusing a sub-grammar (by setting %start at runtime?)
From: |
Akim Demaille |
Subject: |
Re: Reusing a sub-grammar (by setting %start at runtime?) |
Date: |
Thu, 06 Apr 2006 18:49:20 +0200 |
User-agent: |
Gnus/5.110004 (No Gnus v0.4) Emacs/21.4 (gnu/linux) |
>>> "Marcus" == Marcus Holland-Moritz <address@hidden> writes:
> 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.
I use the following technique.
Suppose you want to branch on nonterminals foo and bar. Introduce
fresh tokens START_FOO and START_BAR, and use the following real
axiom:
start:
START_FOO foo
| START_BAR bar
then the trick is to have your scanner pass either START token first.
If you have a bleeding edge bison supporting push parsers, that's
trivial :) If you wrote yylex yourself, it shouldn't be too hard. If
you use a lex generated scanner, there is a simple means to acheive
this: introduce a variable initial_token, and right after the first
%%, insert something like
%{
if (initial_token)
{
int t = initial_token;
initial_token = 0;
return t;
}
%}
- Re: Reusing a sub-grammar (by setting %start at runtime?),
Akim Demaille <=