title: ANTLR - trouble getting AST hierarchy setup
body: <p>I am trying to get my head around the tree construction operators (^ and !) in ANTLR.</p>
  
  <p>I have a grammar for flex byte arrays (a UINT16 that describe number of bytes in array, followed by that many bytes). I've commented out all of the semantic predicates and their associated code that does validate that there are as many bytes in the array as indicated by the first two bytes...that part isn't what I'm having problems with.</p>
  
  <p>My issue is with the tree that is generated after parsing some input. All that happens is that each character is a sibling node. I was expecting that the AST generated would be similar to the tree that you can see in Interpreter window of ANTLRWorks 1.4. As soon as I try to change how the tree would be made using a ^ character, I get an exception of the form:</p>
  
  <pre><code>Unhandled Exception: System.SystemException: more than one node as root (TODO: make exception hierarchy)
  </code></pre>
  
  <p>Here's the grammar (currently targeting C#):</p>
  
  <pre><code>grammar FlexByteArray_HexGrammar;
  
  options 
  {
  //language = 'Java';
  language = 'CSharp2';
  output=AST; 
  }
  
  expr 
      :   array_length remaining_data
          //the amount of remaining data must be equal to the array_length (times 2 since 2 hex characters per byte)
          // need to check if the array length is zero first to avoid checking $remaining_data.text (null reference) in that situation.
          //{ ($array_length.value == 0 &amp;&amp; $remaining_data.text == null) || ($remaining_data.text != null &amp;&amp; $array_length.value*2 == $remaining_data.text.Length) }?
      ;
  
  array_length //returns [UInt16 value]
      :   uint16_little //{ $value = $uint16_little.value; }
      ;
  
  hex_byte1 //needed just so I can distinguish between two bytes in a uint16 when doing a semantic predicate (or whatever you call it where I write in the target language in curly brackets)
      :   hex_byte
      ;
  
  uint16_big //returns [UInt16 value]
      :   hex_byte1 hex_byte //{ $value = Convert.ToUInt16($hex_byte.text + $hex_byte1.text); }
      ;
  
  uint16_little //returns [UInt16 value]
      :   hex_byte1 hex_byte //{ $value = Convert.ToUInt16($hex_byte1.text + $hex_byte.text); }
      ;
  
  remaining_data 
      :   hex_byte*
      ;
  
  hex_byte 
      :   HEX_DIGIT HEX_DIGIT
      ;
  
  HEX_DIGIT : ('0'..'9'|'a'..'f'|'A'..'F')
  ;
  </code></pre>
  
  <p>Here's kind of what I thought the AST would be:</p>
  
  <p><img src="https://i.stack.imgur.com/v9ryy.png" alt="ANTLRWorks 1.4 interpreter output for input of &quot;0001ff&quot;"></p>
  
  <p>Here's the program in C# I was using to get a visual (actually textual, but then I put it thru GraphViz to get picture) representation of the AST:</p>
  
  <pre><code>namespace FlexByteArray_Hex
  {
      using System;
      using Antlr.Runtime;
      using Antlr.Runtime.Tree;
      using Antlr.Utility.Tree;
  
      public class Program
      {
          public static void Main(string[] args)
          {
              ICharStream input = new ANTLRStringStream("0001ff");
              FlexByteArray_HexGrammarLexer lex = new FlexByteArray_HexGrammarLexer(input);
              CommonTokenStream tokens = new CommonTokenStream(lex);
              FlexByteArray_HexGrammarParser parser = new FlexByteArray_HexGrammarParser(tokens);
  
              Console.WriteLine("Parser created.");
  
              CommonTree tree = parser.expr().Tree as CommonTree;
  
              Console.WriteLine("------Input parsed-------");
  
              if (tree == null)
              {
                  Console.WriteLine("Tree is null.");
              }
              else
              {
                  DOTTreeGenerator treegen = new DOTTreeGenerator();
                  Console.WriteLine(treegen.ToDOT(tree));
              }
          }
      }
  }
  </code></pre>
  
  <p>Here's what the output of that program put into GraphViz looks like:
  <img src="https://i.stack.imgur.com/1yJ33.png" alt="graph viz output"></p>
  
  <p>The same program in Java (in case you want to try it out and don't use C#):</p>
  
  <pre><code>import org.antlr.*;
  import org.antlr.runtime.*;
  import org.antlr.runtime.tree.*;
  
  public class Program
  {
      public static void main(String[] args) throws Exception
      {
          FlexByteArray_HexGrammarLexer lex = new FlexByteArray_HexGrammarLexer(new ANTLRStringStream("0001ff"));
          CommonTokenStream tokens = new CommonTokenStream(lex);
          FlexByteArray_HexGrammarParser parser = new FlexByteArray_HexGrammarParser(tokens);
  
          System.out.println("Parser created.");
  
          CommonTree tree = (CommonTree)parser.expr().tree;
  
          System.out.println("------Input parsed-------");
  
  
          if (tree == null)
          {
              System.out.println("Tree is null.");
          }
          else
          {
              DOTTreeGenerator treegen = new DOTTreeGenerator();
              System.out.println(treegen.toDOT(tree));
          }
      }
  }
  </code></pre>