Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

regex - 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;}

[ 
]         {} /* Ignore white space. */

.           {printf("ERR:L:%d
", 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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!@#$%^&*()_+=.,/?-]*["]
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...