In plain words: A code translator learns to convert functions between C++, Java, and Python using only unpaired code samples from each language, so it needs no hand-written rewrite rules or matched example pairs. It produced correct translations far more often than commercial rule-based tools.
Abstract · Unsupervised Translation of Programming Languages
A transcompiler, also known as source-to-source translator, is a system that converts source code from a high-level programming language (such as C++ or Python) to another. Transcompilers are primarily used for interoperability, and to port codebases written in an obsolete or deprecated language (e.g. COBOL, Python 2) to a modern one. They typically rely on handcrafted rewrite rules, applied to the source code abstract syntax tree. Unfortunately, the resulting translations often lack readability, fail to respect the target language conventions, and require manual modifications in order to work properly. The overall translation process is timeconsuming and requires expertise in both the source and target languages, making code-translation projects expensive. Although neural models significantly outperform their rule-based counterparts in the context of natural language translation, their applications to transcompilation have been limited due to the scarcity of parallel data in this domain. In this paper, we propose to leverage recent approaches in unsupervised machine translation to train a fully unsupervised neural transcompiler. We train our model on source code from open source GitHub projects, and show that it can translate functions between C++, Java, and Python with high accuracy. Our method relies exclusively on monolingual source code, requires no expertise in the source or target languages, and can easily be generalized to other programming languages. We also build and release a test set composed of 852 parallel functions, along with unit tests to check the correctness of translations. We show that our model outperforms rule-based commercial baselines by a significant margin.
Marie-Anne Lachaux, Baptiste Roziere, Lowik Chanussot, Guillaume Lample
arXiv:2006.03511 · cs.CL, cs.PL · submitted Jun 5, 2020 · updated Sep 22, 2020
abstract · pdf · html
Seeing C++
translated into Python: is impressive. The translator picked up on two idioms of C++ and translated them to more concise forms in Python. Most "transpilers" just compile into the target language as if compiling to something like machine code, generating worst case wordy output. There's a C to Rust translator which translated C pointer indexing into Rust unsafe pointer arithmetic, which is not a gain in safety.It's also claimed that this system is good at guessing types from untyped languages.
So a good way to use this technology might be to have something that looks at source code and tries to translate all the function signatures and type definitions. This includes looking at function calls and bodies to help guess ("infer" is a stretch for this approach) the types of ambiguous variables.
Example is
Is "s" a single character or a pointer to an array? You can't tell without context. This system might be able to do that. Or A system like this should be able to recognize the intent there.Then try to translate the executable code with that information available. Bad guesses about types will usually result in translation failures or code that compiles with type errors, so someone will notice.
So this is promising for modernizing code.
I want to see one able to turn C pointer arithmetic into slice syntax. Now that would be a big step forward.