[英]Antlr grammar not matching expected lexer rule
I'm trying to match a duration string, like for 30 minutes
or for 2 hours
using the following rules: 我正在尝试使用以下规则匹配持续时间字符串,例如for 30 minutes
或for 2 hours
:
durationPhrase: FOR_STR (MINUTE_DURATION | HOUR_DURATION);
MINUTE_DURATION: NONZERO_NUMBER MINUTE_STR;
HOUR_DURATION: NONZERO_NUMBER HOUR_STR;
MINUTE_STR: 'minute'('s')?;
HOUR_STR: 'hour'('s')?;
FOR_STR: 'for';
NONZERO_NUMBER: [0-9]+;
WS: (' '|[\n\t\r]) -> skip;
With the following input: 输入以下内容:
for 30 minutes
Attempting to debug/match the durationPhrase
rule, I'm presented with the error: 尝试调试/匹配durationPhrase
规则时,出现以下错误:
line 1:4 mismatched input '30' expecting {MINUTE_DURATION, HOUR_DURATION}
But I can't seem to figure out what lexer rule the '30' is matching? 但是我似乎无法弄清楚“ 30”匹配的词法规则是什么? I was under the impression the "longest" lexer rule would win, which would be the MINUTE_DURATION
rule. 我印象中“最长的”词法分析器规则将获胜,这将是 MINUTE_DURATION
规则。
Is it instead matching NONZERO_NUMBER
first? 而是首先匹配NONZERO_NUMBER
吗? And if so, why? 如果是这样,为什么?
It's matching NONZERO_NUMBER
because neither of the other patterns apply. 它与NONZERO_NUMBER
匹配,因为其他任何模式均不适用。 If you had entered 30minutes
, it would have matched MINUTE_DURATION
, but as a token pattern, MINUTE_DURATION
won't match the space character. 如果您输入了30minutes
,则它将与MINUTE_DURATION
相匹配,但作为令牌模式, MINUTE_DURATION
将与空格字符不匹配。
You ignore whitespace by applying -> skip
to the token WS
. 您可以通过-> skip
令牌WS
来忽略空格。 That can only happen after WS
is recognised as a token; 只有在将WS
识别为令牌后才能发生这种情况。 ie after tokenisation. 即在标记化之后。 During tokenisation, whitespace characters are just characters. 在标记化过程中,空格字符只是字符。
If you make MINUTE_DURATION
and HOUR_DURATION
syntax rules rather than lexical rules, it should work as expected. 如果您制定MINUTE_DURATION
和HOUR_DURATION
语法规则而不是词法规则,则它应能按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.