Re: [Pyparsing] grammar with bad performance (with examples :))
Brought to you by:
ptmcg
From: david <asd...@gn...> - 2007-12-13 01:10:36
|
On Mon, 10 Dec 2007 10:53:13 -0600, Paul McGuire wrote: > David - > > Here is a modified version of your program, that runs for me in 0.05 > seconds or so. Paul, Thank you very much, your explanation is excellent, and the new grammar works like a charms :) > The main problem was your universal use of Or instead of MatchFirst. > Using Or in the definition of OPERATOR is not so big a problem, but you > can convert to MatchFirst if you are careful about the ordering of your > operators. And the operators may as well be Literals, and not Keywords, > since Keyword only looks for boundary alphanumeric characters, not > symbols or whitespace. I understand the performance implications between Or and MatchFirst, but I still don't understand why Literals are faster than of Keywords. > Is there any reason you choose the > > OPERATOR = psg.Or([EQUAL, NOT_EQUAL, CONTAINS, NOT_CONTAINS, STARTWITH]) > > style, instead of using the built-in operators: > > OPERATOR = EQUAL ^ NOT_EQUAL ^ CONTAINS ^ NOT_CONTAINS ^ STARTWITH > > or even better: > > OPERATOR = STARTWITH | EQUAL | NOT_EQUAL | CONTAINS | NOT_CONTAINS Writing grammars is not my business, thanks to pyparsing's simplicity I wrote my first grammar (the one your tune) months ago in one day; I tend to prefer a more readable syntax for non repetitive tasks. Thank you david |