Index: ChangeLog from Akim Demaille * doc/bison.texinfo (Multiple start-symbols): New. +2006-04-24 Akim Demaille + * etc/README, etc/bench.pl: New. 2006-04-03 Akim Demaille Index: doc/bison.texinfo =================================================================== RCS file: /cvsroot/bison/bison/doc/bison.texinfo,v retrieving revision 1.184 diff -u -r1.184 bison.texinfo --- doc/bison.texinfo 17 Mar 2006 07:59:20 -0000 1.184 +++ doc/bison.texinfo 24 Apr 2006 09:45:35 -0000 @@ -311,6 +311,7 @@ * How Can I Reset the Parser:: @code{yyparse} Keeps some State * Strings are Destroyed:: @code{yylval} Loses Track of Strings * Implementing Gotos/Loops:: Control Flow in the Calculator +* Multiple start-symbols:: Factoring closely related grammars * Secure? Conform?:: Is Bison @acronym{POSIX} safe? * I can't build Bison:: Troubleshooting * Where can I find help?:: Troubleshouting @@ -7727,6 +7728,7 @@ * How Can I Reset the Parser:: @code{yyparse} Keeps some State * Strings are Destroyed:: @code{yylval} Loses Track of Strings * Implementing Gotos/Loops:: Control Flow in the Calculator +* Multiple start-symbols:: Factoring closely related grammars * Secure? Conform?:: Is Bison @acronym{POSIX} safe? * I can't build Bison:: Troubleshooting * Where can I find help?:: Troubleshouting @@ -7927,6 +7929,55 @@ invited to consult the dedicated literature. address@hidden Multiple start-symbols address@hidden Multiple start-symbols + address@hidden +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. address@hidden display + +Bison does not support multiple start-symbols, but there is a very +simple means to simulate them. If @code{foo} and @code{bar} are the two +pseudo start-symbols, then introduce two new tokens, say address@hidden and @code{START_BAR}, and use them as switches from the +real start-symbol: + address@hidden +%token START_FOO START_BAR; +%start start; +start: START_FOO foo + | START_BAR bar; address@hidden example + +These tokens prevents 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 @address@hidden ... address@hidden +after the first @code{%%} is copied verbatim in the top of the generated address@hidden function. Make sure a variable @code{start_token} is +available in the scanner (e.g., a global variable or using address@hidden etc.), and use the following: + address@hidden + /* @r{Prologue.} */ +%% address@hidden + if (start_token) + @{ + int t = start_token; + start_token = 0; + return t; + @} address@hidden + /* @r{The rules.} */ address@hidden example + + @node Secure? Conform? @section Secure? Conform?