+ print $out <<EOF;
+%language "C++"
+%defines
+%locations
+$directives
+
+%code requires // *.h
+{
+#include <string>
+}
+
+%code // *.c
+{
+#include <algorithm>
+#include <iostream>
+#include <sstream>
+
+#define STAGE_MAX ($max * 10) // max = $max
+
+#define USE_LEX_SYMBOL $lex_symbol
+#define USE_VARIANTS $variant
+
+ // Prototype of the yylex function providing subsequent tokens.
+ static
+#if USE_LEX_SYMBOL
+ yy::parser::symbol_type yylex();
+#else
+ yy::parser::token_type yylex(yy::parser::semantic_type* yylval,
+ yy::parser::location_type* yylloc);
+#endif
+
+ // Conversion to string.
+ template <typename T>
+ inline
+ std::string
+ string_cast (const T& t)
+ {
+ std::ostringstream o;
+ o << t;
+ return o.str ();
+ }
+}
+
+%token END_OF_FILE 0
+EOF
+
+ if ($variant)
+ {
+ print $out <<'EOF';
+%token <std::string> TEXT
+%token <int> NUMBER
+%printer { std::cerr << "Number: " << $$; } <int>
+%printer { std::cerr << "Text: " << $$; } <std::string>
+%type <std::string> text result
+
+%%
+result:
+ text { /* Throw away the result. */ }
+;
+
+text:
+ /* nothing */ { /* This will generate an empty string */ }
+| text TEXT { std::swap ($$, $2); }
+| text NUMBER { $$ = string_cast($2); }
+;
+EOF
+ }
+ else
+ {
+ # Not using Bison variants.
+ print $out <<'EOF';
+%union {int ival; std::string* sval;}
+%token <sval> TEXT
+%token <ival> NUMBER
+%printer { std::cerr << "Number: " << $$; } <ival>
+%printer { std::cerr << "Text: " << *$$; } <sval>
+%type <sval> text result
+
+%%
+result:
+ text { delete $1; }
+;
+
+text:
+ /* nothing */ { $$ = new std::string; }
+| text TEXT { delete $1; $$ = $2; }
+| text NUMBER { delete $1; $$ = new std::string (string_cast ($2)); }
+;
+EOF
+ }
+
+ print $out <<'EOF';
+%%
+#
+
+static
+#if USE_LEX_SYMBOL
+yy::parser::symbol_type yylex()
+#else
+yy::parser::token_type yylex(yy::parser::semantic_type* yylval,
+ yy::parser::location_type* yylloc)
+#endif
+{
+ typedef yy::parser::location_type location_type;
+ typedef yy::parser::token token;
+ static int stage = -1;
+ ++stage;
+ if (stage == STAGE_MAX)
+ {
+#if USE_LEX_SYMBOL
+ return yy::parser::make_END_OF_FILE (location_type ());
+#else
+ *yylloc = location_type ();
+ return token::END_OF_FILE;
+#endif
+ }
+ else if (stage % 2)
+ {
+#if USE_LEX_SYMBOL
+ return yy::parser::make_NUMBER (stage, location_type ());
+#else
+# if defined ONE_STAGE_BUILD
+ yylval->build(stage);
+# elif USE_VARIANTS
+ yylval->build<int>() = stage;
+# else
+ yylval->ival = stage;
+# endif
+ *yylloc = location_type ();
+ return token::NUMBER;
+#endif
+ }
+ else
+ {
+#if USE_LEX_SYMBOL
+ return yy::parser::make_TEXT ("A string.", location_type ());
+#else
+# if defined ONE_STAGE_BUILD
+ yylval->build(std::string("A string."));
+# elif USE_VARIANTS
+ yylval->build<std::string>() = std::string("A string.");
+# else
+ yylval->sval = new std::string("A string.");
+# endif
+ *yylloc = location_type ();
+ return token::TEXT;
+#endif
+ }
+ abort();
+}
+
+// Mandatory error function
+void
+yy::parser::error(const yy::parser::location_type& loc, const std::string& msg)
+{
+ std::cerr << loc << ": " << msg << std::endl;
+}
+
+int main(int argc, char *argv[])
+{
+ yy::parser p;
+#if YYDEBUG
+ p.set_debug_level(!!getenv("YYDEBUG"));
+#endif
+ p.parse();
+ return 0;
+}
+EOF