/* Infix notation calculator--calc */ %{ #define YYSTYPE double #include // printf #include // free int yylex(); void yyerror(char const *msg); %} /* BISON Declarations */ %token NUM %left '-' '+' %left '*' '/' %left NEG /* negation--unary minus */ %right '^' /* exponentiation */ /* Grammar follows */ %% input: /* empty string */ | input line ; line: '\n' | exp '\n' { printf ("\t%.10g\n", $1); } ; exp: NUM { $$ = $1; } | exp '+' exp { $$ = $1 + $3; } | exp '-' exp { $$ = $1 - $3; } | exp '*' exp { $$ = $1 * $3; } | exp '/' exp { $$ = $1 / $3; } | '-' exp %prec NEG { $$ = -$2; } | '(' exp ')' { $$ = $2; } ; %% int yylex() { return getchar(); } void yyerror(char const *msg) { printf("%s\n", msg); } int main() { yyparse(); return 0; }