If using a custom class for the semantic value, then dup_yyval function does not make a copy, but simply returns the same value. In this case $$ will corrupt the previous value on the stack. E.g., for the classical expression grammar (with a custom semantic class identical to ParserValue) the expression 4+(2+1) will give 6 as result, because 3 will be written on the top stack value (equal to 4).
I suggest to replace:
return val;
by
return val.clone();
And require that a custom semantic class should implement the clone() method...
Here is the diff :
1211c1211
< fprintf(code_file," return val;\n");
---
> fprintf(code_file," return val.clone();\n");
It is also possible to just use:
return new @jvalclass();
but in this case we lose the default behaviour $$=$1