Re: Simple boolean expression evaluator
- From: "Mike Williams (TeamB)" <mlwi!!iams@gmail!.com>
- Date: 28 Jun 2005 09:07:53 -0700
Richard Marchand wrote:
> I am searching for a simple function that will take a string of
> boolean expressions like:
>
> "((True of false) and (false or false)) or (true and false)"
>
> And return a boolean result.
>
> Any library available to do this?
What you need is an expression evaluator. The most common approach is
to use a stack to convert the expression to postfix notation (which
effectively removes the parentheses) and then evaluate it by popping
pairs of data from the stack and pushing back each result. I'm sure if
you search the newsgroup archives you'll find plenty of good examples.
Interestingly enough, with just boolean expressions it's possible to do
the evaluation with simple string replace operations. This assumes
that the expression is properly formatted, however:
function Evaluate(Data : string) : string;
var start, temp : string;
begin
temp := lowercase(Data);
while true do begin
Start := temp;
temp := stringreplace(temp, '(true or false)', 'true',
[rfReplaceAll]);
temp := stringreplace(temp, '(true and false)', 'false',
[rfReplaceAll]);
temp := stringreplace(temp, '(false or true)', 'true',
[rfReplaceAll]);
temp := stringreplace(temp, '(false and false)', 'false',
[rfReplaceAll]);
temp := stringreplace(temp, '(true or true)', 'true',
[rfReplaceAll]);
temp := stringreplace(temp, '(true and true)', 'true',
[rfReplaceAll]);
temp := stringreplace(temp, '(false or false)', 'false',
[rfReplaceAll]);
temp := stringreplace(temp, '(false and false)', 'false',
[rfReplaceAll]);
if (start = temp) then break;
end;
Result := temp;
end;
--
-Mike (TeamB)
.
- Prev by Date: Re: HTML Help Authoring tool (must be compatible with MS HTML Help Workshop)
- Next by Date: Re: Key Logger Component
- Previous by thread: HTML Help Authoring tool (must be compatible with MS HTML Help Workshop)
- Next by thread: Re: Simple boolean expression evaluator
- Index(es):
Relevant Pages
|