package step7; import java.util.List; import step7.expr.yyLex; /** output {@link expr} tree in postfix notation. */ public class traverse { /** postorder traversal to output. */ public void visit (Object node) { if (node instanceof Number) System.out.print(" "+node); else if (node instanceof List) { for (int n = 0; n < ((List)node).size(); ++ n) visit(((List)node).get(n)); System.out.print(" "+classname(node)); } } /** return last component of (inner) classname. */ protected static String classname (Object obj) { String name = obj.getClass().getName(); return name.substring(name.lastIndexOf("$")+1); } /** read an expression from standard input and output it in postorder. */ public static void main (String[] args) throws Exception { new traverse().visit(new expr().yyparse(new yyLex(System.in))); System.out.println(); } }