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

后端 未结 2 2038
灰色年华
灰色年华 2021-02-02 07:30

I\'m creating my first grammar with ANTLR and ANTLRWorks 2. I have mostly finished the grammar itself (it recognizes the code written in the described language and builds correc

2条回答
  •  盖世英雄少女心
    2021-02-02 08:11

    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
      

提交回复
热议问题