Re: boolean logic parser - need help

From: Mad Scientist Jr (usenet_daughter_at_yahoo.com)
Date: 02/11/05


Date: 11 Feb 2005 12:04:35 -0800

I'm looking to evaluate basic boolean expressions (no math) like:

( <Var1/> = 10 and <Var1/> = "hello" ) or (<Var3/> > 25)

My old parser can evaluate Expression1 and/or Expression2. The
expressions are in the form of an array:
  arr(0) = value1
  arr(1) = operator1
  arr(2) = value2
  arr(3) = value3
  arr(4) = operator2
  arr(5) = value4

Operators are
  < (or lt)
  <= (or le)
  = (or eq, ==)
  <> (or ne, !=)
>= (or ge)
> (or gt)

The expressions have no math, they are just either variables which are
stored in an associative array (a vb.net hashtable) or literals.

The parser looks to see if a value starts with "<" and ends with "/>"
then it is a variable. If it is, it gets the variable type (integer,
float or string) which is stored in another associative array. I have
comparison routines for each type. If it sees that two incompatible
types are being compared, or a non-numeric value is being compared to a
numeric variable, it returns an error. It is pretty limited but it does
the job.

It would be nice to have a real parser - YACC looks powerful but it
also looks pretty complicated.. I'm not that advanced and don't have a
lot of time and YACC looks like learning a whole language. Can someone
post an example YACC grammar that shows how it works? If it looks easy,
it might be the solution...

Thanks again

Soren Kuula wrote:
> Mad Scientist Jr wrote:
> Hi,
>
> > Here is some pseudocode I put together to work it out. It doesn't
seem
> > right or solid yet. Any help would be appreciated.
> >
> > if odd # of parens (not part of quoted string) return error
>
> That would accept )( ?
>
> Take a look at a real parser generator like yacc; that's the way to
go.
>
> You can also find lots of examples in computer science textbooks
about
> parsing and compiling -- it's a classic problem (researched to bits
and
> pieces in the 60s and 70s).
>
> In short, you will want to write a grammar. The parser generator will

> then, given the grammar, generate a program (parser, surprise!) that
can
> generate an object tree from your input text, like:
>
> (1+2)*3+4
>
> -->
>
> PLUS
> / \
> MULT NUM(4)
> / \
> PLUS NUM(3)
> | \
> NUM(1) NUM(2)
>
> With the right evaluation functions at the nodes, it's piece of cake
then.
>
> Soren



Relevant Pages

  • Re: Allowing zero-dimensional subscripts
    ... items are formed by comma-separated lists of expressions. ... Rather than further complicating the syntax, this is disambiguated by defining that in this case the interpretation as a subscription takes priority over the interpretation as a slicing. ... So there's a regular way to create non-empty tuples, and a subscript ... an array with n-dimensions of length 3 would have ...
    (comp.lang.python)
  • Re: Allowing zero-dimensional subscripts
    ... The items of a tuple are arbitrary Python objects. ... items are formed by comma-separated lists of expressions. ... the grammar rule used for list subscript is different from the ... an array with n-dimensions of length 3 would have ...
    (comp.lang.python)
  • Re: Allowing zero-dimensional subscripts
    ... items are formed by comma-separated lists of expressions. ... So there's a regular way to create non-empty tuples, and a subscript ... But note neither testlist nor subscriptlist can create an empty tuple. ... an array with n-dimensions of length 3 would have ...
    (comp.lang.python)
  • Re: Array summation of product?
    ... the concept of bounds of array expressions is a very confusing one. ... Turns out that in most contexts, you don't care. ... Basically, if subscripting of array expressions is allowed, people are ...
    (comp.lang.fortran)
  • Re: Pseudo variables or substring operators?
    ... it would be worth a special syntax (I agree it would be nice, ... can't do functions, expressions, or other substrings. ... array indices and slices have the same issue as substrings. ... simpler for compilers, but that compiler technology advanced enough ...
    (comp.lang.fortran)