repetition of a set of characters
I have the expression
[A-E]|[A-E]{3}|[A-E]{4}
its made to recognize names of angles (A,B,C,D,E)
or triangles (ABC,BCD)
ect or Rectangles (ABCD,EDCB)
ect
BUT
i want to change the expression so that user CANT input an name with the same letter 2 times, names such AAC or ABAE should not be valid names for a trianlge or rectangle.
I ve seen a regex solution of this type of problem here but cant see how i can do this on flex and cant find a way to solve this on the patterns of flex manual. Any help/guide would be helpful.
thanks
The easiest way to do this in flex is probably with REJECT
:
[A-E]|[A-E]{3}|[A-E]{4} { for (int i = 0; i < yyleng-1; i++) {
if (strchr(yytext+i+1, yytext[i])) {
/* duplicate letter in string */
REJECT; } }
return whatever...; }
[A-Z]+ { return something_else...; }
With this, if you have an input like ABA
, it will match the pattern, but due to the duplicate A
, it will reject that match and go to match the next-best pattern ( [AZ]+
in this case) and return something_else...
Also note from the flex documentation:
'REJECT' is a particularly expensive feature in terms of scanner performance; if it is used in any of the scanner's actions it will slow down all of the scanner's matching. Furthermore, 'REJECT' cannot be used with the '-Cf' or '-CF' options
I have done this
names [A-E]{4}|[A-E]{3}|[A-E]
%%
{names} {int i; for (i = 0; i < yyleng-1; i++) {
if (strchr(yytext+i+1, *yytext)) {
/* duplicate letter in string */
REJECT; } }
return printf( " %s :VALID NAME n", yytext ); }
[A-Z]+ { return printf( " %s :INVALID NAMEn", yytext ); }
But it works only for duplicated letters that are at the start of the expression
ex ABA :invalid ABCD:valid ABCA:invalid
BUT
ABBA :valid (it should be invalid) ACBC :valid (it should be invalid)
i must find a way to make i work for every situation
链接地址: http://www.djcxy.com/p/57870.html上一篇: FlexLexer.h:没有这样的文件或目录flex ++
下一篇: 重复一组字符