Welcome to OGeek Q&A 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...