/* * Yacc/bison input file for a simple expression evaluator. * Adapted from Louden, p. 229. * Further adapted to use a lex-generated scanner. */ %{ #include #include #define YYSTYPE float %} %token NUMBER PLUS MINUS TIMES DIVIDES LPAREN RPAREN NEWLINE ERROR %% commands : commands command | command ; command : exp NEWLINE { printf("%g\n", $1); } | 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 main(void) { return yyparse(); } /* report errors */ int yyerror(char *s) { fprintf(stderr, "%s\n", s); }