%skeleton "glr.cc" %define parse.error verbose %code requires { #include #include class tup; using expr = std::variant, std::unique_ptr, std::unique_ptr, std::unique_ptr>; class tup { public: expr a; expr b; }; } %code provides { #define YY_DECL int yylex (yy::parser::semantic_type *yylval) YY_DECL; } %code top { #include } %define api.value.type union %token NUMBER %% _start: expr { } %type expr; expr: product { $$ = $1; } | expr '+' product { // Some code } | expr '-' product { // Some code } %type product; product: prim { $$ = $1; } | product '*' prim { // Some code } | product '/' prim { // Some code } %type prim; prim: NUMBER { // Some code } | '(' expr ')' { $$ = $2; } %% void yy::parser::error (const std::string &m) { std::cerr << m << "\n"; } int main (void) { try { yy::parser p; if (p.parse () != 0) { return EXIT_FAILURE; } return EXIT_SUCCESS; } catch (const std::exception &e) { std::cerr << "Exception from main: " << e.what () << "\n"; return EXIT_FAILURE; } catch (...) { std::cerr << "Exception from main\n"; return EXIT_FAILURE; } }