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

后端 未结 2 2000
灰色年华
灰色年华 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 07:50

    If you're writing lexer grammar which wouldn't be used across multiple parser grammmar(s) then you can ignore this warning shown by ANTLRWorks2.

    0 讨论(0)
  • 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
      
    0 讨论(0)
提交回复
热议问题