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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:09:54+00:00 2026-05-26T04:09:54+00:00

I am trying to create a heterogeneous tree based on a sample provided here:

  • 0

I am trying to create a heterogeneous tree based on a sample provided here: http://www.antlr.org/wiki/display/ANTLR3/Tree+construction#Treeconstruction-Heterogeneoustreenodes

I have created a grammar file as follows:

grammar T; 

options { 
  language=CSharp3; 
  ASTLabelType=CommonTree;
  output=AST; 
  TokenLabelType=CommonToken;
  k=3;
} 

tokens { 
  ROOT; 
  UNARY_MIN; 
} 

@lexer::header 
{
  using System;
  using System.Text;
  using System.Collections;
  using System.Collections.Generic;
  using ANTLRSandbox.Criteria;
}

@parser::header 
{
  using System;
  using System.Text;
  using System.Collections;
  using System.Collections.Generic;
  using ANTLRSandbox.Criteria;
}


@parser::namespace { ANTLRSandbox } 
@lexer::namespace { ANTLRSandbox } 


public
parse 
  :  exp EOF -> ^(ROOT<RootNode> exp) 
  ; 

exp 
  :  addExp 
  ; 

addExp 
  :  mulExp (('+'<PlusNode> | '-'<MinusNode>)^ mulExp)* 
  ; 

mulExp 
  :  unaryExp (('*' | '/')^ unaryExp)* 
  ; 

unaryExp 
  :  '-' atom -> ^(UNARY_MIN atom) 
  |  atom 
  ; 

atom 
  :  Number 
  |  '(' exp ')' -> exp 
  ; 

Number 
  :  ('0'..'9')+ ('.' ('0'..'9')+)? 
  ; 

Space  
  :  (' ' | '\t' | '\r' | '\n'){Skip();} 
  ; 

And the node classes looks like this:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Antlr.Runtime;
using Antlr.Runtime.Tree;

namespace ANTLRSandbox.Criteria
{
  public class RootNode : CommonTree
  {
    public RootNode(int ttype) { }
    public RootNode(int ttype, IToken t) { }
    public RootNode(IToken t) { }
  }
}

Classes PlusNode and MinusNode are identical with RootNode, so I won’t post them here.

And here is how I create the actual tree:

    string s = "(12.5 + 56 / -7) * 0.5";

    ANTLRStringStream Input = new ANTLRStringStream(s);
    TLexer Lexer = new TLexer(Input);
    CommonTokenStream Tokens = new CommonTokenStream(Lexer);
    TParser Parser = new TParser(Tokens);

    TParser.parse_return ParseReturn = Parser.parse();
    CommonTree Tree = (CommonTree)ParseReturn.Tree;

The code runs without any error, but when I ‘watch’ for Tree object, all its nodes are CommonTree type and all breakpoints I have placed in PlusNode, MinusNode, RootNode constructors are missed.

I have followend the sample provided in ANTLR3 wiki page and I couldn’t find any sample on the web. I know they intend to drop this approach at some point (found this on ANTLR3 preview notes) but this implementation suits me better (I need to create different objects types based on grammar context).

So … any hints? Am I missing something? Some option/flag to put it into grammar definition file?

Thanks!
D.

  • 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-26T04:09:54+00:00Added an answer on May 26, 2026 at 4:09 am

    I’ve never had much luck getting these operators < ... > to work when using inline tree operators (^ for roots and ! for omitting rules). All I can recommend is you use rewrite rules (the ... -> ^(...)) to the right of your parser rules and then only define the custom node, <NodeName>, in the rewrite rule, not on both sides (!) as the Wiki mentioned: I suspect the Wiki info is a bit outdated. I know that such expression-rules are far more readable using the inline operators than with rewrite rules…

    I’m not too fluent in C#, so here’s a Java demo:

    T.g

    grammar T; 
    
    options { 
      ASTLabelType=CommonTree;
      output=AST; 
    } 
    
    tokens {
      ROOT;
      UNARY_MIN;
    }
    
    @members {
    
      public static class RootNode extends CommonTree {
        public RootNode(Token t) { token=t; }
        public RootNode(int ttype) { super(new CommonToken(ttype, "ROOT")); }
        public RootNode(RootNode node) { super(node); }
        public Tree dupNode() { return new RootNode(this); } 
        public String toString() { return "RootNode=" + token.getText(); }
      }
    
      public static class MinusNode extends CommonTree {
        public MinusNode(Token t) { token=t; }
        public MinusNode(MinusNode node) { super(node); }
        public Tree dupNode() { return new MinusNode(this); } 
        public String toString() { return "MinusNode=" + token.getText(); }
      }
    
      public class PlusNode extends CommonTree {
        public PlusNode(Token t) { token=t; }
        public PlusNode(PlusNode node) { super(node); }
        public Tree dupNode() { return new PlusNode(this); } 
        public String toString() { return "PlusNode=" + token.getText(); }
      }
    }
    
    parse 
      :  exp EOF -> ^(ROOT<RootNode> exp)
      ; 
    
    exp 
      :  addExp 
      ; 
    
    addExp 
      :  (mulExp -> mulExp) ( '+' m=mulExp -> ^('+'<PlusNode>  $m $addExp)
                            | '-' m=mulExp -> ^('-'<MinusNode> $m $addExp)
                            )* 
      ; 
    
    mulExp 
      :  unaryExp (('*' | '/')^ unaryExp)* 
      ; 
    
    unaryExp 
      :  '-' atom -> ^(UNARY_MIN atom) 
      |  atom 
      ; 
    
    atom 
      :  Number 
      |  '(' exp ')' -> exp 
      ; 
    
    Number 
      :  ('0'..'9')+ ('.' ('0'..'9')+)? 
      ; 
    
    Space  
      :  (' ' | '\t' | '\r' | '\n') {skip();} 
      ; 
    

    Main.java

    import org.antlr.runtime.*;
    import org.antlr.runtime.tree.*;
    import org.antlr.stringtemplate.*;
    
    public class Main {
    
      private static void traverse(CommonTree tree, int indent) {
        if(tree == null) return;
        for(int i = 0; i < indent; i++) System.out.print("  ");
        System.out.println(tree.getClass().getName() + " -> " + tree.getText());
        for(int i = 0; i < tree.getChildCount(); i++) {
          traverse((CommonTree)tree.getChild(i), indent + 1);
        }
      }
    
      public static void main(String[] args) throws Exception {
        TLexer lexer = new TLexer(new ANTLRStringStream("1 + 2 - 3"));
        TParser parser = new TParser(new CommonTokenStream(lexer));
        CommonTree tree = (CommonTree)parser.parse().getTree();
        traverse(tree, 0);
      }
    }
    

    Running the demo:

    java -cp antlr-3.3.jar org.antlr.Tool T.g 
    javac -cp antlr-3.3.jar *.java
    java -cp .:antlr-3.3.jar Main
    

    will print:

    TParser$RootNode -> ROOT
      TParser$MinusNode -> -
        org.antlr.runtime.tree.CommonTree -> 3
        TParser$PlusNode -> +
          org.antlr.runtime.tree.CommonTree -> 2
          org.antlr.runtime.tree.CommonTree -> 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok so I am trying create a login script, here I am using PHP5
I'm trying create a ASMX webservice that can perform a HTTP GET request. I
Im trying to create a simple dll file.Im following the tutorial http://java.sun.com/docs/books/jni/html/start.html when i
I'm trying create a small http proxy service. This is not working so well.
I am trying create a Regex-based replacement for an article to automatically convert embedded
I am trying create a WCF service that leverages the WPF MediaPlayer on the
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
I am trying create a delegate representation of constructor by emitting a Dynamic Method,
Trying to create a QtRuby application, I get the following error: /usr/lib64/ruby/site_ruby/1.8/Qt/qtruby4.rb:2144: [BUG] Segmentation
Trying to create a user account in a test. But getting a Object reference

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.