在文件中打印词法错误 ANTLR intelliJ

发布于 2025-01-15 10:00:12 字数 2504 浏览 1 评论 0原文

我尝试在文件中打印来自简单输入的词汇错误,我已经定义了我的语法并且一切正常,我的主要是这样的:

public class Main {
    public static void main(String[] args) {
        CharStream charStreamES1 = CharStreams.fromString("{ int ]a; int b; àint c = 1 ; if (c > 1) { b = c  } else { a = b ; }}");
        SimpLanPlusLexer simpleLexer = new SimpLanPlusLexer(charStreamES1);
        SimpLanPlusParser simpleparser = new SimpLanPlusParser(new CommonTokenStream(simpleLexer));
        simpleLexer.removeErrorListeners();
        SyntaxErrorListener act = new SyntaxErrorListener();
        RecognitionException a = null;
        act.syntaxError(simpleparser,0,0,0,"msg",a);
    }
}

在 CharStream 中存在不同的错误,例如“]”或“à” 我已经重写了像这样的syntaxError函数:

public class SyntaxErrorListener extends BaseErrorListener {
    private final List<SyntaxError> SyntaxErrors = new ArrayList();

    public SyntaxErrorListener() {
    }

    List<SyntaxError> getSyntaxErrors() {
        return this.SyntaxErrors;
    }

    public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
        this.SyntaxErrors.add(new SyntaxError(recognizer, offendingSymbol, line, charPositionInLine, msg, e));

        try {
            PrintWriter out = new PrintWriter("Myerrors.txt");
            Iterator var8 = this.getSyntaxErrors().iterator();

            while(var8.hasNext()) {
                SyntaxError i = (SyntaxError)var8.next();
                out.println("Error: " + i.getMessage());
                System.out.println(i.getMessage());
            }

            out.close();
        } catch (FileNotFoundException var10) {
            e.printStackTrace();
        }

    }
}

这是我的SyntaxError类:

public class SyntaxError {
    private final Recognizer<?, ?> recognizer;
    private final Object offendingSymbol;
    private final int line;
    private final int charPositionInLine;
    private final String msg;
    private final RecognitionException e;

    public SyntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
        this.recognizer = recognizer;
        this.offendingSymbol = offendingSymbol;
        this.line = line;
        this.charPositionInLine = charPositionInLine;
        this.msg = msg;
        this.e = e;
    }

在此之下我已经定义了我在这个问题中没有编写的参数的get函数,有人可以帮助我吗?

I tryed to print in a file the lexical errors from a simple input, I already define my grammar and all is ok, my main is this:

public class Main {
    public static void main(String[] args) {
        CharStream charStreamES1 = CharStreams.fromString("{ int ]a; int b; àint c = 1 ; if (c > 1) { b = c  } else { a = b ; }}");
        SimpLanPlusLexer simpleLexer = new SimpLanPlusLexer(charStreamES1);
        SimpLanPlusParser simpleparser = new SimpLanPlusParser(new CommonTokenStream(simpleLexer));
        simpleLexer.removeErrorListeners();
        SyntaxErrorListener act = new SyntaxErrorListener();
        RecognitionException a = null;
        act.syntaxError(simpleparser,0,0,0,"msg",a);
    }
}

In the CharStream there are different error like "]" or "à"
I had Override the syntaxError function like this:

public class SyntaxErrorListener extends BaseErrorListener {
    private final List<SyntaxError> SyntaxErrors = new ArrayList();

    public SyntaxErrorListener() {
    }

    List<SyntaxError> getSyntaxErrors() {
        return this.SyntaxErrors;
    }

    public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
        this.SyntaxErrors.add(new SyntaxError(recognizer, offendingSymbol, line, charPositionInLine, msg, e));

        try {
            PrintWriter out = new PrintWriter("Myerrors.txt");
            Iterator var8 = this.getSyntaxErrors().iterator();

            while(var8.hasNext()) {
                SyntaxError i = (SyntaxError)var8.next();
                out.println("Error: " + i.getMessage());
                System.out.println(i.getMessage());
            }

            out.close();
        } catch (FileNotFoundException var10) {
            e.printStackTrace();
        }

    }
}

And this is my SyntaxError class:

public class SyntaxError {
    private final Recognizer<?, ?> recognizer;
    private final Object offendingSymbol;
    private final int line;
    private final int charPositionInLine;
    private final String msg;
    private final RecognitionException e;

    public SyntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
        this.recognizer = recognizer;
        this.offendingSymbol = offendingSymbol;
        this.line = line;
        this.charPositionInLine = charPositionInLine;
        this.msg = msg;
        this.e = e;
    }

under this I had already defined the get function of my parameters that I did't rite in this questio, can anybody help me pls?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

堇年纸鸢 2025-01-22 10:00:12

您已使用 simpleLexer.removeErrorListeners(); 删除默认错误侦听器,但尚未将错误侦听器(act) 添加到 simpleLexer代码>.

此外,simpleLexer 上的侦听器只会拾取令牌化错误(无效令牌)。您还需要将侦听器添加到解析器中。

顺便说一句,词法分析器和解析器都可以有多个侦听器,因此没有必要删除默认侦听器。如果您想删除它们以清理 System.out,您可能需要保持它们连接,直到您验证您的侦听器已连接并正常运行,因为它们可能会提供一些见解。

You’ve used simpleLexer.removeErrorListeners(); to remove the default error listener, but you have not added your error listener(act) to the simpleLexer.

Also, a listener on simpleLexer will only pick up Tokenization errors (invalid tokens). You’ll also want to add you listener to your parser.

BTW, both Lexers and Parsers can have multiple listeners, so it’s not necessary to remove the default listeners. If you want to remove them to clean up the System.out, you may want to keep them connected until you have verified that you’re listeners are hooked up and functioning correctly, as they may give some insight.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文