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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:19:12+00:00 2026-05-17T02:19:12+00:00

Is the following even possible? I want to reverse the input given to antlr

  • 0

Is the following even possible? I want to “reverse” the input given to antlr and make each token a child of the previous one.

So, for the input (Assume each token is separated by the ‘.’ char) :

Stack.Overflow.Horse

I would like my grammar to produce the following AST:

Horse
  |---Overflow
         |---Stack

So far, I’ve managed to reverse the nodes, but I’m unable to make them children of each other:

function
 : ID PERIOD function
   -> function ID
 | ID
 ;

ID  : 'a'..'z'*
    ;
  • 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-17T02:19:12+00:00Added an answer on May 17, 2026 at 2:19 am

    I don’t think there’s an easy way to do that. You could make your rule like this:

    function
     : ID PERIOD function
       -> ^(function ID)
     | ID
     ;
    

    but that only makes the last node the root and all other nodes its children. For example, the following source:

    a.b.c.d.e
    

    will result in the following tree:

        e
     / / \ \
    d c   b a
    

    I can’t see an easy fix since when you first parse a.b.c.d.e, a will be the ID and b.c.d.e the recursive call to function:

    a.b.c.d.e
    | +-----+
    |    |
    |    `-----> function
    |
    `----------> ID
    

    resulting in the fact that b.c.d.e will have a as its child. When then b becomes the ID, it too is added as a child next to a. In your case, a should be removed as a child and then added to the list of b‘s children. But AFAIK, that is not possible in ANLTR (at least, not in a clean way inside the grammar).


    EDIT

    Okay, as a work-around I had something elegant in mind, but that didn’t work as I had hoped. So, as a less elegant solution, you could match the last node as the root in your rewrite rule:

    function
      :  (id '.')* last=id -> ^($last)
      ;
    

    and then collect all possible preceding nodes (children) in a List using the += operator:

    function
      :  (children+=id '.')* last=id -> ^($last)
      ;
    

    and use a custom member-method in the parser to “inject” these children into the root of your tree (going from right to left in your List!):

    function
      :  (children+=id '.')* last=id {reverse($children, (CommonTree)$last.tree);} -> ^($last)
      ;
    

    A little demo:

    grammar ReverseTree;
    
    options {
      output=AST;
    }
    
    tokens {
      ROOT;
    }
    
    @members {
      private void reverse(List nodes, CommonTree root) {
        if(nodes == null) return;
        for(int i = nodes.size()-1; i >= 0; i--) {
          CommonTree temp = (CommonTree)nodes.get(i);
          root.addChild(temp);
          root = temp;
        }
      }
    }
    
    parse
      :  function+ EOF -> ^(ROOT function+)
      ;
    
    function
      :  (children+=id '.')* last=id {reverse($children, (CommonTree)$last.tree);} -> ^($last)
      ;
    
    id 
      :  ID
      ;
    
    ID
      :  ('a'..'z' | 'A'..'Z')+
      ;
    
    Space
      :  ' ' {skip();}
      ;
    

    And a little test 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("a.b.c.d.e    Stack.Overflow.Horse    singleNode");
            ReverseTreeLexer lexer = new ReverseTreeLexer(in);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            ReverseTreeParser parser = new ReverseTreeParser(tokens);
            ReverseTreeParser.parse_return returnValue = parser.parse();
            CommonTree tree = (CommonTree)returnValue.getTree();
            DOTTreeGenerator gen = new DOTTreeGenerator();
            StringTemplate st = gen.toDOT(tree);
            System.out.println(st);
        }
    }
    

    which will produce an AST that looks like:

    alt text

    • (image generated using http://graph.gafol.net)

    For the input string:

    "a.b.c.d.e    Stack.Overflow.Horse    singleNode"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How is it possible that following code even compiles? As far as I can
Possible Duplicate: Multiple Inheritance in C# In the following example, I want the Shirt
I am not sure if it's even possible. Say I have the following function
What I want to achieve is the following: At any given point in time,
I get the following error even when my JAVA_HOME is set correctly. C:\workspace-sts-2.8.0.RELEASE\JBClient\target>echo %JAVA_HOME%
I am trying to install Googles' eclipse plugin - http://code.google.com/eclipse/docs/getting_started.html Even on following all
The following code continues to be displayed even if there are entries in my
using the following set of rules and style declarations .tableRow.even, .tableRowNS.even, .odd { background-color:
The following code seems not to work, even though the file appears to be
I get the following error for the second argument even when I set the

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.