2
votes

I simplified my Antlr4 grammar to this:

grammar test;

many:
    ('[' INT ']')*;
INT: '-'? '0'..'9'+;

in ANTLRWorks 2.1 it gives me the warning on '[' and ']'

 Implicit token definition in parser rule

Why they are warnings?

I shouldn't use direct char or string in a parser rule?

2

2 Answers

3
votes

Implicit token references are string literals in parser rules that comprise a lexer token, which is created implicitly by ANTLR4. It's bad practice and can lead to unexpected results when using implicit tokens (and the only reason they are still allowed is probably convenience for very simple cases). So, I recommend that you define your token explicitly in lexer rules. This not only avoids the pitfalls of implict tokens, but also allows you to give your tokens speaking names (the rule name), instead of having to go with generic ones (generated by ANTLR4).

1
votes

You should create tokens:

LBRACK:'[';
RBRACK:']';

and replace every ']' and '['. This is because '[' might end up being recognized as a string literal, and match some sort of string* search, when it is actually a special symbol.