title: How do I rewrite a subtree with a composite root using ANTLR?
body: <p>I have an antlr grammer with subtrees like this:</p>
  
  <pre><code> ^(type ID)
  </code></pre>
  
  <p>that I want to convert to:</p>
  
  <pre><code> ^(type DUMMY ID)
  </code></pre>
  
  <p>where type is 'a'|'b'.</p>
  
  <p>Note: what I really want to do is convert anonymous instantiations to explicit by generating dummy names.</p>
  
  <p>I've narrowed it down to the grammars below, but I'm getting this:</p>
  
  <pre><code>(a bar) (b bar)
  got td
  got bu
  Exception in thread "main" org.antlr.runtime.tree.RewriteEmptyStreamException: rule type
          at org.antlr.runtime.tree.RewriteRuleElementStream._next(RewriteRuleElementStream.java:157)
          at org.antlr.runtime.tree.RewriteRuleSubtreeStream.nextNode(RewriteRuleSubtreeStream.java:77)
          at Pattern.bu(Pattern.java:382)
  </code></pre>
  
  <p>The error message continues. My debug so far:</p>
  
  <ol>
  <li>The input made it through the initial grammar generating two trees. a bar and b bar.</li>
  <li>The second grammar does match the trees. it's printing td and bu.</li>
  <li>The rewrite crashes, but I have no idea why? What does RewriteEmptyStreamException mean.</li>
  </ol>
  
  <p><strong>What the proper way to do this kind of a rewrite?</strong></p>
  
  <p>My main grammer Rewrite.g:</p>
  
  <pre><code>grammar Rewrite;
  
  options {
    output=AST;
  }
  
  @members{
    public static void main(String[] args) throws Exception {
        RewriteLexer lexer = new RewriteLexer(new ANTLRStringStream("a foo\nb bar"));
        RewriteParser parser = new RewriteParser(new CommonTokenStream(lexer));
        CommonTree tree = (CommonTree)parser.test().getTree();
        System.out.println(tree.toStringTree());
  
        CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree);        
        Pattern p = new Pattern(nodes);
  
        CommonTree newtree = (CommonTree) p.downup(tree);
    }
  }
  
  type 
      : 'a'
      | 'b'
      ;
  
  test : id+;
  id   : type ID  -&gt; ^(type ID["bar"]);
  
  DUMMY : 'dummy';
  ID   : ('a'..'z')+;
  WS : (' '|'\n'|'r')+ {$channel=HIDDEN;};
  </code></pre>
  
  <p>and Pattern.g</p>
  
  <pre><code>tree grammar Pattern;
  
  options {
      tokenVocab = Rewrite;
      ASTLabelType=CommonTree;
      output=AST;
      filter=true;             // tree pattern matching mode
  }
  
  topdown
      : td
      ;
  
  bottomup
      : bu
      ;
  
  type 
      : 'a'
      | 'b'
      ;
  
  td
      : ^(type ID) { System.out.println("got td"); }
      ;
  
  bu
      : ^(type ID) { System.out.println("got bu"); }
          -&gt; ^(type DUMMY ID)
      ;
  </code></pre>
  
  <p>to do compile:</p>
  
  <pre><code>java -cp ../jar/antlr-3.4-complete-no-antlrv2.jar org.antlr.Tool Rewrite.g 
  java -cp ../jar/antlr-3.4-complete-no-antlrv2.jar org.antlr.Tool Pattern.g 
  javac -cp ../jar/antlr-3.4-complete-no-antlrv2.jar *.java 
  java -classpath .:../jar/antlr-3.4-complete-no-antlrv2.jar RewriteParser
  </code></pre>
  
  <p>EDIT 1: I have also tried using antlr4 and I get the same crash.</p>