Hierarchical Learning of Cross-Language Mappings through Distributed Vector Representations for Code

Nghi D. Q. Bui, Lingxiao Jiang

Abstract

Translating a program written in one programming language to another can be useful for software development tasks that need functionality implementations in different languages. Although past studies have considered this problem, they may be either specific to the language grammars, or specific to certain kinds of code elements (e.g., tokens, phrases, API uses). This paper proposes a new approach to automatically learn cross-language representations for various kinds of structural code elements that may be used for program translation. Our key idea is two folded: First, we normalize and enrich code token streams with additional structural and semantic information, and train cross-language vector representations for the tokens (a.k.a. shared embeddings based on word2vec, a neural-network-based technique for producing word embeddings; Second, hierarchically from bottom up, we construct shared embeddings for code elements of higher levels of granularity (e.g., expressions, statements, methods) from the embeddings for their constituents, and then build mappings among code elements across languages based on similarities among embeddings. Our preliminary evaluations on about 40,000 Java and C# source files from 9 software projects show that our approach can automatically learn shared embeddings for various code elements in different languages and identify their cross-language mappings with reasonable Mean Average Precision scores. When compared with an existing tool for mapping library API methods, our approach identifies many more mappings accurately.

Introduction

Software migration, such as porting a library from Java to C#, remains a labor-intensive manual task. Existing automated solutions generally fall into two categories: rule-based translators and statistical models. Rule-based systems are accurate but brittle, requiring manual definition of complex grammar rules for every language pair. Statistical models offer more flexibility but often struggle with granularity, typically focusing on specific elements like API calls or flat token sequences while ignoring broader structural context.

This research introduces a hierarchical approach to bridge this gap, enabling the mapping of code elements at various levels of granularity—from expressions and statements to entire methods—without the need for hand-crafted translation rules.

Methodology

The proposed approach utilizes a multi-stage pipeline that treats source code as a structured hierarchy rather than a flat string.

The multi-stage pipeline for learning cross-language mappings, from normalization to hierarchical model building.

Token Normalization and Enrichment

To reduce noise from arbitrary naming conventions, raw code is parsed into Abstract Syntax Trees (ASTs). Identifiers are replaced with their type signatures, and structural keywords (e.g., expr_stmt) are injected into the token stream. This process ensures the model captures the underlying intent and structural patterns of the code.

Bilingual Skip-gram Training (BiSkip)

The system leverages "semantically aligned" files—files sharing the same name across Java and C# versions of a project—to train shared embeddings. By using a Bilingual Skip-gram model, the system learns that functionally equivalent keywords (e.g., Java's final and C#'s readonly) should reside in similar regions of the vector space.

Hierarchical Composition

Higher-level code structures are represented through a bottom-up composition. The vector representation for a "Method" is derived by averaging the vectors of its constituent "Statements," which are themselves composed of "Expressions" and "Tokens." This hierarchical approach allows the model to capture semantics that flat models might overlook.

Evaluation and Results

The approach was evaluated using a parallel corpus of approximately 40,000 source files from 9 open-source projects (including Lucene, Antlr, and POI) that exist in both Java and C#. These projects provide a "ground truth" of human-performed translations.

The effectiveness of the model was measured using Mean Average Precision (MAP) for recommending C# equivalents for Java queries.

Mean Average Precision (MAP) scores at different levels of granularity and recommendation depths (k).

As shown in the results, the model achieved MAP scores between 0.53 and 0.59 for statements and methods at top-10 recommendations. In API discovery tasks, the tool significantly outperformed existing baselines like StaMiner, identifying over 400 more SDK API method mappings. Precision for API mapping tasks ($k=1$) across various projects ranged from 0.69 to 0.89, indicating high reliability for direct translation tasks.

Conclusion and Future Work

This work demonstrates that leveraging the inherent hierarchy of code through AST-informed embeddings is a viable path toward automated program translation. Future research will explore more sophisticated composition architectures, such as Tree-RNNs or LSTMs, to better capture the sequential and nested nature of code. Additionally, expanding support to languages beyond the C-family and improving local context awareness through dependency-based file slicing remain key areas for development.

Summary updated

Abstract

Translating a program written in one programming language to another can be useful for software development tasks that need functionality implementations in different languages. Although past studies have considered this problem, they may be either specific to the language grammars, or specific to certain kinds of code elements (e.g., tokens, phrases, API uses). This paper proposes a new approach to automatically learn cross-language representations for various kinds of structural code elements that may be used for program translation. Our key idea is two folded: First, we normalize and enrich code token streams with additional structural and semantic information, and train cross-language vector representations for the tokens (a.k.a. shared embeddings based on word2vec, a neural-network-based technique for producing word embeddings; Second, hierarchically from bottom up, we construct shared embeddings for code elements of higher levels of granularity (e.g., expressions, statements, methods) from the embeddings for their constituents, and then build mappings among code elements across languages based on similarities among embeddings. Our preliminary evaluations on about 40,000 Java and C# source files from 9 software projects show that our approach can automatically learn shared embeddings for various code elements in different languages and identify their cross-language mappings with reasonable Mean Average Precision scores. When compared with an existing tool for mapping library API methods, our approach identifies many more mappings accurately. The mapping results and code can be accessed at https://github.com/bdqnghi/hierarchical-programming-language-mapping. We believe that our idea for learning cross-language vector representations with code structural information can be a useful step towards automated program translation.
BibTeX
@inproceedings{bui2018hierarchical,
  author = {Nghi D. Q. Bui and Lingxiao Jiang},
  title = {{Hierarchical Learning of Cross-Language Mappings through Distributed Vector Representations for Code}},
  booktitle = {ICSE (NIER)},
  year = {2018},
  url = {https://openreview.net/forum?id=xXowJXocJz},
}
Figure