问题
I have defined different states in my lexer, which change not depending on the token but on a sequence of tokens (similarly to how template engines work). I can define longer tokens but I somehow like this approach better.
回答1:
You can stick a function in the third section of the .l file that uses the BEGIN macro, and then call that function from your bison action (or anywhere else for that matter). You need to be careful of the fact that bison may read ahead a token before reducing a rule (running its action), so getting the right state set at the right instant can be tricky.
回答2:
You can add a parameter to yylex() that is then used to set the state explicitly each time it is called. Then you have a parameter in yacc you update in your actions which is then passed to yylex(). This need not be a global parameter.
The actual technique is discussed here, though it may need to be adapted to bison and/or sourceforge flex: http://my.opera.com/myrkraverk/blog/2012/01/04/passing-parameters-to-yacc-and-flex
来源:https://stackoverflow.com/questions/5379930/is-there-a-way-to-change-the-flex-start-state-from-bison