pyparsing / pyparsing

Python library for creating PEG parsers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

pp.ParserElement.enable_left_recursion() gives runtime error

hhoppe opened this issue · comments

Running this code in a fresh environment:

import pyparsing as pp
print(pp.__version__)
pp.ParserElement.enable_left_recursion()

shows:

3.0.9
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
[<ipython-input-3-391db87ce479>](https://localhost:8080/#) in <cell line: 3>()
      1 import pyparsing as pp
      2 print(pp.__version__)
----> 3 pp.ParserElement.enable_left_recursion()

[/usr/local/lib/python3.9/dist-packages/pyparsing/core.py](https://localhost:8080/#) in enable_left_recursion(cache_size_limit, force)
   1021             ParserElement.disable_memoization()
   1022         elif ParserElement._packratEnabled:
-> 1023             raise RuntimeError("Packrat and Bounded Recursion are not compatible")
   1024         if cache_size_limit is None:
   1025             ParserElement.recursion_memos = _UnboundedMemo()

RuntimeError: Packrat and Bounded Recursion are not compatible

What could be wrong? Thanks.

I found the source of the problem.
When import matplotlib.pyplot is performed, it executes:

matplotlib/_mathtext.py:29: ParserElement.enablePackrat()

(It seems that the Python runtime within https://colab.research.google.com/ somehow pre-executes such a statement as well.)

So I cannot use both matplotlib.pyplot (or Google Colab) and ParserElement.enable_left_recursion() in the same Python environment.

It might be nice to somehow allow multiple pyparsing environments (with different settings for different libraries) within the same Python session?

I'm glad you were able to locate the issue. You can disable the packrat cacheing by calling disable_memoization after importing matplotlib, and then enabling left recursion.

pp.ParserElement.disable_memoization() did the trick; thanks!