ANTLR ERROR : The following token definitions can never be matched because prior tokens match the same input

StackOverflow https://stackoverflow.com/questions/21455846

  •  05-10-2022
  •  | 
  •  

Question

I am new to ANTLR and I am trying the following .From the error it seems that I am defining something generic and the rule below it is unreachable/redundant.Redundant ones the ANTLR complains about is MAPPING,STROPS,UNARYOPS,ARITHEMATICOPS,MATHLOGICALOP,LOGICALOP,OP1,OP2,OP3,OP4 .I don't understand where exactly am I going wrong. Please let me know the issue and the concept behind it.

 grammar RA;

options {
  language = Java;
  output = AST;
  k=3;
}



DIVIDE : '/';
PLUS : '+';
MINUS : '-';
STAR : '*';
MOD : '%';
LPAREN : '(';
RPAREN : ')';
COMMA : ',';
COLON : ':';
LANGLEBRACKET : '<';
RANGLEBRACKET : '>';
EQ : '=';
NOT : '!';
UNDERSCORE : '_';
DOT : '.';
GRTRTHANEQTO : RANGLEBRACKET EQ;
LESSTHANEQTO : LANGLEBRACKET EQ;
NOTEQ         : NOT EQ;


fragment A:('a'|'A');
fragment B:('b'|'B');
fragment C:('c'|'C');
fragment D:('d'|'D');
fragment E:('e'|'E');
fragment F:('f'|'F');
fragment G:('g'|'G');
fragment H:('h'|'H');
fragment I:('i'|'I');
fragment J:('j'|'J');
fragment K:('k'|'K');
fragment L:('l'|'L');
fragment M:('m'|'M');
fragment N:('n'|'N');
fragment O:('o'|'O');
fragment P:('p'|'P');
fragment Q:('q'|'Q');
fragment R:('r'|'R');
fragment S:('s'|'S');
fragment T:('t'|'T');
fragment U:('u'|'U');
fragment V:('v'|'V');
fragment W:('w'|'W');
fragment X:('x'|'X');
fragment Y:('y'|'Y');
fragment Z:('z'|'Z');

AND     :   A N D;
OR      :   O R;
COUNT       :   C O U N T;
AVG     :   A V G;
COUNTDISTINCT   :   C O U N T D ;
CAST        :   C A S T;
CORRESPONDING   :   C O R R E S P O N D I N G;
ANY     :   A N Y;

MAPPING     :   (CORRESPONDING|ANY);
MATCHCASE       :   I;

EQUALS      :   E Q U A L S;
LIKE        :   L I K E;
NOTEQUALS       :                     N O T E Q U A L S;
NOTLIKE     :   N O T L I K E;
NOTNULL     :   N O T N U L L;

STROPS      :   (EQUALS | LIKE | NOTEQUALS | NOTLIKE | NOTNULL);
UNARYOPS        :   (COUNT | AVG | COUNTDISTINCT);
ARITHEMATICOPS  :   (DIVIDE|PLUS|MINUS|STAR|MOD);
MATHLOGICALOP   :   (LANGLEBRACKET|RANGLEBRACKET|EQ|GRTRTHANEQTO|LESSTHANEQTO|NOTEQ);
LOGICALOP       :   (AND|OR);

SECATTR     :   ('a'..'z' | 'A'..'Z') UNDERSCORE? ('a'..'z' | 'A'..'Z')* DOT ('a'..'z' | 'A'..'Z') UNDERSCORE? ('a'..'z' | 'A'..'Z')*;
BRACEDSECATTR   :   LPAREN SECATTR RPAREN;
UNOPSECATTR :   OP1 BRACEDSECATTR;

OP1     :   (UNARYOPS | CAST) ;
OP2     :   (ARITHEMATICOPS|MATHLOGICALOP|STROPS);
OP3     :   (MAPPING|MATCHCASE);
OP4     :   (LOGICALOP);
//fragment Letter : 'a'..'z' | 'A'..'Z';
//Alphanumeric      :   (('a'..'z' | 'A'..'Z')| '0'..'9')* ('a'..'z' | 'A'..'Z') (('a'..'z' | 'A'..'Z')| '0'..'9')* ;
SINGLERULE      :   (SECATTR|BRACEDSECATTR|UNOPSECATTR) OP2 ((('a'..'z' | 'A'..'Z')| '0'..'9')|SECATTR|BRACEDSECATTR|UNOPSECATTR);
BRACEDSINGLERULE    :   LPAREN SINGLERULE RPAREN;
UNOPSINGLERULE  :   BRACEDSINGLERULE OP3;

Expr        :   SINGLERULE|UNOPSINGLERULE|((SINGLERULE|UNOPSINGLERULE)OP4(SINGLERULE|UNOPSINGLERULE))+;
Was it helpful?

Solution

ANTLR assigns one, and only one, token type (name starting with uppercase letter) to each non-overlapping sequence of characters in the input. In your case, the ANY rule matches a sequence, and the MAPPING rule defined after it is also defined to match ANY (as one of its alternatives). When the input is any, your lexer will always assign the token type ANY to it since that rule is defined first.

While you have allowed the input any to be a MAPPING, ANTLR is warning you that it will never assign the type MAPPING to this input, so the current definition of MAPPING is misleading. You should update your lexer rules so each sequence only matches one token type.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top