/* * Yacc/bison input file for a simple expression evaluator. * Adapted from Louden, p. 229. * Further adapted to use a lex-generated scanner. * And to use C++. */ %{ #include #include #include #define YYSTYPE float int yylex(); int yyerror(char* err); %} %token NUMBER PLUS MINUS TIMES DIVIDES LPAREN RPAREN NEWLINE ERROR %% commands : commands command | command ; command : exp NEWLINE { std::cout << $1 << std::endl; } | NEWLINE { } ; exp : exp PLUS term { $$ = $1 + $3; } | exp MINUS term { $$ = $1 - $3; } | term { $$ = $1; } ; term : term TIMES factor { $$ = $1 * $3; } | term DIVIDES factor { $$ = $1 / $3; } | factor { $$ = $1; } ; factor : NUMBER { $$ = $1; } | LPAREN exp RPAREN { $$ = $2; } ; %% int yyerror(char* err) { using namespace std; cout << err << endl; } int yylex() { static yyFlexLexer lex; return lex.yylex(); } int main(void) { return yyparse(); }