/* bsp3.y: bison-Parser fuer arithmetische Ausdruecke mit +,*,(,) */ /* Teil 1: Deklarationen */ %{ #include int yylex(void); void yyerror (char const *); %} %token NUM %% /* Teil 2: Angabe der Grammatik */ s: a {printf("Ergebnis: %d\n",$1);} ; a: a '+' m {$$=$1+$3;} | m {$$=$1;} ; m: m '*' f {$$=$1*$3;} | f {$$=$1;} ; f: '(' a ')' {$$=$2;} | NUM {$$=$1;} ; %% /* Teil 3: Prozeduren u. Hauptprogramm */ int yylex(void) { int c; c=getchar(); if (isdigit(c)) { ungetc(c,stdin); scanf("%ld",&yylval); return NUM; } if (c=='\n') return 0; return c; } void yyerror(char const *s) { printf("Aufruf von yyerror\n"); } int main(void) { return yyparse(); }