I have some constants defined in a header file that contain the max length of certain strings to be parsed by a file parser written in flex/bison. I want to move the code for checking string lengths from the c code to my regular expressions to make things a bit more concise.
Right now I have rules that look like:
[[:alnum:]]+ { yylval.sval = (char*) strdup(yytext); return STRING; }
Where the length check takes place in the bison rules.
I want to modify this so that it checks for a match no longer than MAX_STR_LEN which is defined in a header called constants.h. If MAX_STR_LEN is equal to 32 than I would want the same effect as:
[[:alnum:]]{1,32}
Is there anyway to do this without running my flex file through an additional step of preprocessing?
EDIT:
The following rule will fail because MAX_STR_LEN is not a literal number, it is seen as a string so flex thinks that 2 actions have been defined for a single rule.
[[:alnum:]]{1,MAX_STR_LEN} { do_something(); }
Additionally if one tries to define a macro in the declarations part of the flex file than it also fails as such.
max_len 32 /* Also fails if 32 is replaces with MAX_STR_LEN */
%%
[[:alnum:]]{1,max_len} { do_something(); }
You could deal with it in the action rule:
This makes the rule effectively the same as your
{1,MAX_STR_LEN}modifier on the pattern