/* * $ bison -d expLL.y * output: expLL.tab.c and expLL.tab.h */ %{ #include int yylex (void); /* type of yylex() */ void yyerror(char const *s); %} %union { /* type of 'yylval' (stack type) */ int integer ; /* type name is YYSTYPE */ double real ; } %token IC FC %type e ep t tp f %start s /* start symbol */ %% /* Grammar rules and action follows */ s: s line | /* Empty string */ ; line: '\n' | e '\n' { printf("%f\n", $1) ;} | error '\n' { yyerrok ; } ; e: t {$$=$1;} ep {$$ = $3;} // m1 replaced ; ep: '+' t {$$=$0+$2;} ep {$$ = $4;} // m2 replaced | '-' t {$$=$0-$2;} ep {$$ = $4;} // m4 replaced | {$$ = $0;} ; t: f {$$=$1;} tp {$$ = $3;} // m1 replaced ; tp: '*' f {$$=$0*$2;} tp {$$ = $4;} // m3 replaced | '/' f {$$=$0/$2;} tp {$$ = $4;} // m5 replaced | {$$ = $0;} ; f: '(' e ')' {$$ = $2;} | '-' f {$$ = - $2;} | '+' f {$$ = $2;} | IC {$$ = $1;} | FC {$$ = $1;} ; %% int main() { // yydebug = 1 ; To get trace information return yyparse() ; } void yyerror(char const *s) {fprintf(stderr, "%s\n", s);}