Re: [Pyparsing] parsing with operatorPrecedence takes too much time
Brought to you by:
ptmcg
From: Ralf S. <sc...@gm...> - 2008-04-03 08:58:51
|
On Thu, Apr 3, 2008 at 12:15 AM, Paul McGuire <pt...@au...> wrote: > This performance problem has come up a couple of times in the past month. > For a start, you could try enabling packrat parsing - change your import > and > following lines to: > > > from pyparsing import (Word, Literal, CaselessLiteral, > Combine, Optional, nums, StringEnd, > operatorPrecedence, > opAssoc,ParserElement) > ParserElement.enablePackrat() > > > This will speed up your parser about 500X (not kidding!). > fine, thanks. Actually I was thinking pyparsing was kidding me when it was that slow. (I think this is still rather slow if pyparsing can only parse 500 of those very basic expressions in one second). I was using a parser, which didn't use operatorPrecedence before and it never felt slow. Am I better off writing the parsing rules myself? (like in http://code.pediapress.com/hg/mwlib/file/tip/mwlib/expr.py) It's similar to the mathematical expression parser from the examples directory. btw, I am running python 2.6 and with this change I get the following error: ~/ python e.py ralf@rat64ok Traceback (most recent call last): File "e.py", line 41, in <module> res=expr.parseString("1+1+1-(5+2)") File "/home/ralf/py26/lib/python2.6/site-packages/pyparsing-1.4.11-py2.6.egg/pyparsing.py", line 980, in parseString loc, tokens = self._parse( instring.expandtabs(), 0 ) File "/home/ralf/py26/lib/python2.6/site-packages/pyparsing-1.4.11-py2.6.egg/pyparsing.py", line 907, in _parseCache if lookup in ParserElement._exprArgCache: TypeError: unhashable type: 'And' python 2.6 won't let you call hash() on an object which implements it's own __eq__, but doesn't implement __hash__. This is something you might like to fix in an upcoming release.. I will try to fix that issue and will send you a patch... - ralf |