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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:14:49+00:00 2026-05-11T20:14:49+00:00

Is anyone aware of tutorials for walking ANTLR-generated ASTs in C#? The closest I

  • 0

Is anyone aware of tutorials for walking ANTLR-generated ASTs in C#? The closest I was able to find is this, but it’s not terribly helpful.

My goal is to walk through trees that I’m generating based on a domain-specific language that I’m working on, and to use the trees to output generated C# code.

A Java-based tutorial would be helpful, too — anything that provides clear examples of how to traverse ANTLR ASTs.

  • 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-11T20:14:49+00:00Added an answer on May 11, 2026 at 8:14 pm

    I managed to figure this out by adapting the example at the end of Manuel Abadia’s article.

    Here’s my version, which I happen to be using to convert parsed code to C#.
    These are the steps:

    1. Instantiate an ANTLRStringStream or subclass with your input (it can be a file or string).
    2. Instantiate your generated lexer, passing in that string stream.
    3. Instantiate a token stream with the lexer.
    4. Instantiate your parser with that token stream.
    5. Get the top-level value from your parser, and turn it into a CommonTree.
    6. Traverse the tree:

    To get the literal text of a node, use node.Text.
    To get the token name of a node, use node.Token.Text.

    Note that node.Token.Text will only give you the actual name of your token if it’s an imaginary token with no corresponding string. If it’s a real token, then node.Token.Text will return its string.

    For example, if you had the following in your grammar:

    tokens { PROGRAM, FUNCDEC }
    
    EQUALS : '==';
    ASSIGN : '=';
    

    Then you’ll get "PROGRAM", "FUNCDEC", "==", and "=" from the corresponding accesses of node.Token.Text.

    You can see part of my example below, or you can browse the full version.


    public static string Convert(string input)
    {
        ANTLRStringStream sStream = new ANTLRStringStream(input);
        MyGrammarLexer lexer = new MyGrammarLexer(sStream);
    
        CommonTokenStream tStream = new CommonTokenStream(lexer);
    
        MyGrammarParser parser = new MyGrammarParser (tStream);
        MyGrammarParser.program_return parserResult = parser.program();
    
        CommonTree ast = (CommonTree)parserResult.Tree;
    
        Print(ast);
        string output = header + body + footer;
    
        return output;
    }
    
    public static void PrintChildren(CT ast)
    {
        PrintChildren(ast, " ", true);
    }
    
    public static void PrintChildren(CT ast, string delim, bool final)
    {
        if (ast.Children == null)
        {
            return;
        }
    
        int num = ast.Children.Count;
    
        for (int i = 0; i < num; ++i)
        {
            CT d = (CT)(ast.Children[i]);
            Print(d);
            if (final || i < num - 1)
            {
                body += delim;
            }
        }
    }
    
    public static void Print(CommonTree ast)
    {
        switch (ast.Token.Text)
        {
            case "PROGRAM":
                //body += header;
                PrintChildren(ast);
                //body += footer;
                break;
            case "GLOBALS":
                body += "\r\n\r\n// GLOBALS\r\n";
                PrintChildren(ast);
                break;
            case "GLOBAL":
                body += "public static ";
                PrintChildren(ast);
                body += ";\r\n";
                break;
    
          ....
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is anyone aware of any gems, tutorials, or solutions enabling a user to sign
Is anyone aware of where I can find a tutorial for building a custom
Is anyone aware of where to find or implement the Kubelka-Munk function to mix
Is anyone aware of a script/class (preferably in PHP) that would parse a given
Is anyone aware of a language feature or technique in C++ to prevent a
Is anyone aware of a reasonably well documented example of simulated annealing in Visual
Is anyone aware of any text editors with Visual Studio editor functionality? Specifically, I'm
Is anyone aware of general UI design guidelines for increasing ad revenue from web
Is anyone aware of a way of sftp'ing from Unix to Windows Thanks Damien
Is anyone aware of any performance implications from changing the _win32_winnt from 0x400 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.