ANTLR4 不报告错误

问题描述 投票:0回答:1

我创建了一个语法来表示现有的语言。当浏览已经存在的数千个文件时,有一些让我头疼。它们包含一些没有任何意义并且应该抛出错误的语法,但 antlr4 似乎垃圾了一半的代码并且根本不报告错误。有< in places that are not expected and even antlr labs seems to recognize them as unidentified but does not report them as an error. Is there some kind of setting for making the parser report unknown parts of the language? Most of the time it reports errors successfully but i keep running into these situations were it doesn't and just discards part of the code entirely.

尝试解析带有 snytax 错误的代码文件,并且只是得到一些没有错误的部分语法树。

error-handling antlr4
1个回答
0
投票

试试这个:

String source = "input to parse";

Lexer lexer = new YourLexer(CharStreams.fromString(source));
Parser parser = new YourParser(new CommonTokenStream(lexer));

parser.setErrorHandler(new BailErrorStrategy());

这将导致解析过程中的任何错误抛出异常:ANTLR不会尝试从中恢复。

© www.soinside.com 2019 - 2024. All rights reserved.