Lexical Analysers
Author
Discussion

mystomachehurts

Original Poster:

11,669 posts

281 months

Wednesday 15th August 2007
quotequote all
Oh god.... I find myself in need of a lexical analyser.

Please help me!

Essentially, user will type a search criteria string into a text box on a web page.

I want to ensure that what they've typed is lexically correct because I am going to have to translate this into an SQL query that is run against the database.

So we could have something simple like

cncp.opcode == 3

or we can have some more funky stuff such as

(cncp.opcode == 3 || cncp.opcode == 4) && bgan.spotid == 1

As you can see I need to support any level of embedded ANDs ORs NOTs, my server side code needs to then turn this into SQL. So taking the last example

(cncp.opcode == 3 || cncp.opcode == 4) && cncp.spotid == 1

in SQL this would be along the lines of

((service_id = 3331 AND f2 = 3) OR (service_id = 3331 AND f2 = 4)) AND (service_id = 3331 AND f1 = 1)

As you can see user enters cncp, and we get a service_id of 3331, opcode is field f2, and spotid is field f1. But there are lots of services and many more fields than just opcode and spotid.


Currently I'm thinking AJAX, because the list of options are far too big to download to the client as say JavaScript.

So I can send the current string that the user enters to a PHP script, I then need check all the fields can be resolved to columns in the database. I think I need to analyse it with a lexical program to ensure that all the ==, !=, >=, <=, and all the brackets are corrent.

Just at a bit of a loss as to where to start.

The backend is all php, though I can call some other entity, infact, I could have a JAVA/C++/Bash script/program listening on a separate port and do the lexical checks in that, although it will eventually require a trip to the DB to translate the values into colum desriptions at the DB.

Anyone a bit of a guru on this kind of stuff? Feel free to post here or drop me a line, money is available to pay for work done.



cottonfoo

6,053 posts

241 months

Wednesday 15th August 2007
quotequote all
I've not had to do anything like that before, but I'd start with looking at flex/yacc, or perhaps Parse::RecDescent, since I'm a perlhead.

http://dinosaur.compilertools.net/