%{ #include #define YYERROR_VERBOSE void yyerror(char *); %} %token NUMBER ILLEGAL_CHAR %left '-' '+' %left '*' '/' %% eingabe: /* empty */ | eingabe berechnung ; berechnung: term '=' { printf("Ergebnis: %d\n", $1); } | error '=' ; term: NUMBER { @$ = @1; } | term '+' term { $$ = $1 + $3; } | term '-' term { $$ = $1 - $3; } | term '*' term { $$ = $1 * $3; } | term '/' term { if ($3) { $$ = $1 / $3; } else { $$ = 0; printf("\nIm Bereich %d(%d)-%d(%d): ", @3.first_line, @3.first_column, @3.last_line, @3.last_column ); printf("Division durch Null\n"); YYERROR; }; } | '(' term ')' { $$ = $2; } ; %% void yyerror(char *msg) { printf("\nAn Position %d(%d): ", yylloc.first_line, yylloc.first_column ); printf("%s\n", msg); } int main() { return yyparse(); }