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, here.
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.
And the expression grammar section:
expr returns [int value]
: e=mexpr {$value = $e.value;}
( PLUS e=mexpr {$value += $e.value;}
| MINUS e=mexpr {$value -= $e.value;}
)*
;
Compare to Bart's:
additionExp returns [double value]
: m1=multiplyExp {$value = $m1.value;}
( '+' m2=multiplyExp {$value += $m2.value;}
| '-' m2=multiplyExp {$value -= $m2.value;}
)*
;
And of course our rules are using the appropriate datatypes (int for me, float for him)...