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

  • Home
  • SEARCH
  • 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 6081317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:09:42+00:00 2026-05-23T11:09:42+00:00

I’m trying to learn to use ANTLR, but I cannot figure out what’s wrong

  • 0

I’m trying to learn to use ANTLR, but I cannot figure out what’s wrong with my code in this case. I hope this will be really easy for anyone with some experience with it. This is the grammar (really short).

grammar SmallTest;

@header {
package parseTest;
import java.util.ArrayList;
}

prog returns [ArrayList<ArrayList<String>> all]
    :(stat { if ($all == null)
               $all = new ArrayList<ArrayList<String>>();
             $all.add($stat.res);
           } )+
    ;

stat returns [ArrayList<String> res]
    :(element  { if ($res == null)
                   $res = new ArrayList<String>();
                 $res.add($element.text);
               } )+ NEWLINE
    |   NEWLINE
    ;

element: ('a'..'z'|'A'..'Z')+ ;
NEWLINE:'\r'? '\n' ;

The problem is that when I generate the Java code there are some empty if conditions, and the compiler displays an error because of that, I could edit that manually, but that would probably be much worse. I guess something is wrong in this.

Sorry for asking, this has to be really stupid, but my example is so similar to those in the site that I cannot imagine a way to atomize the differences any more.

Thank you very much.

  • 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-23T11:09:43+00:00Added an answer on May 23, 2026 at 11:09 am

    You should put the initialization of your lists inside the @init { ... } block of the rules, which get executed before anything in the rule is matched.

    Also, your element rule should not be a parser rule, but a lexer rule instead (it should start with a capital!).

    And the entry point of your parser, the prog rule, should end with the EOF token otherwise the parser might stop before all tokens are handled properly.

    Finally, the @header { ... } section only applies to the parser (it is a short-hand for @parser::header { ... }), you need to add the package declaration to the lexer as well.

    A working demo:

    SmallTest.g

    grammar SmallTest;
    
    @header {
    package parseTest;
    import java.util.ArrayList;
    }
    
    @lexer::header {
    package parseTest;
    }
    
    prog returns [ArrayList<ArrayList<String>> all]
    @init {$all = new ArrayList<ArrayList<String>>();}
      :  (stat {$all.add($stat.res);})+ EOF
      ;
    
    stat returns [ArrayList<String> res]
    @init {$res = new ArrayList<String>();}
      :  (ELEMENT {$res.add($ELEMENT.text);})* NEWLINE
      ;
    
    ELEMENT : ('a'..'z'|'A'..'Z')+ ;
    NEWLINE : '\r'? '\n' ;
    SPACE   : ' ' {skip();};
    

    Main.java

    package parseTest;
    
    import org.antlr.runtime.*;
    
    public class Main {
      public static void main(String[] args) throws Exception {
        SmallTestLexer lexer = new SmallTestLexer(new ANTLRStringStream("a bb ccc\ndddd eeeee\n"));
        SmallTestParser parser = new SmallTestParser(new CommonTokenStream(lexer));
        System.out.println(parser.prog());
      }
    }
    

    And to run it all, do:

    java -cp antlr-3.3.jar org.antlr.Tool parseTest/SmallTest.g 
    javac -cp .:antlr-3.3.jar parseTest/*.java
    java -cp .:antlr-3.3.jar parseTest.Main

    which yields:

    [[a, bb, ccc], [dddd, eeeee]]
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.