lark: Lark.save does not work using the earley parser

When trying to do call Lark.save(f) on an earley parser, this exception is thrown: AttributeError: 'XEarley' object has no attribute '__serialize_fields__'

To reproduce:

from lark import Lark

grammar = """
    sentence: noun verb noun        -> simple
            | noun verb "like" noun -> comparative
    noun: adj? NOUN
    verb: VERB
    adj: ADJ
    NOUN: "flies" | "bananas" | "fruit"
    VERB: "like" | "flies"
    ADJ: "fruit"
    %import common.WS
    %ignore WS
"""

parser = Lark(grammar, start='sentence', ambiguity='explicit', parser='earley')
with open('saved.pickle', 'wb') as f:
    parser.save(f)

Using lalr saves the parser as expected, but is no option in my use case. From the documentation and various issues (https://github.com/lark-parser/lark/issues/508#issuecomment-596068571, https://github.com/lark-parser/lark/issues/492#issuecomment-596068838) I assumed that parser saving is available regardless of parser type.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 15 (11 by maintainers)

Most upvoted comments

I would prefer if the documentation (where I learned about save in the first place) told me right away that it is not available for the Earley parser. The cache option, which uses this save feature under the hood as far as I see it also has a short notice in the docs telling users that it’s for LALR only.