1

I have a string like ((TRUE && TRUE) || (TRUE && FALSE)) which I want to convert to boolean. How can I achieve this?

I tried using Boolean.parseBoolean(String) and Boolean.valueOf(String) but I think it works only if the string is as expected like 'True' only

user5189893
  • 23
  • 1
  • 5

2 Answers2

3

You can "abuse" the similarity with JavaScript and use a standard available scripting engine:

    ScriptEngineManager mgr = new ScriptEngineManager();
    ScriptEngine engine = mgr.getEngineByName("JavaScript"); 
    String expr = "((TRUE && TRUE) || (TRUE && FALSE))".toLowerCase();
    boolean result = Boolean.valueOf(engine.eval(expr));

Not sure whether lowercase and valueOf is right. Maybe a (Boolean) case will do.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • Thanks and it worked. Now, I have another query say I have a string like '((A&&B)||(A&&C))', then how can I evaluate as an boolean expression? – user5189893 Aug 10 '15 at 06:22
  • `engine.put("A", Boolean.TRUE);` (or just `true`). – Joop Eggen Aug 10 '15 at 07:37
  • This would be fine if the syntax is proper. The actual problem comes when the user gives invalid syntax like '((A&&B)||(AA&&C))'. If we defaultly set the value of A to true, the second part of the statement will not be evaluated and result would be true. The issue would be resolved if we set the value of A to false, but fails in another invalid expression like '((A&&B)||(A&&CC))' in which the CC would not evaluated. – user5189893 Aug 10 '15 at 14:01
  • I take it you do not want the lax handling of undefined in JavaScript. One can implement ones own variable binding: [setBindings/createBindings](http://docs.oracle.com/javase/7/docs/api/javax/script/ScriptEngine.html#setBindings%28javax.script.Bindings,%20int%29) and then throw an exception or such. – Joop Eggen Aug 10 '15 at 14:08
1

you need to build an expression parser. First of all, you need to decide on what the syntax of what your allowed epressions are. From your syntax above it looks like C. You should be able to find abundant examples of parser for C expressions by just Googling. Of course, you can also decide to write one by hand. when parsing a language you can chose to use a generator such as ANTLR or the more traditional LEX + YACC, in which case you will provide a grammar-based description of your language and code generators will spit out the program that parses such language. You will then incorporate such generated code in your program. For well known languages such as C and JAVA those solutions already have pre-cooked grammars, although you might have to extract just the expression part of it.

If the expression is in a traditionally interpreted language such as Python or Javascript, you can opt to incorporate an interpreter in your java program and feed it the expression instead.

Finally, if your expressions are in Java syntax and you're flexible to use an early version of Java 9, you may be able to tap into the new Java Shell feature.

Roberto Attias
  • 1,883
  • 1
  • 11
  • 21