Multiple problems appear in this rule:
piExp returns [double value]
: p1=multiplyExp {$value = $p1.value;}
('pi' p1=multiplyExp {$value = ($p1.value-$p1.value)+Math.PI;}
)*
;
The way this rule is structured, to get the value π you need to use an expression like 3pi4
, i.e. pi
is written as a binary operator instead of a primary expression. The values 3
and 4
are effectively ignored because both are assigned to the label p1
(as opposed to separate labels p1
and p2
like you have in other rules), thus the expression ($p1.value-$p1.value)+Math.PI
simplifies to (0)+Math.PI
which is just Math.PI
.
You should correct this in the following way:
- Remove the rule
piExp
. - Replace references to
piExp
inmatchSinCosExp
with references tomultiplyExp
. Create a new rule
primaryExp
:primaryExp returns [double value] : Number {$value = Double.parseDouble($Number.text);} | 'pi' {$value = Math.PI;} ;
Alter your
atomExp
rule to referenceprimaryExp
instead of justNumber
:atomExp returns [double value] : primaryExp {$value = $primaryExp.value;} | '(' exp=additionExp ')' {$value = $exp.value;} ;