如何在 ANTLR4 中捕获无关输入? [英] How to catch extraneous input in ANTLR4?
本文介绍了如何在 ANTLR4 中捕获无关输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在 antlr4 中捕获无关的错误.它只打印到 stderr 之类的东西
I want to catch extraneous error in antlr4. It only prints to stderr something like
line 1:16 extraneous input 'three' expecting {<EOF>, PLUS_MINUS, MUL_DIV}
但不会抛出任何错误.
推荐答案
ANTLR 词法分析器实现了 ANTLRErrorListener
接口并将其初始化为 ConsoleErrorListener
.
The ANTLR lexer implements the ANTLRErrorListener
interface and initializes it to the ConsoleErrorListener
.
在您构建词法分析器的代码中,可选择删除 ConsoleErrorListener
并添加您自己的实现,ie,YourListener extends BaseErrorListener
.
In your code constructing the lexer, optionally remove the ConsoleErrorListener
and add your own implementation, i.e., YourListener extends BaseErrorListener
.
这篇关于如何在 ANTLR4 中捕获无关输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文