<p>您想使用<a href="http://www.dabeaz.com/ply/ply.html#ply_nn15" rel="nofollow">optimized mode</a>,方法是调用lex:</p>
<pre><code>lexer = lex.lex(optimize=1)
</code></pre>
<p>一。在</p>
<p>值得强调的是(<a href="http://www.dabeaz.com/ply/ply.html#ply_nn15" rel="nofollow">from the same link</a>):</p>
<blockquote>
<p>On subsequent executions, lextab.py will simply be imported to build the lexer. This approach substantially improves the startup time of the lexer and it works in Python's optimized mode.</p>
<p>When running in optimized mode, it is important to note that lex disables most error checking. Thus, this is really <strong>only recommended if you're sure everything is working correctly and you're ready to start releasing production code</strong>.</p>
</blockquote>
<p><em>因为这是生产代码,这听起来正是您想要的。</em></p>
<p>一。在</p>
<p>在研究这个问题时,我遇到了<a href="http://www.dabeaz.com/ply/ply.html#ply_nn36" rel="nofollow">miscellaneous Yacc notes</a>:</p>
<blockquote>
<p>Since the generation of the LALR tables is relatively expensive, previously generated tables are cached and reused if possible. The decision to regenerate the tables is determined by taking an MD5 checksum of all grammar rules and precedence rules. Only in the event of a mismatch are the tables regenerated.</p>
</blockquote>
<p>深入研究<code>yacc.py</code>中的<code>yacc</code>函数,我们发现Optimize在下面的片段中忽略了这种不匹配:</p>
^{pr2}$
<p><em>其中<code>signature</code>与存储在<code>parsetab.py</code>(作为<code>_lr_signature</code>)中的校验和进行比较。</em></p>