As the title states, how do I change the currently active start condition or state in (f)lex outside of action code? Flex manual states that special directives like BEGIN
can only be used inside an action.
I am trying to introduce different "modes" in my scanner which change what is returned, these modes are set externally and not in action code so I can't use BEGIN
.
Any advice? Thank you for the help in advance!
You can (only) use BEGIN
in the lex.l file, but you can use it anywhere the .l file. In particular, you can use it in the third section (after the second %%
), where you can have any C code. So you can define a function that uses BEGIN
to set a start state, and then declare that function in a header file and use it anywhere.
example:
%s STATE1
%%
<STATE1>"foo" /* rule applies in state 1 */ ;
%%
void set_state1() {
BEGIN STATE1;
}
add extern void set_state1();
to a header file and you can call it from anywhere.