[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Grammatica-users] Re-using of token lists in with Analyzer class
From: |
Per Cederberg |
Subject: |
Re: [Grammatica-users] Re-using of token lists in with Analyzer class |
Date: |
Fri, 20 Jul 2007 09:25:59 +0200 |
User-agent: |
Thunderbird 2.0.0.4 (Macintosh/20070604) |
Hi,
Currently there is no way to analyze a parse tree without
rebuilding it each time. That doesn't seem optimal in your
case, so I'd recommend that you create your own internal
data structure to represent your arithmetic expression in
an efficient way.
The easiest most obvious way would perhaps be to create
an interface something like this:
public interface Expression {
/**
* Returns a list of all used variable names.
*/
ArrayList getVariables();
/**
* Evaluates the expression with the specified
* variable bindings (name + Double value).
*/
double eval(HashMap variables);
}
Then the analyzer should just create appropriate subclass
instances for this interface upon parsing the text string.
For the expression below for instance, you'd create
instances of the following imaginary classes:
AdditionExpresson(Expression exp1, Expression exp2)
DivisionExpression(Expression exp1, Expression exp2)
ConstantExpression(Double value)
VariableExpression(String name)
Cheers,
/Per
Sonja wrote:
Hi,
is there any way how I can re-use an already parsed file/string?
Eg. I have this string: (12+y)/3
Now I want to evaluate it with an instance of a class derived from
Analyzer.
So far this is no problem (new ArithmeticParser(inputReade, analyzer) )
but now
I want to evaluate it not just ones but about one million times.
My question now is: Do I always have to parse and build an token-list
again? Or
is there any way to use the previous built token-list again in the
Analyzer object?
(I have to evaluate this string so often because I always replace the
variables
(y) with different values in the Analyzer class!)
Thanks for any help!
Klaus
_______________________________________________
Grammatica-users mailing list
address@hidden
http://lists.nongnu.org/mailman/listinfo/grammatica-users