Declare unkwon tokens as variables.
A java infix evaluator based on "Shunting Yard" algorithm.
Brought to you by:
fathzer
Hi,
Suppose I let the user input a expression like sin(x), but x could be anything: variable12, z, randomName, u1, etc.
I want to get that unknown token (variable/constant) from the expression and use it as a key to retrieve a value in a map.
In other words, I want modify your code so, when parsing an expression for tokens, unkown tokens are automatically declared as StaticVariables.
I could not identify an appropriate place in the code to make such modification. Could you give me some ideias?
Anonymous
Hi,
Sorry if I didn't understand the question but it seems using StaticVariableSet is what you are looking for.
http://javaluator.sourceforge.net/en/doc/tutorial.php?chapter=variables
It does not require any code modification.
Please let me know if I am wrong.
Best regards,
Jean-Marc Astesana
This is what I tried to do: Override the toValue method of DoubleEvaluator as to it search for the literal in a variables map before throwing the Illegal Argument Exception; then setting the variable if found.
When I try to run the testing it throws the exception:
Exception in thread "main" java.lang.NullPointerException
at userInteraction.AdaptedEvaluator.toValue(AdaptedEvaluator.java:63)
at com.fathzer.soft.javaluator.DoubleEvaluator.toValue(DoubleEvaluator.java:48)
at com.fathzer.soft.javaluator.AbstractEvaluator.output(AbstractEvaluator.java:133)
at com.fathzer.soft.javaluator.AbstractEvaluator.evaluate(AbstractEvaluator.java:343)
at com.fathzer.soft.javaluator.AbstractEvaluator.evaluate(AbstractEvaluator.java:214)
at userInteraction.testing.main(testing.java:23)
So
result.doubleValue()is causing the excpetion but I cannot identify why.Is it clearer now what I am asking for?
Regards,
Pedro
Here is a version of your testing code that works without any subclass. It uses StaticVariableSet:
package userInteraction;
import com.fathzer.soft.javaluator.DoubleEvaluator;
import com.fathzer.soft.javaluator.StaticVariableSet;
Best regards,
Jean-Marc Astesana
Last edit: Fathzer 2016-06-27
Sir, you did not understand... (laughs)
In my particular case, I cannot declare the varible in a default way. That is because it would require me to change too many parts in my program. If I can change only one single method, I save myself a lot of work and avoid some bugs.
Anyway, below is what worked for me, in case anyone has a similar need.
Many thanks you for your attention,
Pedro