简体   繁体   中英

Antlr parser rule fails to match either of specified lexer rules

I have a small work-in-progress Antlr grammar that looks like:

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

However, both of those inputs result in null for both the PACE and NUMBER , while trying to parse either:

在此处输入图片说明

However, dropping the option and just picking PACE works fine (it also works fine the other way, opting for NUMBER ):

filterExpression returns [ActivityPredicate pred]
        : NAME OPERATOR PACE { ... };

在此处输入图片说明

Why is it that when I provide the option, they're both 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);
        }
    };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM