From: Akim Demaille Date: Mon, 24 Apr 2006 09:45:57 +0000 (+0000) Subject: * doc/bison.texinfo (Multiple start-symbols): New. X-Git-Tag: v2.3b~422 X-Git-Url: https://git.saurik.com/bison.git/commitdiff_plain/ed2e6384ed2be6598e90d8551ef6d8daf2b65516?ds=sidebyside * doc/bison.texinfo (Multiple start-symbols): New. --- diff --git a/ChangeLog b/ChangeLog index b4c238d5..758a5c36 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2006-04-24 Akim Demaille + + * doc/bison.texinfo (Multiple start-symbols): New. + 2006-04-24 Akim Demaille * etc/README, etc/bench.pl: New. diff --git a/doc/bison.texinfo b/doc/bison.texinfo index 01fa9bf2..9e65195d 100644 --- a/doc/bison.texinfo +++ b/doc/bison.texinfo @@ -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?