Re-using of token lists in with Analyzer class

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Re-using of token lists in with Analyzer class

Sonja-5
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
[hidden email]
http://lists.nongnu.org/mailman/listinfo/grammatica-users
Reply | Threaded
Open this post in threaded view
|

Re: Re-using of token lists in with Analyzer class

Per Cederberg
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
> [hidden email]
> http://lists.nongnu.org/mailman/listinfo/grammatica-users
>
>


_______________________________________________
Grammatica-users mailing list
[hidden email]
http://lists.nongnu.org/mailman/listinfo/grammatica-users
Reply | Threaded
Open this post in threaded view
|

Re: Re-using of token lists in with Analyzer class

Sonja-5
Hi and thanks four your reply,

I'll think about your suggestion. Just hoped there would be a more effortless
solution...

Klaus

Per Cederberg schrieb:

> 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
>> [hidden email]
>> http://lists.nongnu.org/mailman/listinfo/grammatica-users
>>
>>
>
>
> _______________________________________________
> Grammatica-users mailing list
> [hidden email]
> http://lists.nongnu.org/mailman/listinfo/grammatica-users
>


_______________________________________________
Grammatica-users mailing list
[hidden email]
http://lists.nongnu.org/mailman/listinfo/grammatica-users