%{ /* C declarations */ #define YYSTYPE double #include #include void yyerror(char* s) { printf("%s\n", s); } int yylex(void); /* prototype declaration */ %} /* Bison declarations */ %token NUMBER %token FOO %left '+' '-' FOO %left '*' '/' %% input : /* empty */ | input line {} ; line : '\n' { exit (0); } /* exit with an empty line */ | expr '\n' { printf ("\t%g\n", $1); } ; expr : NUMBER { $$ = $1; } | expr FOO expr { $$ = $1 * 256 + $3; } | expr '+' expr { $$ = $1 + $3; } | expr '-' expr { $$ = $1 - $3; } | expr '*' expr { $$ = $1 * $3; } | expr '/' expr { $$ = $1 / $3; } | '(' expr ')' { $$ = $2; } ; %% int main(void) { printf("Exit with Ctrl-c.\n"); yyparse(); return 0; }