-->

Exclude a string as a token in Javacc

2019-08-11 06:30发布

问题:

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.



标签: javacc