From: Daniel Baird Date: 2006-06-05T21:36:57+09:00 Subject: Re: parsing a boolean expression ------=_Part_8992_19138119.1149511015071 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 6/5/06, Matthew Smillie wrote: > > On Jun 5, 2006, at 7:32, Daniel Baird wrote: > > > Hi all, > > > > I need to take a string typed by dirty users' fingers, that should > > contain a > > boolean expression, like this: > > one and (two or three or (four and five) or six) and (seven or eight) > > If your input is always going to be as simple as the above (space- > delimited tokens and parens), and you don't know anything about yacc, > then there's the distinct possibility that you'll lose more time > fiddling with it than you will writing your own parser. > > Ferinstance, you could pretty easily add the 'missing' parens (after > each operator to the end of the expression) to make it explicit, then > change it to an s-expression by switching the order of the first > token and operator in each sub-expression. And an s-expression is > basically a tree. It would depend on what you want to do with it > when you're done. > > matthew smillie. Hmm.. I haven't done anything with yacc before, and my expressions _are_ going to be pretty simple, so I guess your advice is to write it myself :) I'd kinda fancied returning a tree made up of nodes that are either: - a token - a list of nodes with a common operator eg "A and ( B or C or D) and E" would be: node0: AND(A, node1, E) node1: OR(B, C, D) It's not a binary tree but it would be somewhat convenient for the processing / storing / re-displaying I need to do.. I just hoped I'd save a day or two standing on someone else's shoulders. Thanks Matthew. ;D -- Daniel Baird http://danielbaird.com (TiddlyW;nks! :: Whiteboard Koala :: Blog :: Things That Suck) ------=_Part_8992_19138119.1149511015071--