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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T08:45:42+00:00 2026-05-21T08:45:42+00:00

I examine heterogeneous trees in ANTLR (using ANTLRWorks 1.4.2). Here is the example of

  • 0

I examine heterogeneous trees in ANTLR (using ANTLRWorks 1.4.2).

Here is the example of what I have already done in ANTLR.

grammar test;

options {
    language = java;
    output = AST;
}

tokens {
    PROGRAM;
    VAR;
}

@members {
    class Program extends CommonTree {
        public Program(int ttype) {
            token = new CommonToken(ttype, "<start>");
        }
    }
}

start
    :    program var function
        // Works fine:
        //->    ^(PROGRAM program var function)

        // Does not work (described below):
        ->    ^(PROGRAM<Program> program var function)
    ;

program
    :    'program'! ID ';'!
    ;

var
    :    TYPE^ ID ';'!
    ;

function
    :    ID '('! ')'! ';'!
    ;

TYPE
    :    'int'
    |    'string'
    ;

ID
    :    ('a'..'z' | 'A'..'Z')+
    ;

WHITESPACE
    :    (' ' | '\t' '\n'| '\r' | '\f')+ {$channel = HIDDEN;}
    ;

Sample input:

program foobar;
int foo;
bar();

When I use rewrite rule ^(PROGRAM<Program> program var function), ANTLR stumbles over and I get AST like this:

AntLR

Whereas when I use this rewrite rule ^(PROGRAM program var function) it works:

enter image description here

  1. Could anyone explain where am I wrong, please? Frankly, I do not really get the idea of heterogeneous trees and how do I use <…> syntax in ANTLR.

  2. What do r0 and r1 mean (first picture)?

  • 1 1 Answer
  • 1 View
  • 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-21T08:45:42+00:00Added an answer on May 21, 2026 at 8:45 am

    I have no idea what these r0 and r1 mean: I don’t use ANTLRWorks for debugging, so can’t comment on that.

    Also, language = java; causes ANTLR 3.2 to produce the error:

    error(10): internal error: no such group file java.stg

    error(20): cannot find code generation templates java.stg
    error(10): internal error: no such group file java.stg

    error(20): cannot find code generation templates java.stg

    ANTLR 3.2 expects it to be language = Java; (capital “J”). But, by default the target is Java, so, mind as well remove the language = ... entirely.

    Now, as to you problem: I cannot reproduce it. As I mentioned, I tested it with ANTLR 3.2, and removed the language = java; part from your grammar, after which everything went as (I) expected.

    Enabling the rewrite rule -> ^(PROGRAM<Program> program var function) produces the following ATS:

    enter image description here

    and when enabling the rewrite rule -> ^(PROGRAM program var function) instead, the following AST is created:

    enter image description here

    I tested both rewrite rules this with the following class:

    import org.antlr.runtime.*;
    import org.antlr.runtime.tree.*;
    import org.antlr.stringtemplate.*;
    
    public class Main {
        public static void main(String[] args) throws Exception {
            ANTLRStringStream in = new ANTLRStringStream("program foobar; int foo; bar();");
            testLexer lexer = new testLexer(in);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            testParser parser = new testParser(tokens);
            testParser.start_return returnValue = parser.start();
            CommonTree tree = (CommonTree)returnValue.getTree();
            DOTTreeGenerator gen = new DOTTreeGenerator();
            StringTemplate st = gen.toDOT(tree);
            System.out.println(st);
        }
    }
    

    And the images are produced using graph.gafol.net (and the output of the Main class, of course).

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

Sidebar

Related Questions

I have a site search (using Examine & Razor) that breaks if a space
I have a CLI/C++ interface that I want to examine via .NET Reflection. Here's
I can examine the optimization using profiler, size of the executable file and time
I am using pdb to examine a script having called run -d in an
I have implemented Examine in an Umbraco project and have created an index of
I am using OpenCover (and ReportGenerator) to examine my code coverage as part of
I am using gdb to examine a program. In assembly, the code is doing:
Is there a better way to examine whether two string arrays have the same
I examine the parameters of a C# method using reflection. The method has some
I want to examine exactly how my code operates when using other libraries to

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.