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

J

junk1

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
 
S

Stefan Ram

"(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.
 
B

Bryce

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

TechBookReport

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
 
J

Joan

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
 
T

Thomas G. Marshall

Roedy Green coughed up:
On 24 Aug 2005 06:00:23 -0700, (e-mail address removed) wrote or quoted


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

Roedy Green

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

Andrew Thompson

Roedy Green coughed up:

Roedy, did you switch over to CSS or something similar to make the fonts
nonresizable (in IE)?

Yes. ..And Roedy, I am surprised at you - I thought you
had more sense than to lock the font size (for the poor
IE users - most other browsers take those font sizes as
merely 'suggestions')

Note, Roedy, that your pages have sensible* mark-up, and
are therefore quite robust to font resizing. I tested that
page in Mozilla at 200%, and it was still well organised.

* Sensible in that you mark things by their structure and
meaning, rather than 'this is the text box over on the
right, just below the floating logo..'
 
A

Andrew Thompson

People also complained my fonts were way too big.
.....

untested 'pseudo-CSS' follows..
body{
background: #f6fff6 none;
color: #000000;
font: 13px "Tiresias PCfont Z","Tiresias

font: "100%", "Tiresias PCfont Z", "Tiresias
PCfont",TiresiasScreenfont,"Palatino Linotype","Book Antiqua","Bookman
Old Style","Lucida Sans","Trebuchet MS",Verdana,serif;
}

...should do the magic. But I usually separate the
font attributes into..

font-face: ...
font-size: 100%;
....

Note that while the same effect can be achieved using
font-size: 1em;

The em unit was subject to a quirky bug in (guess which browser)
and is best avoided.

HTH
 
R

Roedy Green

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 switched over the style sheet to use ems for all font measures. I
can make the type tiny and big in IE with the change text size
feature. The Applets still stay the same size.

In Opera, the zoom feature gives the Applets more room, but does not
increase the font sizes.

I hope this lets you access the site again.
 
R

Roedy Green

Yes. ..And Roedy, I am surprised at you - I thought you
had more sense than to lock the font size (for the poor
IE users - most other browsers take those font sizes as
merely 'suggestions')

I read a book about CSS and it pointed out the problems with every
approach. I decided that px was the way to go to avoid trouble and
also to make my applet displays consistent with the inline HTML
displays.

I have now flipped to "em"s, which was my second choice on reading the
O'Reilly fish book, before reading your warning against it. We will
see who screams now.

Standards groups need to create validation suites, and sue anyone who
calls their stuff X when it has not passed validation, getting tough
like the Ada people did.

The point needs to be redefined so that 12 point type will always
appear psychologically the same size. 12 point type on a coarse
resolution screen currently is bigger. Further different fonts
nominally 12 point differ wildly in size even on the same screen. They
need to be normalized.

Applets need to automatically use bigger fonts (in terms of pixels) on
a higher res screen when the angular size of the image stays the same.

You need a single global control to deal with eyesight. You should
not have to adjust every program separately to get a bigger image.
 
A

Andrew Thompson

.
Applets need to automatically use bigger fonts (in terms of pixels) on
a higher res screen when the angular size of the image stays the same.

You need a single global control to deal with eyesight. You should
not have to adjust every program separately to get a bigger image.

Hear hear.
 
R

Raymond DeCampo

Roedy said:
You need a single global control to deal with eyesight. You should
not have to adjust every program separately to get a bigger image.

Wouldn't that be setting your screen resolution?

Ray
 
B

Bryce

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;
}

Instead of specifying a font size, you should probably specify small,
medium, etc. That allows the user to make the fonts larger if they
want.
 
A

Andrew Thompson

Instead of specifying a font size, you should probably specify small,
medium, etc. That allows the user to make the fonts larger if they
want.

AFAIU - this has the same effect as using % or em units,
but the interpretation across browsers can be quite
unpredictable, and it offers less possibilities (choices)*
to the page designer.

* Though that might not be a bad thing. Too many page
designers get carried away and specify thirty different
font sizes. OR font sizes that are ludicrously small
or large.

We would probably all be better off if there was no such
thing as 'setting a font size' in a web page. Instead it
would have been better to have the UA automatically render
<P> at the user's default size, <H1> a *set* size larger,
and special elements for <COPYRIGHTCRAPNOBODYREADS> in
slightly smaller fonts..
 
T

Thomas G. Marshall

Raymond DeCampo coughed up:
Wouldn't that be setting your screen resolution?

Nope. Not all programs in windows are affected by this, particularly those
exploiting bitmap fonts.



--
Unix users who vehemently argue that the "ln" command has its arguments
reversed do not understand much about the design of the utilities. "ln
arg1 arg2" sets the arguments in the same order as "mv arg1 arg2".
Existing file argument to non-existing argument. And in fact, mv
itself is implemented as a link followed by an unlink.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top