%{ #include #include #include #define OVECCOUNT 3 int yylex (void); void yyerror (char const *); int regex_cmp(char* s, char* r); %} %union { int b; int n; char* str; } %token CONST %token REGEX %token NUM %left EQ NEQ GE LE GT LT RE NRE %left '|' '&' '^' %left '!' %type boo %% input: /* empty */ | input line ; line: '\n' | boo '\n' { printf("%s\n",$1?"true":"false"); } | error '\n' { yyerrok; } ; boo: CONST EQ CONST { $$ = ! strcmp($1, $3); } | CONST NEQ CONST { $$ = !! strcmp($1, $3); } | CONST RE REGEX { $$ = regex_cmp($1,$3) >= 0 } | CONST NRE REGEX { $$ = regex_cmp($1,$3) < 0 } | NUM EQ NUM { $$ = $1 == $3 } | NUM NEQ NUM { $$ = $1 != $3 } | NUM GE NUM { $$ = $1 >= $3 } | NUM LE NUM { $$ = $1 <= $3 } | NUM GT NUM { $$ = $1 > $3 } | NUM LT NUM { $$ = $1 < $3 } | boo '|' boo { $$ = $1 || $3 } | boo '&' boo { $$ = $1 && $3 } | boo '^' boo { $$ = ($1 || $3) && !($1 && $3) } | '!' boo { $$ = ! $2 } ; ; %% #include #include #define BUFSIZE 4096 int regex_cmp(char* s, char* r) { pcre *brum; int erroroffset; const char* error; int ovector[OVECCOUNT]; brum=pcre_compile(r,0,&error,&erroroffset,NULL); if (!brum) return -1; int res=pcre_exec(brum,NULL,s,strlen(s),0,0,ovector,OVECCOUNT); pcre_free(brum); return res; } char* get_string_out(char delim) { int last = delim; int i = 0; char* s; int c; if (!(s = malloc(BUFSIZE))){ puts("Low memory"); exit(0); } while ((c = getchar()) != delim || last == '\\'){ if (last=='\\' && c != delim) s[i-1] = c; else { s[i] = c; i++; } last = c; if (i >= BUFSIZE-1) break; } s[i] = '\0'; return s; } int yylex(void) { int c, last; while ((c = getchar ()) == ' ' || c == '\t'); if (c == '"'){ last = '"'; yylval.str = get_string_out('"'); return CONST; } if (c == '\''){ last = '\''; yylval.str = get_string_out('\''); return CONST; } if (c == '/'){ last = '/'; yylval.str = get_string_out('/'); return REGEX; } if (isdigit(c)){ ungetc(c,stdin); scanf("%d",&yylval.n); return NUM; } if (c == '!'){ if ((c = getchar ()) == '=') return NEQ; else if (c == '~') return NRE; else ungetc(c,stdin); } if (c == '<'){ if ((c = getchar ()) == '=') return LE; else { ungetc(c,stdin); return LT; } } if (c == '>'){ if ((c = getchar ()) == '=') return GE; else { ungetc(c,stdin); return GT; } } if (c == '='){ if ((c = getchar ()) == '=') return EQ; else ungetc(c,stdin); } if (c == '~'){ if ((c = getchar ()) == '~') return RE; else ungetc(c,stdin); } if (c == EOF) return 0; return c; } void yyerror (char const *s) { fprintf (stderr, "%s\n", s); } int main(void) { return yyparse (); }