From: Dan Zwell Date: 2007-04-30T05:03:14+09:00 Subject: Re: Infix to Postfix Method Thomas Hurst wrote: > Demo: > # Token stream > irb(main):009:0> toks = analyser.simplify(tokenizer.tokenize("foo AND bar OR (wibble OR wobble NOT foo)")) > [#, > :and, > #, > :or, > :open, > #, > :or, > #, > :and, > :not, > #, > :close] > > irb(main):010:0> analyser.rpnise(toks) > [#, > #, > :and, > #, > #, > #, > :not, > :and, > :or, > :or] > > You can then follow the instructions, popping each SubStringQuery onto a > stack and popping them off on :and, :or and :not to build a tree of > query objects: > > #to_s > OR(OR(AND(NOT("foo"),"wobble"),"wibble"),AND("bar","foo")) > > #to_sql > (((NOT (Title LIKE "%foo%") AND Title LIKE "%wobble%") OR Title LIKE > "%wibble%") OR (Title LIKE "%bar%" AND Title LIKE "%foo%")) > > > I'd recommend against using Ruby's own Generator, btw; it uses callcc > and has a tendancy towards being slow and leaky. If you just need a > #next/#peek method, just turn it into an array and shove it in an > appropriate object which tracks the position. > That's pretty neat. I hadn't thought of using this stuff to generate SQL. I'll probably acually use that idea some day. And thanks for the bit about Generator. I'm currently looking through a computational script (ported from c++)--I could never figure out why it used all my ram within a few seconds, but it used Generators several thousand times. I'm rewriting parts of it now, and we'll see what happens. Dan