一句话:提出增量式、可定制且可复用规则的转译方法,实现 Python 到 JavaScript 的 DuoGlot,翻译准确率达 90%。
转译器将一个语言的代码转换到另一语言,但实际使用中常需用户引导或定制输出,以满足性能、可读性、外部环境与分析工具兼容等目标。传统转译器是确定性的规则驱动系统,通常不支持按用户、按程序定制;基于神经网络的转译器虽提供一定交互式定制,却难以精确控制输出,二者还都面临“最后一英里”问题——整体上正确,但并非所有片段都正确。本文提出一种新的转译方法,具备细粒度定制能力,并支持复用他人创建的转译规则,且不要求用户理解源程序的全局语义。该方法基本自动化且增量进行:依照用户引导,逐块构造完成转译所需的规则;用户可依赖已有规则正确翻译大部分程序,而只把精力集中于错误或需定制的局部,从而提高最终结果正确性。作者实现工具 DuoGlot,将 Python 转译为 JavaScript,在 GeeksForGeeks 基准上取得 90% 翻译准确率,优于所有已有手工与神经转译器,且生成代码可读;在两个更难、更长的额外基准上同样优于其他转译器。
A transpiler converts code from one programming language to another. Many practical uses of transpilers require the user to be able to guide or customize the program produced from a given input program. This customizability is important for satisfying many application-specific goals for the produced code such as ensuring performance, readability, ease of exposition or maintainability, compatibility with external environment or analysis tools, and so on. Conventional transpilers are deterministic rule-driven systems often written without offering customizability per user and per program. Recent advances in transpilers based on neural networks offer some customizability to users, e.g. through interactive prompts, but they are still difficult to precisely control the production of a desired output. Both conventional and neural transpilation also suffer from the "last mile" problem: they produce correct code on average, i.e., on most parts of a given program, but not necessarily for all parts of it. We propose a new transpilation approach that offers fine-grained customizability and reusability of transpilation rules created by others, without burdening the user to understand the global semantics of the given source program. Our approach is mostly automatic and incremental, i.e., constructs translation rules needed to transpile the given program as per the user's guidance piece-by-piece. Users can rely on existing transpilation rules to translate most of the program correctly while focusing their effort locally, only on parts that are incorrect or need customization. This improves the correctness of the end result. We implement the transpiler as a tool called DuoGlot, which translates Python to Javascript programs, and evaluate it on the popular GeeksForGeeks benchmarks. DuoGlot achieves 90% translation accuracy and so it outperforms all existing translators (both handcrafted and neural-based), while it produces readable code. We evaluate DuoGlot on two additional benchmarks, containing more challenging and longer programs, and similarly observe improved accuracy compared to the other transpilers.
@article{paperbot2037,
title = {User-Customizable Transpilation of Scripting Languages},
author = {Bo Wang and Aashish Kolluri and Ivica Nikolić and Teodora Baluta and Prateek Saxena},
journal = {Proceedings of the ACM on Programming Languages},
volume = {7},
number = {OOPSLA1},
year = {2023},
doi = {10.1145/3586034}
}