Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flex error negative range in character class

I am writing a parser using Flex and Bison and have defined various tokens as:

[0-9]+          { yylval.str=strdup(yytext); return digit; }

[0-9]+\.[0-9]*      { yylval.str=strdup(yytext); return floating; }

[a-zA-Z_][a-zA-Z0-9_]*  { yylval.str=strdup(yytext);  return key; }

[a-zA-Z/][a-zA-Z_-/.]*  { yylval.str=strdup(yytext);  return string; }

[a-zA-Z0-9._-]+     { yylval.str=strdup(yytext);  return hostname; }

["][a-zA-Z0-9!@#$%^&*()_-+=.,/?]* { yylval.str=strdup(yytext);  return qstring1; }

[a-zA-Z0-9!@#$%^&*()_-+=.,/?]*["] { yylval.str=strdup(yytext);  return qstring2; }

[#].+           { yylval.str=strdup(yytext);  return comment;}

[ \n\t]         {} /* Ignore white space. */

.           {printf("ERR:L:%d\n", q); return ERROR;}

And it shows an error "Negative Range in Character Class" in the regexps for string, qstring1 and qstring2.

Can someone please help me with where I went wrong?

The spec is that: Non quoted strings may contain ASCII alphanumeric characters, underscores, hyphens, forward slash and period and must start with letter or slash.

Quoted strings may contain any alphanumeric character between the quotes.

I have taken two different strings for quoted strings for some more specifications to be fulfilled.

Thanks.

like image 268
user3435894 Avatar asked Oct 24 '25 19:10

user3435894


2 Answers

For (string, qstring1, qstring2) you need to either place the hyphen (-) as the first or last character of your character class [] or just simply escape it \- if elsewhere.

  • (string)

    [a-zA-Z/][a-zA-Z_./-]*
    
  • (qstring1)

    ["][a-zA-Z0-9!@#$%^&*()_+=.,/?-]*
    
  • (qstring2)

    [a-zA-Z0-9!@#$%^&*()_+=.,/?-]*["]
    
like image 188
hwnd Avatar answered Oct 26 '25 10:10

hwnd


- needs to be escaped with a backslash. For qstring1, try the following:

["][a-zA-Z0-9!@#$%^&*()_\-+=.,/?]*
like image 20
Calvin Avatar answered Oct 26 '25 10:10

Calvin