package rebelsky.compiler.parser; import java.io.PrintWriter; /** * A simple mechanism for printing parse trees. Intended mostly * as a debugging aid. * * @author Samuel A. Rebelsky * @version 1.0 of October 2002 */ public class TreePrinter { private static final String indent = " "; /** * Dump a tree to the given output object. * * Pre: obj is either a Node or a Token. */ public static void print(Node root, PrintWriter out) { print(root, out, ""); } // print(Object,PrintWriter) private static void print(Node root, PrintWriter out, String offset) { // Special case: The thing to be printed is null. I don't think // this should happen, but it's good to be careful. In this // case, print nothing. if (root == null) { } // Standard case: A Node // Print the symbol // Print the children else { out.println(offset + root.getSymbol().toString()); int numChildren = root.numChildren(); for (int childNum = 0; childNum < numChildren; ++childNum) { print(root.getChild(childNum), out, offset+indent); } // for each child } // if the object is a node. } // print(Object, PrintWriter, String) } // class TreePrinter