%{ #include #include #include #define YYERROR_VERBOSE void yyerror(char *); char *addString(const char *, const char *); %} %union { double zahl; char * string; } %token ILLEGAL_CHAR %token NUMBER %token STRING %left '-' '+' %left '*' '/' %type zahlTerm %type stringTerm %% eingabe: /* empty */ | eingabe zahlBerechnung | eingabe stringBerechnung ; zahlBerechnung: zahlTerm '=' { printf("Ergebnis: %g\n", $1); } ; stringBerechnung: stringTerm '=' { printf("Ergebnis: \"%s\"\n", $1); } ; zahlTerm: NUMBER | zahlTerm '+' zahlTerm { $$ = $1 + $3; } | zahlTerm '-' zahlTerm { $$ = $1 - $3; } | zahlTerm '*' zahlTerm { $$ = $1 * $3; } | zahlTerm '/' zahlTerm { $$ = $1 / $3; } | '(' zahlTerm ')' { $$ = $2; } ; stringTerm: STRING | stringTerm '+' stringTerm { $$ = addString($1, $3); } ; %% char *addString(const char *s1, const char *s2) { char *retVal; retVal = (char *) malloc(strlen(s1)+strlen(s2)+1); strcpy(retVal, s1); strcat(retVal, s2); return retVal; } void yyerror(char *msg) { printf("\nEingabefehler: %s\n", msg); } int main() { return yyparse(); }