%skeleton "lalr1.cc" %code requires{ // Here you should insert declarations for types you will use in AST, if you will build AST. This will ensure that they will go both to generated parser and to file parser.tab.hh. Then you will include parser.tab.hh everywhere you need this declarations } %{ #include #include "parser.tab.hh" int yylex (yy::parser::semantic_type *yylval); void yy::parser::error (const std::string &m) { std::cerr << m << std::endl; } %} %define api.value.type variant %start document %token NUMBER %% %left '+' '-'; %left '*' '/'; %type expr; expr: NUMBER { $$ = $1; } | expr '+' expr { $$ = $1 + $3; } | expr '-' expr { $$ = $1 - $3; } | expr '*' expr { $$ = $1 * $3; } | expr '/' expr { $$ = $1 / $3; } | '(' expr ')' { $$ = $2; } document: expr { std::cout << $1 << std::endl; } %% int main () { yy::parser p; if (p.parse () != 0) { return EXIT_FAILURE; } return EXIT_SUCCESS; }