%{ #include int yylex(void); void yyerror(char *); %} %left '+' '-' %left '*' '/' %left '(' ')' %token INTEGER %% program: program expr '\n' { printf("%d\n", $2);} | ; expr: INTEGER { $$ =$1; } | expr '+' expr { $$ = $1 + $3; } | expr '-' expr { $$ = $1 - $3; } | expr '*' expr { $$ = $1 * $3; } | expr: '(' expr ')' {$$ = $2;} | expr '/' expr { if($3==0) yyerror("divided by zero\n"); else $$ = $1 / $3; } ; %% void yyerror(char *s){ fprintf(stderr, "%s\n", s); } int main(void){ yyparse(); return 0; }