How to resolve conflict between two choices starting with same tokens in javacc

可紊 提交于 2019-12-11 12:17:47

问题


I'm trying to write a compiler for some specific format of messages. My problem if I simplify it is:

< WORD : ([LETTER]){2,5}>
< ANOTHER_WORD : (<LETTER>|<DIGIT>){1,5}>
< SPECIAL_WORLD : "START">

void grammar():
{
}
{ 
 <WORD><ANOTHER_WORD>
| <SPECIAL_WORD><ANOTHER_WORD>
}

Here my special word is matched always as a WORD which is logical of course but since the conflict is at the beginning of the production I don't know how to resolve it. some help would be appreciated.


回答1:


Put the rule for START first. Like most lexical scanner generators, JavaCC uses the rule that the longest possible token match is selected, and then, if two or more patterns apply, the first of these is chosen.

As a result, you ANOTHER_WORD rule will only match if WORD doesn't, so that it will only match words of length 1 or which contain a digit.

It appears that you expect the parser state to affect how lexical tokens are recognized. That's not how lexical scanners work, in general, but you can implement a limited form of contextual scanning by using lexical states.



来源:https://stackoverflow.com/questions/32008283/how-to-resolve-conflict-between-two-choices-starting-with-same-tokens-in-javacc

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