title: ANTLR 3.x - How to format rewrite rules
body: <p>I'm finding myself challenged on how to properly format rewrite rules when certain conditions occur in the original rule.</p>
  
  <p>What is the appropriate way to rewrite this:</p>
  
  <pre><code>unaryExpression: op=('!' | '-') t=term
    -&gt; ^(UNARY_EXPR $op $t)
  </code></pre>
  
  <p>Antlr doesn't seem to like me branding anything in parenthesis with a label and "op=" fails. Also, I've tried:</p>
  
  <pre><code>unaryExpression: ('!' | '-') t=term
    -&gt; ^(UNARY_EXPR ('!' | '-') $t)
  </code></pre>
  
  <p>Antlr doesn't like the or '|' and throws a grammar error.</p>
  
  <p>Replacing the character class with a token name does solve this problem, however it creates a quagmire of other issues with my grammar.</p>
  
  <p>--- edit ----</p>
  
  <p>A second problem has been added.  Please help me format this rule with tree grammar:</p>
  
  <p><code>multExpression
      :   unaryExpression (MULT_OP unaryExpression)*
      ;</code></p>
  
  <p>Pretty simple: My expectation is to enclose every matched token in a parent (imaginary) token <code>MULT</code> so that I end up with something like:</p>
  
  <pre><code> MULT
    o
    |
    o---o---o---o---o
    |   |   |   |   |
   '3' '*' '6' '%'  2
  </code></pre>