如何在 Python3 的 ANTLR4 中捕获无关输入? [英] How to catch extraneous input in ANTLR4 in Python3?
问题描述
是的,这几乎是 如何在 ANTLR4 中捕获无关输入的重复? - 但在 Java 的情况下,我需要一个 Python 解决方案,而移植 Java 解决方案对我不起作用!
Yes, this is nearly a duplicate of How to catch extraneous input in ANTLR4? - but that is in the case of Java, I need a solution for Python, and porting the Java solution is just not working for me!
ANTLR4 正在写入以下错误":到控制台.我希望这导致硬停止,但解析器继续并最终重新同步:
ANTLR4 is writing the following "error" to the console. I want this to result in a hard stop, but the parser continues and eventually re-syncs:
line 12:28 extraneous input 'FREDDY' expecting {'AS', ...
我希望这是一个致命的错误.
I want this to be a fatal error.
因此,我定义了自己的错误侦听器,并覆盖了所有 4 个方法:
So, I've defined my own Error Listener, and overriden all 4 methods:
class MyErrorListener( ErrorListener ):
__slots__ = [ 'file_name' ]
def __init__(self, file_name="<stdin>"):
self.file_name = file_name
def syntaxError(self, recognizer, offendingSymbol, line, column, msg, e):
print(f"{self.file_name} {line}:{column} {msg[0:min(80,len(msg)-1)]}", file=sys.stderr)
sys.exit(1)
def reportAmbiguity(self, recognizer, dfa, startIndex, stopIndex, exact, ambigAlts, configs):
print(f"{self.file_name} {startIndex}-{stopIndex} reportAmbiguity {ambigAlts}", file=sys.stderr)
sys.exit(1)
def reportAttemptingFullContext(self, recognizer, dfa, startIndex, stopIndex, conflictingAlts, configs):
print(f"{self.file_name} {startIndex}-{stopIndex} conflictingAlts {conflictingAlts}", file=sys.stderr)
sys.exit(1)
def reportContextSensitivity(self, recognizer, dfa, startIndex, stopIndex, prediction, configs):
print(f"{self.file_name} {startIndex}-{stopIndex} ContextSensitivity {prediction}", file=sys.stderr)
sys.exit(1)
我创建了我的词法分析器,删除了默认的控制台错误侦听器,并将我的添加到:
I create my lexer, remove the default console error listener, and add mine in:
st = InputStream(upper_case)
lexer:PlSqlLexer = PlSqlLexer(st)
# Add your error listener to the lexer if required
lexer.removeErrorListeners()
lexer.addErrorListener( MyErrorListener(f) )
stream = CommonTokenStream(lexer)
parser = PlSqlParser(stream)
tree = parser.sql_script()
printer = PlSqlPrintListener(txt)
walker = ParseTreeWalker()
walker.walk(printer, tree)
而这是捕捉一些错误,例如:
And this is catching some errors, such as:
sql_parser.py 1:0 token recognition error at: '"""\n
(那是我将 Python 程序输入 SQL 解析器 - 当然它不应该工作!)
(That's me feeding a Python program into an SQL Parser - of course it shouldn't work!)
但我的错误列表器没有捕捉到我在顶部报告的错误.
But my Error Listner isn't catching the error that I reported at the top.
有什么建议吗?
- ANTLR 解析器生成器版本 4.9.2
- Python 3.7.3
- antlr4-python3-runtime v4.9.2
推荐答案
解决方案;词法分析器和解析器都是识别器".识别器是听众收听的地方.
The solution; both the Lexer and the Parser are "Recognizers". The Recognizer is where the listener listens.
因此,我们有两种类型的错误,词法错误和解析错误.我只是将我的听众附加到 Lexer,我还需要将它附加到 Parser.修改后的代码:
In consequence, we have two types of errors, Lexing errors and Parsing errors. I was only attaching my listener to the Lexer, I needed to also attach it to the Parser. Revised code:
...
lexer:PlSqlLexer = PlSqlLexer(st)
# Add your error listener to the lexer if required
lexer.removeErrorListeners()
lexer.addErrorListener( my_listener )
stream = CommonTokenStream(lexer)
parser = PlSqlParser(stream)
# Add your error listener to the _parser_ if required
parser.removeErrorListeners()
parser.addErrorListener( my_listener )
...
这篇关于如何在 Python3 的 ANTLR4 中捕获无关输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!