Is there a way to change the flex start state from bison?

混江龙づ霸主 提交于 2019-12-06 09:14:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!