[antlr-interest] Making a distinction between float and int calculation

Steven Raemaekers stevenraemaekers at gmail.com
Sat Jan 23 08:36:59 PST 2010


Hello,

In my grammar there should be an evaluator for numeric expressions. These
numeric expressions should return an integer, or a float, depending on the
contents of the expression.
For example:

3 + 2.0: should return float
3 + 2: should return integer
2.0 + 3.0: should return float
1 / 3: should return float
4 / 2: should return int

In my grammar there is only one rule for a numeric expression. I do not know
whether I should duplicate the entire operator precedence rules for the
distinction between float and int.
The following statements are part of my grammar:

expression
: list
| quotedword
| booleanexpression
 ;

booleanexpression
: numericexpression (BOOL^ numericexpression)*
 ;

numericexpression
: mult ((PLUS^ | MINUS^) mult)*
 ;

mult
: atom ((MULTIPLY^ | DIVIDE^) atom)*
;

atom
: INT
| FLOAT
| ID
 | LEFTPAREN expression RIGHTPAREN
-> ^(EXPRESSION expression)
;

Does anybody have a idea how I should take care of this distinction between
float and int? Or is this distinction even necessary?

-- 
Regards,

Steven


More information about the antlr-interest mailing list