Next: , Previous: , Up: Frequently Asked Questions   [Contents][Index]


13.5 Multiple start-symbols

I have several closely related grammars, and I would like to share their implementations. In fact, I could use a single grammar but with multiple entry points.

Bison does not support multiple start-symbols, but there is a very simple means to simulate them. If foo and bar are the two pseudo start-symbols, then introduce two new tokens, say START_FOO and START_BAR, and use them as switches from the real start-symbol:

%token START_FOO START_BAR;
%start start;
start:
  START_FOO foo
| START_BAR bar;

These tokens prevent the introduction of new conflicts. As far as the parser goes, that is all that is needed.

Now the difficult part is ensuring that the scanner will send these tokens first. If your scanner is hand-written, that should be straightforward. If your scanner is generated by Lex, them there is simple means to do it: recall that anything between ‘%{ ... %}’ after the first %% is copied verbatim in the top of the generated yylex function. Make sure a variable start_token is available in the scanner (e.g., a global variable or using %lex-param etc.), and use the following:

  /* Prologue. */
%%
%{
  if (start_token)
    {
      int t = start_token;
      start_token = 0;
      return t;
    }
%}
  /* The rules. */