%{ #include #include #define YYSTYPE double /* defines type for yylval, int by default */ %} %token NUMBER %left '+' '-' %left '*' '/' %right UMINUS %% lines : lines expr '\n' {printf("%g\n", $2);} /* %g shorter of scientific/fixed notation */ | lines '\n' | /* empty */ ; expr : expr '+' expr {$$ = $1 + $3;} /* $$ is attribute value of LHS */ | expr '-' expr {$$ = $1 - $3;} | expr '*' expr {$$ = $1 * $3;} | expr '/' expr {$$ = $1 / $3;} | '(' expr ')' {$$ = $2;} | '-' expr %prec UMINUS {$$ = -$2;} | NUMBER ; %% yylex( ) { int c; while ((c = getchar( ) ) == ' ') ; if ((c == '.' || isdigit(c))) { ungetc(c, stdin); scanf("%lf", &yylval); return NUMBER; } return c; }