JavaCC:与通配符匹配但不消耗状态切换
在JavaCC中,例如在状态DEFAULT
下,我想执行状态切换,如果下一个标记是,我想切换到状态
STATE_A
,否则,我想切换到状态STATE_B
。
我尝试使用类似以下代码的内容,并使用 ""
作为通配符:
TOKEN:
{
<A: "aa"> : STATE_A
| <NOT_A: ""> : STATE_B
}
但是它不起作用,当遇到无法简化为 A
的字符时,函数立即返回,并且不会切换到 STATE_B
,因此 ""
似乎无法完成这项工作。
您有什么建议吗?谢谢。
In JavaCC, for example in state DEFAULT
, I want to perform a state switch, if the next token is <A>
, I want to switch to state STATE_A
, otherwise, I want to switch to state STATE_B
.
I tried to use something like the following code with ""
as a wildcard:
TOKEN:
{
<A: "aa"> : STATE_A
| <NOT_A: ""> : STATE_B
}
But it doesn't work, when a character that cannot be reduced to A
is met, the function returns immediately, and doesn't get switched to STATE_B
, therefore ""
doesn't seem to be able to do the job.
Do you have any suggestions? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,我发现这确实有效。
当
A
无法匹配时,将匹配空字符串,但是我们需要在非终结符的定义中显式引用NOT_A
。 类似的表达式因此,应该重写
以强制执行状态切换。
Well I find that this actually works.
The empty string will be matched when
A
cannot be matched, however we need to explicitly refer toNOT_A
in the definitions of non-terminals. Therefore expressions likeshould be rewritten as
to enforce a state switch.