views:

32

answers:

3

I'm developping a mini search engine, and I want to implement the feature of searches based on logic operators AND OR...

I'm having a difficulty on parsing a query containing AND, OR, NOT... especially when it comes to parentheses... (cat or dog) not (bike not mike)

For simple AND, and OR queries, it's obviously too simple and I figured out how to formulate the sql query, but when it becomes that complicated I'm lost !!!

I'm not sure if search engines have this feature, but I want to dive into it for learning purpose.

I apologize for my last question which wasn't really clear, I hope this time I'm doing better.

Thank you

+2  A: 

I'd recommend looking at a lexer/parser generator like ANTLR. A simple grammar should be able to sort you out. There might even be an existing grammar for such a thing.

duffymo
+1  A: 

Take a look at the searchparser.py example from the pyparsing project.

It shows a way to implement:

  • AND,
  • OR,
  • NOT,
  • grouping and
  • wildcards.

All done in 293 lines of code (including comments and tests) ...

The MYYN
A: 

If you are using MySQL you can use the builtin boolean search:

http://dev.mysql.com/doc/refman/5.1/en/fulltext-boolean.html

truppo