3

I have a file which contains many rows of condition strings, and I need to read that file and convert the string of logical conditions into actual code. There are several parameters, and each line of the condition checks if one or many parameters meet the specified value or contains a specified value. For example:

$parameterA = "value_1" AND ($parameterB CONTAIN "%value_2%" OR $parameterC = "value_3")

the "=", "AND", and "OR" have the meaning of "==", "&&", and "||". "%" is the wild card.

After I convert it to a code, it should look like:

if (obj.parameterA == "value_1" && (obj.parameterB.contains("value_2") || obj.parameterC == "value_3"))
     return true;
else return false;

where "obj" is an instance of some class.

I searched for other posts here, and it seems ANTLR may be a good choice. However, I have no experience with that, and not sure how difficult to use it with Java. I just want to check if there are any other good ideas before I dive into all the details of ANTLR. Thank you very much for your help!

Jeremy
  • 22,188
  • 4
  • 68
  • 81
EXP0
  • 872
  • 2
  • 12
  • 20
  • 2
    Side note, you probably want `obj.parameterA.equals("value_1")` instead of `==`. – hmjd May 08 '12 at 19:41
  • the line must be as it is or can be in javascript? I ask you this because in that case you can use the built in javascript engine. – Juan Alberto López Cavallotti May 08 '12 at 19:45
  • @hmjd : Thank you for pointing that out. I normally use C++ and I'm not good at Java. I should have mentioned that in my post. – EXP0 May 08 '12 at 19:46
  • @JuanAlbertoLópezCavallotti : It's a text file generated by someone else. Right now, it seems the line must be as-is. I have no experience with javascript. What kind of change do I need to make to the line so that I can use javascript? Thank you. – EXP0 May 08 '12 at 19:49
  • Acutally if you can get the line generated in javascript or apply some kind of conversion then you could evaulate it using Rhino, for more information look here http://www.mozilla.org/rhino/doc.html. – Juan Alberto López Cavallotti May 08 '12 at 19:53

2 Answers2

0

Actually I don't know if an existing library does this, but the classic way to evaluate an expression is to make it in two steps:

The expression you have is in infix notation, so you need to convert it to prefix notation, this is to remove the parenthesis and place the operators in front of the operands.

In the second step you evaluate the expression by using a simple recursive algorithm which in turn will also have the responsibility to resolve the variables.

One more thing, you should give the assignment operator the max priority.

I've written an open source implementation of an expression evaluator: maybe you can extend it to fit your needs.

jDTO Expression evaluator source

Hope this helps.

0

For very simple languages (such as boolean expressions), tools like ANTLR work but are really overkill. You can hand-code a parser pretty easily. See my SO answer on hand-coding a recursive descent parser.

Community
  • 1
  • 1
Ira Baxter
  • 93,541
  • 22
  • 172
  • 341