UNB/ CS/ David Bremner/ teaching/ java/ StringParser.java
public class StringParser{
    private String input;

    public StringParser(String str){
        input=str;
    }

    private void advance(int howFar){
        input=input.substring(howFar);
    }

    private int eval(){
        int left;
        if (match("(")) {
            advance(1);
            left=eval();
            insist(")");
        } else {
            left=parseInt();
        }

        if (input.length() > 0){
            char op=input.charAt(0);

            if (op=='+' || op=='*') {
                advance(1);
                int right=eval();
                if (op=='+'){
                    return left+right;
                } else {
                    return left*right;
                }
            }
        }
        return left;
    }

    private boolean match(String pat){
        return input.startsWith(pat);
    }

    private void insist(String pat){
        if (!match(pat)){
            throw new IllegalArgumentException(input);
        }
        advance(pat.length());
    }
    private int parseInt(){
        int i=0;
        int rval;
        while (i<input.length() &&
               '0' <= input.charAt(i) && input.charAt(i) <= '9'){
            i++;
        }

        rval= Integer.parseInt(input.substring(0,i));
        advance(i);
        return rval;
    }

    public static void main(String[] args){

        StringParser Parser=new StringParser("((3+4)*2)+3)");

        int i=Parser.eval();

        System.out.println(i);
    }


}
//