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

  • Home
  • SEARCH
  • 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 8698645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:46:22+00:00 2026-06-13T01:46:22+00:00

A simple example: (grammar): stat: ID ‘=’ expr NEWLINE -> ^(‘=’ ID expr) expr:

  • 0

A simple example:

(grammar):

stat: ID '=' expr NEWLINE -> ^('=' ID expr)

expr: atom '+' atom -> ^(+ atom atom)

atom: INT | ID

...

(input text): a = 3 + 5

The corresponding CommonTree for ‘3 + 5’ contains a ‘+’ token and two children (3, 5).

At this point, what is the best way to recover the original input text that parsed into this tree (‘3 + 5’)?

I’ve got the text, position and the line number of individual tokens in the CommonTree object, so theoretically it’s possible to make sure only white space tokens are discarded and piece them together using this information, but it looks error prone.

Is there a better way to do this?

  • 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-13T01:46:23+00:00Added an answer on June 13, 2026 at 1:46 am

    Is there a better way to do this?

    Better, I don’t know. There is another way, of course. You decide what’s better.

    Another option would be to create a custom AST node class (and corresponding node-adapter) and add the matched text to this AST node during parsing. The trick here is to not use skip(), which discards the token from the lexer, but to put it on the HIDDEN channel. This is effectively the same, however, the text these (hidden) tokens match are still available in the parser.

    A quick demo: put all these 3 file in a directory named demo:

    demo/T.g

    grammar T;
    
    options {
      output=AST;
      ASTLabelType=XTree;
    }
    
    @parser::header {
      package demo;
      import demo.*;
    }
    
    @lexer::header {
      package demo;
      import demo.*;
    }
    
    parse
     : expr EOF -> expr
     ;
    
    expr
    @after{$expr.tree.matched = $expr.text;}
     : Int '+' Int ';' -> ^('+' Int Int)
     ;
    
    Int
     : '0'..'9'+
     ;
    
    Space
     : ' ' {$channel=HIDDEN;}
     ;
    

    demo/XTree.java

    package demo;
    
    import org.antlr.runtime.*;
    import org.antlr.runtime.tree.*;
    
    public class XTree extends CommonTree {
    
      protected String matched;
    
      public XTree(Token t) {
        super(t);
        matched = null;
      }
    }
    

    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 = "12    +  42 ;";
        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();
        System.out.println("tree    : " + root.toStringTree());
        System.out.println("matched : " + root.matched);    
      }
    }
    

    You can run this demo by opening a shell and cd-ing to the directory that holds the demo directory and execute the following:

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

    which will produce the following output:

    tree    : (+ 12 42)
    matched : 12    +  42 ;
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Grammar by definition contains productions, example of very simple grammar: E -> E +
Very simple example: #include <string> #include <boost/program_options.hpp> namespace po = boost::program_options; int main(int argc,
In a simple example, I'm confused about how to turn this grammar into a
I've defined a simple Xtext grammar which looks like this (simplified): grammar org.xtext.example.mydsl.MyDsl with
A simple example of what I'd like to achieve: <body> <div style=float:left;> Hey, look
A simple example: Let's say I have one alias being sourced somewhere as: alias
This simple example fails to compile in VS2K8: io_service io2; shared_ptr<asio::deadline_timer> dt(make_shared<asio::deadline_timer>(io2, posix_time::seconds(20))); As
Very simple example - hoping for a simple solution: char x[7]; if(fgets(x,5,stdin)) printf(y); else
so I have a simple example--a fully crossed three treatment three context experiment, where
I've created a simple example to show what I'm having problems with. Place a

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.