title: ANTLR: What is the fastest way to get grammar tree?
body: <p>What is the fastest (smaller code) way to get grammar tree ?</p>
  
  <p>I am trying to get grammar tree. I've generated C# code based on my simple grammar:</p>
  
  <pre><code>grammar MyPascal;
  options
  {
      language=CSharp3;
      output=AST;
  }
  
  operator: (block | ID);
  block   : BEGIN operator* END;
  BEGIN   :'begin';
  END     :'end';
  ID      :('a'..'z')+;
  WS      :( ' '
          | '\t'
          | '\r'
          | '\n'
          ) {$channel=HIDDEN;};
  </code></pre>
  
  <p>When i'am using ANTLR works for simple input text like:</p>
  
  <pre><code>input.txt:
  begin
    abs
    qwe
    begin
      begin
      end
    end
  end
  </code></pre>
  
  <p>i get nice picture of grammar tree.</p>
  
  <p>Now i'am wonder if there any simple way to get tree structure of my "program" from C# without writing 1000s lines of code.</p>
  
  <p>Here i'am trying to get grammar tree:</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          MyPascalLexer lex = new MyPascalLexer(new ANTLRFileStream(@"M:\input.txt"));
          CommonTokenStream tokens = new CommonTokenStream(lex);
          MyPascalParser g = new MyMyPascalParser(tokens);
          MyPascalParser.myprogram_return X = g.myprogram();                                       
          Console.WriteLine(X.Tree);  // Writes: nill
          Console.WriteLine(X.Start); // Writes: [@0,0:4='begin',&lt;4&gt;,1:0]
          Console.WriteLine(X.Stop);  // Writes: [@35,57:57='end',&lt;19&gt;,12:2]
      }
  }
  </code></pre>