How to collect errors during run time given by a parser in Antlr4

后端 未结 1 1055
孤城傲影
孤城傲影 2020-12-11 18:12

I have upgraded from Antlr 3 to Antlr 4. I was using this code to catch exceptions using this code. But this is not working for Antlr 4.

partial class XPars         


        
相关标签:
1条回答
  • 2020-12-11 18:43

    You need to implement IAntlrErrorListener<IToken>. If all you want to is report errors like you have above, then you should focus on the SyntaxError method. Several base classes are available if you want to extend one.

    • ConsoleErrorListener
    • BaseErrorListener
    • DiagnosticErrorListener

    The error listener is attached to the parser instance by calling parser.AddErrorListener(listener).

    Edit: You need to create a new class which implements the error listener interface. You then attach the listener to the parser. The parser itself will not implement the error listener interface.

    0 讨论(0)
提交回复
热议问题