|
Hmm. Sounds like fun! I remember a college project to parse an arithmetic
expression (with parentheses) and return the result; I imagine similar
logic would work for you. Let me see if I can remember! I don't know how
you are currently parsing your conditional statement; I just assume you
wind up with TRUE or FALSE at the end.
To start off, enclose your original conditional statement in parentheses.
That way you don't have to treat the beginning and end of the statement as
a special case. As you process the conditional statement, whenever you hit
a left parenthesis, save your current status and begin processing as though
you were examining a new statement. You'll need an array to keep track of
your status so you can handle multiple levels of parentheses. When you hit
a right parenthesis, take the value of the statement evaluated so far (TRUE
or FALSE) and jump back to the previous level. The TRUE/FALSE value would
replace the part of the statement that was in parentheses. Continue until
you get to the end of your conditional statement. You'll need to monitor
your array counter. Ideally, the array will be empty when you're done!
Otherwise, you have too many left or right parentheses.
Let's take a look at your example statement
a=b and (c=d or c=e or c=f)
Using the above logic, the program would evaluate "(c=d or c=e or c=f)" as
an individual statement.
Let's assume that evaluates as TRUE. Then the program would "see" the
statement as
a=b and TRUE
To illustrate multiple levels of parentheses, let's try
a=b and (c=d and (e=f or e=g))
The innermost statement "(e=f or e=g)" would be evaluated as an individual
statement (let's assume TRUE).
The program would then "see"
a=b and (c=d and TRUE)
The new innermost statement "(c=d and TRUE)" would be evaluated as an
individual statement (let's assume FALSE).
The program would then "see"
a=b and FALSE
Hopefully this will give you some ideas to work with. Let me know if I need
to clarify.
Richard Casey
-----Original Message-----
From: Terry Grider [SMTP:tgrider@arkansas.net]
Sent: Monday, October 23, 2000 12:44 PM
To: MIDRANGE-L@midrange.com
Subject: Re: Evaluate complex conditions
Sorry guys, I didn't do a very good job of explaining what I was looking
for.
I don't mean I want code that is an example of testing for complex
conditions, I
mean I'm looking for code that will evaluate statements like the one in my
original example and determine the truth of the statement. You see, I'm
writing
a query tool and I want the user to be able to provide selection criteria
in an
english style IF format similar to cobol or other query tools I've seen. I
can
currently handle simple criteria with "and/or" connectors, but I can't seem
to
come up with a good method for handling multiple levels of parenthesis.
+---
| This is the Midrange System Mailing List!
| To submit a new message, send your mail to MIDRANGE-L@midrange.com.
| To subscribe to this list send email to MIDRANGE-L-SUB@midrange.com.
| To unsubscribe from this list send email to MIDRANGE-L-UNSUB@midrange.com.
| Questions should be directed to the list owner/operator: david@midrange.com
+---
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2025 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.