Menu

Printing out the tree

Developers
David Mohr
2008-10-29
2012-10-08
  • David Mohr

    David Mohr - 2008-10-29

    Hi,
    maybe I'm just overlooking something, but it would be very nice to be able to print out the tree that we're manipulating.

    Right now I just use a TreeWalker, and print out each element, but that's without indention. So it's hard to see where the element belongs to. Is there maybe a method for this somewhere that I'm overlooking? Or else, can I get a pointer how to implement it myself?

    Thanks,
    ~David

     
    • David Mohr

      David Mohr - 2008-11-08

      Hey, sorry for the late reply, but thanks, that works out nicely!

       
    • Tobias Gutzmann

      Tobias Gutzmann - 2008-10-30

      Hej,

      you can either call toSource() on any source element, but that won't give you a tree but a real source code representation. If you want the tree, this source code snippet could help (I write it down from my memories, don't be too picky on exact syntax/function names ;-)

      class TreePrinter {
      private void printIndent() {
      for (int i = 0; i < indent; i++) System.out.print(" "); // imperformant solution!!!!
      }

      private int indent = 0;
      public void dfsPrint(ProgramElement node) {
      printIndent();
      System.out.println(node);
      if (node instanceof NonTerminalProgramElement) {
      ++indent;

          int count = ((NonTerminalProgramElement)node).getChildCount();
          for (int i=0;i&lt;count;i++) {
          ProgramElement child = ((NonTerminalProgramElement)node)node.getChild(i);
          dfsPrint(child);
          }
              --indent;     
          }
      

      }

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.