Is “Implicit token definition in parser rule” something to worry about?

I highly recommend correcting all instances of this warning in code of any importance.

This warning was created (by me actually) to alert you to situations like the following:

shiftExpr : ID (('<<' | '>>') ID)?;

Since ANTLR 4 encourages action code be written in separate files in the target language instead of embedding them directly in the grammar, it’s important to be able to distinguish between << and >>. If tokens were not explicitly created for these operators, they will be assigned arbitrary types and no named constants will be available for referencing them.

This warning also helps avoid the following problematic situations:

  • A parser rule contains a misspelled token reference. Without the warning, this could lead to silent creation of an additional token that may never be matched.
  • A parser rule contains an unintentional token reference, such as the following:

    number : zero | INTEGER;
    zero   : '0'; // <-- this implicit definition causes 0 to get its own token
    

Leave a Comment