Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try something like this:</p> <pre class="lang-java prettyprint-override"><code>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 &amp; 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()); </code></pre> <p>which would produce:</p> <pre> (&& a (|| b X)) (&& a (|| b c)) </pre> <p>for the input <code>"a &amp;&amp; (b || c)"</code>. I.e., the <code>tree</code> has <code>X</code>, but the <code>copy</code> would will have the original contents: <code>c</code>.</p> <p>Note that I choose <code>CommonTree</code> and <code>CommonToken</code> objects because those are the default <code>Token</code> and <code>Tree</code> implementations. If you choose to create your own <code>Token</code> and/or <code>Tree</code>, chances are that you'll subclass the <code>CommonTree</code> and <code>CommonToken</code> classes, in which case my suggestion would not break.</p> <p>A <code>CommonTree</code> is nothing more than a wrapper around a <code>CommonToken</code>, holding a bit of extra information: a parent node, and child nodes. That is why I also copy all the information from the <code>CommonToken</code> objects.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload