1
votes

I am building a flex/bison reentrant parser in C, and I am making heavy use of states within the flex lexer, mostly of which are exclusive ones.

But I would appreciate, if possible, to set a lexer state from a bison mid-rule action. Is there any possibility to change the lexer state during execution, from bison - i.e. in the middle of a bison rule, through its associated action?

1

1 Answers

1
votes

This is possible, but ugly. In particular the lexer is always in a consistent state when you're back in the parser (as it has returned a token), but, it may not be in the state you'd expect as the parser may have done a look-ahead call.

In general feedback between parser and lexer is quite messy. This is at least in part why gcc's lexer is hand-coded, as the lexer has to return "typedef word" when parsing a C typedef, and "non-typedef variable name" when parsing a C variable declaration, and it's seriously ugly since:

typedef int X;
void f(void) {
    X X;

is actually legal syntax, using the typedef-name on the left of X X and a non-typedef variable name on the right. (Some of this may have changed since I last was down in the low levels of gcc, which was in the 2.x era. :-) )

If you can handle this some other way, I'd recommend that instead.