Catching token recognition error in ANTLRErrorListener

5,036 views
Skip to first unread message

Murilo Dell' Agnolo Garcia

unread,
Sep 9, 2014, 11:53:47 AM9/9/14
to antlr-di...@googlegroups.com
Hi,

I am trying to do an compiler using ANTLR v4.
To do it, I need to catch and treat "token recognition error" (the message in the console is: "token recognition error at:".
All the other errors at catched in the implementation of ANTLRErrorListener, but "token recognition error" does not call the "syntaxError" function.
How should I catch the this error?

Thanks,

Murilo

David Whitten

unread,
Sep 9, 2014, 5:05:57 PM9/9/14
to antlr-di...@googlegroups.com
What Programming Language are you generating from ANTLR 4 ?

Since the "token recognition error" shows up at run time, you need to tell the group which run time system you are using.

David Whitten
713-870-3834

--
You received this message because you are subscribed to the Google Groups "antlr-discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to antlr-discussi...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Murilo Dell' Agnolo Garcia

unread,
Sep 9, 2014, 5:09:25 PM9/9/14
to antlr-di...@googlegroups.com, whi...@netcom.com
I am using Java.

Murilo Dell' Agnolo Garcia

unread,
Sep 10, 2014, 10:03:26 AM9/10/14
to antlr-di...@googlegroups.com, whi...@netcom.com
I fixed the problem.

I add in my grammar:

ERROR
    : . { this.notifyListeners(new LexerNoViableAltException(this, _input, E, null)); }
    ;

And handled my lexer errors in there.

Jim Idle

unread,
Sep 10, 2014, 9:56:05 PM9/10/14
to antlr-di...@googlegroups.com, whi...@netcom.com
That will work, although if you have this catch all rule, then you are usually going to have a more specific error message. 

However, is the real issue not that you have added an error listener to your parser, but not to your lexer? You need to call removeErrorListeners(), then addErrorListener(myListener) on the lexer. Here is a code excerpt:

        // Our listener listens for syntax and lexer errors and records them so that
        // they can be recorded and listed once the compile is stopped
        //
        final VicaraErrorListener errListener = new VicaraErrorListener();
...

        // The lexer instance, connected to the input stream
        //
        final VicaraLexer lexer = new VicaraLexer(input);

        // Add the error listener to the lexer (this raises the errors for the lexer)
        //
        lexer.removeErrorListeners();
        lexer.addErrorListener(errListener);

        // Create the token stream for the parser to pull from
        //
        final CommonTokenStream tokens = new CommonTokenStream(lexer);

        // And now we instantiate a parser, giving it the token stream that is ready
        // to parse 
        //
        final VicaraParser parser = new VicaraParser(tokens);

        // Install our Error Strategy, as it is this that will raise actual errors for us in the Vicara messaging system
        //
        final VicaraErrorStrategy es = new VicaraErrorStrategy();
...
        parser.setErrorHandler(es);

        // Final step is to add the error message listener, which does not raise errors, it just detects
        // that we somehow did not intercept an error that we should have done and records it as a bug.
        //
        parser.removeErrorListeners();
        parser.addErrorListener(errListener);

Jim

Murilo Dell' Agnolo Garcia

unread,
Sep 11, 2014, 8:36:45 AM9/11/14
to antlr-di...@googlegroups.com, whi...@netcom.com
You are right Jim.

It is not necessary to add the ERROR rule. Simply adding the ErrorListener to the Lexer fixes the problem.

Thank you.

Jim Idle

unread,
Sep 11, 2014, 9:49:47 PM9/11/14
to antlr-di...@googlegroups.com, whi...@netcom.com
No problem.
Reply all
Reply to author
Forward
0 new messages