private static final Pattern numberPattern = Pattern.compile("^_*[\\d.,]+$"); private static final Pattern operatorPattern = Pattern.compile("^[-+*/]$"); private static final Pattern parenPattern = Pattern.compile("^[()]$"); /** * Inspects the given string and returns an appropriate instance of a * subclass of {@link Token}. * * @param tok the token to evaluate. * @return Token representation of tok. */ public static Token toToken(String tok) { if(Assignment2.operatorPattern.matcher(tok).matches()){ return new Operator(tok); } else if(Assignment2.numberPattern.matcher(tok).matches()){ return new Operand(Double.parseDouble(tok)); } else if(Assignment2.parenPattern.matcher(tok).matches()){ return new Parenthesis(tok); } else throw new java.lang.RuntimeException(); }