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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:22:35+00:00 2026-05-23T14:22:35+00:00

With antlr, I’m trying to make a TreeWalker for a tree like this: input:

  • 0

With antlr, I’m trying to make a TreeWalker for a tree like this:

input: int x = 3

output AST: ^(VARDEF int x 3)

My parser works just fine and also generates an AST like shown above, but whenever I want to resolve anything from the AST, like with $variableType.text, there is allways a NullReferenceException in the generated C# 2.0 TreeWalker.

My TreeWalker:

tree grammar SGLTreeWalker;
options {
    tokenVocab = SGL;
    language = 'CSharp2'; 
}

[...]

compilationUnit
    :   (statement)+
    ;

statement
    :   variableDefinitionList
    ;

variableDefinitionList
    :   ^(VARDEF variableType variableName expression) { Console.WriteLine($variableType.text); }
    ;
[...]

The problematic part, generated by the rule “variableDefinitionList” looks like this:

Match(input, Token.UP, null);
Console.WriteLine(((variableType1 != null) ? input.TokenStream.ToString(
    input.TreeAdaptor.GetTokenStartIndex(variableType1.Start),
    input.TreeAdaptor.GetTokenStopIndex(variableType1.Start)) : null)); 

It turns out that input.TokenStream is null so it throws the NullReferenceException. I read that this can happen if the used TreeNodeStream isn’t buffered, but I used the CommonTreeNodeStream so it should be buffered I think. Here is the code I used to commit the AST:

[...]
SGLParser parser = new SGLParser(tStream);
CommonTree t = (CommonTree) parser.compilationUnit().Tree;
Console.WriteLine("; " + t.ToStringTree());
CommonTreeNodeStream  treeStream = new CommonTreeNodeStream(t);
SGLTreeWalker tw = new SGLTreeWalker(treeStream);
tw.compilationUnit();

Any idea on why input.TokenStream resolves to null when I just want to get the $variableType.text attribute?

  • 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-23T14:22:36+00:00Added an answer on May 23, 2026 at 2:22 pm

    That is because inside a tree grammar, the production rules return a TreeRuleReturnScope which doesn’t have a text attribute (or GetText() method).

    If you want to grab the text of a variableType rule, you’ll need to explicitly return a string.

    A demo:


    SGL.g

    grammar SGL;
    
    options {
      language=CSharp2;
      output=AST;
    }
    
    tokens {
      VARDEF;
    }
    
    @parser::namespace { SGL }
    @lexer::namespace { SGL }
    
    public compilationUnit
      :  statement+ EOF
      ;
    
    statement
      :  variableDefinitionList
      ;
    
    variableDefinitionList
      :   variableType variableName '=' expression 
          -> ^(VARDEF variableType variableName expression)
      ;
    
    variableType
      :  Type
      ;
    
    variableName
      :  ID
      ;
    
    expression
      :  Int
      ;
    
    Type
      :  'int'
      |  'double'
      ;
    
    Int
      :  '0'..'9'+
      ;
    
    ID
      :  ('a'..'z' | 'A'..'Z')+
      ;
    
    Space
      :  ' ' {Skip();}
      ;
    

    SGLTreeWalker.g

    tree grammar SGLTreeWalker;
    
    options {
      tokenVocab=SGL;
      language=CSharp2; 
      ASTLabelType=CommonTree;
    }
    
    @namespace { SGL }
    
    compilationUnit
      :  statement+
      ;
    
    statement
      :  variableDefinitionList
      ;
    
    variableDefinitionList
      :   ^(VARDEF variableType variableName expression) 
          {Console.WriteLine($variableType.txt);}
      ;
    
    variableType returns [string txt]
      :  Type {$txt = $Type.text;} // note that `Type` is still a normal token! 
      ;
    
    variableName
      :  ID
      ;
    
    expression
      :  Int
      ;
    

    And the class:

    using System;
    using Antlr.Runtime;
    using Antlr.Runtime.Tree;
    
    namespace SGL
    {
      public class Test
      {
        public static void Main (string[] args)
        {
          ANTLRStringStream Input = new ANTLRStringStream("int x = 3");
          SGLLexer Lexer = new SGLLexer(Input);
          CommonTokenStream Tokens = new CommonTokenStream(Lexer);
          SGLParser parser = new SGLParser(Tokens);
          CommonTree t = (CommonTree) parser.compilationUnit().Tree;
          CommonTreeNodeStream  treeStream = new CommonTreeNodeStream(t);
          SGLTreeWalker tw = new SGLTreeWalker(treeStream);
          tw.compilationUnit();
        }
      }
    }
    

    produces the following output:

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

Sidebar

Related Questions

Antlr users usually create a parser that generates the AST(Abstract syntax tree), and a
I have an AST derived from the ANTLR Parser Generator for Java. What I
This ANTLR example does not parse input 1; . Can you explain why? It
In ANTLR I want to define a rule like this: rule : ( a
We've used ANTLR to create a parser for a SQL-like grammar, and while the
I am having problems with my Antlr grammar. I'm trying to write a parser
I'm trying to call an Antlr task in my Ant build.xml as follows: <path
I am trying to build an ANTLR grammar that parses tagged sentences such as:
I'm completely new to ANTLR and EBNF grammars to begin with, so this is
I have an AST generated via ANTLR, and I need to convert it 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.