Why minus sign cannot be read correctly in my Antlr grammar

18 views
Skip to first unread message

Lin Zhang

unread,
Mar 28, 2017, 10:10:53 AM3/28/17
to antlr-discussion
I have written the following Antlr grammar:

grammar Hello;
file: row+ ;
row: karyotype NEWLINE ;
karyotype: chrNum (',' sexChr CONST?)? (',' event)* ;
event: prefixPlus gainChr (CONST | INH)?       # gainChrEvent
     | prefixMinus lossChr (CONST | INH)?      # lossChrEvent
     ;
chrNum: numRangeTypeI ;
numRangeTypeI: INT (APPROX INT)? ;
gainChr: INT | SEX ;
lossChr: INT | SEX ;
prefixPlus: PLUS QUES? | QUES PLUS ;
prefixMinus: MINUS QUES? | QUES MINUS ; 
sexChr: (SEX | QUES)+ ;

APPROX:  '~' | '-' ;
CONST: 'c' ;
INH: 'dn' | 'inh' | 'mat' | 'pat' ;
INT: [0-9]+ ;
MINUS: '-' ;
NEWLINE: '\r'? '\n' ;
PLUS: '+' ;
QUES: '?' ;
SEX: [XY]+ ;

WS : [ \t]+ -> skip ;


But when I use the following for parsing:

43-45,XX,-4

The Antlr told me "line 1:9 mismatched input '-' expecting {'-', '+', '?'}"

Do you know what's wrong with my grammar? The grun -gui parse-tree picture has been attached.
antlr4_parse_tree.png

Mike Lischke

unread,
Mar 28, 2017, 10:38:07 AM3/28/17
to antlr-di...@googlegroups.com

APPROX:  '~' | '-' ;
CONST: 'c' ;
INH: 'dn' | 'inh' | 'mat' | 'pat' ;
INT: [0-9]+ ;
MINUS: '-' ;
NEWLINE: '\r'? '\n' ;
PLUS: '+' ;
QUES: '?' ;
SEX: [XY]+ ;

WS : [ \t]+ -> skip ;
APROX and MINUS both can match the same input. APROX wins as it comes earlier in the grammar.


Reply all
Reply to author
Forward
0 new messages