0

I'm using python lark to parse a domain specific language and I havn't yet made the grammar file of the quality required to use lalr parsing. The parsing of a simple file is therefore taking over 3 seconds. Is there a simple way to serialise the parsed lark file, so that I can store it in a file and reopen it without having to reparse the text.

Is there anything better than using pickle?

jhylands
  • 984
  • 8
  • 16
  • What is the performance problem? `parser='earley'` (e.g. the default) is slow for large files. Unless your grammar file is enormous, I can not imagine that loading the grammar is the performance bottleneck. However, if that is the case, you can try passing `cache=True` to `Lark`. – MegaIng Jun 28 '21 at 11:14
  • It's the parser.parse(string) call that is taking the time. The file isnt that big only a couple hundred lines. – jhylands Jun 28 '21 at 11:16
  • 1
    Then you will have to redesign the grammar to use `parser='lalr'`. That will be a lot faster. Currently, there is nothing better to serialize `Tree` instances better than `pickle`. You could however create a simple to/from json serializer. – MegaIng Jun 28 '21 at 11:40
  • If you mean saving and loading the parser itself, there's save and load: https://lark-parser.readthedocs.io/en/latest/classes.html?highlight=save#lark.Lark.save – Erez Aug 01 '21 at 18:20

0 Answers0