bug in cloneList() method in SableCC3.2

45 views
Skip to first unread message

Benne Otten

unread,
Jun 9, 2012, 2:09:30 PM6/9/12
to sab...@googlegroups.com
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:

Description (last modified by benne...@gmail.com) (diff

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. ;)

utils.txt

Etienne Gagnon

unread,
Jun 25, 2012, 11:30:33 AM6/25/12
to sab...@googlegroups.com
Hi Benne,

Thanks! As soon as I got your confirmation about copyright information, I started woking on updating SableCC 3.x.

I have just released SableCC 3.3 which includes your modifications (see http://sablecc.org/wiki/DownloadPage ).

Have fun!

Etienne

-- 
Etienne Gagnon, Ph.D.

On Saturday, June 9, 2012 2:09:30 PM UTC-4, Benne Otten wrote:
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:
[...]

Phuc Luoi

unread,
Jun 25, 2012, 11:41:09 AM6/25/12
to sab...@googlegroups.com
Cool! Now I can use official release in my project. I will port the
new release in Netbeans Plugin.
> --
> -- You received this message because you are subscribed to the SableCC
> group. To post to this group, send email to sab...@googlegroups.com. To
> unsubscribe from this group, send email to
> sablecc+u...@googlegroups.com. For more options, visit this group at
> https://groups.google.com/d/forum/sablecc?hl=en

Phuc Luoi

unread,
Jun 26, 2012, 9:15:25 AM6/26/12
to sab...@googlegroups.com
I updated the netbeans plugin to integrated the new sablecc 3.3.
Reply all
Reply to author
Forward
0 new messages