Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   How do I evaluate a logic expression entered at run time? (http://www.velocityreviews.com/forums/t145773-how-do-i-evaluate-a-logic-expression-entered-at-run-time.html)

junk1@davidbevan.co.uk 08-24-2005 01:00 PM

How do I evaluate a logic expression entered at run time?
 
I have a problem with a program I am designing. Basically at some point
it will be passed a random logic query, for example...

"(A=B and C=D) OR (E=F)"

....the brackets and 'AND' and 'OR' statements may be in any order and
there maybe any number of '=' expressions.

I can deal with evaluating the 'A=B' etc and so the statment above
becomes...

"(true AND false) OR (true)"

....which of course evaluates to true.

What I am stuck on is writing something that evaluates all the AND and
OR and '()' logic.

Is there an easy way to evaluate this?


Thanks

David Bevan
http://www.davidbevan.co.uk


Stefan Ram 08-24-2005 01:15 PM

Re: How do I evaluate a logic expression entered at run time?
 
junk1@davidbevan.co.uk writes:
>"(true AND false) OR (true)"
>Is there an easy way to evaluate this?


What is "easy" depends on the person.

To someone writing compilers as his day-job this is almost
trivial or obvious.

Of all people who just started to learn a programming
language, on the other side, some of them will never be
capable to implement this.

Another answer would be:

- write a parser to parse the expression into an AST
- write an evaluator for that AST

For me, it is easier to write this in Java, for
others it is easier to use libraries similar to
"lex" and "yacc".

https://javacc.dev.java.net/

Or, you might use Java 1.6, which has scripting support,
AFAIK, and might be available as a preview. Then the
above expression might be (slightly rewritten and )
interpret by the scripting language.




iamfractal@hotmail.com 08-24-2005 01:17 PM

Re: How do I evaluate a logic expression entered at run time?
 

junk1@davidbevan.co.uk skrev:

> I have a problem with a program I am designing. Basically at some point

Snip.
> Is there an easy way to evaluate this?
>
>
> Thanks
>
> David Bevan
> http://www.davidbevan.co.uk



You're in the twilight world of compiler-heads, there.

There's source code for a recursive parser available at:
http://www.jamesholmes.com/TheArtOfJava/

Though you'll probably have to buy the book to get the most from it.

..ed

--
www.EdmundKirwan.com - Home of The Fractal Class Composition.


Bryce 08-24-2005 02:21 PM

Re: How do I evaluate a logic expression entered at run time?
 
On 24 Aug 2005 06:00:23 -0700, junk1@davidbevan.co.uk wrote:

>I have a problem with a program I am designing. Basically at some point
>it will be passed a random logic query, for example...
>
>"(A=B and C=D) OR (E=F)"
>
>...the brackets and 'AND' and 'OR' statements may be in any order and
>there maybe any number of '=' expressions.
>
>I can deal with evaluating the 'A=B' etc and so the statment above
>becomes...
>
>"(true AND false) OR (true)"
>
>...which of course evaluates to true.
>
>What I am stuck on is writing something that evaluates all the AND and
>OR and '()' logic.
>
>Is there an easy way to evaluate this?


What I've done in the past is to convert the infix expression above to
RPN (reverse polish notation), then its easy to evaluate RPN.

--
now with more cowbell

TechBookReport 08-24-2005 02:26 PM

Re: How do I evaluate a logic expression entered at run time?
 
junk1@davidbevan.co.uk wrote:
> I have a problem with a program I am designing. Basically at some point
> it will be passed a random logic query, for example...
>
> "(A=B and C=D) OR (E=F)"
>
> ...the brackets and 'AND' and 'OR' statements may be in any order and
> there maybe any number of '=' expressions.
>
> I can deal with evaluating the 'A=B' etc and so the statment above
> becomes...
>
> "(true AND false) OR (true)"
>
> ...which of course evaluates to true.
>
> What I am stuck on is writing something that evaluates all the AND and
> OR and '()' logic.
>
> Is there an easy way to evaluate this?
>
>
> Thanks
>
> David Bevan
> http://www.davidbevan.co.uk
>

Have you look at something like the Java Expression Parser (JEP)? It
includes support for boolean expressions.

Take a look at: http://www.singularsys.com/jep/

Pan
---

TechBookReport Java: http://www.techbookreport.com/JavaIndex.html

barilla 08-24-2005 02:30 PM

Re: How do I evaluate a logic expression entered at run time?
 
BSH is great for that.
http://www.beanshell.org/

you can use strict java or simplified java "beanshell"
or a mix of both.

paul.

Roedy Green 08-24-2005 05:35 PM

Re: How do I evaluate a logic expression entered at run time?
 
On 24 Aug 2005 06:00:23 -0700, junk1@davidbevan.co.uk wrote or quoted
:

>What I am stuck on is writing something that evaluates all the AND and
>OR and '()' logic.
>
>Is there an easy way to evaluate this?


See http://mindprod.com/jgloss/eval.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com

Joan 08-24-2005 05:56 PM

Re: How do I evaluate a logic expression entered at run time?
 

<junk1@davidbevan.co.uk> wrote in message
news:1124888423.648957.219170@g44g2000cwa.googlegr oups.com...
>I have a problem with a program I am designing. Basically at
>some point
> it will be passed a random logic query, for example...
>
> "(A=B and C=D) OR (E=F)"
>
> ...the brackets and 'AND' and 'OR' statements may be in any
> order and
> there maybe any number of '=' expressions.
>
> I can deal with evaluating the 'A=B' etc and so the statment
> above
> becomes...
>
> "(true AND false) OR (true)"
>
> ...which of course evaluates to true.
>
> What I am stuck on is writing something that evaluates all the
> AND and
> OR and '()' logic.
>
> Is there an easy way to evaluate this?
>
> Summary JavaCC is a parser/scanner generator for java



Thomas G. Marshall 08-24-2005 11:49 PM

Re: How do I evaluate a logic expression entered at run time?
 
Roedy Green coughed up:
> On 24 Aug 2005 06:00:23 -0700, junk1@davidbevan.co.uk wrote or quoted
>>

>
>> What I am stuck on is writing something that evaluates all the AND
>> and OR and '()' logic.
>>
>> Is there an easy way to evaluate this?

>
> See http://mindprod.com/jgloss/eval.html



Roedy, did you switch over to CSS or something similar to make the fonts
nonresizable (in IE)? They are murder to read on my system, and it's XP
setup with "huge fonts". Is it using the default size of some charset
encoding on my system perhaps?


--
I've seen this a few times--Don't make this mistake:

Dwight: "This thing is wildly available."
Smedly: "Did you mean wildly, or /widely/ ?"
Dwight: "Both!", said while nodding emphatically.

Dwight was exposed to have made a grammatical
error and tries to cover it up by thinking
fast. This is so painfully obvious that he
only succeeds in looking worse.



Roedy Green 08-25-2005 06:06 AM

Re: How do I evaluate a logic expression entered at run time?
 
On Wed, 24 Aug 2005 23:49:16 GMT, "Thomas G. Marshall"
<tgm2tothe10thpower@replacetextwithnumber.hotmail. com> wrote or quoted
:

>Roedy, did you switch over to CSS or something similar to make the fonts
>nonresizable (in IE)? They are murder to read on my system, and it's XP
>setup with "huge fonts". Is it using the default size of some charset
>encoding on my system perhaps?


I have been using CSS for many years. I did change the default body
fonts month or so ago when I got requests to make the default a serif
font. People also complained my fonts were way too big.

the style that is causing you trouble is:

body{
background: #f6fff6 none;
color: #000000;
font: 13px "Tiresias PCfont Z","Tiresias
PCfont",TiresiasScreenfont,"Palatino Linotype","Book Antiqua","Bookman
Old Style","Lucida Sans","Trebuchet MS",Verdana,serif;
}

CSS drives you nuts. You get something working in one browser and it
breaks another. Then it breaks with somebody else's fonts or
settings.

If you are interested in working on this, you can download a page and
the style sheet at mindprod.css and fiddle it until it works ok on
your machine. Then I can try to find a compromise that works on your
machine and works on the various browsers I attempt to support too.


What happens when you see the program listings displayed by Applet?
e.g. on http://mindprod.com/jgloss/bitcount.html How are those fonts
for size?

Are you doing something unusual? i.e. have you cranked up the font
size because you have trouble seeing?

Java has a fundamental problem working in pixels. Pixels will get
smaller and smaller over time. Applet will shrink to unreadably small
size. At some point Applets will all have to be rewritten in floating
point co-ordinates is some of user-centric units -- e.g. radians
subtended at the eye.


Have you tried Opera? It lets you zoom the font and image size
independent of what the website does.

--
Canadian Mind Products, Roedy Green.
http://mindprod.com


All times are GMT. The time now is 01:58 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.