/* http://www.linux-france.org/article/devl/lexyacc/minimanlexyacc-4.html */ %{ /*#include "global.h"*/ #define YYSTYPE double #include #include #include %} %token NUM %token PLUS MINUS TIMES DIV %token LPAR RPAR %token NL %left PLUS MINUS %left TIMES DIV %left NEG %start input %% input: /* Vide */ | input line ; line: NL { printf("Resultat :\n"); } | exp NL { printf("Resultat : %f\n",$1); } ; exp: NUM { printf("num: %f\n",$1); $$ = $1; } | exp PLUS exp { $$ = $1 + $3; } | exp MINUS exp { $$ = $1 - $3; } | exp TIMES exp { $$ = $1 * $3; } | exp DIV exp { $$ = $1 / $3; } | MINUS exp %prec NEG { $$ = -$2; } | LPAR exp RPAR { $$ = $2; } ; %% int yyerror(char *s) { printf("error: %s\n",s); return 1; } int main(void) { yyparse(); return 0; }