Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can do it by <a href="http://en.wikipedia.org/wiki/Lexical_analysis" rel="nofollow">Lexical Analysis</a>. There are many lexical generator for java like: JavaCC , JLex etc.</p> <p>Or you can use something like pre/post -fix calculator or stack calculator for parenthesis balancing and to maintain the hierarchy, originally it is done by <a href="http://en.wikipedia.org/wiki/Tree_traversal" rel="nofollow">traversing through a "tree"</a> and in that calculator in place of "1"-"9" numbers (which comes as string) you have to match "true" or "false" and in place of "+", "-" binary operator you have to match and operate "and" and "or"! </p> <p>I have made a Boolean Algebra Parser as I have committed in my previous comment, I think it works, you are also welcome to fork and contribute: <a href="https://github.com/sadaf2605/Java-Boolean-Algebra-Parser" rel="nofollow">https://github.com/sadaf2605/Java-Boolean-Algebra-Parser</a></p> <p>code:</p> <pre><code>public class BooleanAlgebra { static final String CONST_and="And"; static final String CONST_or="Or"; static final String CONST_true="True"; static final String CONST_false="False"; public static void main(String[] args) { System.out.println(booleanAlgebra("False")==false); System.out.println(booleanAlgebra("True")==true); System.out.println(booleanAlgebra("False And False")==false); System.out.println(booleanAlgebra("True And False")==false); System.out.println(booleanAlgebra("True And True")==true); System.out.println(booleanAlgebra("False And True")==false); System.out.println(booleanAlgebra("(False And True)")==false); System.out.println(booleanAlgebra("True Or (False And True)")==true); System.out.println(booleanAlgebra("(True And False) And (False And True) Or True")==true); System.out.println(booleanAlgebra("( (True And False) Or True )")==true); System.out.println(booleanAlgebra("( False Or (True And (False Or True)) Or True )")==true); } static boolean booleanAlgebra(String str){ return TrueFalse(str, false, "Or"); } static boolean TrueFalse(String s,boolean b, String op){ boolean btemp=false; s=s.replaceAll(" ", ""); while(!s.isEmpty()){ if(s.startsWith(CONST_true)){ btemp=true; s=s.substring(4); }else if(s.startsWith(CONST_false)){ btemp=false; s=s.substring(5); }else if(s.startsWith(CONST_and)){ op=CONST_and; s=s.substring(3); }else if(s.startsWith(CONST_or)){ op=CONST_or; s=s.substring(2); }else if(s.startsWith("(")){ int end=s.indexOf(")"); if(end&gt;0){ b=TrueFalse(s.substring(1, end&gt;-1?end:1),b,op); s=s.substring(end); } }else{ s=s.substring(1); } if (op.equals(CONST_and)){ b=b&amp;&amp;btemp; }else if(op.equals(CONST_or)){ b=b||btemp; } } return b; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload