/* Simple variant-based parser. -*- C++ -*- Copyright (C) 2018-2021 Free Software Foundation, Inc. This file is part of Bison, the GNU Compiler Compiler. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ %require "3.2" %language "c++" %define api.value.type variant %code { // Print a list of strings. auto operator<< (std::ostream& o, const std::vector& ss) -> std::ostream& { o << '{'; const char *sep = ""; for (const auto& s: ss) { o << sep << s; sep = ", "; } return o << '}'; } } %define api.token.constructor %code { namespace yy { // Return the next token. auto yylex () -> parser::symbol_type { static int count = 0; switch (int stage = count++) { case 0: return parser::make_TEXT ("I have three numbers for you."); case 1: case 2: case 3: return parser::make_NUMBER (stage); case 4: return parser::make_TEXT ("And that's all!"); default: return parser::make_YYEOF (); } } } } %% result: list { std::cout << $1 << '\n'; } ; %nterm > list; list: %empty { /* Generates an empty string list */ } | list item { $$ = $1; $$.push_back ($2); } ; %nterm item; %token TEXT; %token NUMBER; item: TEXT | NUMBER { $$ = std::to_string ($1); } ; %% namespace yy { // Report an error to the user. auto parser::error (const std::string& msg) -> void { std::cerr << msg << '\n'; } } int main () { yy::parser parse; return parse (); }