I use ANTLR to build a tree (CommonTree) like follwing (language: JAVA):
Parser.prog_return r = parser.prog();
CommonTree t = (CommonTree) r.getTree();
Now, I need to pass "t" as a parameter, and make some changes without affecting the original tree. However, with Java's pointer, this could not been done, so I need to duplicate the tree.
I have been search on the internet, the cloested thing I could find is the dupTree() method of ASTFactory class.
Any suggestion or advises on how to achive this would be appreciated!
EDIT
@Bart Kiers, Thanks for your answer, it absolutely works!
I see you are doing a depth first walk over the tree, and create a CommonTree object for each node that was visited.
My question is now, what is the relation between CommonToken and CommonTree, and what are these attributes do:
cTok.setCharPositionInLine(oTok.getCharPositionInLine());
cTok.setChannel(oTok.getChannel());
cTok.setStartIndex(oTok.getStartIndex());
cTok.setStopIndex(oTok.getStopIndex());
cTok.setTokenIndex(oTok.getTokenIndex());
Try something like this:
public static CommonTree copyTree(CommonTree original) {
CommonTree copy = new CommonTree(original.getToken());
copyTreeRecursive(copy, original);
return copy;
}
private static void copyTreeRecursive(CommonTree copy, CommonTree original) {
if(original.getChildren() != null) {
for(Object o : original.getChildren()) {
CommonTree originalChild = (CommonTree)o;
// get the token from the original child node
CommonToken oTok = (CommonToken)originalChild.getToken();
// create a new token with the same type & text as 'oTok'
CommonToken cTok = new CommonToken(oTok.getType(), oTok.getText());
// copy all attributes from 'oTok' to 'cTok'
cTok.setLine(oTok.getLine());
cTok.setCharPositionInLine(oTok.getCharPositionInLine());
cTok.setChannel(oTok.getChannel());
cTok.setStartIndex(oTok.getStartIndex());
cTok.setStopIndex(oTok.getStopIndex());
cTok.setTokenIndex(oTok.getTokenIndex());
// create a new tree node with the 'cTok' as token
CommonTree copyChild = new CommonTree(cTok);
// set the parent node of the child node
copyChild.setParent(copy);
// add the child to the parent node
copy.addChild(copyChild);
// make a recursive call to copy deeper
copyTreeRecursive(copyChild, originalChild);
}
}
}
...
// get the original tree
CommonTree tree = (CommonTree)parser.parse().getTree();
// create a copy of the tree
CommonTree copy = copyTree(tree);
// change the contents of the right node of the right node of the root
((CommonTree)tree.getChild(1).getChild(1)).getToken().setText("X");
System.out.println(tree.toStringTree());
System.out.println(copy.toStringTree());
which would produce:
(&& a (|| b X))
(&& a (|| b c))
for the input "a && (b || c)"
. I.e., the tree
has X
, but the copy
would will have the original contents: c
.
Note that I choose CommonTree
and CommonToken
objects because those are the default Token
and Tree
implementations. If you choose to create your own Token
and/or Tree
, chances are that you'll subclass the CommonTree
and CommonToken
classes, in which case my suggestion would not break.
A CommonTree
is nothing more than a wrapper around a CommonToken
, holding a bit of extra information: a parent node, and child nodes. That is why I also copy all the information from the CommonToken
objects.
I had the same issue stumbling over dupTree()
, which seemed to be deprecated and then Bart's posting, which steered me into the right direction. I finally ended up with the following, which is using the constructor of CommonTree
accepting a CommonTree
- abstracting it from the need to copy the individual fields.
private static CommonTree copyTreeRecursive(CommonTree original) {
CommonTree copy = new CommonTree(original); // Leverage constructor
if(original.getChildren() != null) {
for(Object o : original.getChildren()) {
CommonTree childCopy = copyTreeRecursive((CommonTree)o);
childCopy.setParent(copy);
copy.addChild(childCopy);
}
};
return copy;
}
NB: I stuck with Bart's naming convention.