%{ #include #include "proto-color.h" extern int yylex(); int yyerror (char const *message) { fprintf(stderr,RED("<%s>")"\n", message); return 0; } void SemERR (char const *message) { fprintf(stderr,RED("[Erreur Semantique : %s]")"\n", message); } void Prompt(){ printf("yash> "); } /* Yet Another Shell */ #include "vect.h" int i; %} %error-verbose %token HELLO THANKS FUCK BYE %token NEWLINE CMD_SEP PARENT_IN PARENT_OUT %token VECT_IN VECT_OUT VECT_SEP F_DIM %left PLUS %left MULT %union { int scalaire; Type_Vect vecteur; } %token SCALAIRE %type Sexp %type Vexp Vconst suitecomp /* suitecomp indexé a partir de 1; suitecom[0] reste non utilisé */ %% Shell : {Prompt();} | Shell Line NEWLINE {Prompt();} | Shell error NEWLINE {yyerrok; printf(BLUE("Ligne invalide")"\n"); Prompt();} ; Line : Command | Line CMD_SEP Command | error CMD_SEP /* Action a l'interieur d'une regle */ {yyerrok; printf(BLUE("Command invalide: \n"));} Command ; Command : | Builtin | Sexp { printf(" SCAL= %d\n",$1);} | Vexp { printf(" VECT_%d= [",$1.taille); if ($1.taille>0) printf("%d", $1.vect[0]); for(i=1;i<$1.taille;i++) printf(", %d", $1.vect[i]); printf("]\n"); } ; Sexp : SCALAIRE {$$=$1;} | Sexp PLUS Sexp {$$=$1+$3;} | Sexp MULT Sexp {$$=$1*$3;} | PARENT_IN Sexp PARENT_OUT {$$=$2;} | F_DIM PARENT_IN Vexp PARENT_OUT {$$=$3.taille;} ; Vexp : Vconst {$$=$1;} | Vexp PLUS Vexp { if ($1.taille != $3.taille) { SemERR("Tailles imcompatibles pour l'addition"); YYERROR;} else { $$.taille = $1.taille; for(i=0;i<$$.taille;i++) $$.vect[i]=$1.vect[i]+$3.vect[i]; } } | Sexp MULT Vexp { $$.taille = $3.taille; for(i=0;i<$$.taille;i++) $$.vect[i]=$1*$3.vect[i]; } | Vexp MULT Sexp { $$.taille = $1.taille; for(i=0;i<$$.taille;i++) $$.vect[i]=$3*$1.vect[i]; } | PARENT_IN Vexp PARENT_OUT {$$=$2;} ; Vconst : VECT_IN VECT_OUT {$$.taille=0;} | VECT_IN Sexp suitecomp VECT_OUT { $$=$3; $$.vect[0]=$2; } ; suitecomp : /* vide */ {$$.taille=1;} /*suitecomp est indexé par rapport au vecteur final, suitecomp[0] reste libre */ | suitecomp VECT_SEP Sexp { $$=$1; $$.taille=1+$1.taille; if ($$.taille>MAX_TAILLE) { SemERR("Vecteur trop grand"); YYERROR; } $$.vect[$1.taille]=$3; } ; Builtin : HELLO {printf("Nice to meet you\n");} | THANKS {printf("You are welcome\n");} | FUCK {SemERR("Censored !"); YYERROR;} | BYE {printf("See you\n"); return(0);} ; %% int main(void) { return yyparse(); }