class Exp { Tokens tokens; public Exp parse(Tokens tokens) throws SyntaxError { this.tokens = tokens; return parseExp(null); } private Exp parseExp(Exp exp) throws SyntaxError { if (tokens.atEnd()) { if (exp == null) throw new SyntaxError ("expression"); else return exp; } else if (tokens.nextIs("+")) { tokens.eat("+"); Exp right = parseExp(null); return parseExp(new Plus(exp, right)); } else if (tokens.nextIs("*")) { tokens.eat ("*"); Exp right = parseExp(null); return parseExp(new Times(exp, right)); } else if (tokens.nextIs("(")) { tokens.eat("("); Exp subexp = parseExp(null); tokens.eat(")"); return parseExp(subexp); } else if (tokens.nextIs(")")) { return exp; } else { try { String s = tokens.next(); int i = Integer.parseInt(s); return parseExp(new Constant(i)); } catch (NumberFormatException e) { throw new SyntaxError ("integer"); } } } public void compile() {} }