[英]Antlr parser rule fails to match either of specified lexer rules
I have a small work-in-progress Antlr grammar that looks like: 我有一个小的正在进行的Antlr语法,看起来像:
filterExpression returns [ActivityPredicate pred]
: NAME OPERATOR (PACE | NUMBER) {
if ($PACE != null) {
$pred = new SingleActivityPredicate($NAME.text, Operator.fromCharacter($OPERATOR.text), $PACE.text);
} else {
$pred = new SingleActivityPredicate($NAME.text, Operator.fromCharacter($OPERATOR.text), $NUMBER.text);
}
};
OPERATOR: ('>' | '<' | '=') ;
NAME: ('A'..'Z' | 'a'..'z')+ ;
NUMBER: ('0'..'9')+ ('.' ('0'..'9')+)? ;
PACE: ('0'..'9')('0'..'9')? ':' ('0'..'5')('0'..'9');
WS: (' ' | '\t' | '\r'| '\n')+ -> skip;
Hoping to parse things like: 希望解析如下内容:
distance = 4
or pace < 8:30
distance = 4
或pace < 8:30
However, both of those inputs result in null
for both the PACE
and NUMBER
, while trying to parse either: 但是,这两个输入都会导致
PACE
和NUMBER
都为null
,同时尝试解析以下任何一个:
However, dropping the option and just picking PACE
works fine (it also works fine the other way, opting for NUMBER
): 但是,删除选项并仅选择
PACE
可以很好地工作(相反,也可以选择NUMBER
):
filterExpression returns [ActivityPredicate pred]
: NAME OPERATOR PACE { ... };
Why is it that when I provide the option, they're both null
? 为什么当我提供选项时,它们都为
null
?
Try this. 尝试这个。
filterExpression returns [ActivityPredicate pred]
: n=NAME o=OPERATOR (p=PACE | i=NUMBER) {
if ($PACE != null) {
$pred = new SingleActivityPredicate(
$n.text, Operator.fromCharacter($o.text), $p.text);
} else {
$pred = new SingleActivityPredicate(
$n.text, Operator.fromCharacter($o.text), $i.text);
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.