简体   繁体   中英

The following token definitions can never be matched because prior tokens match the same input: INT,STRING

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

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 ' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM