简体   繁体   中英

Syntactic predicate is not working

First rule never works. It should handle something like 'ID1.ID2.ID3.ID4.ID5'. But other rules work as expected. What is wrong with it?

grammar testInt;

data_source:
     (ID '.' ID '.' ID ('.' ID)+)=>program_ref
    | (ID '.' ID '.' ID)=>var_ref
    | (ID '.' ID)=>program_ref
    | resource;

program_ref: ID ('.' ID)+;
var_ref:    ID '.' ID '.' ID;
resource:   ID;

ID:  (LETTER | ('_'(LETTER | DIGIT))) ('_'? (LETTER | DIGIT))*;
WSFULL:(' '|'\r'|'\t'|'\u000C'|'\n') {$channel=HIDDEN;};

fragment LETTER: (('a'..'z') | ('A'..'Z'));
fragment DIGIT: '0'..'9';

It appears ANTLR first tries to match var_ref before program_ref because the latter can potentially match just 2 ID 's where var_ref matches 3 and the parser matches tokens greedily.

I assume this is just a dummy (part of your) grammar: is there a real world problem you're trying to solve?

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