title: ANTLR Floating Point and Int returns in grammar
body: <p>I am working on creating a language with ANTLR, and started a base language with the expr.g example.  I am using ANTLR 3.  I am ready to work on more data types than ints, and after looking around, I found Bart Kiers' wonderful example for floats, <a href="https://stackoverflow.com/questions/1931307/antlr-is-there-a-simple-example/1932664#1932664">here</a>.</p>
  
  <p>In his example, he has the expression return float, however, this means that it doesn't return int.  I am confused on how to have a return rule allow for more than one return type?  I was thinking I could make a combined "number" type that incorporated ints and floats and return that, but then of course I run into the issue that within Java I have to return an int or a float specifically.  Do I have to have two versions of the expression code, one for each data type?  I would think this isn't necessary, but I am stumped...  I can imagine using optional rules for the expression (i.e. either float or int is valid in this expression), but the return type itself requires a specific type.  I am sorry for repeating myself, but I hopefully am clear with what I am confused about.</p>
  
  <p><a href="https://code.google.com/p/rongo/source/browse/rongo.g" rel="nofollow noreferrer">Here is my grammar file</a></p>
  
  <p>And the expression grammar section:</p>
  
  <pre><code>expr returns [int value]
      : e=mexpr {$value = $e.value;}
        ( PLUS e=mexpr {$value += $e.value;}
        | MINUS e=mexpr {$value -= $e.value;}
        )*
      ;
  </code></pre>
  
  <p>Compare to Bart's:</p>
  
  <pre><code>additionExp returns [double value]
      :    m1=multiplyExp       {$value =  $m1.value;} 
           ( '+' m2=multiplyExp {$value += $m2.value;} 
           | '-' m2=multiplyExp {$value -= $m2.value;}
           )* 
      ;
  </code></pre>
  
  <p>And of course our rules are using the appropriate datatypes (int for me, float for him)...</p>