Question

I am stuck at a strange issue in my antlr grammar . It looks like the following. Now the problem here is that while I am testing for "singlerule"

X.Y EQUALS A.B works but all the math operation such as X.Y > 100 doesn't work . It throws an error which is : line 1:4 no viable alternative at input '>' Please help me understand why this issue is cropping up .

grammar RA;

options {
  language = Java;

}


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

WS       :           (' '|'\t'|'\f'|'\n'|'\r')+{ $channel=HIDDEN; };


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');



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   : ((C O U N T | A V G | C O U N T D I S T I N C T) 
      | C A S T) ;

OP2   : DIVIDE|PLUS|MINUS|STAR|MOD
      |LANGLEBRACKET|RANGLEBRACKET|EQ|GRTRTHANEQTO|LESSTHANEQTO|NOTEQ
      |E Q U A L S | L I K E | N O T E Q U A L S | N O T L I K E | N O T N U L L;

OP3   : ((C O R R E S P O N D I N G | A N Y)|I);
OP4   : (A N D | O R);

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

singlerule    : SECATTR OP2 DIGIT*
              | SECATTR OP2 SECATTR
              | BRACEDSECATTR OP2 BRACEDSECATTR
              | UNOPSECATTR OP2 UNOPSECATTR
;
expr    : LPAREN? singlerule RPAREN? 
        | LPAREN singlerule RPAREN OP3 
        |((LPAREN? singlerule RPAREN?|LPAREN singlerule RPAREN OP3) OP4 (LPAREN? singlerule RPAREN?|LPAREN singlerule RPAREN OP3))+;
Was it helpful?

Solution

Try making all of your math token fragments:

fragment LANGLEBRACKET : '<';
fragment RANGLEBRACKET : '>';
...

I think because you are using them as part of another token definition, they need to be fragments, not other tokens.

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