为什么在我的 Antlr 语法中无法正确读取减号
Why minus sign cannot be read correctly in my Antlr grammar
我写了如下Antlr语法:
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 ;
但是当我使用以下内容进行解析时:
43-45,XX,-4
Antlr 告诉我的"line 1:9 mismatched input '-' expecting {'-', '+', '?'}"
你知道我的语法有什么问题吗?
APPROX
和 MINUS
规则相互不明确。尝试进行这些更改:
numRangeTypeI: INT ((APPROX | MINUS) INT)? ;
APPROX: '~' ;
我知道为什么了。更改后
APPROX: '~' | '-' ;
至
approx: '~' | MINUS ;
我的代码现在可以运行了!
实际上,The Definite ANTLR4 Reference 在 p.1 中说。 280、"Be careful that you don’t specify the same string literal on the right side of multiple lexer rules. Such literals are ambiguous and could match multiple token types."
我写了如下Antlr语法:
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 ;
但是当我使用以下内容进行解析时:
43-45,XX,-4
Antlr 告诉我的"line 1:9 mismatched input '-' expecting {'-', '+', '?'}"
你知道我的语法有什么问题吗?
APPROX
和 MINUS
规则相互不明确。尝试进行这些更改:
numRangeTypeI: INT ((APPROX | MINUS) INT)? ;
APPROX: '~' ;
我知道为什么了。更改后
APPROX: '~' | '-' ;
至
approx: '~' | MINUS ;
我的代码现在可以运行了!
实际上,The Definite ANTLR4 Reference 在 p.1 中说。 280、"Be careful that you don’t specify the same string literal on the right side of multiple lexer rules. Such literals are ambiguous and could match multiple token types."