0
votes

Trying a simple Grammar on antlr. it should parse inputs such as L=[1,2,hello]. However, antlr is producing this error: The following token definitions can never be matched because prior tokens match the same input: INT,STRING.Any Help?

  grammar List;
    decl: ID '=[' Inside1 ']'; // Declaration of a List. Example : L=[1,'hello']
    Inside1: (INT|STRING) Inside2| ; // First element in the List. Could be nothing
    Inside2:',' (INT|STRING) Inside2 | ; //

    ID:('0'..'Z')+;
    INT:('0'..'9')+;
    STRING:('a'..'Z')+;

EDIT: The updated Grammar. The error remains with INT Only.

grammar List;
decl: STRING '=[' Inside1 ']'; // Declaration of a List. Example : L=[1,'hello']
Inside1: (INT|'"'STRING'"') Inside2| ; // First element in the List. Could be nothing
Inside2:',' (INT|'"'STRING'"') Inside2 | ; //

STRING:('A'..'Z')+;
INT:('0'..'9')+;
1
Those ranges look very weird (except '0'..'9' - that one looks fine). What exactly do you want them to match? Do you not get an error message about the range 'a'..'Z' being empty?sepp2k

1 Answers

1
votes

Your ID pattern matches everything that would be matched by INT or STRING, making them irrelevant. I don't think that's what you want.

ID shouldn't match tokens starting with a digit; 42 is not an identifier. And your comment implies that STRING is intended to be a string literal ('hello') but your lexical pattern makes no attempt to match '.