1 /* Test file for C++ parsers using variants.
2 Based on an example by Michiel De Wilde <mdewilde.agilent@gmail.com>. */
19 static yy::parser::token_type yylex(yy::parser::semantic_type* yylval);
22 %token <std::string> TEXT
24 %printer { debug_stream() << $$; } <int> <std::string>
27 %type <std::string> text result
32 text { std::cout << $1 << std::endl; }
36 /* nothing */ { /* This will generate an empty string */ }
37 | text TEXT { std::swap($$,$1); $$.append($2); }
47 // The yylex function providing subsequent tokens:
48 // TEXT "I have three numbers for you:"
52 // TEXT " and that's all!"
56 yy::parser::token_type
57 yylex(yy::parser::semantic_type* yylval)
60 yy::parser::token_type result;
65 yylval->build<std::string>();
66 yylval->as<std::string>() = std::string("I have three numbers for you:");
67 result = yy::parser::token::TEXT;
73 yylval->as<int>() = stage;
74 result = yy::parser::token::NUMBER;
77 yylval->build<std::string>();
78 yylval->as<std::string>() = std::string(" and that's all!");
79 result = yy::parser::token::TEXT;
82 result = yy::parser::token::END_OF_FILE;
90 // Mandatory error function
92 yy::parser::error(const yy::parser::location_type& yylloc,
93 const std::string& message)
95 std::cerr << yylloc << ": " << message << std::endl;
99 main(int argc, char *argv[])
102 p.set_debug_level(!!getenv("YYDEBUG"));