Re: boolean logic parser - need help
From: Mad Scientist Jr (usenet_daughter_at_yahoo.com)
Date: 02/11/05
- Next message: aatayyab: "Re: internet fraud by rentacoder.com"
- Previous message: Alan Wilcox: "Programming Fax Routing Extension"
- In reply to: Soren Kuula: "Re: boolean logic parser - need help"
- Next in thread: Mad Scientist Jr: "Re: boolean logic parser - need help"
- Reply: Mad Scientist Jr: "Re: boolean logic parser - need help"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: aatayyab: "Re: internet fraud by rentacoder.com"
- Previous message: Alan Wilcox: "Programming Fax Routing Extension"
- In reply to: Soren Kuula: "Re: boolean logic parser - need help"
- Next in thread: Mad Scientist Jr: "Re: boolean logic parser - need help"
- Reply: Mad Scientist Jr: "Re: boolean logic parser - need help"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|