Search code examples
antlrantlr3antlrworks

Why doesn't this ANTLR grammar file generate and how do I fix it?


grammar mygrammar;

string  : '"' ( ESC | ~('\u0000'..'\u001f' | '\\' | '\"' ) )* '"';

number : HEX_NUMBER | '-'? INTEGER_NUMBER ( '.' INTEGER_NUMBER )?;

HEX_NUMBER : '0x' HEX_DIGIT+;

INTEGER_NUMBER : DIGIT+;

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

fragment 
ESC     :   '\\' (UNI_ESC |'b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\');

fragment 
UNI_ESC : 'u' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT;

fragment
HEX_DIGIT : ('0'..'9'|'a'..'f'|'A'..'F') ;

fragment
DIGIT   :   ('0'..'9');

ID  :   ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*;

here is the error message I get in the ANTLRWorks console

[14:49:09] error(208): mygrammar.g:7:1: The following token definitions can never be matched because prior tokens match the same input: T__16

If I comment out the string line it generates code, how do I have both the string and the number rules at the same time?


Solution

  • I think you're seeing a bit of a strange error because you're using an operator in an illegal place. The range operator, .. (dot-dot), is only valid inside lexer rules. Your string rule is a parser rule, which should be made a lexer rule instead.

    So, instead of:

    string  : '"' ( ESC | ~('\u0000'..'\u001f' | '\\' | '\"' ) )* '"';
    

    do:

    STRING  : '"' ( ESC | ~('\u0000'..'\u001f' | '\\' | '\"' ) )* '"';