Code processing method and device fusing grammar structure and graph structure learning

By converting AST into graph-structured data and using the Graph Attention Network (GAT) model to analyze code structure, the challenge of code feature extraction in cross-project and cross-language environments is solved, achieving efficient and accurate code analysis and feature extraction, and improving code understanding and optimization efficiency.

CN121116319APending Publication Date: 2025-12-12HUNAN UNIV
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202511308798.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-09-15
Publication Date
2025-12-12

AI Technical Summary

Technical Problem

Existing technologies struggle to efficiently and accurately extract and utilize code structure features in large-scale, cross-project, and cross-language environments, resulting in limited accuracy and poor adaptability in code analysis.

Method used

The Abstract Syntax Tree (AST) is converted into graph-structured data and combined with the Graph Attention Network (GAT) for code structure analysis. By collecting code from multiple high-performance projects, hot code segments are extracted, a GAT model is constructed, and the model is trained to predict code structure features in parallel.

Benefits of technology

It improves the automation and accuracy of code structure feature extraction, significantly enhances the accuracy of code feature extraction and classification, supports good adaptability to multiple languages ​​and projects, and reduces the cost of manual analysis.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121116319A_ABST
    Figure CN121116319A_ABST
Patent Text Reader

Abstract

The invention discloses a code processing method and device fusing grammar structure and graph structure learning, and the method comprises the steps: collecting high-performance project codes, extracting hot code segments, and analyzing the structural features of the hot code segments; converting the hot code segment into an abstract syntax tree AST file, performing structured analysis, converting the file into Python tree structure data, and expanding node attributes; converting the tree structure data into graph structure data, generating edge, graph and node index files, and constructing the graph structure data and corresponding code structure labels; constructing a graph attention network model for different code structures, and training a plurality of GAT models based on graph structure data and labels; and converting the user input code, inputting the converted code into each GAT model for prediction, and integrating and outputting a feature vector. According to the method, grammar logic is accurately captured through AST conversion and node expansion, multi-language AST is unified into a graph structure, the recognition accuracy of a complex code structure is improved through a GAT multi-model architecture, full-process automation is achieved, and the labor cost is reduced.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the field of computer technology, and in particular relates to a code processing method and apparatus that integrates syntactic structure and graph structure learning. Background Technology

[0002] With the rapid development of software engineering and artificial intelligence technologies, the amount of program code is growing exponentially. Projects that previously took a long time to complete can now be finished quickly using AI technologies. However, this also brings a problem: a large number of AI-generated code projects urgently need further modification and improvement. To improve the efficiency of code management, understanding, and optimization, more and more research is dedicated to analyzing source code through automated programs.

[0003] Traditional code analysis methods mainly rely on rule-based static analysis tools or simple text mining techniques. These methods often make it difficult for users to accurately capture the deep syntactic structures and semantic features between codes, resulting in limited accuracy of code analysis. Furthermore, different analysis methods are required for code obtained from different programming languages, making them poorly adaptable.

[0004] Abstract Syntax Trees (ASTs) are an important way to represent program structure. They can accurately represent the syntactic relationships between source code in a hierarchical form and are widely used in various fields such as compilers, program analysis, and code optimization. However, directly extracting and analyzing code features based on ASTs often makes it difficult to fully explore the complex contextual relationships between nodes, and the information obtained directly from the AST is too obscure, requiring further human analysis to obtain specific code features.

[0005] In recent years, the rise of GAT (Graph Attention Network) has provided new ideas for modeling and learning structured data. As a branch of graph neural networks, GAT can effectively capture the dependencies between nodes and their neighbors, and retain rich structural information during the encoding process. It has been widely used in social network analysis, recommender systems and molecular modeling.

[0006] While some existing research has explored the use of GAT for code modeling, challenges remain in efficiently and accurately extracting and utilizing code structural features in large-scale, cross-project, and cross-language environments. These challenges include complex feature engineering, weak model generalization ability, and insufficient understanding of code context. AST, as a tree representation and a type of graph, offers a viable new approach to code structure analysis by converting AST to graph-structured data and combining it with GAT. This approach can improve the automation level and accuracy of code structural feature extraction.

[0007] Based on this, the present invention proposes a code processing method and apparatus that integrates grammatical structure and graph structure learning. Summary of the Invention

[0008] To address the above technical problems, this invention provides a code processing method and apparatus that integrates syntactic structure and graph structure learning. Its purpose is to improve the automation and accuracy of code structure feature extraction, solve the problems of insufficient understanding of code structure and low feature extraction efficiency in existing methods, improve the accuracy of large models in analyzing code, or assist developers in quickly understanding and optimizing code structure.

[0009] The technical solution adopted by this invention to solve its technical problem is:

[0010] A code processing method that integrates syntactic structure and graph structure learning includes the following steps:

[0011] S100: Collects code from multiple high-performance projects, reads all the code from different projects one by one, extracts hot code segments and analyzes the structural characteristics of hot code segments;

[0012] S200: Converts each hot code segment into an Abstract Syntax Tree (AST) file;

[0013] S300: Performs structured analysis on AST files, utilizes syntax tree file format rules, designs algorithms to convert them into tree structure data described in Python, and expands node attributes;

[0014] S400: Converts tree-structured data into graph-structured data, generates edge index files, graph index files, and node index files, and constructs graph-structured data and corresponding code structure tags based on the three index files;

[0015] S500: For different code structures, construct graph attention network (GAT) models respectively, and train multiple GAT models based on graph structure data and code structure labels;

[0016] S600: Obtain user input code, convert it into graph structure data according to S100-S400, input it into each GAT model in S500 to predict code structure features, and finally integrate the output results of each model into a feature vector output.

[0017] Preferably, S100 includes:

[0018] S110: Collect high-performance project code from the code platform;

[0019] S120: For Git projects, hot files are identified by counting file change frequency using Git commands; for non-Git projects, hot files are identified based on function body size and call depth.

[0020] S130: Extract the main function, class definition, frequently called functions, and code segments with keyword comments from hot files, and identify the programming language based on the file extension;

[0021] S140: Code structure features of hotspot code are marked through large model assistance and manual inspection. The code structure includes at least one of loop unrolling, nested conditions, recursive calls, and dead code.

[0022] Preferably, S200 includes:

[0023] S210: Use Clang tools to generate .ast files from C / C++ code;

[0024] S210: Use the ast module to generate AST objects from Python code and save them as .pkl files using the pickle module.

[0025] Preferably, S300 includes:

[0026] S310: Call the differential parsing algorithm according to the AST file format: For C / C++ files in .ast format, parse them into tree-structured data objects according to the hierarchical identification rules; for Python files in .pkl format, directly generate tree-structured data objects through deserialization;

[0027] S320: Add basic attributes to all nodes, including the AST node type and the corresponding AST data type of the node;

[0028] S330: Expand node attributes through a unified traversal algorithm, including at least one of the following attributes: height of the newly added node, number of child nodes, AST type of the parent node, and number of nodes in the current layer.

[0029] Preferably, S400 includes:

[0030] S410: Recursively traverse the tree structure object to generate the edge index file edge_index.txt. Each line records the relationship between nodes in the format [graph number, tail node, head node].

[0031] S420: Synchronously generate graph index file graph_index.txt and node index file node_index.txt. Each line of the graph index file records graph-level features in the format [graph number, code structure vector]; each line of the node index file records node features in the format [graph number, node number, node attribute vector].

[0032] S430: Based on the built-in functions in the PyG module, read the above three index files and construct graph structure data and corresponding code structure data tags.

[0033] Preferably, S500 includes:

[0034] S510: Divide the graph structure data and code structure data labels into training and test sets according to a certain ratio;

[0035] S520: First, construct several basic GAT networks with the same parameters and structure, and perform supervised training. One GAT model is trained for each code structure. During the training process, the optimal parameters are found through hyperparameter search, and the model structure is adjusted based on the type of code structure on the basis of the basic GAT network. Finally, several different GAT models are obtained through training.

[0036] Preferably, S600 includes:

[0037] S610: The user input code is processed by S100 to S400 to generate graph structure data, which is then input into multiple trained GAT models in S500 for parallel prediction. Each model outputs a different code structure result.

[0038] S620: The code structure results output by each model are concatenated into a one-dimensional feature vector in a preset order, which serves as the representation result of the code structure.

[0039] A code processing device that integrates grammatical structure and graph structure learning includes:

[0040] The code collection module is used to collect code from multiple high-performance projects, read all the code from different projects one by one, extract hot code segments, and analyze the structural characteristics of hot code.

[0041] The AST conversion module is used to convert each hot code segment into an Abstract Syntax Tree (AST) file.

[0042] The tree structure extension module is used to perform structured analysis on AST files. It uses syntax tree file format rules to design algorithms to convert them into tree structure data described in Python language and extends node attributes.

[0043] The graph structure conversion module is used to convert tree structure data into graph structure data, generate edge index file, graph index file and node index file, and construct graph structure data and corresponding code structure tags based on the three index files;

[0044] The multi-model training module is used to build graph attention network (GAT) models for different code structures, and train multiple GAT models based on graph structure data and code structure labels.

[0045] The feature integration output module is used to acquire user input codes, convert them into graph structure data according to S100-S400, input each GAT model in S500 to predict code structure features, and finally integrate the output results of each model into a feature vector output.

[0046] A computer device includes a memory and a processor, the memory storing a computer program, and the processor executing the computer program implementing a code processing method that integrates syntactic structure and graph structure learning.

[0047] A computer-readable storage medium having a computer program stored thereon, the computer program being executed by a processor to implement the steps of a code processing method that integrates syntactic structure and graph structure learning.

[0048] The aforementioned code processing method and apparatus, which integrates syntactic structure and graph structure learning, accurately captures the syntactic logic features of code through the structured transformation of Abstract Syntax Trees (ASTs) and dynamic expansion of node attributes, overcoming the problem that traditional text analysis cannot identify nested structures. It differentiates the parsing of C / C++ (.ast) and Python (.pkl) AST files and uniformly converts them into graph structure data, solving the adaptability problem of multi-language code analysis. Based on the multi-model parallel architecture of the Graph Attention Network (GAT), it utilizes a multi-head attention mechanism to dynamically learn node dependencies, significantly improving the accuracy of identifying complex code structures such as loop unrolling and conditional nesting. The fully automated processing from raw code to feature vectors greatly reduces the cost of manual analysis. Attached Figure Description

[0049] Figure 1 This is a flowchart of a code processing method that integrates syntactic structure and graph structure learning in one embodiment of the present invention. Detailed Implementation

[0050] To enable those skilled in the art to better understand the technical solution of the present invention, the present invention will be further described in detail below with reference to the accompanying drawings.

[0051] In one embodiment, such as Figure 1 As shown, a code processing method that integrates syntactic structure and graph structure learning includes the following steps:

[0052] S100: Collects code from multiple high-performance projects, reads all the code from different projects one by one, extracts hot code segments and analyzes the structural characteristics of hot code segments;

[0053] S200: Converts each hot code segment into an Abstract Syntax Tree (AST) file;

[0054] S300: Performs structured analysis on AST files, utilizes syntax tree file format rules, designs algorithms to convert them into tree structure data described in Python, and expands node attributes;

[0055] S400: Converts tree-structured data into graph-structured data, generates edge index files, graph index files, and node index files, and constructs graph-structured data and corresponding code structure tags based on the three index files;

[0056] S500: For different code structures, construct graph attention network (GAT) models respectively, and train multiple GAT models based on graph structure data and code structure labels;

[0057] S600: Obtain user input code, convert it into graph structure data according to S100-S400, input it into each GAT model in S500 to predict code structure features, and finally integrate the output results of each model into a feature vector output.

[0058] Specifically, an Abstract Syntax Tree (AST) is a data structure commonly used in compilers, interpreters, and program analysis tools. Simply put, an AST is a structured representation of source code. It displays the syntactic structure of the code in a tree-like hierarchy while abstracting away some irrelevant details. Its core characteristic is that it doesn't rely on a character-by-character mapping but extracts the syntactic meaning of the code. Although it abstracts some details, it retains the key structure and logic of the source code. Represented as a tree structure, each node represents a syntactic component, and parent and child nodes express the inclusion and dependency relationships between components. This data structure is easily converted into graph-based data.

[0059] Graph Neural Networks (GNNs) are a class of neural network models specifically designed for processing graph data structures. Graphs are composed of nodes and edges, and GNNs aim to perform tasks such as node classification, edge prediction, and graph classification on this complex connection structure. In summary, GNNs allow "nodes" to gradually learn information about the entire graph through "neighbor interactions." The Graph Attention Network (GAT) used in this invention is an improved version of GNN. When aggregating neighbor features, it doesn't simply average or weight them, but dynamically learns the importance weights of neighbors through an attention mechanism. That is, different neighbors have different impacts on nodes, and GAT can learn which are more important, which contribute significantly, and which can be ignored, making it more flexible and accurate than GNNs. In practice, GAT often uses multiple attention heads, and the results are concatenated (or averaged). This results in a more stable model with stronger expressive power. In summary, GAT is equivalent to a graph plus an attention mechanism, allowing nodes to selectively learn features based on the importance of their neighbors.

[0060] In one embodiment, S100 includes:

[0061] S110: Collect high-performance project code from code platforms, including GitHub and other publicly available code platforms.

[0062] To extract hotspot code from high-performance projects, different methods are needed for different projects. First, the hotspot files need to be identified, and then the hotspot code portions need to be extracted from those files. Specifically:

[0063] S120: For Git projects, hot files are identified by counting file change frequency using Git commands, i.e., the files with the highest change frequency are identified as hot files; for non-Git projects, hot files are identified based on function body size and call depth, i.e., files with particularly large function body code segments and deep call depths are identified as hot files.

[0064] S130: Further analyze hot files, focusing on extracting the main function, class definition, frequently called functions, and code segments containing keyword comments in the hot files as the hot code parts of the project. For projects with ambiguous hot code, manual inspection is required to further determine or discard them. For different hot codes, the programming language is identified according to the suffix of the corresponding hot file (including .py, .cpp, and .c) to facilitate the selection of subsequent AST parsing tools.

[0065] S140: Code structure features marked with hotspots through large model assistance and manual inspection. The code structure includes at least one of the following: loop unrolling, nested conditions (if statements containing if, for, while, etc.), recursive calls (functions calling themselves), and dead code (code that will never be executed, such as code after a return statement).

[0066] Specifically, the process primarily involves examining the comments of hot-spot code using a large model, followed by manual inspection to determine the code structure of the hot-spot code. If there are code structures that are difficult to determine or are not obvious, data cleaning is required.

[0067] Furthermore, the obtained hotspot code determines the required parsing tool based on the corresponding language obtained in S130, and parses it into the corresponding abstract syntax tree file format.

[0068] In one embodiment, S200 includes:

[0069] S210: Use Clang tools to generate .ast files from C / C++ code;

[0070] S210: Use the built-in ast module to generate AST objects from Python code and save them as .pkl files using the built-in pickle module.

[0071] In one embodiment, S300 includes:

[0072] S310: Call the differential parsing algorithm according to the AST file format: For C / C++ files in .ast format, parse them into tree-structured data objects according to the hierarchical identification rules; for Python files in .pkl format, directly generate tree-structured data objects through deserialization.

[0073] Specifically, based on the AST file obtained from S200, a corresponding algorithm is selected. The algorithm is determined by the file annotations, with C / C++ code (.ast) and Python code (.pkl) corresponding to different parsing algorithms. The main content of each parsing algorithm is consistent: it transforms the corresponding file to be parsed into a tree-structured data object described in Python. The algorithm consists of two parts: one part constructs the basic tree-structured data object, and the other part further expands the attributes of the nodes in the tree-structured data object.

[0074] For the first part, the main logic of the algorithm for the C / C++ code is to write code according to the rules of the .ast file (mainly including three dots: double spaces indicate that the current node is a child node of the parent node, "|" indicates that the current node is one of the child nodes of the parent node, but not the rightmost node, and "'" indicates that the current node is the rightmost child node of the parent node) and convert it into a tree structure data object described in Python. For the Python code, since the file is directly converted using the pickle module, the "visualized" structure can be printed directly without further analysis; it can be directly converted into the corresponding tree structure data object described in Python.

[0075] S320: Add basic attributes to all nodes, including the AST node type and the corresponding AST data type of the node;

[0076] S330: Expand node attributes through a unified traversal algorithm, including at least one of the following attributes: height of the newly added node, number of child nodes, AST type of the parent node, and number of nodes in the current layer.

[0077] In one embodiment, S400 includes:

[0078] S410: Recursively traverse the tree structure object to generate an edge index file edge_index.txt. Each line records the relationships between nodes in the format [graph number, tail node, head node].

[0079] Specifically, the algorithm recursively traverses each node in the tree structure object, and the graph structure is represented by edge index using sparse graph representation. Each tree corresponds to a graph structure. The edges of all tree structure objects obtained by S300 are represented by edge_index.txt, where each line represents a corresponding edge in the form of [graph number corresponding to the edge, tail node, head node]. The tree structure data object described in Python language has been generated in S300, and the above file can be generated by traversing the nodes of all files once.

[0080] S420: Synchronously generate a graph index file graph_index.txt and a node index file node_index.txt. Each line of the graph index file records graph-level features in the format [graph number, code structure vector]; each line of the node index file records node features in the format [graph number, node number, node attribute vector].

[0081] Specifically, in addition to edge_index.txt, it is also necessary to represent the code structure corresponding to each graph and the attributes of each node to facilitate subsequent training. Therefore, two more files, graph_index.txt and node_index.txt, need to be generated. Each line in graph_index.txt represents the code structure of the hotspot code corresponding to one graph, in the form of [graph number, [vector of graph code structure]]. Each line in node_index.txt represents which graph a specific node belongs to, in the form of [graph number, node number, [vector of node attributes]]. The tree structure data object described in Python language has already been generated in S300, and the above files can be generated by traversing the nodes of all files once.

[0082] S430: Based on the built-in functions in the PyG (PyTorch Geometric) module, read the above three index files and construct graph structure data and corresponding code structure data labels.

[0083] Specifically, using the built-in functions in the PyG module, the three files obtained in S410 and S420 are read sequentially. edge_index.txt and node_index.txt are used to generate graph structure data that can be used for training (or user-input code features for model prediction), and graph_index.tx is used to generate the corresponding code structure data to facilitate subsequent training.

[0084] In one embodiment, S500 includes:

[0085] S510: Divide the graph structure data and code structure data labels into training set and test set according to a ratio; in this embodiment, the ratio is 9:1.

[0086] S520: First, construct several basic GAT networks with the same parameters and structure, and perform supervised training. One GAT model is trained for each code structure. During the training process, the optimal parameters are found through hyperparameter search, and the model structure is adjusted based on the type of code structure on the basis of the basic GAT network. Finally, several different GAT models are obtained through training.

[0087] In one embodiment, S600 includes:

[0088] S610: The user input code is processed by S100 to S400 to generate graph structure data, which is then input into multiple trained GAT models in S500 for parallel prediction. Each model outputs a different code structure result.

[0089] S620: The code structure results output by each model are concatenated into a one-dimensional feature vector in a preset order, which serves as the representation result of the code structure.

[0090] In one embodiment, a code processing apparatus that integrates syntactic structure and graph structure learning is also provided, comprising:

[0091] The code collection module is used to collect code from multiple high-performance projects, read all the code from different projects one by one, extract hot code segments, and analyze the structural characteristics of hot code.

[0092] The AST conversion module is used to convert each hot code segment into an Abstract Syntax Tree (AST) file.

[0093] The tree structure extension module is used to perform structured analysis on AST files. It uses syntax tree file format rules to design algorithms to convert them into tree structure data described in Python language and extends node attributes.

[0094] The graph structure conversion module is used to convert tree structure data into graph structure data, generate edge index file, graph index file and node index file, and construct graph structure data and corresponding code structure tags based on the three index files;

[0095] The multi-model training module is used to build graph attention network (GAT) models for different code structures, and train multiple GAT models based on graph structure data and code structure labels.

[0096] The feature integration output module is used to acquire user input codes, convert them into graph structure data according to S100-S400, input each GAT model in S500 to predict code structure features, and finally integrate the output results of each model into a feature vector output.

[0097] Specific limitations regarding the code processing device that integrates syntactic structure and graph structure learning can be found in the limitations of the code processing method that integrates syntactic structure and graph structure learning described above, and will not be repeated here. Each module in the aforementioned code processing device that integrates syntactic structure and graph structure learning can be implemented entirely or partially through software, hardware, or a combination thereof. These modules can be embedded in or independent of the processor in a computer device, or stored in the memory of a computer device in software form, so that the processor can call and execute the operations corresponding to each module.

[0098] In summary, compared with the prior art, the above-described technical solutions conceived by this invention can achieve the following beneficial effects:

[0099] (1) Since this invention uses S100 to S100, it collects high-performance projects of various programming languages ​​through public platforms such as GitHub, reads all the code of different projects one by one, and converts the hot code of each project into AST file. The AST isolates the differences in language, and finally converts it into tree structure data described in Python language. This eliminates the differences between different languages ​​in terms of data representation. This makes the invention have good scalability and adaptability, can be adapted to different programming languages, and supports the expansion of more code structure feature categories and neural network model structures as needed.

[0100] (2) Since the present invention uses S400 to S600, after converting tree structure data into graph structure data, it uses this as training data to build special prediction models for various code features and integrates the analysis results into a unified feature vector. This method supports multi-dimensional code feature modeling and helps to depict code features more meticulously.

[0101] (3) Since the present invention uses S100 to S600, the syntactic and structural information of the source code is completely preserved through AST, and the relationship between nodes is combined with GAT deep modeling, which significantly improves the accuracy of code feature extraction and classification.

[0102] By training multiple independent GATs on large-scale, cross-project, and cross-language datasets, the model maintains good analytical performance across different code projects and language environments. The entire process, from data preprocessing and feature extraction to feature integration and output, is highly automated, reducing manual intervention and the complexity of feature engineering.

[0103] A computer device includes a memory and a processor, the memory storing a computer program, and the processor executing the computer program implementing a code processing method that integrates syntactic structure and graph structure learning.

[0104] A computer-readable storage medium having a computer program stored thereon, the computer program being executed by a processor to implement the steps of a code processing method that integrates syntactic structure and graph structure learning.

[0105] Those skilled in the art will understand that all or part of the processes in the methods of the above embodiments can be implemented by a computer program instructing related hardware. The computer program can be stored in a non-volatile computer-readable storage medium, and when executed, it can include the processes of the embodiments of the methods described above. Any references to memory, storage, databases, or other media used in the embodiments provided in this application can include at least one of non-volatile and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, or optical storage, etc. Volatile memory can include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM), etc.

[0106] The foregoing has provided a detailed description of the code processing method and apparatus for integrating syntactic structure and graph structure learning provided by this invention. Specific examples have been used to illustrate the principles and implementation methods of this invention, and the descriptions of the embodiments above are merely for the purpose of helping to understand the core ideas of this invention. It should be noted that those skilled in the art can make various improvements and modifications to this invention without departing from its principles, and these improvements and modifications also fall within the protection scope of the claims of this invention.

Claims

1. A code processing method that integrates syntactic structure and graph structure learning, characterized in that, Includes the following steps: S100: Collects code from multiple high-performance projects, reads all the code from different projects one by one, extracts hot code segments and analyzes the structural characteristics of hot code segments; S200: Converts each hot code segment into an Abstract Syntax Tree (AST) file; S300: Performs structured analysis on AST files, utilizes syntax tree file format rules, designs algorithms to convert them into tree structure data described in Python, and expands node attributes; S400: Converts tree-structured data into graph-structured data, generates edge index files, graph index files, and node index files, and constructs graph-structured data and corresponding code structure tags based on the three index files; S500: For different code structures, construct graph attention network (GAT) models respectively, and train multiple GAT models based on graph structure data and code structure labels; S600: Obtain user input code, convert it into graph structure data according to S100-S400, input it into each GAT model in S500 to predict code structure features, and finally integrate the output results of each model into a feature vector output.

2. The method according to claim 1, characterized in that, S100 includes: S110: Collect high-performance project code from the code platform; S120: For Git projects, hot files are identified by counting file change frequency using Git commands; for non-Git projects, hot files are identified based on function body size and call depth. S130: Extract the main function, class definition, frequently called functions, and code segments with keyword comments from hot files, and identify the programming language based on the file extension; S140: Code structure features of hotspot code are marked through large model assistance and manual inspection. The code structure includes at least one of loop unrolling, nested conditions, recursive calls, and dead code.

3. The method according to claim 2, characterized in that, S200 includes: S210: Use Clang tools to generate .ast files from C / C++ code; S210: Use the ast module to generate AST objects from Python code and save them as .pkl files using the pickle module.

4. The method according to claim 3, characterized in that, The S300 includes: S310: Call the differential parsing algorithm according to the AST file format: For C / C++ files in .ast format, parse them into tree-structured data objects according to the hierarchical identification rules; for Python files in .pkl format, directly generate tree-structured data objects through deserialization; S320: Add basic attributes to all nodes, including the AST node type and the corresponding AST data type of the node; S330: Expand node attributes through a unified traversal algorithm, including at least one of the following attributes: height of the newly added node, number of child nodes, AST type of the parent node, and number of nodes in the current layer.

5. The method according to claim 4, characterized in that, The S400 includes: S410: Recursively traverse the tree structure object to generate the edge index file edge_index.txt. Each line records the relationship between nodes in the format [graph number, tail node, head node]. S420: Synchronously generate graph index file graph_index.txt and node index file node_index.txt. Each line of the graph index file records graph-level features in the format [graph number, code structure vector]; each line of the node index file records node features in the format [graph number, node number, node attribute vector]. S430: Based on the built-in functions in the PyG module, read the above three index files and construct graph structure data and corresponding code structure data tags.

6. The method according to claim 5, characterized in that, The S500 includes: S510: Divide the graph structure data and code structure data labels into training and test sets according to a certain ratio; S520: First, construct several basic GAT networks with the same parameters and structure, and perform supervised training. One GAT model is trained for each code structure. During the training process, the optimal parameters are found through hyperparameter search, and the model structure is adjusted based on the type of code structure on the basis of the basic GAT network. Finally, several different GAT models are obtained through training.

7. The method according to claim 6, characterized in that, The S600 includes: S610: The user input code is processed by S100 to S400 to generate graph structure data, which is then input into multiple trained GAT models in S500 for parallel prediction. Each model outputs a different code structure result. S620: The code structure results output by each model are concatenated into a one-dimensional feature vector in a preset order, which serves as the representation result of the code structure.

8. A code processing device that integrates grammatical structure and graph structure learning, characterized in that, include: The code collection module is used to collect code from multiple high-performance projects, read all the code from different projects one by one, extract hot code segments, and analyze the structural characteristics of hot code. The AST conversion module is used to convert each hot code segment into an Abstract Syntax Tree (AST) file. The tree structure extension module is used to perform structured analysis on AST files. It uses syntax tree file format rules to design algorithms to convert them into tree structure data described in Python language and extends node attributes. The graph structure conversion module is used to convert tree structure data into graph structure data, generate edge index file, graph index file and node index file, and construct graph structure data and corresponding code structure tags based on the three index files; The multi-model training module is used to build graph attention network (GAT) models for different code structures, and train multiple GAT models based on graph structure data and code structure labels. The feature integration output module is used to acquire user input codes, convert them into graph structure data according to S100-S400, input each GAT model in S500 to predict code structure features, and finally integrate the output results of each model into a feature vector output.

9. A computer device comprising a memory and a processor, wherein the memory stores a computer program, characterized in that, When the processor executes the computer program, it implements the steps of the method according to any one of claims 1 to 7.

10. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1 to 7.

Citation Information

Cited By

  • Code data management method based on artificial intelligence and related device

    CN122018956A