相关疑难解决方法(0)

Antlr处理异常

我使用AST树使用Antlr 3开发了一个复杂的语法.ANTLR生成Lexer和Parser.问题是当用户输入例如无效的语法时,语法期望';'.用户不输入,然后在我的Eclipse IDE中我得到以下异常:

 line 1:24 mismatched input '<EOF>' expecting ';'
Run Code Online (Sandbox Code Playgroud)

如何处理此异常,因为我试图捕获此异常,但是没有捕获异常.这是一个例外吗?我似乎不明白为什么没有捕获此异常.我试图找出答案,但Antlr网站似乎已经停止了一段时间了.

我查看了以下内容:使用"$"和Java进行ANTLR异常处理并遵循该示例,但是当Lexer通过添加RuntimeException()生成代码时,我得到了无法访问的代码.

我不知道该怎么做.

当我尝试从解析器获取语法错误的数量时,它显示0.

编辑:

我找到了一个解决方案,通过查看:ANTLR不会在无效输入上抛出错误

但是,当我尝试获取Exception消息时,它为null.我是否正确设置了一切?请参阅示例语法:

grammar i;

options {
output=AST;
}

@header {
package com.data;
}

@rulecatch {
    catch(RecognitionException e) {
        throw e;
   }
}

// by having these below it makes no difference
/**@parser::members {
    @Override
    public void reportError(RecognitionException e) {
        throw new RuntimeException("Exception : " + " " + e.getMessage());
    }
}

@lexer::members {
    @Override
    public void reportError(RecognitionException e) {
       throw new RuntimeException("Exception …
Run Code Online (Sandbox Code Playgroud)

java error-handling antlr

7
推荐指数
1
解决办法
7890
查看次数

如何在Antlr4中由解析器给出的运行时收集错误

我已经从Antlr 3升级到Antlr 4.我使用此代码来捕获使用此代码的异常.但这不适用于Antlr 4.

partial class XParser
{
    public override void ReportError(RecognitionException e)
    {
        base.ReportError(e);
        Console.WriteLine("Error in Parser at line " + ":" + e.OffendingToken.Column + e.OffendingToken.Line + e.Message);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是出现的错误

'Parser.ReportError(Antlr4.Runtime.RecognitionException)': no suitable method found to override
Run Code Online (Sandbox Code Playgroud)

在Antlr 4中,累积输入流中发生的错误的预期方式是什么.我无法在网上找到实现这一目标的方法.请给我一些指导.

编辑:

我已经实现了XParser,如下所示

partial class XParser : IAntlrErrorListener<IToken>
{
    public void SyntaxError(IRecognizer recognizer, IToken offendingSymbol, int line, int charPositionInLine, string msg, RecognitionException e)
    {
        Console.WriteLine("Error in parser at line " + ":" + e.OffendingToken.Column + e.OffendingToken.Line + e.Message);
    }
} …
Run Code Online (Sandbox Code Playgroud)

antlr antlr3 c#-4.0 antlr4

6
推荐指数
1
解决办法
3935
查看次数

标签 统计

antlr ×2

antlr3 ×1

antlr4 ×1

c#-4.0 ×1

error-handling ×1

java ×1