title: How to handle left associative grammar in ANTLR
body: <p>I am using antlr to generate parser that produces abstract syntax tree. I got some problem about left associative operators. My grammar is like the following:</p>
  
  <pre><code>add_expr returns [ASTNode value]
      :a=mul_expr {$value = a;}
      (
          o = ('+' | '-') b = add_expr
          {
          $value = new AddNode(a, b, $o.text);
          }
      )?
      ;
  mul_expr returns [ASTNode value]
      :a=term {$value = a;}
      (
          o = ('*' | '/') b = mul_expr
          {
          $value = new MultiplyNode(a, b, $o.text);
          }
      )?
      ;
  </code></pre>
  
  <p>The constructor of AddNode and MultiplyNode is like:</p>
  
  <pre><code>AddNote(ASTNode left, ASTNode right, String operatorr)
  </code></pre>
  
  <p>The problem is, for input a-b-c, it is parsed to something like a-(b-c), rather than (a-b)-c. Antlr does not accept left recursive grammar. How to modify the grammar to make the left associative syntax work in expression like a-b-c?</p>