%{ #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 NUM %token VAR %token KW_IF KW_ELSE KW_PIPE KW_MAIL KW_COPY %token '(' ')' '{' '}' %left ARROW %left EQ NEQ GE LE '<' '>' RE NRE %left '+' '-' %left '*' '/' %left '|' '&' '^' %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 CONST { $$ = regex_cmp($1,$3) >= 0 } | CONST NRE CONST { $$ = 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 '>' NUM { $$ = $1 > $3 } | NUM '<' NUM { $$ = $1 < $3 } | boo '|' boo { $$ = $1 || $3 } | boo '&' boo { $$ = $1 && $3 } | boo '^' boo { $$ = ($1 || $3) && !($1 && $3) } | '!' boo { $$ = ! $2 } ; ; %% 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; } void yyerror (char const *s) { fprintf (stderr, "%s\n", s); } int main(void) { // yydebug=1; return yyparse (); }