Pyparsing对于一个非常小的语法工作得很好,但随着语法的增长,性能下降并且通过屋顶使用内存。
我目前的gramar是:
newline = LineEnd ()
minus = Literal ('-')
plus = Literal ('+')
star = Literal ('*')
dash = Literal ('/')
dashdash = Literal ('//')
percent = Literal ('%')
starstar = Literal ('**')
lparen = Literal ('(')
rparen = Literal (')')
dot = Literal ('.')
comma = Literal (',')
eq = Literal ('=')
eqeq = Literal ('==')
lt = Literal ('<')
gt = Literal ('>')
le = Literal ('<=')
ge = Literal ('>=')
not_ = Keyword ('not')
and_ = Keyword ('and')
or_ = Keyword ('or')
ident = Word (alphas)
integer = Word (nums)
expr = Forward ()
parenthized = Group (lparen + expr + rparen)
trailer = (dot + ident)
atom = ident | integer | parenthized
factor = Forward ()
power = atom + ZeroOrMore (trailer) + Optional (starstar + factor)
factor << (ZeroOrMore (minus | plus) + power)
term = ZeroOrMore (factor + (star | dashdash | dash | percent) ) + factor
arith = ZeroOrMore (term + (minus | plus) ) + term
comp = ZeroOrMore (arith + (eqeq | le | ge | lt | gt) ) + arith
boolNot = ZeroOrMore (not_) + comp
boolAnd = ZeroOrMore (boolNot + and_) + boolNot
boolOr = ZeroOrMore (boolAnd + or_) + boolAnd
match = ZeroOrMore (ident + eq) + boolOr
expr << match
statement = expr + newline
program = OneOrMore (statement)
解析以下内容时
print (program.parseString ('3*(1+2*3*(4+5))\n') )
这需要很长时间:
~/Desktop/m2/pyp$ time python3 slow.py
['3', '*', ['(', '1', '+', '2', '*', '3', '*', ['(', '4', '+', '5', ')'], ')']]
real 0m27.280s
user 0m25.844s
sys 0m1.364s
内存使用率上升至 1.7 GiB (原文如此!)。
我是否在实施此语法时犯了一些严重错误,或者我如何在可承受的边距内保留内存使用量?
答案 0 :(得分:11)
导入pyparsing后启用packrat解析以记忆解析行为:
ParserElement.enablePackrat()
这应该会大大提高绩效。