[antlr-interest] Changing the root of a tree in a tree parser

franck102 franck102 at yahoo.com
Mon Nov 28 14:15:53 PST 2011


And a similar question: I am trying to "collapse" the children that have the
same type as the parent, e.g. transform ^( DOT foo ^( DOT bar qwe ) ) into
^( DOT foo bar qwe ).

I wrote the method below, which seems to kind of work but generates the
warnings below.
Is there some subtle issue with calling 
          adaptor.addChild(newTree, adaptor.getChild(oldTree, i));

Franck

---------------------------------------------------
[org.antlr.works.debugger.tree.DBASTModel - event 24735] Warning: [addChild]
Child node 2569 not found, ignoring
No token info for token index 1454
[org.antlr.works.debugger.tree.DBASTModel - event 25470] Warning: [addChild]
Root node 2652 not found, ignoring.
----------------------------------------------------
private CommonTree collapse(CommonTree tree)
    	{
    			boolean needCollapse = false;
    			int count = adaptor.getChildCount(tree);
    			int treeType = adaptor.getType(tree);
    			for (int i = 0; ! needCollapse && i < count; i++) {
    				Object child = adaptor.getChild(tree, i);
    				if (child instanceof CommonTree && adaptor.getType(child) ==
treeType) {
    					needCollapse = true;
    				}
    			}
    			if ( ! needCollapse ) {
    				return tree;
    			}
    			Object newTree = adaptor.create(adaptor.getToken(tree));
    			for (int i = 0; i < count; i++) {
    				Object child = adaptor.getChild(tree, i);
    				if (child instanceof CommonTree && adaptor.getType(child) ==
treeType) {
    					for (int j = 0; j < adaptor.getChildCount(child); j++) {
    						adaptor.addChild(newTree, adaptor.getChild(child, j));
    					}
    				}
    				else {
    					adaptor.addChild(newTree, child);
    				}
    			}
    			return (CommonTree)newTree;
        	}

--
View this message in context: http://antlr.1301665.n2.nabble.com/Changing-the-root-of-a-tree-in-a-tree-parser-tp7038239p7040599.html
Sent from the ANTLR mailing list archive at Nabble.com.


More information about the antlr-interest mailing list