NLTK CFG recursion depth error
<pre><code>import nltk   
from nltk.parse.generate import generate,demo_grammar   
from nltk import CFG   
grammar = CFG.fromstring("""   
ROOT -&gt; S
S -&gt; NP VP
NP -&gt; NP PP
NP -&gt; DT NN
DT -&gt; 'The'
NN -&gt; 'work'
PP -&gt; IN NP
IN -&gt; 'of'
NP -&gt; DT NN
DT -&gt; 'the'
NN -&gt; 'painter'
VP -&gt; VBZ ADJP
VBZ -&gt; 'is'
ADJP -&gt; JJ
JJ -&gt; 'good'
""")    
print(grammar)   
for sentence in generate(grammar, n=100):   
   print(' '.join(sentence))
</code></pre>

<p>Gives an error</p>

<pre><code>RuntimeError: maximum recursion depth exceeded while calling a Python object
</code></pre>

<p>Tried changing covert function in functools.py, still the same issue.</p>

<p>The function <code>generate</code>, as its docstring states, "Generates an iterator of all sentences from a CFG." Clearly it does so by choosing alternative expansions in the order they are listed in the grammar. So, the first time is sees an <code>NP</code>, it expands it with the rule <code>NP -&gt; NP PP</code>. It now has another <code>NP</code> to expand, which it also expands with the same rule... and so on ad infinitum, or rather until python's limits are exceeded.</p>

<p>To fix the problem with the grammar you provide, simply reorder your first two <code>NP</code> rules so that the recursive rule is not the first one encountered: </p>

<pre><code>grammar = CFG.fromstring("""   
ROOT -&gt; S
S -&gt; NP VP
NP -&gt; DT NN
NP -&gt; NP PP
DT -&gt; 'The'
...
""")
</code></pre>

<p>Do it like this and the generator will produce lots of complete sentences for you to examine. Note that the corrected grammar is still recursive, hence infinite; if you generate a large enough number of sentences, you will eventually reach the same recursion depth limit.</p>

python nltk