1 /* Test file for C++ parsers using variants.
2 Based on an example by Michiel De Wilde <mdewilde.agilent@gmail.com>. */
12 typedef std::list<std::string> strings_type;
22 // Prototype of the yylex function providing subsequent tokens.
23 static yy::parser::token_type yylex(yy::parser::semantic_type* yylval);
25 // Printing a list of strings.
26 // Koening look up will look into std, since that's an std::list.
30 operator<<(std::ostream& o, const strings_type& s)
32 std::copy(s.begin(), s.end(),
33 std::ostream_iterator<strings_type::value_type>(o, "\n"));
38 // Conversion to string.
42 string_cast (const T& t)
50 %token <std::string> TEXT;
52 %printer { debug_stream () << $$; } <int> <std::string> <strings_type>;
55 %type <std::string> item;
56 %type <strings_type> list;
61 list { std::cout << $1 << std::endl; }
65 /* nothing */ { /* Generates an empty string list */ }
66 | list item { std::swap ($$, $1); $$.push_back ($2); }
70 TEXT { std::swap ($$, $1); }
71 | NUMBER { $$ = string_cast ($1); }
75 // The yylex function providing subsequent tokens:
76 // TEXT "I have three numbers for you:"
80 // TEXT " and that's all!"
84 yy::parser::token_type
85 yylex (yy::parser::semantic_type* yylval)
88 yy::parser::token_type result;
93 yylval->build (std::string ("I have three numbers for you."));
94 result = yy::parser::token::TEXT;
99 yylval->build (stage);
100 result = yy::parser::token::NUMBER;
103 yylval->build (std::string ("And that's all!"));
104 result = yy::parser::token::TEXT;
107 result = yy::parser::token::END_OF_FILE;
115 // Mandatory error function
117 yy::parser::error (const yy::parser::location_type& yylloc,
118 const std::string& message)
120 std::cerr << yylloc << ": " << message << std::endl;
124 main (int argc, char *argv[])
127 p.set_debug_level (!!getenv ("YYDEBUG"));