Concerns version 1.14
If a target has an empty right-hand side, assigning to $$ in Java mode may lead to unpredictable results, because values may get unexpectedly overwritten.
The problem does not occur when C code is generated.
I attach one rules file for Java and one for C++, which both do essentially the same thing. There is also an input file valid for both.
Test case in C mode:
g++ -o mini y.tab.c
Test case in Java mode:
byacc -Jclass=Mini minij.y
java Mini mini.in
The output should be the same, but isn't.
The reason is the statement
if (yym>0) //if count of rhs not 'nil'
yyval = val_peek(yym-1); //get current semantic value
just before the "switch(yyn)" statement in "Mini.java".
If a target has no right-hand side, yym is zero and yyval is not assigned for the rule. Is has whatever value it had from the previous rule. This "if" condition is not present in the generated C code.
I have made a temporary fix by removing the "if" in "skeleton.c" and making a little change in "output.c" to how the method "val_peek" is generated.
This seems to remedy the situation, but because I don't fully understand the code, I may have overlooked something and created a potential new problem.
Please find attached the two changed sources for reference.
Log in to post a comment.