Re: Simple boolean expression evaluator



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)
.



Relevant Pages

  • RE: FileSearch to locate the latest (last saved) file
    ... Dim sReport As Workbook, sDashboard As Workbook ... Dim fLdr As String, Fil As String, FPath As String, x As String, _ ... FileDates= temp ... For i = 1 To NewestFile ...
    (microsoft.public.excel.programming)
  • Re: locating strings approximately
    ... > I'd like to see if a string exists, even approximately, in another. ... > have looked at edit distance, but that isn't a good choice for finding ... if temp < dist: dist = temp ... dprev, dcurr = dcurr, dprev ...
    (comp.lang.python)
  • Re: Unmanaged code(dll) function: int myfunc (char* temp)
    ... //Assigning an value to temp ... I am using char* not TCHAR* ... Can you help me how to get the value of temp when using string builder ... int myfunc ...
    (microsoft.public.dotnet.framework.compactframework)
  • Help with a program
    ... program.cpp: see declaration of 'movie' ... string title; ... void getline ... {int temp; ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Return of string from function
    ... >void getoutput(char* temp) ... be int if you didn't include the header file where malloc is ... >with out any error messages. ... need, copy the string, and return the pointer from the function. ...
    (comp.lang.c)