How does one use REJECT in flex?
Let's be clear I'm talking about the lexical analysis tool.
For the following input:
aa bb cc dd
I need the parser to match the following sub strings:
aa bb
bb cc
cc dd
I have this regex:
[a-zA-Z]+[ ]+[a-zA-Z]+
But this only matches开发者_运维百科:
aa bb
cc dd
I've been told the REJECT command can be used to get the output I need but I don't know how to apply it. Could someone give me a hand?
REJECT will start the scan over, then you just need to "eat" the first alphabetic string that you already matched. Possibly this is what you wanted (10 years ago :-):
%%
[a-zA-Z]+[ ]+[a-zA-Z]+ { printf("found %s\n", yytext); REJECT; }
[a-zA-Z]+[ ]*
Given the input string:
a b c d e
it will produce:
found a b
found b c
found c d
found d e
You can use a regex to enumerate n and n + 1 pairs through an entire set.
Look into a grouping with a "greedy" backwards (or forwards) match.
This is one Flex solution to the problem :
%% "aa bb" {printf( "matched aa bb\n");REJECT;} "bb cc" {printf( "matched bb cc\n");REJECT;} "cc dd" {printf( "matched cc dd\n");REJECT;} . /*ignore anything else*/ %% int main() { yylex(); return 0; } When I run the code on your input I get : matched aa bb matched bb cc matched cc dd
精彩评论