%{ #include #include int yylex (void); void yyerror (char const *); %} %union { int b; char* str; } %token CONST %left EQ %left '|' '&' '^' %left '!' %% input: /* empty */ | input line ; line: '\n' | boo '\n' { printf("%s\n",$1?"true":"false"); } ; boo: CONST EQ CONST { $$ = ! strcmp($1, $2); } | boo EQ boo { $$ = $1 == $2 } | boo '|' boo { $$ = $1 || $2 } | boo '&' boo { $$ = $1 && $2 } | boo '^' boo { $$ = ($1 || $2) && !($1 && $2) } | '!' boo { $$ = ! $1 } ; ; %% #include #define BUFSIZE 4096 int yylex(void) { int c, last; char temp[BUFSIZE]; char* p=temp; while ((c = getchar ()) == ' ' || c == '\t'); if (c == '"'){ last = '"'; while ((c = getchar()) != '"' || last == '\\'){ *p = c; last = c; p++; if (p-temp >= BUFSIZE-1) break; } *p = '\0'; strcpy(&yylval,temp); return CONST; } if (c == '='){ if ((c = getchar ()) == '=') return EQ; } if (c == EOF) return 0; return c; } void yyerror (char const *s) { fprintf (stderr, "%s\n", s); } int main(void) { return yyparse (); }