0

I am new to the flex regular expression and I don't why it keep showing the bad character. I am wondering if the "?!" is exist in flex regular expression. If not, how to replace it with correct one?

Here is my code

%option noyywrap
%{
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

%}

/* ------------------- Rules space -------------------- */
%%

^aa(a|b)*$                    {printf("Token1:%s\n" ,yytext);}
a{3}(a|b)*$             {printf("Token2:%s\n" ,yytext);}
a{2}(a|b)*$             {printf("Token3:%s\n" ,yytext);}
^(?:(a|b){2})+$         {printf("Token4:%s\n" ,yytext);}
^b*(ab*ab*)*$           {printf("Token5:%s\n" ,yytext);}
^(?:.{5})*$             {printf("Token6:%s\n" ,yytext);}
\b((?!aaa)\w)+\b        {printf("Token7:%s\n" ,yytext);} // line 19 error appear here


.|\n        {} // default rule (always include to match all other strings)


%%

/* ----------------- User code space ------------------ */

main()
{
    yylex();
    return;
}

Error

owl
  • 1
  • Indeed, lookaheads are not supported but Flex, or any "traditional" regex really. – tripleee Feb 21 '22 at 19:13
  • Probably the Perl extension `\w` isn't supported either. – tripleee Feb 21 '22 at 19:20
  • See https://stackoverflow.com/questions/7333145/regex-get-string-between-two-words-that-doesnt-contain-word for a way to code a negation in old-school regex. In Flex there are probably better ways to do what you want, though (depending also on what exactly you want. This looks like it maybe should be handled on the parser level). – tripleee Feb 21 '22 at 19:22
  • [The flex manual section on patterns](https://westes.github.io/flex/manual/Patterns.html) contains a complete list of the regular expression operators understood by Flex. Please read it; it's not very long. I'd suggest you read all of the introductory sections to the manual; it's important to understand what Flex is designed to do (which is to partition an input stream into consecutive lexical items, *not* find matches for regular expressions). – rici Feb 21 '22 at 21:12
  • Note that in flex, `\w` matches a `w` and `\b` matches a backspace character (character code 8). Also, flex does not do captures, so there is no point using `(?...)` to avoid a capture. (It's not illegal, but it doesn't do anything.) And, as @triplee says, lookahead assertions are not in the list of patterns accepted by Flex, which is why you get an error message. – rici Feb 21 '22 at 21:16

0 Answers0