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 9008857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:02:45+00:00 2026-06-16T02:02:45+00:00

I can load a grammar file in to my java project (in netbeans) and

  • 0

I can load a grammar file in to my java project (in netbeans) and generate/build my lexer and parser classes, load them via reflection and then parse what I need to parse.

What I would like is a simple example of redirecting the results from the generate/build of the lexer and parser to a string so that I can put the errors to a popup like JOptionPane.
Just like ANTLRworks does when you do CTRL-R (in windows at least) showing Error(123) grammar.g:100:12: …Warning…etc…

Now I am already using a DiagnosticCollector but this does not always enough details as the CTRL-r in ANTLRworks does. The idea being the user corrects the grammar in order to correct the mistake.

Here’s a snippet of my code

            Tool tool = new Tool(new String[]{userDir + grammarPath + grammarName + ".g"});
            //tool.setOutputDirectory(userDir + grammarPath );
            tool.process();
            if(tool.getNumErrors()>0) {
                errors=true;
                unableToLoadGrammar_AddLine("1. Detected "+tool.getNumErrors()+" Error(s)"+tool.toString()+"\n");
            }

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-06-16T02:02:46+00:00Added an answer on June 16, 2026 at 2:02 am

    Call org.antlr.tool.ErrorManager‘s setErrorListener method:

    public class ToolTest {
        public static void main(String[] args) {
    
    
            org.antlr.Tool tool = new org.antlr.Tool(new String[] { "GrammarWithErrors.g" });
    
            final ArrayList<String> messages = new ArrayList<String>();
    
            ErrorManager.setErrorListener(new ANTLRErrorListener() {
    
                @Override
                public void warning(Message msg) {
                    messages.add("Warning: " + msg.toString());
                }
    
                @Override
                public void info(String msg) {
                    messages.add("Info: " + msg.toString());
                }
    
                @Override
                public void error(ToolMessage msg) {
                    messages.add("Tool Error: " + msg.toString());
                }
    
                @Override
                public void error(Message msg) {
                    messages.add("General Error: " + msg.toString());
                }
            });
    
            tool.process();
    
            System.out.println("Done processing...");
    
            for (String msg : messages) {
                System.out.println(msg);
            }
        }
    }
    

    Passing a bad grammar to the code above produces the following output:

    Done processing...
    General Error: error(100): GrammarWithErrors.g:1:14: syntax error: antlr: MismatchedTokenException(74!=52)
    Tool Error: error(150):  grammar file GrammarWithErrors.g has no rules
    General Error: error(100): GrammarWithErrors.g:82:7: syntax error: assign.types: NoViableAltException(0@[])
    

    Note that Tool still operates directly on System.err and System.out. If you want to handle everything coming out of Tool, you’ll have to call System.setErr and System.setOut as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know we can load a Object from .class file and my question is
I have a web application that can load plugins through reflection. It currently uses
I can load the fixture file in my django application by using loaddata: manage.py
I understand that Java can load/execute DLL code, but I'm wondering if there are
In Java, you can load all kinds of resources using the same API but
I can load a matrix from text file: load mydata.txt The problem is my
the php gd imagettftext function can only load true type fonts from a file.
I know that we can load classes dynamically by using custom class loaders. But
SHORT VERSION: How can I load classes during runtime from a bundle, when those
I need to load an xml file as String in android so I can

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.