%{ #include #include void yyerror(const char *s); int yylex(); %} %token NUMBER %token PLUS MINUS MULTIPLY DIVIDE %token LPAREN RPAREN %% input: | input line ; line: '\n' | expr '\n' { printf("Result: %d\n", $1); } ; expr: term | expr PLUS term { $$ = $1 + $3; } | expr MINUS term { $$ = $1 - $3; } ; term: factor | term MULTIPLY factor { $$ = $1 * $3; } | term DIVIDE factor { if ($3 == 0) yyerror("Division by zero"); else $$ = $1 / $3; } ; factor: NUMBER | LPAREN expr RPAREN { $$ = $2; } ; %% void yyerror(const char *s) { fprintf(stderr, "Error: %s\n", s); } int main() { printf("Enter expressions:\n"); return yyparse(); }