/* * desk calculator */ %token YConstant %token YLP %token YRP %token YNL %token YPLUS %token YMINUS %token YMUL %token YDIV %left YPLUS YMINUS %left YMUL YDIV %start line %% line : line expression YNL { printf("%d\n", $2); } | /* empty */ ; expression : expression YPLUS expression { $$ = $1 + $3; } | expression YMINUS expression { $$ = $1 - $3; } | expression YMUL expression { $$ = $1 * $3; } | expression YDIV expression { $$ = $1 / $3; } | YLP expression YRP { $$ = $2; } | YConstant /*$$ = $1; */ ; %% #include extern FILE *yyin; void main() { while(!feof(yyin)) { yyparse(); } } void yyerror(s) char *s; { fprintf(stderr,"%s\n",s); }