Flex error negative range in character class

2019-07-20 03:16发布

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.

3条回答
走好不送
2楼-- · 2019-07-20 03:46

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.

查看更多
干净又极端
3楼-- · 2019-07-20 03:47

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

["][a-zA-Z0-9!@#$%^&*()_\-+=.,/?]*
查看更多
Deceive 欺骗
4楼-- · 2019-07-20 03:48

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!@#$%^&*()_+=.,/?-]*["]
    
查看更多
登录 后发表回答