如何防止铺层中的台面再生

2024-06-25 22:44:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我在一个命令行应用程序中使用PLY,该应用程序打包为Python egg,通过pip安装。每次从命令行运行脚本时,都会看到以下消息:

"Generating LALR tables"

另外,解析器.out以及parsetab.py文件被写入调用脚本的目录。有没有什么方法可以将这些文件与应用程序一起发送,这样它就不会每次都重新生成表了?在


Tags: pip文件命令行py脚本应用程序解析器消息
3条回答

您想使用optimized mode,方法是调用lex:

lexer = lex.lex(optimize=1)

一。在

值得强调的是(from the same link):

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.

When running in optimized mode, it is important to note that lex disables most error checking. Thus, this is really only recommended if you're sure everything is working correctly and you're ready to start releasing production code.

因为这是生产代码,这听起来正是您想要的。

一。在

在研究这个问题时,我遇到了miscellaneous Yacc notes

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.

深入研究yacc.py中的yacc函数,我们发现Optimize在下面的片段中忽略了这种不匹配:

^{pr2}$

其中signature与存储在parsetab.py(作为_lr_signature)中的校验和进行比较。

我最终做的是关闭优化。我在浏览PLI3.4源代码时,在lexer代码中发现了一个小亮点:

# If in optimize mode, we write the lextab
if lextab and optimize:
    lexobj.writetab(lextab,outputdir)

return lexobj

通过将构建lexer和解析器的代码更改为:

self.lexer = lex.lex(module=self, optimize=False, debug=False, **kwargs)

以及

self.lexer = lex.lex(module=self, optimize=False, debug=False, **kwargs)

我避免了所有的文件写出来。调试器将.out文件写入目录,Python文件是optimize标志的结果。在

虽然这暂时有效,但我不能说我对这种方法完全满意。想必,有某种方法来保持优化,同时保持工作目录的干净,将是一个更好的解决方案,将导致更好的性能。如果别人有更好的方法,我会非常乐意接受。在

使用

yacc.yacc(debug=0, write_tables=0)

相关问题 更多 >