Exclude a string as a token in Javacc

荒凉一梦 提交于 2019-12-11 04:11:31

问题


Im a trying to create a grammar in Javacc, and for a function name, I can have any assortment of lower character letters, or 'main'. If 'main' is the function name, then different procedures needed to be taken.

My problem is that main can not be matched as a string literal when it is included as a choice, and is matched to instead. So I was hoping that if I exclude the word 'main' from the token, then it might work!

< FUNCNAME: (["a"-"z"])+ ~["main"]>

回答1:


The easiest thing to do is to make "main" a different token.

TOKEN: { <MAIN: "main" > }
TOKEN: { <FUNCNAME: (["a"-"z"])+ > }

Both rules match the prefix "main", but the first one wins because it is first. Note that "maintain" is still a FUNCNAME by the maximal munch rule. See FAQ 3.3 in the JavaCC FAQ.



来源:https://stackoverflow.com/questions/16320116/exclude-a-string-as-a-token-in-javacc

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