%{ /* yacc program for CS4905, Lab 2, desk calculator */ /* modified Jan. 29, 2007 for UNB Computer Science */ /* Linux environment (lex -> flex, cc -> gcc) */ /* B.G. Nickerson */ #include #include #define YYSTYPE double /* double type for yacc stack */ %} %token NUMBER %left '+' '-' %left '*' '/' %right UMINUS %% lines : lines expr '\n' { printf("%g\n", $2); } | lines '\n' | /* empty */ | error '\n' {yyerror("reenter last line:"); yyerrok; } ; expr : expr '+' expr {$$ = $1 + $3; } | expr '-' expr {$$ = $1 - $3; } | expr '*' expr {$$ = $1 * $3; } | expr '/' expr {$$ = $1 / $3; } | '(' expr ')' {$$ = $2; } | '-' expr %prec UMINUS { $$ = -$2; } | NUMBER ; %% #include "lex.yy.c" void yyerror(char * s) /* yacc error handler */ { fprintf (stderr, "%s\n", s); } int main(void) {return yyparse();}