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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:51:06+00:00 2026-06-07T11:51:06+00:00

ANTLR parsers produce the following generated code: protected TreeAdaptor adaptor = new CommonTreeAdaptor(); I’ve

  • 0

ANTLR parsers produce the following generated code:

protected TreeAdaptor adaptor = new CommonTreeAdaptor();

I’ve implemented my own TreeAdaptor, and I want the parser to always use mine and never use CommonTreeAdaptor(). The only method I’ve found is to set it inside the calling code every time I create a new parser:

Parser parser = new MyParser();
parser.setTreeAdaptor(new MyAdaptor());

Is there some way I can set the default TreeAdaptor or append some initialization code to the generated constructor?

  • 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-07T11:51:08+00:00Added an answer on June 7, 2026 at 11:51 am

    I think I’ve found a better solution than Bart’s named-constructor method.

    I’m not able to modify the generated constructor directly (including to make it private, which is why I dislike the named-constructor solution). However, I can introduce an initialization block:

    grammar T;
    
    options {
      output=AST;
      ASTLabelType=CommonTree;
    }
    
    @parser::members {
        {
            this.adaptor = new MyAdaptor();
        }
    }
    

    According to the Java documentation, “The Java compiler copies initializer blocks into every constructor. Therefore, this approach can be used to share a block of code between multiple constructors.”

    So this.adaptor will first be set to new CommonTreeAdaptor() by the generated code, but will then be set to new MyAdaptor() by my initialization block. Testing confirms that this is what actually happens.

    The advantage of this solution is that callers of my parser don’t need to be aware that it’s using a custom TreeAdaptor or doing anything out of the ordinary (my adaptor is a subclass of CommonTreeAdaptor, so any code dependent on that is fine).

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

Sidebar

Related Questions

I use the following Java-code to instantiate a parser generated with ANTLR. package foo;
I'm using ANTLR V3 to produce C# code for DSL language. The produced code
I could see bunch of java parsers like OpenCSV, antlr, jsapar etc, but I
By default, a parser generated using ANTLR-3 extends from org.antlr.runtime.Parser. How do I have
i have an problem with antlr. I have the following simple grammar: grammar bxk;
I have an antlr generated Java parser that uses the C target and it
I want to parser a simple matlab-like for-loop, using ANTLR. The loop is like
I am new to parser generators and I am wondering how the ANTLR grammar
I am using ANTLR 3.1.3 to generate the parser. After importing the generated testParser,
I have lexer/parser (Generated from an ANTLR grammar file) which (for performance reasons) I

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.