How Get error messages of antlr parsing?

假如想象 提交于 2019-11-29 11:12:11

Using ANTLR for CSV parsing is a nuclear option IMHO, but since you're at it...

  • Implement the interface ANTLRErrorListener. You may extend BaseErrorListener for that. Collect the errors and append them to a list.
  • Call parser.removeErrorListeners() to remove the default listeners
  • Call parser.addErrorListener(yourListenerInstance) to add your own listener
  • Parse your input

Now, for the lexer, you may either do the same thing removeErrorListeners/addErrorListener, or add the following rule at the end:

UNKNOWN_CHAR : . ;

With this rule, the lexer will never fail (it will generate UNKNOWN_CHAR tokens when it can't do anything else) and all errors will be generated by the parser (because it won't know what to do with these UNKNOWN_CHAR tokens). I recommend this approach.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!