473,569 Members | 2,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

boolean logic parser - need help

Has anyone worked on code that that can parse evaluation expressions
(could be numbers or strings) like

( ( "dog" = "dog" ) or "foo" = "bar" ) and ("cow" = "bat" and "bye" =
"hi") or ("math" = "fun")

or

( ( 1 = 5 ) or ( 2 < 3 ) ) and (1 <= 6)

or (here it gets a little complicated with math expressions)

( ( 1 = ((5*2)+1) ) or ( 2 < 3 ) ) and (1 <= 6)

?

I have a compare functions to evaluate <, <=, =, !=, >=, > for strings
and numbers, and a math parser, and all the values would be literal. I
am looking either to figure out how to do it, or find existing code
that would work.

I am not sure recursion is correct to do this, or how to put it all
together, parsing out the "AND"s and "OR"s, and keeping track of the
parenthesis hierarchy (while ignoring math expressions which also have
parenthesis) in the correct order of precedence, and ultimately
returning a single boolean value for the entire thing.

Here is some pseudocode I put together to work it out - any help would
be appreciated.

EVALUATE A BOOLEAN EXPRESSION IN A STRING
AND RETURN TRUE OR FALSE:
if odd # of parens (not part of quoted string) return error
if quotes, string mode, else math mode
has parens?
yes: analyze further
iPosNextEnd = next closing parens
find closing parens by adding +1 evertime an open paren
(not part of quoted text) found, -1 every time a closed
paren found. when parencount back to 0 we know we have
reached the end of the current paren block.
between iStart and iPosNextEnd
look inside (outside of quotes)
for comparison operator
yes? break down further
no? found value.
if math mode
send contents to math parser to evaluate
use string value as is
no: evaluate
look inside (outside of quotes)
for AND/OR
if 1 AND/OR
evaluate left side vs right side
else MULTIPLE AND/ORs (see below):
look for all ANDs, all ORs, store position in list (OPERATOR, VAL
1, VAL 2, RESULT)
traverse list and evaluate
evaluate ANDs, store result
evaluate ORs, store result

MULTIPLE AND/OR WITHOUT PARENS:
it seems expressions with "and" are evaluated first.
take a statement like:
If T1 = 1 And T2 = 2 Or T3 = 3 Then

T1 T2 T3 Evaluates to
-- -- -- ------------
1 2 3 true
1 2 4 true
1 5 4 false
1 5 3 true

PS I found some Java code on a Deutch site that looks like it might be
similar to what I need to do. I know vb.net not Java, and the comments
aren't in English, so I don't really understand all it is doing, but I
see it is using some kind of lists or tokens (what's a token?) to keep
track of things. Can anyone convert this to .NET without too much
trouble?

http://www.brian-schroeder.de/parser/

Jul 21 '05 #1
5 3594
You can try http://babelfish.altavista.com to translate the German to
English (or whatever other language you'd prefer), try it, it's quite good.

As for what tokens are, in java speak, a token is a part of something. If
for instance you have a string "abc,def,gh i" (comma-seperated), you can use
what Java calls a StringTokenizer to break the string up into it's parts (or
tokens). It is similar to string.Split, which returns an array, the
StringTokenizer however returns a collection which you can iterate through
using methods like NextToken().
"Mad Scientist Jr" <us************ *@yahoo.com> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.com...
Has anyone worked on code that that can parse evaluation expressions
(could be numbers or strings) like

( ( "dog" = "dog" ) or "foo" = "bar" ) and ("cow" = "bat" and "bye" =
"hi") or ("math" = "fun")

or

( ( 1 = 5 ) or ( 2 < 3 ) ) and (1 <= 6)

or (here it gets a little complicated with math expressions)

( ( 1 = ((5*2)+1) ) or ( 2 < 3 ) ) and (1 <= 6)

?

I have a compare functions to evaluate <, <=, =, !=, >=, > for strings
and numbers, and a math parser, and all the values would be literal. I
am looking either to figure out how to do it, or find existing code
that would work.

I am not sure recursion is correct to do this, or how to put it all
together, parsing out the "AND"s and "OR"s, and keeping track of the
parenthesis hierarchy (while ignoring math expressions which also have
parenthesis) in the correct order of precedence, and ultimately
returning a single boolean value for the entire thing.

Here is some pseudocode I put together to work it out - any help would
be appreciated.

EVALUATE A BOOLEAN EXPRESSION IN A STRING
AND RETURN TRUE OR FALSE:
if odd # of parens (not part of quoted string) return error
if quotes, string mode, else math mode
has parens?
yes: analyze further
iPosNextEnd = next closing parens
find closing parens by adding +1 evertime an open paren
(not part of quoted text) found, -1 every time a closed
paren found. when parencount back to 0 we know we have
reached the end of the current paren block.
between iStart and iPosNextEnd
look inside (outside of quotes)
for comparison operator
yes? break down further
no? found value.
if math mode
send contents to math parser to evaluate
use string value as is
no: evaluate
look inside (outside of quotes)
for AND/OR
if 1 AND/OR
evaluate left side vs right side
else MULTIPLE AND/ORs (see below):
look for all ANDs, all ORs, store position in list (OPERATOR, VAL
1, VAL 2, RESULT)
traverse list and evaluate
evaluate ANDs, store result
evaluate ORs, store result

MULTIPLE AND/OR WITHOUT PARENS:
it seems expressions with "and" are evaluated first.
take a statement like:
If T1 = 1 And T2 = 2 Or T3 = 3 Then

T1 T2 T3 Evaluates to
-- -- -- ------------
1 2 3 true
1 2 4 true
1 5 4 false
1 5 3 true

PS I found some Java code on a Deutch site that looks like it might be
similar to what I need to do. I know vb.net not Java, and the comments
aren't in English, so I don't really understand all it is doing, but I
see it is using some kind of lists or tokens (what's a token?) to keep
track of things. Can anyone convert this to .NET without too much
trouble?

http://www.brian-schroeder.de/parser/

Jul 21 '05 #2
Aha.. thanks for that explanation.

Jako Menkveld wrote:
You can try http://babelfish.altavista.com to translate the German to English (or whatever other language you'd prefer), try it, it's quite good.
As for what tokens are, in java speak, a token is a part of something. If for instance you have a string "abc,def,gh i" (comma-seperated), you can use what Java calls a StringTokenizer to break the string up into it's parts (or tokens). It is similar to string.Split, which returns an array, the
StringTokenizer however returns a collection which you can iterate through using methods like NextToken().
"Mad Scientist Jr" <us************ *@yahoo.com> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.com...
Has anyone worked on code that that can parse evaluation expressions (could be numbers or strings) like

( ( "dog" = "dog" ) or "foo" = "bar" ) and ("cow" = "bat" and "bye" = "hi") or ("math" = "fun")

or

( ( 1 = 5 ) or ( 2 < 3 ) ) and (1 <= 6)

or (here it gets a little complicated with math expressions)

( ( 1 = ((5*2)+1) ) or ( 2 < 3 ) ) and (1 <= 6)

?

I have a compare functions to evaluate <, <=, =, !=, >=, > for strings and numbers, and a math parser, and all the values would be literal. I am looking either to figure out how to do it, or find existing code
that would work.

I am not sure recursion is correct to do this, or how to put it all
together, parsing out the "AND"s and "OR"s, and keeping track of the parenthesis hierarchy (while ignoring math expressions which also have parenthesis) in the correct order of precedence, and ultimately
returning a single boolean value for the entire thing.

Here is some pseudocode I put together to work it out - any help would be appreciated.

EVALUATE A BOOLEAN EXPRESSION IN A STRING
AND RETURN TRUE OR FALSE:
if odd # of parens (not part of quoted string) return error
if quotes, string mode, else math mode
has parens?
yes: analyze further
iPosNextEnd = next closing parens
find closing parens by adding +1 evertime an open paren
(not part of quoted text) found, -1 every time a closed
paren found. when parencount back to 0 we know we have
reached the end of the current paren block.
between iStart and iPosNextEnd
look inside (outside of quotes)
for comparison operator
yes? break down further
no? found value.
if math mode
send contents to math parser to evaluate
use string value as is
no: evaluate
look inside (outside of quotes)
for AND/OR
if 1 AND/OR
evaluate left side vs right side
else MULTIPLE AND/ORs (see below):
look for all ANDs, all ORs, store position in list (OPERATOR, VAL 1, VAL 2, RESULT)
traverse list and evaluate
evaluate ANDs, store result
evaluate ORs, store result

MULTIPLE AND/OR WITHOUT PARENS:
it seems expressions with "and" are evaluated first.
take a statement like:
If T1 = 1 And T2 = 2 Or T3 = 3 Then

T1 T2 T3 Evaluates to
-- -- -- ------------
1 2 3 true
1 2 4 true
1 5 4 false
1 5 3 true

PS I found some Java code on a Deutch site that looks like it might be similar to what I need to do. I know vb.net not Java, and the comments aren't in English, so I don't really understand all it is doing, but I see it is using some kind of lists or tokens (what's a token?) to keep track of things. Can anyone convert this to .NET without too much
trouble?

http://www.brian-schroeder.de/parser/


Jul 21 '05 #3
you don't happen to know any java to vb.net conversion programs?
: )

Jul 21 '05 #4
No, sorry, I don't.

What I can suggest is write psuedo code for the java and then try to write
VB from that. If you have problems understanding any of the java logic,
please feel free to ask! (I know this isn't a java newsgroup, but I'm sure
nobody would mind...)

"Mad Scientist Jr" <us************ *@yahoo.com> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.com...
you don't happen to know any java to vb.net conversion programs?
: )

Jul 21 '05 #5
Found it !

The Microsoft Script Control has an Eval function.
http://www.devx.com/vb2themax/Tip/18773

It works like a charm ... it even compares string expressions. All you
have to do is parse your expression for your variable names and drop in
the values (if strings drop in quoted values).

Does anyone know of a native .NET version?

Jul 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
313
by: Mad Scientist Jr | last post by:
Has anyone worked on code that that can parse evaluation expressions (could be numbers or strings) like ( ( "dog" = "dog" ) or "foo" = "bar" ) and ("cow" = "bat" and "bye" = "hi") or ("math" = "fun") or ( ( 1 = 5 ) or ( 2 < 3 ) ) and (1 <= 6)
8
4844
by: shawnk | last post by:
Given several nullable boolean flags; bool? l_flg_01 = true; bool? l_flg_02 = false; bool? l_flg_03 = true; bool? l_result_flg = null; I would have liked one of these syntax formats to work; // if ( l_flg_01 && l_flg_02 && l_flg_03 ) // Line A
16
3230
by: Shawnk | last post by:
I would like to perform various boolean operations on bitmapped (FlagsAttribute) enum types for a state machine design as in; ------------------- enum portState { Unknown, Open,
10
8783
by: Simon Brooke | last post by:
The DOM API has included public Node importNode(Node,boolean) as a method of the Document interface for a long time. Does anything actually implement it? Xerces 2 is giving me: org.w3c.dom.DOMException: NOT_SUPPORTED_ERR: The implementation does not support the requested type of object or operation. at...
5
2605
by: Abandoned | last post by:
Hi.. I try a boolean parser in python since 1 weak.. But i can't do this because this is very complicated :( Do you know any blooean parser script in python or how do i write a boolean parser ? example query: ((google or yahoo) or (live msn)) not web I'm sorry my bad english. King Regards..
4
6625
by: =?Utf-8?B?ZGF2ZWJ5dGhlc2Vh?= | last post by:
Hi folks, Boolean.Parse("true") returns true, but what if you need to parse something such as - Boolean.Parse("(false && false) || (false || (true && true))"); Does something exist in the .NET framework to Parse this kind of string to one final boolean result?
4
3219
by: Bartc | last post by:
"vaib" <vaibhavpanghal@gmail.comwrote in message news:26a44cc5-0f08-41fe-859b-0d27daf3ca1d@f24g2000prh.googlegroups.com... I don't know the formal approach to these things but I haven't come across an RE grammar before, not for an entire language anyway. The usual approach if you're not using external tools is to program using...
3
1248
by: Kevin Audleman | last post by:
I wrote a piece of boolean logic that doesn't work and I don't know why. The code should ONLY do something if the session variable is equal to 900123: if( ! $_SESSION == '900123') { //do some logic } However it always returns true. After staring at it for a while, I realized that the PHP way of doing things is to use the != operator,
1
2105
by: sunil | last post by:
Hello, I have a boolean expression thats specified in pre/in/post fix notations, lets assume infix for the sake of this argument. The expression can be in any of the following forms: Form A: ------------- (As1&&/||Bs1&&/||Cs1 )|| (As2&&/||Bs2&&/||Cs2)||....(Asn&&/||Bsn&&/|| Csn) with following rules: -Inner ANDs/ORs are optional but if...
0
7614
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8125
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7676
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7974
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.