Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6830309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:36:09+00:00 2026-05-26T22:36:09+00:00

I am using antlr to generate my parser, but I want to override some

  • 0

I am using antlr to generate my parser, but I want to override some of the error reporting. At the moment if I give some incorrect syntax, for example a missing token, antlr gives the error “line 1:11 missing TYPE at ‘.'”

However I can’t find in which method this error is outputted. It is not, as I originally thought, in the reportError() method. Does anyone know where the message is generated?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:36:09+00:00Added an answer on May 26, 2026 at 10:36 pm

    A MissingTokenException does pass through reportError(...). Let’s say you would like to parse assignments using the grammar below:

    grammar T;
    
    parse      : assignment EOF;
    assignment : Id '=' Number ';';
    Number     : '0'..'9'+ ('.' '0'..'9'+)?;
    Id         : ('a'..'z' | 'A'..'Z')+;
    Space      : ' ' {skip();};
    

    Now simply override the reportError(...) method like this:

    grammar T;
    
    @parser::members {
      @Override
      public void reportError(RecognitionException e) {
        System.out.println("CUSTOM ERROR...\n" + e);
      }
    }
    
    parse      : assignment EOF;
    assignment : Id '=' Number ';';
    Number     : '0'..'9'+ ('.' '0'..'9'+)?;
    Id         : ('a'..'z' | 'A'..'Z')+;
    Space      : ' ' {skip();};
    

    and then try to parse, say, "= 123;" (a missing Id):

    java -cp antlr-3.3.jar org.antlr.Tool T.g
    javac -cp antlr-3.3.jar *.java
    java -cp .:antlr-3.3.jar Main "= 123;"
    
    CUSTOM ERROR...
    MissingTokenException(inserted [@-1,0:0='<missing Id>',<4>,1:0] at =)
    

    As you can see, the custom error message is being printed to the console.

    EDIT

    And a warning like “no viable alternative …” is a problem in the lexer, not the parser. This happens when the lexer encounters a character that you did not account for in your grammar (or not as a proper token, at least).

    Let’s say you parse the input a = 123: (note the : at the end instead of a ;). The lexer will now produce a “no viable alternative …” warning because I didn’t define any token for that :.

    An easy solution to account for such mistakes is to add a “catch-all” rule at the end of your lexer grammar that will match any character that is not matched by any lexer rule before it. Whenever such a “catch-all” rule matches, you simply throw an exception (or do something else, of course!) in the @after{...} block of that rule.

    Here’s a demo:

    grammar T;
    
    @parser::members {
      @Override
      public void reportError(RecognitionException e) {
        System.out.println("CUSTOM ERROR...\n" + e);
      }
    }
    
    parse      : assignment EOF;
    assignment : Id '=' Number ';';
    Number     : '0'..'9'+ ('.' '0'..'9'+)?;
    Id         : ('a'..'z' | 'A'..'Z')+;
    Space      : ' ' {skip();};
    
    FallThrough
    @after{
      throw new RuntimeException(String.format(
          "Encountered an illegal char on line \%d, column \%d: '\%s'", 
          getLine(), getCharPositionInLine(), getText()
        )
      );
    }
      :  . // match any char not matched by Number, Id or Space
      ;
    

    If you now parse a = 123:, you will see the following on your console:

    Exception in thread "main" java.lang.RuntimeException: 
        Encountered an illegal char on line 1, column 8: ':'
        ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using ANTLR 3.1.3 to generate the parser. After importing the generated testParser,
I am using ANTLRWorks 1.4.3 together with ANTLR 3.4 to generate a Java-based parser
By default, a parser generated using ANTLR-3 extends from org.antlr.runtime.Parser. How do I have
I want to replace a token using ANTLR. I tried with TokenRewriteStream and replace,
I'm using StringTemplate to generate some xml files from datasets. Sometimes I have more
I'm using ANTLR3 with the C runtime. I'd like to do some custom error
I am using ANTLR to generate Java source files. I can make Eclipse understand
I am creating a parser using ANTLR 3.x that targets java. I have written
I'm using ANTLR 3 to create an AST. I want to have two AST
I am using ANTLR to create an and/or parser+evaluator. Expressions will have the format

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.