为什么这个 python Lark 语法这么慢?

Why is this this python Lark grammar so slow?

我正在尝试解析“ypcat -k netgroup”的输出 输出看起来像这种格式的许多行:

group1 (host1,user1,domain1) (host2,user2,domain2) (host3,user3,domain3) ...

或有时

group2 groupa groupb groupc ...

我第一次尝试使用这个 lark 语法:

def getNetgroups():
  parser = Lark(ypcat_grammer)
  res = subprocess.check_output(['ypcat -k netgroup'], shell=True).decode('utf-8')
  print(parser.parse(res).pretty())

ypcat_grammer = r"""
  ?start: _line+
  _line: groupname members NEWLINE
  members: (member|groupname)*
  member: "(" hostname? "," username? "," domainname? ")"
  
  username: _name
  domainname: _name
  groupname: _name
  hostname: _name
  _name: /([a-zA-Z0-9_\.\-]+)/
  %import common.WS_INLINE
  %import common.NUMBER
  %import common.NEWLINE
  %ignore WS_INLINE
"""

解析 4000 行需要 60 秒!!? 看起来太长了,所以我写了一个手工编码的解析器:

member = re.compile('\(([^,]*),([^,]*),([^,]*)\)')

def parseNetGroups():
  res = subprocess.check_output(['ypcat -k netgroup'], shell=True).decode('utf-8')
  rows = []
  for line in res.split('\n'):
    words = re.split('\s+', line)
    groupname = words.pop(0)
    members = []
    for word in words:
      if m:=member.match(word):
        members.append((m.group(1),m.group(2),m.group(3)))
      else:
        members.append(word)
    rows.append({'GROUPNAME':groupname, 'MEMBERS':members})
  return pd.DataFrame(rows)

这用了 0.8 秒。 我做错了什么?

更改为解析器='lalr' 将运行时间减少到 3.8 秒。 这对我来说已经足够了。