I have defined different states in my lexer, which change not depending on the token but on a s开发者_如何转开发equence of tokens (similarly to how template engines work). I can define longer tokens but I somehow like this approach better.
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.
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
精彩评论