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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:38:01+00:00 2026-05-13T05:38:01+00:00

I’m trying to use Antlr for some text IDE-like functions — specifically parsing a

  • 0

I’m trying to use Antlr for some text IDE-like functions — specifically parsing a file to identify the points for code folding, and for applying syntax highlighting.

First question – is Antlr suitable for this requirement, or is it overkill? This could be achieved using regex and/or a hand-rolled parser … but it seems that Antlr is out there to do this work for me.

I’ve had a look through the … and the excellent tutorial resource here.

I’ve managed to get a Java grammar built (using the standard grammar), and get everything parsed neatly into a tree. However, I’d have expected to see elements nested within the tree. In actual fact, everything is a child of the very top element.

Eg. Given:

package com.example
public class Foo {
   String myString = "Hello World"
   // etc
}

I’d have expected the tree node for Foo to be a child of the node for the package declaration. Likewise, myString would be a child of Foo.

Instead, I’m finding that Foo and myString (and everything else for that matter) are all children of package.

Here’s the relevant excerpt doing the parsing:

public void init() throws Exception {
    CharStream c = new ANTLRFileStream(
            "src/com/inversion/parser/antlr/Test.code");

    Lexer lexer = new JavaLexer(c);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    JavaParser parser = new JavaParser(tokens);
    parser.setTreeAdaptor(adaptor);

    compilationUnit_return result = parser.compilationUnit();
}

static final TreeAdaptor adaptor = new CommonTreeAdaptor() {
    public Object create(Token payload) {
        if (payload != null)
        {
            System.out.println("Create " + JavaParser.tokenNames[payload.getType()] + ":  L" + payload.getLine() + ":C" + payload.getCharPositionInLine() + " " + payload.getText());
        }
        return new CommonTree(payload);
    }
};

Examining result.getTree() returns a CommonTree instance, whose children are the result of the parsing.

Expected value (perhaps incorrectly)

package com.example (4 tokens)
   |
   +-- public class Foo (3 tokens)
        |
        +--- String myString = "Hello World" (4 tokens)
        +--- Comment "// etc"

(or something similar)

Actual value (All values are children of the root node of result.getTree() )

package
com
.
example
public
class
Foo
String
myString
=
"Hello World"

Is my understanding of how this should be working correct?

I’m a complete noob at Antlr so far, and I’m finding the learning curve quite steep.

  • 1 1 Answer
  • 2 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-13T05:38:01+00:00Added an answer on May 13, 2026 at 5:38 am

    The Java-6 grammar at the top of the file sharing section on antlr.org does not include tree building. You’ll need to do two things. First, tell ANTLR you want to build an AST:

    options {
        output=AST;
    }
    

    Second, you need to tell it what the tree should look like by either using the tree operators or by using the rewrite rules. See the documentation on tree construction. I usually end up doing a combination of both.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to render a haml file in a javascript response like so:
I am trying to understand how to use SyndicationItem to display feed which is
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a reasonable size flat file database of text documents mostly saved in
I'm parsing an XML file, the creators of it stuck in a bunch social

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.