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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:31:56+00:00 2026-05-30T07:31:56+00:00

Hi I want to do a java or c# method which will return a

  • 0

Hi I want to do a java or c# method which will return a boolean expression true or false based on the given input. For example:

if (input matches the antlr grammar)
return true;
else
return false;
So the problem is i dont know how to check if the commontree have any mismatchetoken. I tried to traverse the tree but it doesnt give any mismatchetoken as node of the tree. Probably the problem is that the AST doesnt show the mismatched tokens only the parse tree. It could be help also if someone tell me how get the parsetree from parser?

I have done the ANTLR .g file and it works good, now I need to do the following: i have to check if the input was correct or not, I have done this but it doesnt work:

 public static boolean check() {
    String file = "test.txt";
    ANTLRReaderStream input;
    try{

            input = new ANTLRReaderStream(new FileReader(file));
            regExLexer lexer = new regExLexer(input);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            regExParser parser = new regExParser(tokens);

            CommonTree root = (CommonTree)parser.goal().getTree();

        return true;}
catch{
return false;}
}

So i expect from my method to return true only when the input string is correct, and false otherwise. The way I have done it always returns true, but when the string is not correct it prints

“line 1:4 extraneous input ‘+’ expecting EOF”

in console.

  • 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-30T07:31:57+00:00Added an answer on May 30, 2026 at 7:31 am

    As I hinted in the comments under your question: this previous Q&A answers your question. I’ll demonstrate with an example.

    Let’s say you have a grammar that accepts numbers (and ignores spaces). You only need to override both the parser’s- and the lexer’s reportError and throw an exception in that method: that way the parser (or lexer) will not continue. Catch the error and simply return false in a static helper method.

    grammar T;
    
    @parser::members {
    
      public static boolean matches(String input) {
        try {
          TLexer lexer = new TLexer(new ANTLRStringStream(input));
          TParser parser = new TParser(new CommonTokenStream(lexer));
          parser.parse();
          return true;
        } catch(Exception e) {
          return false;
        }
      }
    
      @Override
      public void reportError(RecognitionException e) {
        throw new RuntimeException(e); 
      }
    }
    
    @lexer::members {
      @Override
      public void reportError(RecognitionException e) {
        throw new RuntimeException(e); 
      }
    }
    
    parse
     : NUMBER* EOF
     ;
    
    NUMBER : '0'..'9'+;
    SPACE  : ' ' {skip();};
    

    Now running the class:

    import org.antlr.runtime.*;
    
    public class Main {
      public static void main(String[] args) throws Exception {
        System.out.println(TParser.matches(""));
        System.out.println(TParser.matches("1 234 42"));
        System.out.println(TParser.matches("12 556 f"));
      }
    }
    

    will print:

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

Sidebar

Related Questions

How can I write DAO method which will return as a result only first
I want to get a regex which will match a tag in a java
I'm trying to declare an interface that contains a method which will return a
I want to replace the first context of web/style/clients.html with the java String.replaceFirst method
I want to have a java app running, using a function/method (with as little
I'm using Java. I want to have a setter method of one class that
I am trying to create a Response class in Java which has a method
I want to implement an intialization mechanism that is annotation-based in Java. Specifically, I
I'm designing a Java application using SWT. I have some tables which will display
How do I write a static method in Java that will take a List,

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.