* doc/bison.texinfo (Multiple start-symbols): New.

This commit is contained in:
Akim Demaille
2006-04-24 09:45:57 +00:00
parent 3cedc2dc0d
commit ed2e6384ed
2 changed files with 55 additions and 0 deletions

View File

@@ -311,6 +311,7 @@ Frequently Asked Questions
* 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 @@ are addressed.
* 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 @@ This topic is way beyond the scope of this manual, and the reader is
invited to consult the dedicated literature.
@node Multiple start-symbols
@section Multiple start-symbols
@display
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.
@end 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
@code{START_FOO} and @code{START_BAR}, and use them as switches from the
real start-symbol:
@example
%token START_FOO START_BAR;
%start start;
start: START_FOO foo
| START_BAR bar;
@end 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 @samp{%@{ ... %@}}
after the first @code{%%} is copied verbatim in the top of the generated
@code{yylex} function. Make sure a variable @code{start_token} is
available in the scanner (e.g., a global variable or using
@code{%lex-param} etc.), and use the following:
@example
/* @r{Prologue.} */
%%
%@{
if (start_token)
@{
int t = start_token;
start_token = 0;
return t;
@}
%@}
/* @r{The rules.} */
@end example
@node Secure? Conform?
@section Secure? Conform?