/* AST.java Abstract Syntax Tree implementation */ public class AST { private char node; // holds node operator private Object val; // holds node value private AST left, right; // left and right subtrees // Constructor - construct AST leaf with integer value public AST(Object value) { node = '#'; val = value; } // Constructor - construct AST node for operator with one operand public AST(char op, AST arg) { node = op; left = arg; } // Constructor - construct AST node for operator with two operands public AST(char op, AST arg1, AST arg2) { node = op; left = arg1; right = arg2; } // toLisp - Print AST as a Lisp expression public void toLisp() { switch (node) { case '#': // leaf with integer constant System.out.print(" " + val); break; case '+': case '-': case '*': case '/': System.out.print(" (" + node); left.toLisp(); right.toLisp(); System.out.print(")"); break; case '%': // unary minus (one operand) System.out.print("(-"); left.toLisp(); System.out.print(")"); break; } } }