Hello all,
While tinkering with SableCC3.2 I found a bug in the generated code. I was trying to store values in a hashmap, using nodes as the key, and cloning nodes when it was necessary, to prevent overwriting of keyValue pairs in that hashmap.
I noticed that eventhough I used the method clone on an object, the keyValu pairs would still be overwritten, causing some more severe errors in my project.
After some debugging of myself I found out that the method cloneList() in the class Node does in fact NOT clone the nodes in the list at all. Instead it just composes a new list with the same nodes, and returns that.
The code:
protected <T> List<T> cloneList(List<T> list) {
List<T> clone = new LinkedList?<T>();
for(T n : list) {
clone.add(n);
}
return clone;
}
should in fact be more like this:
@SuppressWarnings?("unchecked")
protected <T extends Node> List<T> cloneList(List<T> list)
{
List<T> clone = new LinkedList?<T>();
for(T n : list) {
clone.add((T) n.clone());
}
return clone;
}
I've tried to fix the bug in SableCC3.2, and I've succeeded. The bug is present in the file utils.txt in the org.sablecc.sablecc package
I've changed the lines 309-319 from:
protected <T> List<T> cloneList(List<T> list) {
List<T> clone = new LinkedList?<T>();
for(T n : list) {
clone.add(n);
}
return clone;
}
to:
@SuppressWarnings?("unchecked")
protected <T extends Node> List<T> cloneList(List<T> list)
{
List<T> clone = new LinkedList?<T>();
for(T n : list) {
clone.add((T) n.clone());
}
return clone;
}
SableCC now generates a correct cloneList() method in the class Node, and my weird errors did not occur anymore.
PS: the error did occur with trying to execute code to calculate the nth fibonacci number. This wen't wrong from the 5th fibonacci number or higher, while lower fibonacci numbers would still be calculated correctly. I also did not encounter the error while calculating the nth factoral. It took me some while to figure out why the error occured. ;)
I submitted a new ticket on the SableCC3.2, but there doesn't seem to be a lot of activity there. The description of the bug is:
[...]