From what I gather, the following won't work in flex:
...
std::string opt_val = "--help";
%%
opt_val { /* do something */ }
%%
Can I expand a C macro instead? I've tried this:
%{
#define MCR_OPTION "--help"
%}
ALIAS_OPTION MCR_OPTION
%%
{ALIAS_OPTION} { /* do something */ }
%%
Unfortunately, this doesn't seem to work. I want to keep the string literal values someplace else than the rule section of the scanner, so that if they change, I will have one less place to update. Am I missing something obvious?
(EDIT): Here is a more complete sample that I tested:
test.l
%option noyywrap
%{
#include <iostream>
#define MCR_HELP_OPT "--help"
#define MCR_HELP_OPT2 --help
%}
ALIAS_HELP_OPT MCR_HELP_OPT
ALIAS_HELP_OPT2 MCR_HELP_OPT2
%%
"--works" { std::cout << "this works" << std::endl; }
{ALIAS_HELP_OPT} { std::cout << "help" << std::endl; }
{ALIAS_HELP_OPT2} { std::cout << "help2" << std::endl; }
\n { return 0; }
. { /* consume */ }
%%
int main(int, char**) {
while (yylex()) {}
}
I tested it like so:
> flex test.l && c++ lex.yy.c
> echo "--help" | ./a.out
> echo "--works" | ./a.out
this works
In the end, I decided to use CMake's configure_file functionality; Here is a minimal working example:
CMakeLists.txt:
test.l.in
Then building and running it: