/* Compilation d'expressions arithme'tiques on genere du code a utiliser avec etf.lex qui reconnait les differents terminaux NUMBER: nombre entier, PARO PARF: parentheses, MULT PLUS multiplication addition , POINTV point virgule, ID identificateur (ne sert pas ici) */ %{ #define YYSTYPE char* #define LMAX 1000 trans (char *u){ /* ajoute 1 a tous les numeros de registres */ int i; for (i = 0; u[i] != '\0'; i++) if(isdigit(u[i])) u[i]++; } %} %token NUMBER PLUS POINTV MULT PARO PARF ID EGAL %% statement : expression POINTV {printf("%s \n", $1);} ; expression : expression PLUS terme { trans($3); strcat(strcat ($1,$3), "add R1 R2 \n") ; $$ = $1;} | terme {$$ = $1;} ; terme: terme MULT facteur { trans($3); strcat(strcat($1,$3), "mult R1 R2 \n"); $$ = $1;} | facteur {$$ = $1;} ; facteur : ID {$$ = (char *) malloc(LMAX); strcpy($$,"load " ); strcat($$, $1); strcat($$ ," R1 \n");} | PARO expression PARF {$$ = $2;} ; %% #include "lex.yy.c" main() { yyparse(); } yyerror(char *s) { printf ("%s\n",s); }