%{ extern int yylex(); #include #include int yyerror (char const *message) { fprintf(stderr,"<%s>\n", message); return 0; } /* table de symbole rudimentaire symbole = char dans [a-z] regs[ symbole - 'a' ] == valeur du symbole s valeurs : -1=undef, 0=FAUX, 1=VRAI */ int regs[26]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}; %} %error-verbose %union { int index; /* valeur = index dans regs = symbole - 'a' */ int bool; /* valeur = Vrai(1) ou Faux(0) */ } %token VAR %token '(' ')' '\n' AFF VRAI FAUX %left OU IMP EQU OUX %left ET %left NON %type Expr %% Line : | Line Expr '\n' { if ($2==1) printf("\n ==VRAI\n"); else printf("\n ==FAUX\n");} | Line VAR AFF Expr '\n' { regs[$2] = $4; printf("\n Var %c = %d\n", 'a'+$2, $4);} | Line error '\n' {yyerrok; printf("\n ERREUR SYNTAXE \n");} ; Expr : VRAI { $$ = 1; } | FAUX { $$ = 0; } | '(' Expr ')' { $$=$2; } | Expr ET Expr { $$=$1 && $3; } | Expr OU Expr { $$=$1 || $3; } | Expr IMP Expr { $$=!$1 || $3; } | Expr EQU Expr { $$= ($1 == $3) ; } | Expr OUX Expr { $$=($1 && !$3) || (!$1 && $3); } | NON Expr { $$= !$2; } | VAR {if (regs[$1]==-1) { printf("\nWarning : variable %c non initialisee, assume %c=FALSE\n",$1+'a',$1+'a'); $$=0;} else $$=regs[$1]; } ; %% int main(void) { return yyparse(); }