If you only need a mechanical conversion, then something like this might be a starting point

#! perl -sw use strict; my @stack; while( <DATA> ) { print; s[NOT\s+][-]g; 1 while s[(?<!\+)(\b\w+\b)\s+AND\s+][+$1 AND ]g or s[\s+AND\s+(?<!\+)(\b\w+\b)][ AND +$1]g; s[\sAND\s][ ]g; s[\s+OR\s+][ ]g; print $_, $/; } __DATA__ word1 AND word2 word1 OR word2 word1 AND NOT word2 (word1 AND NOT word2) OR word3 word1 AND word2 AND word3 (word1 AND word2 OR word3) AND NOT (word4 OR word5) (word1 AND (word2 OR (word3 AND word4 AND NOT word5)) AND word6) OR wo +rd7

Output

D:\Perl\test>268325 word1 AND word2 +word1 +word2 word1 OR word2 word1 word2 word1 AND NOT word2 +word1 -word2 (word1 AND NOT word2) OR word3 (+word1 -word2) word3 word1 AND word2 AND word3 +word1 +word2 +word3 (word1 AND word2 OR word3) AND NOT (word4 OR word5) (+word1 +word2 word3) -(word4 word5) (word1 OR (word2 OR (word3 AND word4 AND NOT word5)) AND word6) OR wor +d7 (word1 (word2 (+word3 +word4 -word5)) +word6) word7

If you want to syntax check the input before conversion then you have more work to do. Regexp::Common::balanced would be a good starting point for extracting the sub-expressions.

Might be fun to code, but it would need a much fuller explanation of what is leagal in the output format.

Eg. is -(sub condition) legal?

What about (+word1 +word2) (+word3 +word4)?

The toughest part is probably giving meaningful error messages when you encounter syntax errors.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller



In reply to Re: converting a boolean syntax by BrowserUk
in thread converting a boolean syntax by glwtta

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.