众所周知地,木兰编程语言的实现中使用RPly而非更为人知的Ply进行词法和语法分析器生成。 为什么要用这样一个更小众的工具?Ply自2006年开始开发,RPly自2012年,它的pypi主页上的介绍是: ApurePythonparsergenerator,thatalsoworkswithRPython。ItisamoreorlessdirectportofDavidBeazley’sawesomePLY,withanewpublicAPI,andRPythonsupport。 API的改进感觉不是决定性因素,因为木兰的语法设计规模并不算小,更多的开发精力应该是用于语法设计而非实现。粗浅地比较了Ply的例程,感觉两者的API虽然有差别,但实现效果仍是大同小异。 假如的确如此,那么是什么动力才使设计者选择了一个看起来风险更高的工具呢? 剩下的区别,就只有RPly对RPython的支持。 初步了解,RPython和PyPy几乎是绑定的。知乎上相关技术介绍文章寥寥,实战更少。在PyPy的这篇近十年前的入门文档Tutorial:WritinganInterpreterwithPyPy,Part1中,对它的作用可见一斑: Wouldntitbeniceifyoucouldwriteyourlanguageinanexistinghighlevellanguagelike,forexample,Python?Thatsurewouldbeideal,youdgetalltheadvantagesofahighlevellanguagelikeautomaticmemorymanagementandrichdatatypesatyourdisposal。Oh,butaninterpretedlanguageinterpretinganotherlanguagewouldbeslow,right?Thatstwiceasmuchinterpretinggoingon。 Asyoumayhaveguessed,PyPysolvesthisproblem。PyPyisasophisticatedtoolchainforanalyzingandtranslatingyourinterpretercodetoCcode(orJVMorCLI)。Thisprocessiscalledtranslation,anditknowshowtotranslatequitealotofPythonssyntaxandstandardlibraries,butnoteverything。AllyouhavetodoiswriteyourinterpreterinRPython,asubsetofthePythonlanguagecarefullydefinedtoallowthiskindofanalysisandtranslation,andPyPywillproduceforyouaveryefficientinterpreter。 简单说,用Python写出的解释器执行想必很慢。现在可以用RPython语言(Python语言的子集)写个解释器,PyPy可以直接将RPython代码翻译为C代码,得到更高执行效率的解释器。 说回到RPly,它提供了RPython的支持,那么用它实现的解释器应该可以用PyPy翻译后得到性能提升(待确认)。 这貌似是个更有价值的理由。