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.
In reply to Re: converting a boolean syntax
by BrowserUk
in thread converting a boolean syntax
by glwtta
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |