lark-parser / lark

Lark is a parsing toolkit for Python, built with a focus on ergonomics, performance and modularity.
MIT License
4.75k stars 401 forks source link

Saving Earley parser doesn't work #1323

Closed ivkovicdjordje closed 1 year ago

ivkovicdjordje commented 1 year ago

Describe the bug

Saving Earley parser with parser.save(file) throws TypeError: cannot pickle 'module' object.

To Reproduce

from lark.lark import Lark
grammar = """
hello_world: HELLO_TOKEN " " WORLD_TOKEN "."
HELLO_TOKEN: "hello"
WORLD_TOKEN: "world"
"""
parser = Lark(grammar, start="hello_world", parser="earley")
print(parser.parse("hello world."))

with open("parser.pkl", "wb") as file:
    parser.save(file)

Logs:

Tree(Token('RULE', 'hello_world'), [Token('HELLO_TOKEN', 'hello'), Token('WORLD_TOKEN', 'world')])
Traceback (most recent call last):
  File "/home/djiv/dev/examples/lark_pickle/src/save_lark.py", line 13, in <module>
    parser.save(file)
  File "/home/djiv/dev/examples/lark_pickle/venv/lib/python3.10/site-packages/lark/lark.py", line 500, in save
    pickle.dump({'data': data, 'memo': m}, f, protocol=pickle.HIGHEST_PROTOCOL)
TypeError: cannot pickle 'module' object

Lark version used is 1.1.7

erezsh commented 1 year ago

The save method doesn't work for Earley.

But we need a better error for that.

ivkovicdjordje commented 1 year ago

Can you explain why save method doesn't work for Earley?

erezsh commented 1 year ago

I just never implemented it.

See issue #788

erezsh commented 1 year ago

But if anyone wants to contribute a PR for it, they are more than welcome.