int yylex (void);
void yyerror (char const *);
+int regex_cmp(char* s, char* r);
+
%}
%union {
boo: CONST EQ CONST { $$ = ! strcmp($1, $3); }
| CONST NEQ CONST { $$ = !! strcmp($1, $3); }
- | CONST RE REGEX {
- pcre *brum;
- int erroroffset;
- const char* error;
- int ovector[OVECCOUNT];
-
- brum=pcre_compile($3,0,&error,&erroroffset,NULL);
- if (!brum){
- puts("Mnau");
- return 1;
- }
-
- int res=pcre_exec(brum,NULL,$1,strlen($1),0,0,ovector,OVECCOUNT);
-
- $$ = res >= 0;
- pcre_free(brum);
- }
- | CONST NRE REGEX {
- pcre *brum;
- int erroroffset;
- const char* error;
- int ovector[OVECCOUNT];
-
- brum=pcre_compile($3,0,&error,&erroroffset,NULL);
- if (!brum){
- puts("Mnau");
- return 1;
- }
-
- int res=pcre_exec(brum,NULL,$1,strlen($1),0,0,ovector,OVECCOUNT);
-
- $$ = res < 0;
- pcre_free(brum);
- }
+ | 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 }
#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 i, c, last;
+ int c, last;
while ((c = getchar ()) == ' ' || c == '\t');
if (c == '"'){
last = '"';
- i = 0;
- if (!(yylval.str = malloc(BUFSIZE))){
- puts("Low memory");
- exit(0);
- }
- while ((c = getchar()) != '"' || last == '\\'){
- if (last=='\\' && c != '"')
- yylval.str[i-1] = c;
- else {
- yylval.str[i] = c;
- i++;
- }
- last = c;
- if (i >= BUFSIZE-1)
- break;
- }
- yylval.str[i] = '\0';
-
+ yylval.str = get_string_out('"');
return CONST;
}
if (c == '\''){
last = '\'';
- i = 0;
- if (!(yylval.str = malloc(BUFSIZE))){
- puts("Low memory");
- exit(0);
- }
- while ((c = getchar()) != '\'' || last == '\\'){
- if (last=='\\' && c != '\'')
- yylval.str[i-1] = c;
- else {
- yylval.str[i] = c;
- i++;
- }
- last = c;
- if (i >= BUFSIZE-1)
- break;
- }
- yylval.str[i] = '\0';
-
+ yylval.str = get_string_out('\'');
return CONST;
}
if (c == '/'){
last = '/';
- i = 0;
- if (!(yylval.str = malloc(BUFSIZE))){
- puts("Low memory");
- exit(0);
- }
- while ((c = getchar()) != '/' || last == '\\'){
- if (last=='\\' && c != '/')
- yylval.str[i-1] = c;
- else {
- yylval.str[i] = c;
- i++;
- }
- last = c;
- if (i >= BUFSIZE-1)
- break;
- }
- yylval.str[i] = '\0';
-
+ yylval.str = get_string_out('/');
return REGEX;
}
if (c == '<'){
if ((c = getchar ()) == '=')
return LE;
- else
+ else {
+ ungetc(c,stdin);
return LT;
+ }
}
if (c == '>'){
if ((c = getchar ()) == '=')
return GE;
- else
+ else {
+ ungetc(c,stdin);
return GT;
+ }
}
if (c == '='){