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

Discussion in 'Java' started by junk1@davidbevan.co.uk, Aug 24, 2005.

  1. Guest

    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
     
    , Aug 24, 2005
    #1
    1. Advertising

  2. Stefan Ram Guest

    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.
     
    Stefan Ram, Aug 24, 2005
    #2
    1. Advertising

  3. Guest

    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.
     
    , Aug 24, 2005
    #3
  4. Bryce Guest

    On 24 Aug 2005 06:00:23 -0700, 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
     
    Bryce, Aug 24, 2005
    #4
  5. 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
     
    TechBookReport, Aug 24, 2005
    #5
  6. barilla Guest

    BSH is great for that.
    http://www.beanshell.org/

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

    paul.
     
    barilla, Aug 24, 2005
    #6
  7. Roedy Green Guest

    On 24 Aug 2005 06:00:23 -0700, 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
     
    Roedy Green, Aug 24, 2005
    #7
  8. Joan Guest

    <> wrote in message
    news:...
    >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
     
    Joan, Aug 24, 2005
    #8
  9. Roedy Green coughed up:
    > On 24 Aug 2005 06:00:23 -0700, 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.
     
    Thomas G. Marshall, Aug 25, 2005
    #9
  10. Roedy Green Guest

    On Wed, 24 Aug 2005 23:49:16 GMT, "Thomas G. Marshall"
    <> 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
     
    Roedy Green, Aug 25, 2005
    #10
  11. On Wed, 24 Aug 2005 23:49:16 GMT, Thomas G. Marshall wrote:

    > Roedy Green coughed up:


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

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

    --
    Andrew Thompson
    physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
    "You make connections, you win elections, but that ain't gonna be enough.."
    The Angels 'City out of Control'
     
    Andrew Thompson, Aug 25, 2005
    #11
  12. On Thu, 25 Aug 2005 06:06:55 GMT, Roedy Green wrote:

    > 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

    --
    Andrew Thompson
    physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
    "I know what I want but I just don't know ..how to go about gettin' it."
    Jimi Hendrix 'Manic Depression'
     
    Andrew Thompson, Aug 25, 2005
    #12
  13. Roedy Green Guest

    On Wed, 24 Aug 2005 23:49:16 GMT, "Thomas G. Marshall"
    <> 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 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.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com
     
    Roedy Green, Aug 25, 2005
    #13
  14. TechBookReport, Aug 25, 2005
    #14
  15. Roedy Green Guest

    On Thu, 25 Aug 2005 07:10:52 GMT, Andrew Thompson
    <> wrote or quoted :

    >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.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com
     
    Roedy Green, Aug 25, 2005
    #15
  16. On Thu, 25 Aug 2005 08:32:00 GMT, Roedy Green wrote:
    ....
    > 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.

    --
    Andrew Thompson
    physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
    "Sing out Hare Hare, dance the Hoochie Koo."
    Led Zeppelin 'The Song Remains The Same'
     
    Andrew Thompson, Aug 25, 2005
    #16
  17. Roedy Green wrote:
    >
    > 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

    --
    XML is the programmer's duct tape.
     
    Raymond DeCampo, Aug 25, 2005
    #17
  18. Bryce Guest

    On Thu, 25 Aug 2005 06:06:55 GMT, Roedy Green
    <> wrote:

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

    --
    now with more cowbell
     
    Bryce, Aug 25, 2005
    #18
  19. On Thu, 25 Aug 2005 09:52:14 -0400, Bryce wrote:

    > On Thu, 25 Aug 2005 06:06:55 GMT, Roedy Green
    > <> wrote:
    >
    >>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.


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

    --
    Andrew Thompson
    physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
    "I knew it was you who did it with your voodoo.."
    Divinyls 'Siren (Never Let You Go)'
     
    Andrew Thompson, Aug 25, 2005
    #19
  20. Raymond DeCampo coughed up:
    > Roedy Green wrote:
    >>
    >> 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?


    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.
     
    Thomas G. Marshall, Aug 25, 2005
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TN Bella
    Replies:
    1
    Views:
    1,504
    TN Bella
    Jul 1, 2004
  2. Jason luo
    Replies:
    3
    Views:
    548
    Peter Ammon
    Aug 19, 2004
  3. Michael Haberfellner

    Evaluate Checkboxes entered at Runtime

    Michael Haberfellner, Mar 5, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    477
    Michael Haberfellner
    Mar 6, 2007
  4. Pierre Yves
    Replies:
    2
    Views:
    490
    Pierre Yves
    Jan 10, 2008
  5. Ray Wood

    Time on Update is exactly 3 hours less than time entered

    Ray Wood, Jul 17, 2003, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    168
    Ray Wood
    Jul 17, 2003
Loading...

Share This Page