简体   繁体   中英

Having command line arguments to flex as search strings

I use flex, the linux/unix not the Adobe type, to generate small scanners. In the past I have always used static search strings. I now want to provide a command line provided search string by providing a string via getopt and then being able to use it for searching with.

The old way of searching was:

.*"_"\n      ECHO;

To find lines that ended with an underscore.

Now I want to search this way:

.*<arbitrary string>.*\n    ECHO;

I don't know how to get flex to accept the <arbitrary string> . I can get it via getopt, but I haven't been able to get flex to accept my syntax.

What I am doing is a special purpose very limited grep for a special problem I am having.

Any help would be appreciated.

.*\n { if(strstr(yytext, "arbitrary string")) ECHO; else REJECT; }

The REJECT statement will skip to next rule if yytext doesn't contain "arbitrary string". This will of course not provide the same performance as if the search string was known at compile time. regcomp()/regexec() in glibc might be faster than flex if you are implementing your own grep program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM