1

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

Community
  • 1
  • 1

2 Answers2

2

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 ([A-Z]+ 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

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • 1
    thanks for the help , i ll try to figure out how i will make it work, the code with the reject loop should be put on the definitions,rules or user code part of my lex? – Vasileios Kougioumtzis Apr 23 '13 at 08:13
  • 1
    ok i made it work , btw declare i outside of the loop to avoid the "'for' loop initial declaration used outside C99 mode" Error, it works for a AAC making it A ,A , C , but not for CAA...it recognizes it as CAA which is not valid name for triangle... – Vasileios Kougioumtzis Apr 23 '13 at 10:36
1

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 NAME\n", 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