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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:27:03+00:00 2026-05-26T02:27:03+00:00

With ANTLR2, you could define something like this in grammar definition file: options {

  • 0

With ANTLR2, you could define something like this in grammar definition file:

options
{
   language = "CSharp";
   namespace = "Extended.Tokens";
}

tokens {
   TOKEN<AST=Extended.Tokens.TokenNode>;
}

And then, you could create a class:

public class TokenNode: antlr.BaseAST
{
    ...
}

Any ideea if something like this can be used (delegate class creation to AST factory instead of me doing the tree replication manually)? It’s not working just by simple grammar definition copy from old to new format, and I tried to search their site and samples for somthing similar. Any hints?

EDIT

I’m not trying to create custom tokens, but custom ‘node parsers’.

In order to ‘execute’ a tree you have 2 choices (as far as I understood):

  1. create a ‘tree visitor’ and handle values, or
  2. create a tree parser by ‘almost-duplicating’ the grammar definition.

In v2 case, I could decorate the tree node to whateveer method I would have liked and then call them after the parser ran by just calling something like ‘execute’ from root node.

  • 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-26T02:27:03+00:00Added an answer on May 26, 2026 at 2:27 am

    I know little C#, but there shouldn’t be much difference with the Java target.

    You can create – and let ANTLR use – a custom tree by setting the ASTLabelType in the options { ... } section (an XTree in this case):

    T.g

    grammar T;
    
    options {
      output=AST;
      ASTLabelType=XTree;
    }
    
    tokens {
      ROOT;
    }
    
    @parser::header {
      package demo;
      import demo.*;
    }
    
    @lexer::header {
      package demo;
      import demo.*;
    }
    
    parse
      :  Any* EOF -> ^(ROOT Any*)
      ;
    
    Any
      :  .
      ;
    

    You then create a custom class which extends a CommonTree:

    demo/XTree.java

    package demo;
    
    import org.antlr.runtime.*;
    import org.antlr.runtime.tree.*;
    
    public class XTree extends CommonTree {
    
      public XTree(Token t) {
        super(t);
      }
    
      public void x() {
        System.out.println("XTree.text=" + super.getText() + ", children=" + super.getChildCount());
      }
    }
    

    and when you create an instance of your TParser, you must create and set a custom TreeAdaptor which creates instances of your XTree:

    demo/Main.java

    package demo;
    
    import org.antlr.runtime.*;
    import org.antlr.runtime.tree.*;
    
    public class Main {
    
      public static void main(String[] args) throws Exception {
        String source = "ABC";
        TLexer lexer = new TLexer(new ANTLRStringStream(source));
        TParser parser = new TParser(new CommonTokenStream(lexer));
        parser.setTreeAdaptor(new CommonTreeAdaptor(){
          @Override
          public Object create(Token t) {
            return new XTree(t);
          }
        }); 
        XTree root = (XTree)parser.parse().getTree();
        root.x();
      }
    }
    

    Running the demo:

    java -cp antlr-3.2.jar org.antlr.Tool T.g -o demo/
    javac -cp antlr-3.2.jar demo/*.java
    java -cp .:antlr-3.2.jar demo.Main
    

    will print:

    XTree.text=ROOT, children=3
    

    For more info, see: http://www.antlr.org/wiki/display/ANTLR3/Tree+construction

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

Sidebar

Related Questions

If I have an ANTLR grammar as follows: grammar Test; options { language =
Given a lexer definition file, a grammar file (say, postgresql .y , .l flex
I have an ANTLR grammar file as part of a C# project file and
In ANTLR version 2.X you could specify something was to go before or after
I would like to write a compiler for a toy-language for Java. I would
Is this possible? Given that C# uses immutable strings, one could expect that there
I am trying realize python like indent-depending grammar. Source example: ABC QWE CDE EFG
So, I'm getting this data. From the network socket, or out of a file.
I have a simple grammar which allows the user to define some objects with
The following grammar works, but also gives a warning: test.g grammar test; options {

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.