Source code similarity analysis method and system based on semantic enhancement AST

By generating a semantically enhanced AST and combining it with a graph convolutional network and attention mechanism, the problem of function similarity judgment in cross-language code clone detection is solved, achieving high accuracy and efficiency in cross-language function similarity detection.

CN119293520APending Publication Date: 2025-01-10Chinese People's Liberation Army Cyberspace Force Information Engineering University
View PDF 0 Cites 7 Cited by

Patent Information

Application Number
CN202411312473.5
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-09-20
Publication Date
2025-01-10

AI Technical Summary

Technical Problem

Existing technologies struggle to accurately detect cross-language code clones, especially due to inaccurate function similarity assessments caused by syntactic and semantic differences between different programming languages.

Method used

By generating a semantically enhanced AST, the differences in AST node representations between different programming languages ​​are eliminated by using a node vocabulary, and the control flow edges and data flow edges are expanded. The AST is transformed into graph vectors by combining graph convolutional networks and attention mechanisms, and similarity judgment is performed using a fully connected neural network.

Benefits of technology

It improves the accuracy and efficiency of cross-language function similarity detection, and can better identify functions with similar functions.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119293520A_ABST
    Figure CN119293520A_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of program similarity, in particular to a source code similarity analysis method and system based on semantic enhancement AST, and the method comprises the steps: respectively generating corresponding abstract syntax trees AST for two functions using different programming languages; enabling nodes with the same type but different names in different programming languages AST to have the same representation by utilizing a node vocabulary, and shielding the node representation difference of the different programming languages AST; a control flow edge and a data flow edge are expanded for the AST after node standardization processing, a semantic enhancement AST is generated, and the semantic enhancement AST integrates grammar, structure and semantic information of a function; a function similarity analysis network is designed, semantic enhancement AST is converted into graph vectors in combination with a graph convolutional network and an attention mechanism, the two graph vectors are spliced and then input into a full-connection neural network, and the similarity of different programming language functions is judged. According to the method, similarity judgment of different programming language functions can be realized, and the detection efficiency is improved while high accuracy of the detection effect is achieved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of program similarity, and in particular to a source code similarity analysis method and system based on semantically enhanced AST. Background Art

[0002] Although similarity detection can be performed using API semantics and some token features between different programming languages, these features only reflect the similarity of the function lexical level, and do not judge the similarity of the function semantics. Therefore, it is not possible to achieve good results when detecting cross-language code clones only through source code lexical level analysis. In order to more accurately detect cross-language code clones, further explore the relationship between the grammatical, semantic, and structural features of functions, so as to more comprehensively evaluate the similarity between two functions.

[0003] The Abstract Syntax Tree (AST) represents the syntax and structural features of the code in a tree structure. For similar functions implemented in different languages, their syntax and structural features have certain similarities. However, AST cannot perceive the semantics of the program, so AST-based methods cannot identify programs with different syntax but similar semantics. Graph-based methods can represent the code by constructing a Program Dependence Graph (PDG). PDG contains both data dependency and control dependency, and can predict semantic similarity by discovering isomorphic subgraphs. Although graph-based methods have high accuracy in similarity detection, they perform poorly in cross-language program detection because this representation needs to capture more fine-grained details. In addition, due to the different lexical methods used in different programming languages, nodes of the same type also have different names, which makes it difficult to generate a good universal representation for cross-language functions. Summary of the invention

[0004] The purpose of the present invention is to solve the problems existing in the prior art and propose a source code similarity analysis method and system based on semantically enhanced AST, which can realize the similarity judgment of functions in different programming languages, achieve high detection accuracy and improve detection efficiency.

[0005] To achieve the above purpose, the technical solution adopted is:

[0006] A source code similarity analysis method based on semantically enhanced AST, including:

[0007] Generate corresponding abstract syntax trees AST for two functions using different programming languages;

[0008] The node vocabulary is used to make nodes of the same type but different names in different programming language ASTs have the same representation, shielding the differences in node representations of different programming language ASTs;

[0009] The control flow edges and data flow edges of the AST after node standardization are expanded to generate a semantically enhanced AST, which integrates the syntax, structure and semantic information of the function;

[0010] A function similarity analysis network is designed, and the semantically enhanced AST is converted into a graph vector by combining the graph convolutional network and the attention mechanism. The two graph vectors are concatenated and input into a fully connected neural network to determine the similarity of functions in different programming languages.

[0011] According to the source code similarity analysis method based on semantically enhanced AST of the present invention, further, the process of generating a node vocabulary is:

[0012] First, we counted all the node names in the abstract syntax trees of a large number of different programming languages ​​and classified them according to the node types to obtain all the node names and types of the AST.

[0013] Then, a unified name is specified for nodes of the same type, a node vocabulary of the abstract syntax tree is generated, and a correspondence between the node names of the abstract syntax trees of different programming languages ​​and the node vocabulary names is established; the rules for formulating names in the node vocabulary need to ensure that the unified names in the vocabulary do not conflict with the original names of different node types in the abstract syntax tree.

[0014] According to the source code similarity analysis method based on semantically enhanced AST of the present invention, further, using a node vocabulary table, nodes of the same type but different names in ASTs of different programming languages ​​have the same representation including:

[0015] First, all node names in the AST are traversed. If the node name belongs to the node name in the node vocabulary, the name is mapped to the unified name in the table for replacement; if the node name does not belong to the node name in the node vocabulary, it is replaced with the "unk" character. Ultimately, all node names of the same type in the abstract syntax trees of functions of different programming languages ​​have the same name.

[0016] According to the source code similarity analysis method based on semantically enhanced AST of the present invention, further, the AST extension control flow edge after the node standardization processing includes:

[0017] Each node of AST represents a statement in the source code, and the function statement type is identified based on the AST node name;

[0018] In AST, find the code statements with the same parent node according to the statement type;

[0019] Based on the position of the node in the AST, connect the above parent nodes and child nodes in the order they appear in the source code;

[0020] When the node content is a transfer control statement, the relationship between statements is updated according to the transfer control statement semantics, and finally all control flow edges added in the AST are obtained.

[0021] According to the source code similarity analysis method based on semantically enhanced AST of the present invention, further, the AST extended data flow edge after the node standardization processing includes:

[0022] First, traverse the AST to collect the definition and usage information of all variables;

[0023] Establish links between variable definitions and usage information, associating each definition with a usage;

[0024] Construct use-def chains: Each chain starts from a use of the variable, traces back to the definition corresponding to the use, and then continues to trace back to other uses of the definition until the entire abstract syntax tree is traversed. By removing redundant chains or merging duplicate chains, a chain linking all related uses and definitions is finally obtained.

[0025] According to the source code similarity analysis method based on semantically enhanced AST of the present invention, further, the semantically enhanced AST is converted into a graph vector by combining a graph convolutional network and an attention mechanism, which is divided into a node embedding stage and a graph vector generation stage. In the node embedding stage, the graph convolutional network is used to update the node feature vector of the semantically enhanced AST by neighbor aggregation; in the graph vector generation stage, the updated node feature vector is weighted using the attention mechanism according to the different importance of different nodes in similarity judgment, thereby generating a graph vector.

[0026] According to the source code similarity analysis method based on semantically enhanced AST of the present invention, further, in the node embedding stage, all nodes in the semantically enhanced AST are firstly encoded using one-hot, and the node initialization vector matrix is ​​obtained as follows: Where N represents the number of nodes in the graph, F represents the dimension of the feature vector of each node, Represents the initialization vector of the node in the graph; then the graph convolution network GCN is used to update the feature vector of the node. In the process of node feature propagation using GCN, each AST node fuses the feature information of the current node and its neighboring nodes, and gradually fuses the local features of the graph in the process of convolution with different layers, and finally obtains the updated node feature vector set

[0027]

[0028] According to the source code similarity analysis method based on semantically enhanced AST of the present invention, further, in the graph vector generation stage, according to the node feature vector set updated by GCN First, the vector values ​​of all nodes are averaged and a nonlinear transformation is used to obtain the global feature c of the graph, c∈R F ; Then the graph vector g is obtained by the following formula:

[0029]

[0030] Where f2(·) is the sigmoid function and T is the transpose of the matrix.

[0031] According to the source code similarity analysis method based on semantically enhanced AST of the present invention, further, the two graph vectors are spliced ​​and input into the input layer of the fully connected neural network. After the complex relationship between the vectors is learned by the hidden layer, the output is mapped to a real number between 0 and 1 using the sigmoid function, and compared with the set similarity threshold. If it is greater than the threshold, the function pair is judged to be similar; if it is less than the threshold, the function pair is judged to be dissimilar.

[0032] Furthermore, the present invention also provides a source code similarity analysis system based on semantically enhanced AST, comprising:

[0033] AST generation module, used to generate corresponding abstract syntax trees AST for two functions using different programming languages;

[0034] The node standardization module is used to use the node vocabulary to make nodes of the same type but different names in different programming language ASTs have the same representation, shielding the differences in node representations of different programming language ASTs;

[0035] The semantic enhancement module is used to expand the control flow edges and data flow edges of the AST after node standardization to generate a semantically enhanced AST that integrates the syntax, structure, and semantic information of the function;

[0036] The similarity judgment module is used to design a function similarity analysis network. It combines the graph convolutional network and the attention mechanism to convert the semantically enhanced AST into a graph vector. The two graph vectors are concatenated and input into a fully connected neural network to judge the similarity of functions in different programming languages.

[0037] The beneficial effects achieved by adopting the above technical solution are:

[0038] The source code similarity analysis method based on semantically enhanced AST of the present invention proposes a new code representation and designs a function similarity analysis network to judge code similarity. First, a node vocabulary is designed to standardize the AST node names of different programming languages, so that the same type of AST nodes in different programming languages ​​have a unified name, thereby eliminating the node name differences of AST between different programming languages; then the control flow and data flow analysis of the function are performed, and the relationship between AST nodes is added to obtain a semantically enhanced AST, thereby enriching the semantic features of AST and improving the code representation ability of AST. In addition, a function similarity analysis network is designed, which uses a graph convolutional network to update the semantically enhanced AST node features, and uses an attention mechanism to fuse node weights to obtain a vector representation of the function, and the vectors of two different functions are spliced ​​and input into a fully connected neural network for similarity judgment. The present invention has greatly improved the efficiency and accuracy of cross-language function similarity detection. BRIEF DESCRIPTION OF THE DRAWINGS

[0039] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the following briefly introduces the drawings of the embodiments of the present invention, wherein the drawings are only used to illustrate some embodiments of the present invention, but not to limit all embodiments of the present invention thereto.

[0040] Figure 1 It is a flowchart of a source code similarity analysis method based on semantically enhanced AST according to an embodiment of the present invention;

[0041] Figure 2 It is an AST structure diagram of a Java function in an embodiment of the present invention;

[0042] Figure 3 It is an AST structure diagram of the Python function of an embodiment of the present invention;

[0043] Figure 4 It is a schematic diagram of a process of generating a semantically enhanced AST according to an embodiment of the present invention;

[0044] Figure 5 It is a semantically enhanced AST structure diagram of a Java function in an embodiment of the present invention;

[0045] Figure 6 It is a semantically enhanced AST structure diagram of a Python function in an embodiment of the present invention;

[0046] Figure 7 is a flow chart of a function similarity analysis network according to an embodiment of the present invention;

[0047] Figure 8 It is a schematic diagram of AST node aggregation according to an embodiment of the present invention;

[0048] Fig. 9is a flow chart of similarity judgment using a fully connected neural network according to an embodiment of the present invention;

[0049] Fig.10 This is a cross-language clone detection effect diagram of an embodiment of the present invention;

[0050] Fig.11 This is the influence of different numbers of convolutional layers in the embodiments of the present invention on the effect of the method. DETAILED DESCRIPTION

[0051] The following will be combined with the drawings of specific embodiments of the present invention to clearly and completely describe the exemplary scheme of the embodiment of the present invention. Unless otherwise defined, the technical terms or scientific terms used in the present invention should be the common meanings understood by people with ordinary skills in the field.

[0052] This embodiment discloses a source code similarity analysis method based on semantically enhanced AST, the process is as follows: Figure 1 As shown, the following steps are included:

[0053] Step S101 : Generate corresponding abstract syntax trees AST for two functions using different programming languages ​​respectively.

[0054] Since different programming languages ​​have large differences when performing syntax parsing, it is necessary to select appropriate tools to generate suitable AST. At present, although there are many syntax tree analysis tools for generating AST, most tools and methods are only effective for a single programming language and cannot process multiple programming language programs at the same time. For example, Python's "ast" library is limited to parsing Python programs, but cannot parse languages ​​such as JAVA or C / C++. If different parsing tools are used to generate AST for each programming language, the AST may have too large differences when generating codes with similar meanings due to differences in the parsing methods and implementation principles of different tools. Therefore, in order to reduce the differences in the generation of abstract syntax trees between programs in different programming languages, a unified tool should be used to parse the codes of different programming languages. In this embodiment, tree-sitter is used to generate an abstract syntax tree, and tree-sitter is a tool for parsing and analyzing source code. It provides an efficient and flexible way to build an abstract syntax tree and supports the parsing of multiple programming languages. Although there are still certain differences in the representation of node content in the AST generated by parsing programs in different programming languages ​​through tree-sitter, due to the same principle of its parsing method, the difference in the expression of similar functions on AST is not significant.

[0055] Step S102: Using the node vocabulary, nodes of the same type but different names in ASTs of different programming languages ​​have the same representation, thereby shielding differences in node representations of ASTs of different programming languages.

[0056] like Figure 2 and Figure 3 The following figure shows the abstract syntax trees generated by tree-sitter for two similar functions. These two functions have similar functions and can both determine whether the input is an even number. It can be seen that although the same function is executed using different language programs, their abstract syntax trees are not completely consistent. Their differences are mainly reflected in some structures and node names. Although some nodes are in the same position in the AST, the content representation of the node is still quite different. For example, the AST root node of the Java language program is called "program", but it is called "module" in the Python program. Although the two names are different in vocabulary, they actually represent the same meaning. Therefore, in order to eliminate the differences between node names and make similar types of AST nodes in different programming languages ​​have the same representation, a "node vocabulary" is designed to standardize the AST node names in different languages.

[0057] According to the different node types, a node vocabulary is created for all node names of AST, so that nodes of the same type but different names in different language ASTs have unified names. And the AST names are replaced with the node names in the vocabulary. Finally, the nodes of the same type in different programming language ASTs have the same name, thereby eliminating the differences in node names of ASTs between different programming languages. In addition, the node vocabulary created has strong scalability and can be easily extended to other programming languages.

[0058] The process of generating a node vocabulary is:

[0059] First, we count all the node names in the abstract syntax trees of a large number of different programming languages ​​and classify them according to the node type, so as to establish a unified naming standard for the syntax tree nodes of the same type. Figure 2 and Figure 3 Nodes marked with the same color have the same type, so the same name should be used for such nodes. When counting node types, we follow the semantics of the name and expert experience. After analyzing a large number of abstract syntax trees of Python and Java languages, we finally get all the node names and types of AST.

[0060] Then, a unified name is specified for nodes of the same type, a node vocabulary of the abstract syntax tree is generated, and a correspondence between the node names of the abstract syntax trees of different programming languages ​​and the node vocabulary names is established. For example, the node named "method_declaration" in the AST generated by the Java program and the node named "function_declaration" in the Python program belong to the same type of node, so they have the same "function_declaration" name in the node vocabulary. For the formulation rules of the names in the node vocabulary, it is first necessary to ensure that the names defined in the vocabulary will not conflict with the original names of different node types in the abstract syntax tree. After that, for nodes of the same type with different names in different programs, a unified node name is generated. The set of these unified node names constitutes the abstract syntax tree node vocabulary, and the corresponding relationship constitutes a mapping from the abstract syntax tree node to the node vocabulary, which can correspond the node names of different programming language programs to the names in the node vocabulary. Some of the contents of the node vocabulary finally generated are shown in Table 1.

[0061] Table 1 Part of the node vocabulary

[0062]

[0063] When using the node vocabulary to generate a node standardized AST, first traverse all node names in the AST. If the node name belongs to the node name in the node vocabulary, the name is mapped to the unified name in the table for replacement; if the node name does not belong to the node name in the node vocabulary, it is replaced with the "unk" character. Ultimately, all node names of the same type in the abstract syntax trees of functions of different programming languages ​​are replaced with names in the node vocabulary so that they have the same name. The function abstract syntax tree node standardization algorithm is shown in Algorithm 1.

[0064]

[0065] After the node names of AST are replaced by a unified vocabulary, a new standardized AST is obtained. This method reduces the differences between ASTs of different programming language programs, and can greatly enhance the consistency of node content representation while retaining the integrity of the AST syntax structure. In the subsequent vector generation process, the standardized AST can more effectively learn the characteristics of different programming languages, thereby better solving the cross-language similarity problem.

[0066] Therefore, the final AST structure is G AST =<V AST ,E AST >, where V AST Represents the set of nodes after the AST node name is standardized, E ASTRepresents the set of edges connecting two AST nodes. The set of edges is represented by E AST ={<v,w>|v,w∈P(v,w)}, where P(v,w) indicates that there is a direct abstract syntax tree edge from node v to node w.

[0067] Step S103: Expand the control flow edges and data flow edges of the AST after the node standardization processing to generate a semantically enhanced AST, which integrates the syntax, structure and semantic information of the function and can comprehensively capture the essential properties of the function.

[0068] AST contains the grammatical structure features of the code. Although functions written in different programming languages ​​have some similar structures in the abstract syntax tree, the representation based on the syntax level often ignores the deeper semantic information in the source code, making it particularly insufficient to use only AST for similarity judgment when processing complex programs. This is because for functions with similar functions, their semantic information does not change in essence due to different programming languages. Therefore, function semantics, as an important feature in function similarity analysis, plays a vital role in accurately evaluating the similarity between different functions.

[0069] The control flow and data flow of a function are important semantic features of the function, which reveal the execution logic and data processing process of the function respectively. The control flow describes the process and execution path of the program during execution, which can help analyze the execution order of the code, determine possible branches and loops, etc., and provide the structure and path information of the code execution. The control flow reflects the logical structure and execution flow within the function, and is the key to understanding the behavior of the function. The data flow focuses on the transmission and changes of data in the function in the program. By tracking the definition and operation of variables and dependencies, it provides dependency relationships and data transmission information between variables. The data flow reflects the function's ability to operate and process data, and is the basis for understanding the function's function. The control flow and data flow work together to analyze the meaning and behavior of the code, and can fully understand the semantics of the code. Therefore, control flow and data flow analysis are performed on the basis of the abstract syntax tree to obtain a semantically enhanced AST. The specific process is as follows: Figure 4 shown.

[0070] In program analysis, there are various code representations to describe different program properties, such as control flow graphs, data flow graphs, AST, etc. However, a single representation usually only shows specific aspects of code snippets and is not enough to reveal their functions. In order to establish a semantic representation of function code, some methods use program dependency graphs to build semantics. PDG analyzes the control dependency and data dependency between function nodes and uses graphical representations to understand function semantics, but this representation ignores the grammatical structure information of the function, which limits the comprehensive understanding of function features to a certain extent. Therefore, in order to integrate the grammatical structure and semantic features of the code, the abstract syntax tree of the function is chosen to perform semantic expansion. The control flow edges and data flow edges are expanded by the AST after node standardization to obtain the semantic features of the function. This code representation presents the basic grammatical and semantic features of the underlying software program and is not limited to a specific programming language.

[0071] The Control Flow Graph (CFG) enumerates all possible execution orders of function code statements, each of which is determined by a set of conditional statements (for example, if and while statements). Therefore, the nodes in the CFG represent code statements, the edges represent the transfer of control, and the relationship between nodes and edges reveals the control flow of the function. CFG is usually built on assembly code or intermediate language. However, in order to obtain the control flow of the function at the source code level, it is necessary to add CFG edges between the nodes of the AST, so that the abstract syntax tree obtains the control flow information of the function without adding new graph structures.

[0072] The structure obtained at this time is G CFG =<V AST ,E CFG >, where V AST Represents the set of nodes after the AST node name is standardized, E CFG Represents the set of CFG edges connecting two AST nodes, E CFG ={<v,w>|v,w∈P CFG (v,w)},P CFG (v,w) indicates that there is a direct control flow edge from node v to node w.

[0073] The specific steps of constructing the control flow edge in the CFG of the function based on AST are:

[0074] 1. Identify the function statement type based on the AST node name: Each node of the AST represents a statement in the source code. By analyzing the type of the node (usually the name of the node or a specific identifier), you can determine what kind of statement it represents. For example, a node named if_statement in the AST may represent an if statement, while a node named function_declaration may represent a function declaration statement. The next step of analysis is carried out according to different statement types.

[0075] 2. Find code statements with the same parent node: In AST, the relationship between nodes reflects the structural relationship between statements in the source code. Nodes with the same parent node usually belong to the same basic block of CFG in the source code, and are crucial for subsequent determination of the direction of control flow. For example, the condition, body, and optional else part of an if statement may all be child nodes of the if_statement node.

[0076] 3. Connect the corresponding AST nodes: Based on the position of the nodes in the AST, connect these nodes in the order they appear in the source code. This helps to understand how these statements are combined. When building a CFG, it is not enough to just know the order relationship, you also need to understand the forks and merges of different control paths.

[0077] 4. Processing control flow statements: When the node content is a transfer control statement, it is necessary to deeply analyze the semantics of each control flow statement to correctly represent structures such as conditional branches and loops. Because these control structures change the execution flow of the code, the statement relationship needs to be updated according to their semantics. For example, an if statement may cause the program to jump to other code blocks, while a while loop may repeatedly execute a section of code. This embodiment selects and analyzes nine types of transfer control: if, switch, while, do while, for, try catch, break, continue, and goto.

[0078] 5. Update statement relations: After understanding the semantics of the transfer control statement, update the relations between statements and add relations between the corresponding nodes in the AST. This step ensures that the CFG reflects all possible execution paths, thus finally obtaining all control flow edges added in the AST.

[0079] Algorithm 2 is a specific algorithm implementation for adding control flow edges on AST. The algorithm first initializes CFG and creates a stack for traversing AST nodes. The algorithm uses a depth-first search strategy to visit each node in the AST one by one, ensuring that all code constructs are taken into account. For each node in the AST, the algorithm determines whether it is a control flow node. For control flow nodes, the algorithm performs specific processing logic based on the node type and adds edges between them. Non-control flow nodes are considered part of the basic block, and edges are added to them directly in the CFG. These nodes represent linear paths in program execution and do not involve branches.

[0080]

[0081]

[0082] A data flow graph (DFG) describes how code statements define and use variables. The nodes in the DFG represent statements, and the edges reflect the impact of statements on variable values. Since DFG is variable-oriented, it is often used to support program analysis that requires tracking the life cycle of variables. When it comes to reading or updating operations on variables, the data flow edges in the DFG can clearly identify subsequent access or modification behaviors of the same variable. Therefore, the edges of the DFG can provide in-depth insights into the flow trajectory of data during program execution. In order to extract function-level data flow information from the abstract syntax tree, it is necessary to deeply study the relationship between the nodes of the AST and the edges of the DFG in order to accurately capture the details of the data flow inside the function.

[0083] When adding the data flow in DFG on the basis of AST, it is necessary to perform static analysis on the program to obtain the use-def chain of the function. The specific steps are as follows:

[0084] 1. First, traverse the AST to collect all variable definitions and usage information. This usually requires identifying assignment statements and expressions that use variables. Assignment statements usually define the value of the variable, and these variable expressions are usually the use of the variable. This stage is the basis for building an accurate use-def chain because it is directly related to whether the subsequent steps can accurately identify the dependencies between variables.

[0085] 2. Establish a link between the definition and usage information of the variable: For each variable definition, record which statements or expressions use this definition. Similarly, for each use of the variable, record which definition this use corresponds to. This can be achieved by maintaining a mapping or association list to associate each definition with the use.

[0086] 3. Based on the collected definition and usage information of the variables, build use-def chains: each chain starts from a use of the variable, traces back to the definition corresponding to the use, and then continues to trace back to other uses of the definition until the entire abstract syntax tree is traversed. By removing redundant chains or merging duplicate chains, we finally get a chain linking all related uses and definitions.

[0087] By constructing use-def chains, the definition and subsequent usage of each variable can be clearly traced. These chain relationships not only reveal the dependencies between variables, but also provide key path information for analyzing data flow. By traversing these chains, the propagation path and flow direction of data in the program can be determined. Therefore, identifying the use-def chains of variables on the AST can form data flow edges in the DFG, and adding these edges to the AST increases the data flow of the AST.

[0088] At this time, the graph structure of DFG obtained by AST is G CFG =<V AST ,E DFG >, where V AST Represents a collection of AST nodes, E DFG Represents the set of data flow edges connecting two AST nodes, E DFG ={<v,w>|v,w∈P DFG (v,w)},P DFG (v,w) indicates that there is a direct data flow edge from node v to node w.

[0089] Algorithm 3 describes the main process of adding data flow edges on the AST. First, each node of the AST is traversed. By checking the node type, the node representing the variable definition is identified. For each identified definition node, all nodes that use the variable are further found. Once the definition point and the use point are identified, the data flow edge is established between these two types of points. The algorithm also considers the definition and use of variables within the function scope. By recursively processing the nodes in the function body, it ensures accurate analysis of nested scopes and local variable flows.

[0090]

[0091] By analyzing the CFG and DFG of the function, the control flow and data flow of the function are obtained. In order to integrate the syntax and semantic features of different programming languages, the control flow and data flow are combined with the standardized AST, and data flow edges and control flow edges are added to the AST to form a semantically enhanced AST with syntax and semantic features, and the graph structure G is used as the intermediate representation of the function.

[0092] The resulting graph G consists of two tuples G=<V AST,E>, the node V AST Represents the set of nodes of the AST generated by the function, E = E AST ∪E CFG ∪ DFG Represents the set of AST, CFG, and DFG edges generated by a function.

[0093] Figure 5 and Figure 6 The graph representation after data flow and control flow expansion is added to the standardized AST of Java function and Python function respectively. The edges marked in red represent the edges of AST and CFG, the green edges represent the edges of DFG, and the blue edges represent the edges of CFG. For the data flow in the function, that is, the edges of DFG, it can be seen in the function that the definition of the variable num is located in the function definition, and the use of num is located in the final return statement, so the data flow should start from function_declaration and flow to return_statement. Since there is no related control transfer statement in the function, the control flow in the function is the order of function execution, and the control flow edges in the AST are connected according to the grammatical order in which the function AST nodes appear.

[0094] Step S104: design a function similarity analysis network, combine the graph convolution network and the attention mechanism to convert the semantically enhanced AST into a graph vector, concatenate the two graph vectors and input them into a fully connected neural network to determine the similarity of functions in different programming languages.

[0095] Semantic enhancement AST can merge the AST, control flow and data flow of a function into a graph structure, which contains the syntax, semantics and structural features of the function. Therefore, after converting the source code of different programming languages ​​into semantic enhancement AST, the similarity between two functions can be judged by calculating graph similarity. There are also many algorithms for calculating graph similarity, including graph edit distance and maximum common subgraph, but these methods have high computational costs and cannot calculate similarity well when the graph structure is more complex. In order to efficiently calculate the similarity between graphs, this embodiment designs a function similarity analysis network, which converts the graph into a vector representation by extracting graph features and uses a fully connected neural network to judge similarity. The vector representation combines the features of the graph level and the features of the node level, and the similarity between vectors is learned through a fully connected neural network, which reflects the similarity of the grammatical structure and semantic information between functions. Therefore, the AST with expanded data flow and control flow can be analyzed using the neural network to obtain the similarity of functions in different languages.

[0096] The function similarity analysis network consists of three parts, namely, graph convolutional network, attention mechanism and fully connected neural network. The overall processing flow of similarity judgment is as follows: Figure 7As shown in the figure, the input of the model is the graph structure of the two functions and their node features. First, the graph convolution network is used to convolve the initialized node vector to obtain the vector representation of each node after the update; then the attention mechanism is used to fuse the node vector and the weight relationship between the nodes to obtain the overall graph vector, which is the function vector. Finally, the two vectors are concatenated and input into the fully connected neural network, and the similarity between the two vectors is output.

[0097] (1) Function embedding based on the combination of graph convolutional network and attention mechanism.

[0098] When embedding a function, an abstract syntax tree structure G=<V is first generated, which extends the function data flow and control flow. AST ,E>. In this structure, each node contains the grammatical information of the function, and each edge carries the grammatical structure or semantic information of the function. In order to fully combine this information for code representation, the graph is input into the graph convolutional neural network and the attention mechanism is added to achieve the embedding of the AST graph after the extended control flow and data flow, thereby comprehensively modeling and representing the function information.

[0099] Graph embedding mainly consists of two stages: node embedding and graph vector generation. In the node embedding stage, GCN is used to aggregate the node features of the graph and update the node feature vector. In the graph vector generation stage, the attention mechanism is used to aggregate the node feature vectors to generate the graph vector according to the different importance of different nodes in similarity judgment.

[0100] In the node embedding stage, in order to extract the node features in the graph, all the nodes in the graph need to be encoded first. According to the previous consistency processing of nodes in abstract syntax trees of different programming languages, the same node types have the same name representation. Therefore, one-hot encoding is used to initialize nodes with different names in the AST to generate node initialization vectors. In one-hot encoding, each different category is represented as a unique binary vector in which only one element is 1 and the rest are 0. That is, for a node feature space X={x1,x2,x3,...,x n}, where x i Represents the name of the i-th node, and the corresponding one-hot vector is represented by x i =[0,0,...,1,...,0], where the vector x i The length of is n, the i-th position is 1, and the rest of the positions are 0. Through one-hot encoding, the original discrete node feature space is mapped to an n-bit binary feature space, so that the model can effectively model and learn discrete features.

[0101] After encoding all nodes in the semantically enhanced AST using one-hot, the initialization vector matrix of the node is obtained as follows: Where N represents the number of nodes in the graph, F represents the dimension of the feature vector of each node, In addition, GCN also needs to input the adjacency matrix and degree matrix of the graph, where the adjacency matrix and degree matrix of the graph G are represented by A and D respectively, and A, D ∈ R N*N .

[0102] Then, the graph convolutional network (GCN) is used to update the feature vector of the node. GCN performs message passing, aggregation, and update operations through neighbor aggregation. GCN uses the adjacency relationship of the graph to learn the representation vector of each node, and generates a new node feature representation by convolving the node feature with the features of its neighbor nodes. In this way, while retaining the characteristics of the node itself, it can take into account the feature relationship with all neighbor nodes to achieve embedded representation learning of the node.

[0103] Figure 8 The following figure shows the contents of the AST after the control flow and data flow expansion of the statement "boolisEven(intnum)" in the C language function. If the statement is convolved in two layers, the yellow node is convolved twice. In the first convolution, the yellow "function_declaration" node will aggregate the information of its direct neighbor nodes, which can extract the direct relationship between code statements. Figure 8 The blue node in the middle is the direct neighbor of the node, that is, it extracts the feature information from the "function_declaration", "identifier", and "parameter" nodes to update its own node. In the second layer of convolution, it can indirectly aggregate the second-order neighbor information of the statement, that is Figure 8 The green nodes in ("unit", "parameter"). Therefore, in the process of using GCN convolution, the yellow "function_declaration" node learns the local structure and semantic features of its neighboring nodes. Figure 8 The right side of is the adjacency matrix of the AST. It is worth noting that An identity matrix is ​​added to its adjacency matrix, indicating that nodes can also learn their own features.

[0104] Before using GCN for convolution, in order to improve the stability of the GCN model and eliminate the influence of node degree, so as to better learn and represent the complex relationships in the graph, the adjacency matrix of the graph needs to be normalized to obtain the normalized adjacency matrix processing formula as shown in formula (1).

[0105]

[0106] Then, in order to realize the transmission and aggregation of information in the graph structure and obtain a more global and richer node representation, the node features are propagated. The propagation process can be expressed as formula (2).

[0107]

[0108] Among them, H (l+1) represents the feature representation of node i in layer l+1, H (l) represents the features of all neighbor nodes and itself of node i in layer l, W (l) represents the trainable weight matrix of the lth layer, and σ represents the activation function (such as ReLU). Therefore, the overall operation formula for node update using GCN is formula (3).

[0109]

[0110] in, The sum of the adjacency matrix A of the graph and the identity matrix is ​​calculated as formula (4), which means that the information of the node itself is also considered during the node update process.

[0111] In the process of node feature propagation using GCN, each AST node fuses the feature information of the current node and its neighboring nodes, and gradually fuses the local features of the graph in the process of convolution at different layers, and finally obtains the updated node feature vector set

[0112] However, after the GCN convolution, what is obtained is a set of feature vectors of the nodes, and no vector is generated for the entire graph. In order to obtain a vector that incorporates the overall information of the function, the entire graph needs to be embedded. In order to obtain a vector representation of the entire graph, the usual method is to average the embedding of each node, or to determine the node weight value based on the degree of each node, and obtain the weight of the entire graph by weighted summation. However, in the semantic AST graph of the function, it is unreliable to infer the weight of the node only by the degree of the node, because in the function similarity judgment, some nodes may contain more function feature information, which is more critical for similarity judgment, but they do not have the highest degree in the graph.

[0113] As in Figure 5In the example, the information in the return_statement node is the key to distinguishing the semantics of the function. However, as a key node for judging function similarity, the degree of this node in the graph is not much different from that of some uncritical nodes. Therefore, which nodes are more important and get more weight depends on a specific similarity metric. Therefore, the following attention mechanism is proposed, which enables the model to learn node weights under the guidance of a specific similarity metric, thereby obtaining the vector of the entire graph.

[0114] According to the node feature vector previously updated by GCN First, the vector values ​​of all nodes are averaged and a nonlinear transformation is used to obtain the global feature c of the graph, c∈R F , the calculation formula of c is shown in formula (5), where W2∈R F×F is a learnable weight matrix. The global feature c of the graph provides the global structure and feature information of the graph, and can adapt to the given similarity metric by learning the weight matrix. Based on c, the attention weight of each node can be calculated.

[0115]

[0116] When obtaining the global feature, if it is found that the feature of a certain node is similar to the global feature, it means that the node feature has a higher attention coefficient for the calculation of the global feature. For a certain node n, in order to make its attention aware of the global feature, the inner product calculation between the global feature c of the graph and the corresponding node embedding is used to obtain the attention coefficient a of the node n In order to ensure that the weight of attention is between 0 and 1, the sigmoid activation function is used to process the attention. The activation function calculation formula is shown in formula (6).

[0117]

[0118] When calculating graph similarity, the weights are not normalized to 1. This preserves the relative size information of the weights and takes into account the size of the graph, which is critical for subsequent graph similarity calculations. Because the size of the weights often reflects the importance of nodes or edges in the graph structure, it is a key element in graph feature extraction and representation. By maintaining this relative size information, the structure and characteristics of the graph can be more accurately characterized, thus providing strong support for the calculation of graph similarity. The final graph embedding g∈R F It is calculated by the weighted sum of the embeddings of each node, and the calculation formula is formula (7).

[0119]

[0120] Therefore, the final calculation formula of the node attention mechanism is formula (8).

[0121]

[0122] Where f2(·) is the sigmoid function σ(·); the overall graph vector g is obtained through this formula, which integrates the node information and the global graph representation information, and can take into account the importance of different node features to the similarity of the overall graph features.

[0123] Using graph convolutional networks, the nodes of the semantically enhanced AST are convolved, and the feature vectors of the nodes are obtained by aggregating the features of neighboring nodes. Then, the attention mechanism is used to weight the feature vectors of the nodes according to the importance of the node features for the graph similarity judgment, and the vector representation of the semantically enhanced AST is obtained. Therefore, the vector contains the grammatical semantics and structural information of the function, so the vector can be regarded as the feature vector of the function. And because the generation process of the semantically enhanced AST is independent of the programming language, the vector is independent of the programming language, and the similarity between functions of different programming languages ​​can be judged by the similarity between vectors.

[0124] (2) Similarity analysis based on fully connected neural network.

[0125] After obtaining the vectors generated by functions of different programming languages, a fully connected neural network is also applied to determine the similarity. First, the feature vectors g1 and g2 of two different programming languages ​​are concatenated to form a joint feature vector g0, which is then input into a two-layer fully connected neural network to determine the similarity between the vectors.

[0126] As a classic deep learning model, the fully connected neural network has a structure including input layer, hidden layer and output layer, and each layer is connected by weights. In the input layer, the concatenated feature vector is used as the input of the neural network. As the data is forward propagated in the neural network, the hidden layer performs nonlinear transformation on the input features through a combination of activation function and weight parameters, and continuously updates the weight parameters in the network through the back propagation algorithm. In this process, the model parameters are optimized to minimize the loss function, thereby improving the model's ability to predict function similarity. In the output layer, the sigmoid activation function is used to map the input to a continuous real number between 0 and 1, thereby obtaining the final similarity score. The similarity prediction formula between two feature vectors is formula (9), where "||" represents the concatenation of two vectors.

[0127]

[0128] The pre-set similarity threshold is 0.5, which can be used to determine whether two functions are similar. If the similarity is higher than the threshold, the two functions are considered to be similar. If the value is lower than the threshold, they are considered to be dissimilar.

[0129] During the training process, the model loss function uses the cross entropy loss function, which is calculated as formula (10), where Represents the similarity score between the i-th pair of vectors predicted by the model, y i Represents the label of the i-th pair of codes. If the vectors corresponding to the code pair are similar, the label value is 1, and if they are not similar, the label value is 0.

[0130]

[0131] Therefore, by concatenating the function feature vectors of different programming languages ​​and inputting the new vectors generated after concatenation into a fully connected neural network, similarity prediction can be performed. This method improves the model's ability to understand the similarity between generated semantic vectors, can better analyze the differences between different vectors, and provides strong support for similarity judgment of cross-programming language functions.

[0132] The process of similarity judgment using fully connected neural network is as follows: Fig. 9 As shown. The two vectors are concatenated and input into the input layer of the fully connected neural network. After the hidden layer learns the complex relationship between the vectors, the Sigmoid function is used to map the output to a real number between 0 and 1, and compared with the set similarity threshold. If it is greater than the threshold, the function pair is judged to be similar, and if it is less than the threshold, the function pair is judged to be dissimilar.

[0133] Corresponding to the above method, this embodiment also proposes a source code similarity analysis system based on semantically enhanced AST, comprising:

[0134] The AST generation module is used to generate corresponding abstract syntax trees AST for two functions using different programming languages.

[0135] The node standardization module is used to use the node vocabulary to make nodes of the same type but different names in ASTs of different programming languages ​​have the same representation, thereby shielding the differences in node representations of ASTs of different programming languages.

[0136] The semantic enhancement module is used to expand the control flow edges and data flow edges of the AST after node standardization processing to generate a semantically enhanced AST, which integrates the syntax, structure and semantic information of the function.

[0137] The similarity judgment module is used to design a function similarity analysis network. It combines the graph convolutional network and the attention mechanism to convert the semantically enhanced AST into a graph vector. The two graph vectors are concatenated and input into a fully connected neural network to judge the similarity of functions in different programming languages.

[0138] The effectiveness and superiority of the present invention are further verified by experiments below.

[0139] The dataset is divided into training set, test set and validation set in a ratio of 6:2:2. In order to prove the correctness and effectiveness of this method, experiments are conducted on the effect of clone detection, the effect of model selection and the generalization effect of the method. In the experiment, this method is called "enAST".

[0140] (1) Clone detection effect

[0141] In order to evaluate the effect of enAST in cross-language clone detection, it is compared with three advanced cross-language clone detection methods: CLCDSA, C4, and UAST on multiple evaluation indicators. Fig.10 shown.

[0142] Compared with other methods, enAST has achieved the best experimental results in all evaluation indicators, with a precision of 0.95, a recall of 0.97, and an F1 score of 0.96. This is because this method captures program syntax and semantic information by extending the control flow and data flow on AST, and uses GCN and attention mechanisms to merge the information of the node's n-hop neighborhood through message passing to learn node representations, and aggregates these node information into a vector representation of the graph according to weights. This enables it to fully learn the grammatical and semantic information of the function, and uses fully connected neural networks to better learn the similarity relationship between vectors.

[0143] In addition, experiments were conducted on the effect of enAST on single-language code similarity analysis to prove the effectiveness of the method in clone detection. In order to detect the similarity between Java codes, the BigCloneBench dataset was selected. This dataset is a common dataset for detecting Java code clones. It is mined from the IJaDataset and manually confirmed by experts, and contains a variety of different clone types. For clone detection of Python code, 100,000 pairs of similar functions and 100,000 pairs of dissimilar functions were selected from the previous Python dataset. In addition, in order to evaluate the effect of enAST in single-language similarity detection, the CodeBert model was added to the experiments on Java and Python. The CodeBert model is a model pre-trained using programming languages. It converts code into vector representation, and the vector also achieves good results in clone detection. For Java clone detection, the TreeCen method was additionally added for comparison. This method is a similarity detection method specifically for Java code. The experimental results of enAST on the Java dataset are shown in Table 2, and the experimental results on the Python dataset are shown in Table 3.

[0144] Table 2 Effect of Java code clone detection

[0145]

[0146] Table 3 Effect of Python code clone detection

[0147]

[0148] As shown in Tables 2 and 3, enAST also achieved good results in clone detection on Java and Python. When performing Java code clone detection, TreeCen achieved the best results, because TreeCen is a method specifically for clone detection in Java. However, enAST can also achieve a precision of 0.93, a recall of 0.92, and an F1 score of 0.92. When performing Python code clone detection, enAST achieved the best results compared to other methods, with a precision of 0.90, a recall of 0.93, and an F1 score of 0.91. Therefore, enAST also achieved good results in single-language clone detection.

[0149] (2) Effect of model design

[0150] The graph structure was obtained by standardizing the AST and analyzing the control flow and data flow to expand its semantic information. In the subsequent process of generating graph vectors, GCN was used for convolution, and the attention mechanism was used to generate graph vectors using the features of the nodes. Finally, the fully connected network was used to judge the similarity. In order to judge the effect of each step on similarity detection, ablation experiments were designed to judge the effect of standardizing the abstract syntax tree, adding the control flow and data flow, adding the attention mechanism, and using the fully connected neural network to prove the effectiveness of the model design. In addition, the optimal number of convolution layers of GCN was also judged to obtain the optimal number of convolutions suitable for the structure.

[0151] In order to shield the node differences between different languages, a node vocabulary is used to unify the node representation of AST in different languages, and the standardized AST is expanded with data flow control flow to increase its semantic information. In addition, the attention mechanism is an important step in the process of graph vector generation, which can assign node weights according to the importance of the node to the graph. If attention is not added, the generation of graph vectors is carried out by averaging the node vectors. In addition, the fully connected neural network can better learn the relationship between vectors and better judge the similarity relationship between them than cosine similarity. Therefore, an ablation experiment was designed to verify the effectiveness of the node vocabulary, the effect of adding control flow data flow, the effect of adding attention mechanism, and the effectiveness of the fully connected neural network. The results are shown in Table 4.4.

[0152] Table 4 Ablation experiment

[0153]

[0154] It can be found from Table 4 that the control flow data flow has the greatest impact on the similarity results. After adding the control flow data flow, the precision, recall and F1 scores increased by 0.21, 0.20 and 0.21 respectively, which shows that semantic information plays a very important role in similarity judgment. The second is the impact of adding the attention mechanism. The indicators increased by 0.10, 0.18 and 0.14 respectively. This shows that the attention mechanism effectively learns the importance of each node for similarity judgment and improves the effect of the model. The impact of adding the node vocabulary is equivalent to that of the attention mechanism. The indicators increased by 0.13, 0.14 and 0.14 respectively. This shows that eliminating the node differences between different programming languages ​​can also improve the effect of cross-language similarity detection. The next is the impact of using a fully connected neural network. The indicators increased by 0.06, 0.07 and 0.07 respectively. It shows that the fully connected neural network can better learn the language similarity relationship between vectors than the cosine similarity, thus achieving better results.

[0155] In order to obtain the best number of convolutions for GCN in similarity judgment, experiments were conducted with different numbers of convolutions, and the effects of similarity results on precision, recall, and F1 scores were obtained. The experimental results are shown in Figure 2. Fig.11 shown.

[0156] Fig.11 The figure shows the effect of different convolution layers on the method effect. Because the number of convolution layers of GCN should not be too many, a maximum of four convolutions were performed in the experiment. It can be seen that when the number of convolution layers is 2, the model achieves the best effect. At this time, the precision, recall rate and F1 score are 0.95, 0.97 and 0.96 respectively. However, when the number of convolution layers continues to increase, the indicators begin to decline again. Therefore, the second convolution achieves the best effect, and two convolutions should be performed when generating node feature vectors.

[0157] Finally, it should be noted that the above-described embodiments are only specific implementations of the present invention, which are used to illustrate the technical solutions of the present invention, rather than to limit them. The protection scope of the present invention is not limited thereto. Although the present invention is described in detail with reference to the above-described embodiments, ordinary technicians in the field should understand that any technician familiar with the technical field can still modify the technical solutions recorded in the above-described embodiments within the technical scope disclosed by the present invention, or can easily think of changes, or make equivalent replacements for some of the technical features therein; and these modifications, changes or replacements do not make the essence of the corresponding technical solutions deviate from the spirit and scope of the technical solutions of the embodiments of the present invention, and should be included in the protection scope of the present invention. Therefore, the protection scope of the present invention shall be subject to the protection scope of the claims.

Claims

1. A source code similarity analysis method based on semantically enhanced AST, characterized in that: Include: Generate corresponding abstract syntax trees AST for two functions using different programming languages; The node vocabulary is used to make nodes of the same type but different names in different programming language ASTs have the same representation, shielding the differences in node representations of different programming language ASTs; The control flow edges and data flow edges of the AST after node standardization are expanded to generate a semantically enhanced AST, which integrates the syntax, structure and semantic information of the function; A function similarity analysis network is designed, and the semantically enhanced AST is converted into a graph vector by combining the graph convolutional network and the attention mechanism. The two graph vectors are concatenated and input into a fully connected neural network to determine the similarity of functions in different programming languages.

2. The source code similarity analysis method based on semantically enhanced AST according to claim 1, characterized in that: The process of generating a node vocabulary is: First, we counted all the node names in the abstract syntax trees of a large number of different programming languages ​​and classified them according to the node types to obtain all the node names and types of the AST. Then, a unified name is specified for nodes of the same type, a node vocabulary of the abstract syntax tree is generated, and a correspondence between the node names of the abstract syntax trees of different programming languages ​​and the node vocabulary names is established; the rules for formulating names in the node vocabulary need to ensure that the unified names in the vocabulary do not conflict with the original names of different node types in the abstract syntax tree.

3. The source code similarity analysis method based on semantically enhanced AST according to claim 2 is characterized in that: Using the node vocabulary, nodes of the same type but different names in different programming language ASTs have the same representation, including: First, all node names in the AST are traversed. If the node name belongs to the node name in the node vocabulary, the name is mapped to the unified name in the table for replacement; if the node name does not belong to the node name in the node vocabulary, it is replaced with the "unk" character, so that all node names of the same type in the abstract syntax trees of functions of different programming languages ​​have the same name.

4. The source code similarity analysis method based on semantically enhanced AST according to claim 1 is characterized in that: The AST extended control flow edges after node standardization include: Each node of AST represents a statement in the source code, and the function statement type is identified based on the AST node name; In AST, find the code statements with the same parent node according to the statement type; Based on the position of the node in the AST, connect the above parent nodes and child nodes in the order they appear in the source code; When the node content is a transfer control statement, the relationship between statements is updated according to the transfer control statement semantics, and finally all control flow edges added in the AST are obtained.

5. The source code similarity analysis method based on semantically enhanced AST according to claim 1 is characterized in that: The AST extended data flow edges after node standardization include: First, traverse the AST to collect the definition and usage information of all variables; Establish links between variable definitions and usage information, associating each definition with a usage; Construct use-def chains: Each chain starts from a use of the variable, traces back to the definition corresponding to the use, and then continues to trace back to other uses of the definition until the entire abstract syntax tree is traversed. By removing redundant chains or merging duplicate chains, a chain linking all related uses and definitions is finally obtained.

6. The source code similarity analysis method based on semantically enhanced AST according to claim 1 is characterized in that: Combining graph convolutional networks with attention mechanisms, the semantically enhanced AST is converted into a graph vector, which is divided into a node embedding stage and a graph vector generation stage. In the node embedding stage, the graph convolutional network is used to update the node feature vector of the semantically enhanced AST through neighbor aggregation. In the graph vector generation stage, the updated node feature vectors are weighted using the attention mechanism according to the different importance of different nodes in similarity judgment to generate a graph vector.

7. The source code similarity analysis method based on semantically enhanced AST according to claim 6 is characterized in that: In the node embedding stage, all nodes in the semantically enhanced AST are first encoded using one-hot, and the node initialization vector matrix is ​​obtained as Where N represents the number of nodes in the graph, F represents the dimension of the feature vector of each node, Represents the initialization vector of the node in the graph; then the graph convolution network GCN is used to update the feature vector of the node. In the process of node feature propagation using GCN, each AST node fuses the feature information of the current node and its neighboring nodes, and gradually fuses the local features of the graph in the process of convolution with different layers, and finally obtains the updated node feature vector set 8. The source code similarity analysis method based on semantically enhanced AST according to claim 7 is characterized in that: In the graph vector generation stage, according to the node feature vector set updated by GCN First, the vector values ​​of all nodes are averaged and a nonlinear transformation is used to obtain the global feature c of the graph, c∈R F ; Then the graph vector g is obtained by the following formula: Where f2(·) is the sigmoid function and T is the transpose of the matrix.

9. The source code similarity analysis method based on semantically enhanced AST according to claim 1, characterized in that: The two graph vectors are concatenated and input into the input layer of the fully connected neural network. After the hidden layer learns the complex relationship between the vectors, the sigmoid function is used to map the output to a real number between 0 and 1 and compare it with the set similarity threshold. If it is greater than the threshold, the function pair is judged to be similar; if it is less than the threshold, the function pair is judged to be dissimilar.

10. A source code similarity analysis system based on semantically enhanced AST, characterized in that: Include: AST generation module, used to generate corresponding abstract syntax trees AST for two functions using different programming languages; The node standardization module is used to use the node vocabulary to make nodes of the same type but different names in different programming language ASTs have the same representation, shielding the differences in node representations of different programming language ASTs; The semantic enhancement module is used to expand the control flow edges and data flow edges of the AST after node standardization to generate a semantically enhanced AST that integrates the syntax, structure, and semantic information of the function; The similarity judgment module is used to design a function similarity analysis network. It combines the graph convolutional network and the attention mechanism to convert the semantically enhanced AST into a graph vector. The two graph vectors are concatenated and input into a fully connected neural network to judge the similarity of functions in different programming languages.

Citation Information

Cited By

  • Code reuse recommendation method and system based on multi-language unified analysis and vectorization retrieval

    CN121255180A

  • Code reuse recommendation method and system based on multilingual unified parsing and vectorized retrieval

    CN121255180B

  • Cross-language compilation optimization method based on graph neural network

    CN121764480A

  • Cross-language compilation optimization method based on graph neural network

    CN121764480B

  • SQL (Structured Query Language) injection attack detection method and device, equipment and storage medium

    CN122333463A