token structure

I'm currently implementing my own math parser - and i have a few questions on how to proceed.

So far my parser is converting an input string into Tokens:

public class Token 
{   
    final public String expression;
    final public int value;

    public Token(String expression, int value)
    {  
        this.expression = expression;
        this.value = value;
    }
}

Each String, expression, is a valid entry - number, operator or a function. The Integer type is passed into an enum to identify the token.

When the input string is separated into tokens the expression will be parsed with the Shunting-yard algorithm.

My question : I want the tokens to create objects of their type. One way of doing this might be:

    public abstract class MathCommand 
    {

    final static Hashtable<Character, Operator> operatorTable = new Hashtable<Character, Operator>()
    {{      

            put('+',    new Addition());
            put('-',    new Subtraction());
            put('/',    new Division());
            put('*',    new Multiplication());

    }};

    public abstract Object getMathCommand();

    }

And a similar Hashtable for functions.

Now, the class Token extends MathCommand - and Token can return either a function or an operator of its type. The downside is that Token returns an Object, and not an Operator or a Function. The difference between methods in an operator and a function is obviously not big.

An operator has the method:

getValue(String number1, String number2);

And a function has the method:

getValue(String number);

Can i somehow implement an Interface and override the method getValue to be one of the above?


他们可以有一个共同的接口:

interface Callable {
    int getNumberOfArguments();
    String getValue(String[] args);
}
链接地址: http://www.djcxy.com/p/66548.html

上一篇: 使用Java构建HTML5离线Web应用程序

下一篇: 令牌结构