问题描述
我想为一些特定格式的消息编写一个编译器。
如果我简化它的问题是:
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>
}
这里我的特殊字符匹配总是作为一个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.
推荐答案
为 START
第一。像大多数词法扫描器生成器一样,JavaCC使用选择最长可能的令牌匹配的规则,然后如果应用两个或更多个模式,则选择这些模式中的第一个。
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.
因此, ANOTHER_WORD
规则只会在 WORD
不匹配时才匹配,以便它只匹配单词长度为1或包含数字。
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.
这篇关于如何解决从javacc中的相同令牌开始的两个选择之间的冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!