Cross-language code semantic alignment method based on unified abstract syntax tree and graph matching neural network
By using a unified abstract syntax tree and graph matching neural network, a unified representation and semantic alignment of cross-language code is constructed, which solves the code alignment problem in a multilingual environment, realizes deep semantic understanding and functional equivalence judgment, and improves the accuracy of cross-language code analysis.
Patent Information
- Application Number
- CN202511383134.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-09-25
- Publication Date
- 2025-12-26
AI Technical Summary
In a multilingual environment, existing technologies struggle to construct a unified code representation space to align codes from different programming languages at the semantic level, leading to difficulties in cross-language code analysis, particularly in code clone detection and retrieval.
We employ a unified abstract syntax tree and graph matching neural network approach to construct a unified AST representation through node classification mapping, label refinement and abstraction, and structural enhancement pruning. We then utilize the graph matching network to perform cross-graph semantic alignment and generate vector representations of functional similarity.
It achieves precise semantic alignment of code from different programming languages, improves the accuracy and robustness of cross-language code clone detection and retrieval, and overcomes the limitations of traditional methods.
Smart Images

Figure CN121209879A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the interdisciplinary field of software engineering and program analysis, and in particular relates to a cross-language code semantic alignment method based on a unified abstract syntax tree and graph matching neural network. Background Technology
[0002] As software systems continue to grow in scale, multi-language hybrid development has become the norm. Large software projects often employ multiple programming languages simultaneously, such as Java, C++, Python, and C#, to fully leverage their respective strengths. However, this multi-language environment also presents significant challenges to code understanding and maintenance, especially in cross-language code analysis, where identifying functionally equivalent or semantically similar implementations becomes a difficult problem. In practical applications, cross-language code understanding involves tasks such as code clone detection, code retrieval, refactoring, and reuse, requiring developers to establish functional-level correspondences between different languages. However, due to the significant differences in vocabulary, syntax, and abstract syntax tree (AST) structures among different programming languages, direct cross-language comparisons often fail to achieve ideal results.
[0003] Existing research attempts to alleviate this problem from different angles. On the one hand, code snippets are treated as natural language sequences, represented using sequence-to-sequence models or pre-trained code language models. While these methods can capture some contextual order information, they lack effective modeling of the internal structure of the code and struggle to characterize deep semantics. On the other hand, some methods utilize structured information such as Abstract Syntax Trees (ASTs) and Control Flow Graphs (CFGs) to enhance representations. ASTs can reflect the hierarchical features of syntax, and CFGs can reveal the execution path of control flow. However, both have limitations when dealing with multiple languages: the node types and levels of ASTs vary greatly across different languages, making direct alignment difficult; while CFGs help understand execution logic, they lack the expression of data dependencies and therefore cannot fully reflect the semantics of the program. Furthermore, existing methods mostly employ simple concatenation or pre- and post-processing when combining external information, lacking deep interaction between the code's representation and structural information, making it difficult for models to fully utilize semantic cues when aligning across languages.
[0004] Therefore, how to construct a unified code representation space in a multilingual environment so that codes from different languages can be aligned at the semantic level remains a key challenge in current cross-language software engineering research. Summary of the Invention
[0005] This invention aims to address the difficulty in aligning the semantics of code across different programming languages. It proposes a unified representation and matching method to achieve deep semantic understanding and functional equivalence judgment of cross-language code. By constructing a shared semantic space, functionally equivalent code fragments are brought closer together in their vector representations, effectively supporting practical applications such as cross-language code clone detection and code retrieval.
[0006] To achieve the above-mentioned technical objectives, the technical solution adopted by the present invention is as follows:
[0007] A cross-language code semantic alignment method based on a unified abstract syntax tree (AST) and a graph matching neural network (GMN) is proposed. This method consists of two core modules: a unified AST module and a cross-graph semantic alignment module based on a graph matching network (GMN).
[0008] Step 1: Construct a unified abstract AST representation for the source code of different programming languages (such as Java, C++, Python, C#);
[0009] Step 2: Based on GMN, perform joint encoding and semantic alignment on the two unified ASTs to generate a vector representation that can measure functional similarity.
[0010] Step 1 belongs to the Unified AST Abstraction Module, and Step 2 belongs to the GMN Semantic Alignment Module. The two modules are executed sequentially.
[0011] To optimize the above technical solution, the specific measures also include:
[0012] Step 1 above constructs a unified abstract AST representation for source code in different programming languages (such as Java, C++, Python, and C#), including:
[0013] 1.1) Node Classification and Mapping: Extract AST node types from compilers or parsers of various languages and map them to a common tag set based on semantics. For example, Java's MethodInvocation, C++'s CallExpression, and Python's Call are uniformly mapped to the "CALL" tag; loop statement nodes from various languages are unified into the "FOR_LOOP" category; nodes with similar semantics such as conditional statements, declarations, and assignments are also merged into their corresponding common tags, compressing thousands of language-specific node types into hundreds of common tags.
[0014] 1.2) Tag Refinement and Abstraction: Nodes with consistent semantics but different names (such as function definition nodes in various languages) are further unified into higher-level general tags; low-frequency and irrelevant nodes such as comments and empty statements are classified into the "Other" category to reduce noise.
[0015] 1.3) Structural Enhancement and Pruning: Insert a global root node at the top of each AST, and take the original top-level control nodes (such as function declarations, class declarations, etc.) as its direct child nodes to provide a common anchor point for cross-graph alignment; retain the complete subtree structure and attributes of key nodes such as control flow, declarations, and constants; prune or fold non-key nodes such as modifiers, comments, and empty statements to simplify the tree structure.
[0016] Step 2 above performs joint encoding and semantic alignment of the two unified ASTs based on GMN to generate a vector representation with measurable functional similarity, including:
[0017] 2.1) Node feature encoding: The general label embedding and attribute embedding of each node are concatenated into an initial feature vector, which is then processed by MLP mapping, LayerNorm and Dropout to obtain an initial node representation with a unified dimension.
[0018] 2.2) Cross-graph attention interaction: For nodes in two ASTs, the attention weights between nodes are calculated through the cross-graph attention mechanism, and the cross-graph context vector of each node relative to the other AST is generated in a weighted aggregation manner to realize the semantic interaction between nodes in the two graphs.
[0019] 2.3) Intragraph Aggregation Update: By fusing the cross-graph context and local graph neighbor information of a node through a gated recurrent unit (GRU), the node representation is updated in multiple rounds of iteration, so that each node simultaneously encodes the structural information of the local graph and the semantic clues of the cross-graph.
[0020] 2.4) Graph-level representation generation: Based on learnable attention weights, the final representation of all nodes is weighted and pooled to generate the overall vector representation of the two ASTs. The vector pair has fully perceived the information of the other graph and can be directly used to calculate the functional equivalence through cosine similarity.
[0021] The present invention has the following beneficial effects:
[0022] This invention maps syntax nodes of different programming languages to a common tag set through a unified AST abstraction mechanism, which significantly reduces syntax noise and structural inconsistencies in cross-language analysis and effectively eliminates grammatical differences between multiple languages.
[0023] This invention innovatively introduces a Graph Matching Network (GMN), which leverages its cross-graph attention mechanism to achieve bidirectional semantic interaction between nodes of different abstract syntax trees. Combined with a gated recurrent unit (GRU) for multi-round intra-graph iterative updates, this invention achieves accurate semantic matching of cross-language codes at the functional level. It overcomes the limitations of traditional methods that rely solely on text or monolingual structural features, and achieves deep semantic alignment.
[0024] This invention introduces a contrastive learning strategy into the constructed shared semantic vector space. By constructing semantically equivalent positive sample pairs and significantly different negative sample pairs, the model is driven to optimize the vector representation. This results in functionally equivalent codes being highly clustered in the vector space, while non-equivalent codes are separated from each other, significantly enhancing the model's ability to discriminate and generalize cross-language code semantics. Attached Figure Description
[0025] Figure 1 A flowchart of the cross-language code semantic alignment method provided by the present invention;
[0026] Figure 2 This is an example of mapping multilingual AST nodes to common labels. Detailed Implementation
[0027] To make the objectives, technical solutions, and advantages of this invention clearer, the invention will be further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the invention.
[0028] This invention proposes a cross-language code semantic alignment method, the method flow is as follows: Figure 1 As shown, this method takes source code functions or code snippets from different programming languages as input units and outputs a pair of mutually perceptible vector representations in a semantic vector space. It then determines the functional equivalence of the code by calculating vector similarity. The entire method is divided into the following two modules.
[0029] Unified AST Abstraction Processing Module: This module aims to eliminate syntactic differences between different programming languages, providing a unified structured representation for cross-language code semantic analysis. Taking source code from different programming languages (such as Java, C++, Python, and C#) as input, the module first constructs a corresponding Abstract Syntax Tree (AST) for each code segment. Then, through a system-designed type mapping rule, the language-specific node types are normalized to the same universal label system, and node attributes are discretized and embedded. Each AST node is ultimately composed of a high-dimensional initial feature vector formed by concatenating universal type label embeddings and attribute embeddings. This module not only achieves structural alignment of multilingual ASTs but also provides standardized and vectorized input for downstream graph neural networks, significantly reducing syntactic noise in cross-language analysis.
[0030] The cross-graph semantic alignment module based on a graph matching neural network undertakes the crucial task of aligning structure with semantics. It takes a unified abstract AST pair as input and outputs a graph-level vector representation that reflects the functional semantics of the code. The module is implemented based on a Graph Matching Network (GMN) and includes four core processing stages: First, it encodes node features and unifies dimensions using a Multilayer Perceptron (MLP); second, it introduces a cross-graph attention mechanism, enabling nodes from different ASTs to interact and perceive semantic information in the peer graph; third, it utilizes a Gated Recurrent Unit (GRU) to combine the local graph's structural neighborhood with the cross-graph context for multi-round iterative message passing and node state updates; finally, it uses a learnable global attention pooling mechanism to aggregate all node states to generate a fixed-dimensional graph-level semantic vector. Through this series of processes, functionally equivalent code segments can generate highly similar vector representations in a shared semantic space, thereby accurately achieving deep functional matching and semantic retrieval of cross-language code.
[0031] This method focuses on deep functional semantic understanding of cross-language code, aiming to overcome the limitations of traditional grammatical structure matching. By introducing a dual mechanism of unified AST abstraction and cross-graph semantic alignment based on graph matching neural networks (GMN), it achieves cross-language code understanding from syntax to semantics. The functional semantics of the code are captured not only by modeling its logical structure and execution flow, but also through the cross-graph attention interaction and iterative update mechanism of GMN, enabling explicit alignment of code representations from different languages in the vector space. Most code segments in cross-language code carry specific functions; therefore, the identification of functional semantics plays a crucial role in cross-language code alignment. The unified AST abstraction mechanism adopted in this invention is built based on multilingual grammar specifications, preserving the unique grammatical structures of various languages while extracting shared functional semantic units across languages. Furthermore, deep cross-graph semantic fusion is achieved through the GMN model, resulting in highly consistent vector representations of functionally equivalent code. Therefore, simultaneously introducing unified semantic representation and cross-graph alignment mechanisms in cross-language code understanding allows the model to more accurately capture functionally equivalent code patterns, significantly improving the accuracy and robustness of cross-language code clone detection and retrieval.
[0032] The specific implementation process is as follows:
[0033] Step 1: Construct a universal tag mapping system. This system processes raw Abstract Syntax Trees (ASTs) from different programming languages (such as Java, C++, Python, and C#) and maps them to the same set of "universal semantic tags," preserving necessary attributes and structural information. The implementation process is as follows:
[0034] 1.1) First, perform node classification mapping.
[0035] Extract all node types from various compilers or parsers (such as Eclipse JDT, Clang, Python AST), and then compare them with their semantic meanings, such as... Figure 2 As shown, Java's `MethodInvocation`, C++'s `CallExpression`, and Python's `Call` are all mapped to the same generic tag "CALL"; Java's `ForStatement`, C++'s `ForStmt`, and Python's `For` are grouped into the `for_statement` category; and nodes with similar semantics, such as conditional statements, declarations, and assignments, are merged into their respective generic tags. In this way, the original thousands of language-specific node types are compressed into a few hundred generic tags, laying the foundation for subsequent cross-language comparisons.
[0036] 1.2) Secondly, these general labels are refined or further abstracted.
[0037] For example, for nodes related to "function definitions," Java might use `FunctionDeclaration` or `MethodDeclaration`, while C++ might use `FunctionDecl` or `MethodDecl`. Although the names differ, the semantics are consistent. Therefore, these nodes are unified into a more general label to ensure they receive the same representation across any language. At the same time, to avoid interference from low-frequency nodes that are not closely related to core tasks, rarely occurring types such as comments, empty statements, and compiler-generated partial nodes are categorized as "Other." This maintains the compactness of the label system while reducing noise.
[0038] 1.3) Enhance the overall structure of the unified abstract AST to provide a more concise, semantically focused and easily aligned input for the subsequent graph matching network.
[0039] A global root node is inserted at the very top of each syntax tree. This global node does not correspond to any specific syntax element, but rather serves to unify previously independent control or declaration flows to the same level. Specifically, all top-level control nodes (such as function declarations, class declarations, module entry points, etc.) are made direct children of the root node. The advantage of this is that regardless of how the order or hierarchy of these top-level nodes in the original AST changes across different languages, the global root node can centralize them under the same parent node, providing a common anchor point for overall convergence during cross-graph alignment. Next, key nodes in the abstract syntax tree are marked and protected. These key nodes mainly include:
[0040] Control flow nodes, such as loops (FOR_LOOP) and conditional statements (IF_COND), often contain the core of the program logic.
[0041] Declaration nodes, such as FUNC_DEF and VAR_DECL (variable declarations), reveal the abstract structure and type information of the program.
[0042] Key attribute nodes: such as nodes with constant values (CONST_NUM, CONST_STR) or nodes representing types (TYPE_REF), which carry runtime semantics or type constraints.
[0043] During processing, complete subtree information is preserved for these types of nodes, including all their child nodes and attribute embeddings. Regardless of depth or width, these subtrees are not pruned or collapsed.
[0044] Conversely, non-critical nodes that are not significant for cross-language semantic alignment are selectively pruned during processing. Non-critical nodes typically include:
[0045] Pure modifier and comment nodes: such as access modifiers, annotations, and comment nodes. This information has already been extracted at the attribute level, so it no longer needs to be retained in the AST structure.
[0046] Empty statements or placeholder nodes: such as empty wrapper structures automatically generated by some language parsers, placeholder nodes with no practical meaning, etc. These types of nodes do not contribute to the semantics of the code and can be safely removed.
[0047] Auxiliary intermediate nodes: Some AST architectures and specific languages introduce additional hierarchical nodes to express internal details, but these levels are not necessary for semantic alignment. For such intermediate nodes that only serve an organizational function, if they have no key child nodes or only a single path, they can be skipped and their child nodes can be moved directly to a higher level, thus simplifying the tree structure.
[0048] Step 2: The cross-graph semantic alignment method based on graph matching neural networks is used to encode and align the two ASTs simultaneously. The implementation process is as follows:
[0049] 2.1) First, feature encoding is performed on each node in each AST.
[0050] Each node in each AST, after processing, possesses an initial feature vector composed of "general label embedding + attribute embedding". In the first step of GMN, these feature vectors are input into a multilayer perceptron (MLP) for mapping. Through two or three fully connected layers with ReLU activation, the node features are projected into a unified dimensional space. After processing with LayerNorm and Dropout, the initial node representations are obtained.
[0051]
[0052] This process integrates and denoises heterogeneous attribute information, allowing all nodes to share a unified vector dimension format.
[0053] 2.2) Secondly, a cross-graph attention mechanism is introduced to enable bidirectional semantic interaction between nodes in two ASTs.
[0054] On the one hand, for each node i in AST1, calculate its attention score with all nodes j in AST2:
[0055]
[0056] Then, the weights αij are obtained by normalizing the j-th dimension:
[0057]
[0058] This allows the features of all nodes on AST2 to be aggregated in a weighted sum manner, generating a cross-graph context ci.
[0059]
[0060] Similarly, each node j in AST2 will perform the same operation on all nodes in AST1 to obtain the cross-graph context. This interactive attention ensures that every node in AST1 receives global information from AST2 and vice versa, thus beginning to reveal potential correspondences in depth across graphs.
[0061] 2.3) Deep integration of cross-language semantics is achieved by fusing the cross-graph context and local graph structure information of nodes through gated recurrent units (GRU).
[0062] Specifically, GMN updates the node representation in each iteration as follows:
[0063]
[0064] Based on the concatenated vector ui, it aggregates its own neighbor features through the adjacency relationship of this graph.
[0065]
[0066] GRU takes the concatenated vector and the neighbor aggregation vector as input, based on the update gate and reset gate policies, and outputs a new node representation.
[0067]
[0068] Through multiple rounds of cross-graph attention and intra-graph GRU updates, each node gradually learns a comprehensive representation that includes both its own syntactic structure within the graph and semantic cues from the other graph. After T iterations, the vectors of each node in AST1 and AST2 are fully integrated with cross-graph information.
[0069] 2.4) Node-level representations are aggregated into graph-level vectors through a learnable attention pooling mechanism to capture global semantic information of the entire code structure.
[0070] Specifically, GMN generates the overall representation of the AST in the following manner:
[0071] For AST1, the final iterative representation zi(T)|i∈Nodes(AST1) of all nodes is weighted and summed according to the trainable pooling weights yi:
[0072]
[0073] Similarly, the nodes in AST2 yield pooling weights δj and the overall vector v2. Since the representation of each node has fully interacted with the other graph during the iteration process, this pair of vectors v1 and v2 essentially possesses deep semantic information that "perceives each other," making it suitable for direct cosine similarity calculation.
[0074]
Claims
1. A cross-language code semantic alignment method, characterized in that, include: Step 1: Map the Abstract Syntax Tree (AST) nodes of various programming languages to a unified set of common tags and perform structural enhancement. Step 2: Perform feature encoding on the unified AST nodes to obtain the initial node representation; Step 3: Perform node-level interactions between the two ASTs based on the cross-graph attention mechanism; Step 4: Iteratively generate node representations through an in-graph loop update mechanism, and obtain the overall semantic vector through global attention pooling; Step 5: In the shared semantic space, through contrastive learning training, the distance between semantically equivalent positive sample vectors is shortened, while the distance between non-equivalent negative sample vectors is widened, thereby obtaining a semantic representation with stronger discriminative ability.
2. The cross-language code semantic alignment method according to claim 1, characterized in that, The unified AST mentioned in step 1 includes: Semantic classification is performed on the original AST nodes of languages such as Java, C++, Python, and C#, and functionally equivalent node types are mapped to the same general label; A global root node is inserted at the top level of the AST to unify the differences in the top-level structure of different languages. Non-critical nodes are selectively pruned to eliminate redundant syntactic structures.
3. The cross-language code semantic alignment method according to claim 1, characterized in that, Step 2, the node feature encoding, includes: The general label embedding and attribute embedding are concatenated as node features; The node features are input into an MLP (Multilayer Perceptron) and a unified-dimensional initial node representation is obtained through nonlinear mapping.
4. The cross-language code semantic alignment method according to claim 1, characterized in that, The cross-graph attention mechanism described in step 3 includes: For each node in the first AST, calculate the attention weight with all nodes in the second AST; The cross-graph node features are weighted and aggregated according to their weights to obtain the cross-graph context representation. The reverse operation performs the same steps on the second AST node, thereby achieving bidirectional information representation.
5. The cross-language code semantic alignment method according to claim 1, characterized in that, Step 4, the intra-graph cyclic update, includes: Concatenate the cross-graph context representation with the current node representation; Aggregate neighbor features by combining adjacency relationships; By using a gated recurrent unit (GRU), information from the local graph and cross-graph context are fused to achieve multiple rounds of iterative updates, resulting in the final node representation.
6. The cross-language code semantic alignment method according to claim 1, characterized in that, The comparative learning described in step 5 includes: During training, cross-language functionally equivalent code pairs are used as positive samples, and semantically different code pairs are used as negative samples. By using cosine similarity as a metric, the loss function is optimized to minimize the vector distance between positive samples and maximize the vector distance between negative samples.
7. The cross-language code semantic alignment method according to claim 1, characterized in that, The method is applicable to tasks such as cross-language code analysis, representation learning, and semantic modeling, and can provide basic support for code understanding and processing in multilingual scenarios.
Citation Information
Cited By
Cross-language compilation optimization method based on graph neural network
CN121764480A
Cross-language compilation optimization method based on graph neural network
CN121764480B
Automatic rule generation method and system for Web application firewall and electronic equipment
CN122160197A