简体   繁体   中英

Print in file the lexical errors ANTLR intelliJ

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM