1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| /* file name : log.y */
/* Calculator grammar for arithmetic expressions. */
/* Associativity and precedence declarations used to */
/* disambiguate the grammar. */
%{
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
extern FILE* yyin;
extern int yylex(void);
void yyerror(char *);
void clearAllRegisters(void);
double factorial(double);
%}
%token term
%left 'or'
%left 'and'
%right not
%%
input
: /* empty */
| input line
;
line
: '\n'
| expression '\n' { printf ("\t= %d\n", $1); }
;
expression
: expression 'or' expression { if ( $1 != 0) $$ = 1;
else if ($3 != 0 ) $$ = 1;
else $$ = 0 ;
}
| expression 'and' expression { if ( $1 != 0) { if ($3 != 0 ) $$ = 1;}
else $$ = 0 ;
}
| 'not' expression %prec not { if ( $2 == 0) $$ = 1;
else $$ = 0 ;}
| '(' expression ')' { $$ = $2; }
| term { $$ = $1; if($1 != 0) printf("\ttrue\n");
else printf("\t fasle"); }
;
%%
void yyerror(char *s){
printf("\n%s ", s);}
int main()/*int argc,char** argv)
{
if (argc>1){
if (fopen(argv[1],"r") == NULL){
printf("Can't read from %s\n",argv[1]);
return 0;
} else
yyin = fopen(argv[1],"r");
} else
yyin = stdin;
printf(">> ");
yyparse();
}*/
{
//if (yyparse(c) == 0 )
printf("expression correcte\n");
} |