title: Associativity and precedence of Java operators in ANTLRv4 grammar
body: <p>I am trying to understand the features of antlr4, and going through the reference code and the book, and I have a question on the 'expression' rules in Java.g4 (<a href="https://raw.githubusercontent.com/antlr/grammars-v4/master/java/Java.g4" rel="nofollow noreferrer">https://raw.githubusercontent.com/antlr/grammars-v4/master/java/Java.g4</a>) and the book (2nd ed 2012, page 73):</p>
  
  <ol>
  <li><p>According to the book, '+' has less priority than '^' and hence the '+' rule is below the '^' rule. But the Java code is the opposite.</p></li>
  <li><p>According to the book, the '^' rule has assoc=right. But the Java code does not have it, except for '^=' and similar rules.</p></li>
  </ol>
  
  <p>Which is right, the book or the code?</p>
  
  <p>BTW, there is another difference between the code and the book. In the code, assoc=right is at the beginning of the rule, whereas in the book it is after the operator, but it looks like the syntax was changed (the code works for 4.5.3) and the book has to be updated for this.</p>
  
  <p>Here is the relevant Java.g4 code snippet:</p>
  
  <pre><code>expression
    :   primary
    |   expression ('*'|'/'|'%') expression
    |   expression ('+'|'-') expression
    |   expression '^' expression
          // this indicates that precedence is: * &gt; + &gt; ^
          //    BUT it SHOULD be: ^ &gt; * &gt; +
          // and both + and ^ are assoc=left
          //    BUT it SHOULD be: LEFT for + and RIGHT for ^
    |   &lt;assoc=right&gt; expression
        (   '='
        |   '+='
        |   '^='
        )
        expression
    ;
  </code></pre>
  
  <p><strong>Note:</strong> In the above question, I had assumed that ^ meant exponentiation, but as the answer pointed out, in java ^ has a different meaning.</p>