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.


Flex error negative range in character class, 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  Character classes are expanded immediately when seen in the flex input. This means the character classes are sensitive to the locale in which flex is executed, and the resulting scanner will not be sensitive to the runtime locale. This may or may not be desirable. If your scanner is case-insensitive (the ‘ -i ’ flag),


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

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

regex - Flex error negative range in character class, regex - Flex error negative range in character class - Stack Overflow. 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. a "character class" with a range in it; matches an `a', a `b', any letter from `j' through `o', or a `Z' [^A-Z] a "negated character class", i.e., any character but those in the class. In this case, any character except an uppercase letter. [^A-Z ] any character except an uppercase letter or a newline r*


I guess while writing a regular expression you should always write it with it's priority order : for example for this line of code : [+-/*><=] {printf("Operator %c\n",yytext[0]); return yytext[0];} won't give any error. whereas : [+-*/><=] {printf("Operator %c\n",yytext[0]); return yytext[0];} will. hope it helps.

regex - Flex error negative range in character class, You can use a hyphen inside a character class to specify a range of characters. the q, and only the q, in both strings, you need to use negative lookahead: q(?!u). classes where they can't form a range may be interpreted as literals or as errors. PowerGREP—The world's most powerful tool to flex your regex muscles​! `-Ce' directs flex to construct equivalence classes, i.e., sets of characters which have identical lexical properties (for example, if the only appearance of digits in the flex input is in the character class "[0-9]" then the digits '0', '1', , '9' will all be put in the same equivalence class). Equivalence classes usually give dramatic reductions in the final table/object file sizes (typically a factor of 2-5) and are pretty cheap performance-wise (one array look-up per character scanned).


Regexp Tutorial, convert a regex with Unicode character classes to create a pattern FSM: An error reflex::regex_error is thrown as an exception if conversion was not possible, which is RE/flex iterators are useful in C++11 range-based loops. The input() , winput() , and peek() methods return a non-negative character code and EOF  [firstCharacter-lastCharacter] where firstCharacter is the character that begins the range and lastCharacter is the character that ends the range. A character range is a contiguous series of characters defined by specifying the first character in the series, a hyphen (-), and then the last character in the series.


RE/flex user guide, convert a regex with Unicode character classes to create a pattern FSM: An error reflex::regex_error is thrown as an exception if conversion was not possible, which is RE/flex iterators are useful in C++11 range-based loops. The input() , winput() , and peek() methods return a non-negative character code and EOF  The Fast Lexical Analyzer - scanner generator for lexing in C and C++ - westes/flex


RE/flex user guide, A CHARACTER CLASS is a class of characters specified using the operator character \ as above,; (b): the minus character - which is used for ranges like in The Fast Lexical Analyzer - scanner generator for lexing in C and C++ - westes/flex