Code conversion method and device and electronic equipment

By building a code semantic network and performing preorder traversal, determining the dependencies and context information of code blocks is solved, and a more accurate and stable code conversion effect is achieved.

CN119806535AActive Publication Date: 2025-04-11CHINA AUTOMOTIVE INNOVATION CORP
View PDF 4 Cites 0 Cited by

Patent Information

Application Number
CN202411886058.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-20
Publication Date
2025-04-11
Estimated Expiration
2044-12-20

AI Technical Summary

Technical Problem

Prior art In cross-language code conversion, especially when preserving specific business logic or optimizing performance, the generated code may lack a deep understanding of the overall system, resulting in functional bias or performance degradation.

Method used

By building a code semantic network of code to be converted and performing pre-order traversal, the dependencies and context information of the code blocks to be converted are determined, ensuring that all code blocks it depends on when converting each code block has been converted into the target programming language.

Benefits of technology

It realizes the preservation of code dependencies and context information during cross-language code conversion, avoids functional missing or logical errors caused by dependency order errors, and improves the accuracy and stability of code conversion.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119806535A_ABST
    Figure CN119806535A_ABST
Patent Text Reader

Abstract

The invention discloses a code conversion method and device and electronic device.The code conversion method can be applied to the field of computer language compilation and comprises the steps that a to-be-converted code and a target programming language are obtained; constructing a code semantic network based on the elements in the to-be-converted code and the relationship among the elements; performing preorder traversal on the code semantic network, and determining a current to-be-converted code block in the to-be-converted code based on a current traversal element and a traversal path thereof; based on the code semantic network, expressing the current to-be-converted code block as a target programming language to obtain a target code corresponding to the current to-be-converted code block; when elements which are not traversed exist in the code semantic network, preorder traversal continues, the current to-be-converted code block is updated, a target code corresponding to the updated current to-be-converted code block is determined, and when elements which are not traversed do not exist in the code semantic network, preorder traversal is ended, and the target code corresponding to the to-be-converted code is obtained. According to the method, the code conversion accuracy is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer language compilation, and in particular to a code conversion method, device and electronic equipment. Background Art

[0002] With the rapid development of information technology, the types and number of programming languages ​​are increasing, and each language has its own unique characteristics and applicable scenarios. When enterprises upgrade, maintain and refactor systems, they often need to migrate existing code from one programming language to another. In this process, the demand for cross-language code conversion is growing. Traditional manual conversion methods are not only time-consuming and labor-intensive, but also prone to errors, and it is difficult to meet the needs of large-scale and efficient development. Although rule-based automation tools can improve efficiency to a certain extent, they still have limitations when dealing with complex code structures and specific business logic.

[0003] In recent years, with the rapid development of deep learning and natural language processing technologies, machine learning models have gradually become a new way to solve the problem of cross-language code conversion, such as LLM (Large Language Model). By learning a large amount of code data, the model can generate high-quality conversion suggestions and effectively handle complex contextual problems. The application of these models in automatic code conversion not only improves the accuracy and efficiency of conversion, but also reduces the cost of manual intervention.

[0004] However, despite the significant progress made in automatic code transformation by LLM, some challenges still exist. In particular, when it is necessary to retain specific business logic or optimize performance, the code generated by LLM often lacks a deep understanding of the overall system, which may lead to functional deviation or performance degradation, semantic errors or unstable transformation results. These problems limit the reliability and effectiveness of LLM in practical applications. Summary of the invention

[0005] In order to solve the problems of the prior art, the embodiments of the present application provide a code conversion method, device and electronic device. The technical solution is as follows:

[0006] In one aspect, a code conversion method is provided, the method comprising:

[0007] Get the code to be converted and the target programming language;

[0008] Constructing a code semantic network based on the code to be converted; the code semantic network indicates elements in the code to be converted and the relationship between the elements;

[0009] Performing a pre-order traversal on the code semantic network, and determining a current code block to be converted in the code to be converted based on a current traversal element and a traversal path corresponding to the current traversal element;

[0010] Based on the code semantic network, the current code block to be converted is expressed as the target programming language to obtain the target code corresponding to the current code block to be converted;

[0011] In the case that there are elements that have not been traversed in the code semantic network, the pre-order traversal is continued, the current code block to be converted is updated, and the target code corresponding to the updated current code block to be converted is determined, until there are no elements that have not been traversed in the code semantic network, the pre-order traversal is ended, and the target code corresponding to the code to be converted is obtained.

[0012] On the other hand, a code conversion device is provided, the device comprising:

[0013] An information acquisition module, used to acquire the code to be converted and the target programming language;

[0014] A network construction module, used to construct a code semantic network based on the code to be converted; the code semantic network indicates elements in the code to be converted and the relationship between the elements;

[0015] A first traversal module is used to perform a pre-order traversal on the code semantic network, and determine a current code block to be converted in the code to be converted based on a current traversal element and a traversal path corresponding to the current traversal element;

[0016] A language conversion module, used to express the current code block to be converted into the target programming language based on the code semantic network, and obtain the target code corresponding to the current code block to be converted;

[0017] The second traversal module is used to continue the pre-order traversal, update the current code block to be converted, and determine the target code corresponding to the updated current code block to be converted when there are elements that have not been traversed in the code semantic network, until there are no elements that have not been traversed in the code semantic network, then end the pre-order traversal and obtain the target code corresponding to the code to be converted.

[0018] In an exemplary embodiment, the language conversion module includes:

[0019] A current language module, used to determine the current programming language of the code to be converted;

[0020] A candidate instance module, configured to determine a candidate conversion instance in a code conversion instance based on the current programming language and the target programming language;

[0021] A similar instance module, configured to determine, based on a preset similarity requirement, a similar conversion instance corresponding to the current code block to be converted from among the candidate conversion instances;

[0022] An example guidance module is used to express the current code block to be converted into the target programming language based on the code semantic network, using a similar conversion example corresponding to the current code block to be converted as a guide, and obtain a target code corresponding to the current code block to be converted.

[0023] In an exemplary embodiment, the apparatus further comprises a direct conversion module for performing code conversion without example guidance in the absence of similar conversion examples, the direct conversion module comprising:

[0024] A direct conversion module is used to express the current code block to be converted into the target programming language based on the code semantic network when there is no similar conversion instance corresponding to the current code block to be converted in the code conversion instance, so as to obtain the target code corresponding to the current code block to be converted.

[0025] In an exemplary embodiment, the apparatus further includes an instance adding module for accumulating code conversion instances during the conversion process of the code block, and the instance adding module includes:

[0026] The instance adding module is used to add the current code block to be converted and the target code corresponding to the current code block to be converted to the code conversion instance to update the code conversion instance.

[0027] In an exemplary embodiment, the language conversion module, or the example guidance module, or the direct conversion module includes:

[0028] A target rule module, configured to determine a target conversion rule in a code conversion rule based on the current programming language and the target programming language;

[0029] A rule guidance module is used to express the current code block to be converted into the target programming language based on the target conversion rule and the code semantic network, and obtain the target code corresponding to the current code block to be converted.

[0030] In an exemplary embodiment, the language conversion module includes:

[0031] A document acquisition module, used to acquire the project document corresponding to the code to be converted;

[0032] A project guidance module is used to express the current code block to be converted into the target programming language based on the code semantic network and the project document corresponding to the code to be converted, so as to obtain the target code corresponding to the current code block to be converted.

[0033] In an exemplary embodiment, the device further includes an annotation-guided conversion module for annotating the current code block to be converted to guide code conversion, and the annotation-guided conversion module includes:

[0034] A code annotation module, used for annotating the current code block to be converted based on the code semantic network in the absence of a project document corresponding to the code to be converted;

[0035] An annotation guidance module is used to express the current code block to be converted into the target programming language based on the code semantic network and the annotations of the code to be converted, so as to obtain the target code corresponding to the current code block to be converted.

[0036] In an exemplary embodiment, the language conversion module includes:

[0037] A context information module, used to determine the context information of the current code block to be converted based on the code semantic network;

[0038] A model conversion module is used to input the current code block to be converted, the target programming language and the context information into a pre-trained language model, so that the pre-trained language model expresses the current code block to be converted as the target programming language and generates a target code corresponding to the current code block to be converted.

[0039] On the other hand, an electronic device is provided, comprising a processor and a memory, wherein the memory stores at least one instruction or at least one program, and the at least one instruction or the at least one program is loaded and executed by the processor to implement the code conversion method of any of the above aspects.

[0040] On the other hand, a computer-readable storage medium is provided, in which at least one instruction or at least one program is stored, and the at least one instruction or the at least one program is loaded and executed by a processor to implement a code conversion method as described in any of the above aspects.

[0041] On the other hand, a computer program product or a computer program is provided, the computer program product or the computer program includes computer instructions, the computer instructions are stored in a computer-readable storage medium. A processor of an electronic device reads the computer instructions from the computer-readable storage medium, and the processor executes the computer instructions, so that the electronic device performs the code conversion method of any of the above aspects.

[0042] The embodiment of the present application constructs a code semantic network corresponding to the code to be converted, performs a pre-order traversal on it, determines the current code block to be converted in the code to be converted based on the current traversal element and its corresponding traversal path, and expresses the current code block to be converted as the target programming language based on the code semantic network. When converting each code block, it is ensured that all the code blocks it depends on have been converted into the target programming language, thereby ensuring the integrity and consistency of the dependencies in the code to be converted in the target code, and avoiding functional defects or logical errors caused by incorrect dependency order. In addition, the code semantic network also helps understand the context of the current code block to be converted during code conversion, which not only improves the accuracy of code conversion, but also enhances the ability to understand when processing complex code blocks. BRIEF DESCRIPTION OF THE DRAWINGS

[0043] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.

[0044] Figure 1 It is a flowchart of a code conversion method provided by an embodiment of the present application;

[0045] Figure 2 is an example diagram of a code semantic network provided by an embodiment of the present application;

[0046] Figure 3 It is a flowchart of another code conversion method provided by an embodiment of the present application;

[0047] Figure 4 is a structural block diagram of a code conversion system provided by an embodiment of the present application;

[0048] Figure 5 is a structural block diagram of a code conversion device provided in an embodiment of the present application;

[0049] Figure 6 It is a hardware structure block diagram of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0050] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of this application.

[0051] It should be noted that the terms "first", "second", etc. in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments of the present application described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, for example, a process, method, system, product or server that includes a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.

[0052] It is understandable that in the specific implementation of this application, related data such as user information is involved. When the above embodiments of this application are applied to specific products or technologies, user permission or consent is required, and the collection, use and processing of relevant data need to comply with relevant laws, regulations and standards of relevant countries and regions.

[0053] See also Figure 1 , which shows a flow chart of a code conversion method provided by an embodiment of the present application. It should be noted that this specification provides method operation steps such as the embodiments or flow charts, but may include more or fewer operation steps based on conventional or non-creative labor. The order of steps listed in the embodiments is only one way of executing the steps among many orders, and does not represent the only order of execution. When the system or product is executed in practice, it can be executed in the order shown in the embodiments or the drawings or in parallel (for example, in a parallel processor or multi-threaded processing environment). Specifically, Figure 1 As shown, the method may include:

[0054] S101, obtaining the code to be converted and the target programming language.

[0055] The code to be converted refers to source code that needs to be converted from one programming language to another programming language.

[0056] The target programming language refers to the programming language into which the code to be converted is ultimately converted.

[0057] For example, convert from Java to TypeScript, from Python to Java, or from C++ to JavaScript.

[0058] Specifically, a user or a system provides a source code file to be converted and a target programming language, reads the source code file, parses its content, and determines the language type of the source code and the target programming language.

[0059] S103, constructing a code semantic network based on the code to be converted.

[0060] The code semantic network is a graph structure that uses nodes and edges to represent the elements in the code to be converted and the relationship between the elements. Figure 2 In the form of the smart graph shown, the smart graph can represent elements such as functions, classes, variables, etc. in the code in the form of nodes and edges, and reveal the relationship between them.

[0061] In the specific implementation, the code block can be used as the element unit, and the nodes in the code semantic network represent the code blocks, such as functions, classes, variables, etc. The edges in the code semantic network represent the dependency relationships between code blocks, such as function call relationships, class inheritance relationships, variable reference relationships, module import relationships, object instantiation relationships, data flow relationships, event triggering relationships, conditional dependencies, circular dependencies, etc.

[0062] In specific implementation, a lexical analyzer and a syntax analyzer can be used to parse the source code, extract the syntax and semantic information, convert the parsed information into nodes and edges, and construct a code semantic network. Nodes represent code blocks, and edges represent the dependencies between code blocks.

[0063] For example, suppose we have a piece of source code. After parsing, the code blocks and dependencies are as follows: node A represents the main function, node B represents auxiliary function 1, node C represents auxiliary function 2, edge A->B represents that the main function calls auxiliary function 1, and edge A->C represents that the main function calls auxiliary function 2.

[0064] S105 , performing a pre-order traversal on the code semantic network, and determining a current code block to be converted in the code to be converted based on a current traversal element and a traversal path corresponding to the current traversal element.

[0065] Among them, pre-order traversal is an algorithm for traversing a tree or graph, which traverses the tree or graph by visiting the root node first, then the left subtree and the right subtree in sequence. In code transformation, pre-order traversal ensures that before processing a code block, all its dependent code blocks have been processed.

[0066] The currently traversed element refers to the node currently being visited when traversing the code semantic network.

[0067] The traversal path corresponding to the current traversal element refers to the path from the root node to the current traversal element. This path records all the nodes and edges passed through to reach the current traversal element, provides context information, and helps to understand the dependency and position of the current traversal element.

[0068] The current code block to be converted refers to the code block represented by the current traversal element and its corresponding traversal path. This code block is the part that needs to be converted into the target programming language, and contains the code of the current traversal element and its context information.

[0069] In the specific implementation, the elements representing the functions and the calling relationships between these elements can be extracted from the code semantic network to obtain the call tree, and the call tree is traversed in pre-order to determine the current code block to be converted. Specifically, if a function to be converted contains called functions, these called functions are added to the function to be converted, and the function to be converted is reconstructed as the current code block to be converted. This process ensures that complete context information can be provided during code conversion, improving the accuracy of conversion.

[0070] S107, based on the code semantic network, expressing the current code block to be converted into a target programming language, and obtaining a target code corresponding to the current code block to be converted.

[0071] The target code corresponding to the current code block to be converted refers to the code after the current code block to be converted is converted into the target programming language.

[0072] Specifically, according to the syntax and semantic rules of the target programming language, the current code block to be converted is converted into the target code, and the converted code block is stored to form a part of the final target code.

[0073] Specifically, in the process of expressing the current code block to be converted into the target programming language, the code semantic network is used to provide context information of the current code block to be converted. Compared with the plain text-based code conversion method in the prior art, the code semantic network can provide richer context information, capturing the structure, dependencies and business logic of the code, thereby improving the accuracy and stability of the conversion, and further generating target code that is more in line with the original intention.

[0074] In an exemplary embodiment, the above step S107 may include the following steps:

[0075] Based on the code semantic network, determine the context information of the current code block to be converted;

[0076] The current code block to be converted, the target programming language and the context information are input into the pre-trained language model, so that the pre-trained language model expresses the current code block to be converted into the target programming language and generates the target code corresponding to the current code block to be converted.

[0077] The context information refers to the environment information related to the current code block to be converted, including but not limited to dependency, scope, function description, etc. In a specific implementation, the extracted dependency, scope, function description and other information are integrated into a structured context information.

[0078] Among them, the pre-trained language model refers to a deep learning model that has been trained with a large amount of data and can understand and generate text in natural language or programming language, such as LLM.

[0079] In the specific implementation, a large amount of code data and related context information is obtained, including collecting implementation code pairs of the same function in different programming languages, such as Java and Python. The collected code and context information are converted into a format that can be processed by the language model. A model architecture suitable for the code conversion task is selected, such as Transformer, GPT, BERT, etc. Considering the size and complexity of the model, the appropriate number of layers and parameters are selected, and the weights and bias items of the language model are initialized. The preprocessed data is input into the language model and the output of the model is calculated. The loss function is calculated based on the output of the model and the true label, and the parameters of the model are updated to minimize the loss function.

[0080] Specifically, taking LLM as an example, LLM is usually based on the Transformer architecture. The input is converted into a digital form that the model can process, and the input data is processed through the model's multi-layer neural network to generate an intermediate representation. Specifically, the encoder layer is responsible for processing the input sequence and generating an intermediate representation. Each encoder layer usually includes a self-attention mechanism and a feedforward neural network. The self-attention mechanism allows the model to focus on different parts of the input sequence and capture long-distance dependencies. The feedforward neural network performs nonlinear transformations on the features of each position; the decoder layer is responsible for generating the output sequence. Each decoder layer includes a self-attention mechanism, an encoder-decoder attention mechanism, and a feedforward neural network. The self-attention mechanism allows the model to focus on the generated output sequence part. The encoder-decoder attention mechanism allows the model to focus on relevant information in the input sequence. The feedforward neural network performs nonlinear transformations on the features of each position. Convert the intermediate representation of the model to the final output code; specifically, convert the output of the decoder layer into a vector of the vocabulary size through a linear transformation, apply the softmax function to convert the output vector into a probability distribution, indicating the probability of each word appearing in the next position, and select the next word based on the probability distribution. You can choose the word with the highest probability (greedy search) or select words through sampling (such as Top-k sampling, temperature sampling); the generated word is added to the output sequence as the input of the next time step, and the next word is generated until the maximum length is reached or the end symbol is generated. Convert the generated digital sequence back to code form.

[0081] It can be seen from the above technical solutions of the embodiments of the present application that, based on the code semantic network and context information in the embodiments of the present application, the pre-trained language model can more accurately understand and convert complex code blocks. Context information provides detailed environmental information, which helps to generate more accurate target code. Using a pre-trained language model for code conversion greatly reduces the need for manual intervention and improves the efficiency and consistency of code conversion. The automatically generated target code has a clear structure and annotations, which is convenient for subsequent development and maintenance. By utilizing the language understanding and generation capabilities of the pre-trained language model, a highly automated code conversion process can be achieved, improving the efficiency and quality of code conversion.

[0082] In an exemplary embodiment, Figure 3 As shown, the above step S107 may include:

[0083] S301, determining the current programming language of the code to be converted.

[0084] The current programming language refers to the programming language currently used by the code to be converted.

[0085] Specifically, the source code file is parsed to determine the language type of the source code.

[0086] S303: Based on the current programming language and the target programming language, determine whether there is a candidate conversion instance in the code conversion instance.

[0087] Specifically, if the result of the judgment is yes, step S305 can be executed; otherwise, if the result of the judgment is no, step S309 can be executed.

[0088] The code conversion instance stores a conversion example from any programming language to another programming language, including a source code block and a corresponding target code block.

[0089] The candidate conversion instance stores conversion examples from the current programming language to the target programming language, including source code blocks and corresponding target code blocks.

[0090] Specifically, it is determined whether there is a conversion instance matching the current programming language and the target programming language in the code conversion instance. If so, it is screened out as a candidate conversion instance. If not, the conversion is performed directly based on the code semantic network.

[0091] S305: Based on a preset similarity requirement, determine whether there is a similar conversion instance corresponding to the current code block to be converted in the candidate conversion instances.

[0092] Specifically, if the result of the judgment is yes, step S307 can be executed; otherwise, if the result of the judgment is no, step S309 can be executed.

[0093] The similar transformation instance refers to a transformation example that meets a preset similarity requirement among the candidate transformation instances.

[0094] Specifically, the similarity between the current code block to be converted and each candidate conversion instance is calculated, and the conversion instance that meets the preset similarity requirement is selected. In a specific implementation, the preset similarity requirement generally includes the similarity being greater than a preset threshold, and the similarity may also be required to be ranked in the top few. Exemplarily, there are 5 instances in the candidate conversion instance whose similarity with the current code block to be converted is greater than the preset threshold, and the instances ranked in the top three in similarity may be selected as similar conversion instances.

[0095] Specifically, if there is a similar transformation instance that meets the preset similarity requirement, the similar transformation instance is used as a guide and combined with the code semantic network to perform code transformation. If there is no similar transformation instance, the transformation is performed directly based on the code semantic network.

[0096] S307, using similar conversion instances corresponding to the current code block to be converted as a guide, based on the code semantic network, the current code block to be converted is expressed as a target programming language, and a target code corresponding to the current code block to be converted is obtained.

[0097] Specifically, continue to execute step S3011.

[0098] S309: Based on the code semantic network, the current code block to be converted is expressed as a target programming language to obtain a target code corresponding to the current code block to be converted.

[0099] S3011, adding the current code block to be converted and the target code corresponding to the current code block to be converted to the code conversion instance to update the code conversion instance.

[0100] Specifically, taking the conversion from Java to TypeScript as an example, in the early stage of code conversion, if there is a lack of Java to TypeScript conversion samples, the zero-shot method can be used for conversion, which directly converts Java to TypeScript code based on the understanding and conversion rules of the two languages. When a certain number of code conversion samples have been accumulated, the RAG based on few-shot learning can be used.

[0101] The Retrieval-Augmented Generation (RAG) method provides reference examples of similar codes during conversion to help better understand the conversion rules and improve the quality and consistency of the conversion.

[0102] Existing LLMs usually rely on pre-trained knowledge when performing code conversion. This static knowledge base is difficult to cope with various complex scenarios in actual development. Factors such as changes in the code base and adjustments to business needs will affect the accuracy of code conversion, and traditional fine-tuning methods require a lot of time and computing resources to retrain the model, which is inefficient. The embodiment of the present application chooses RAG instead of the traditional fine-tuning method, mainly because RAG can use the existing code base or locally stored code corpus to enable it to dynamically adapt to different code conversion tasks without a lot of retraining. Use the existing code conversion repository to store code conversion instances, dynamically retrieve the most relevant examples for the current conversion task, and use the input context to guide LLM to generate new code segments. The RAG method allows the model to maintain its generation capability while combining the reference information provided by the external knowledge base to greatly improve the quality of code conversion. This dynamic adaptability enables the model to flexibly cope with complex scenarios in actual development, thereby improving the efficiency and accuracy of code conversion.

[0103] See also Figure 4 , which is a structural block diagram of a code conversion system provided by an embodiment of the present application, which is mainly divided into the following parts:

[0104] Prompt Template is a predefined template used to construct input prompts for conversion tasks.

[0105] Java Code to be Translated is the original Java code that needs to be converted into TypeScript.

[0106] Most Similar Translation Examples are the translation examples that are most similar to the code to be translated and retrieved from the ChromaDB vector database.

[0107] Few-shot Prompt With Examples combines the most similar conversion examples and the Java code to be converted to form a new prompt containing multiple examples.

[0108] Embedding Model is used to convert text data into numerical vectors for easy comparison and analysis.

[0109] Embedding from Examples is the embedding vector extracted from the transformed examples.

[0110] Embedding from Java is the embedding vector extracted from the Java code to be converted.

[0111] ChromaDB Vector Database of Code Examples is a database that stores code example vectors for fast retrieval of similar code snippets.

[0112] The Similarity Matrix records the similarity between different code snippets.

[0113] LLM is responsible for generating corresponding outputs based on the input prompts.

[0114] Response with TS Translation is the result generated by LLM and contains a version of the Java code converted to TypeScript.

[0115] Specifically, an input prompt is constructed through the prompt template and the Java code to be converted; then, the most similar conversion example is retrieved from the ChromaDB vector database and merged with the input prompt to form a new prompt with example; then, the embedding model is used to obtain the embedding vectors of the example and Java code respectively, and the similarity between them is calculated; finally, the prompt with example is input into the LLM to generate the conversion result.

[0116] It can be seen from the above technical solutions of the embodiments of the present application that the embodiments of the present application can more accurately understand and convert complex code blocks based on code semantic networks and similar conversion instances. Similar conversion instances provide references, which help to generate more accurate target codes. The use of similar conversion instances improves the flexibility of conversion and can better handle different types of code blocks. The code semantic network provides detailed contextual environment information, enhances the ability to understand complex code blocks, makes the conversion process more reliable and efficient, and further improves the accuracy of the conversion. By adding the converted code blocks and their corresponding target codes to the code conversion instance, it is possible to continuously accumulate conversion experience, improve the efficiency and accuracy of subsequent conversions, and gradually improve the performance and reliability of code conversion.

[0117] In an exemplary embodiment, the above step S107, or the step of expressing the current code block to be converted into a target programming language based on the code semantic network in the above step S307, to obtain the target code corresponding to the current code block to be converted, or the above step S309 may include the following steps:

[0118] Determine a target conversion rule in the code conversion rule based on the current programming language and the target programming language;

[0119] Based on the target conversion rules and the code semantic network, the current code block to be converted is expressed as a target programming language to obtain the target code corresponding to the current code block to be converted.

[0120] The target conversion rule refers to a specific conversion rule for the current programming language and the target programming language.

[0121] Specifically, code conversion rules are loaded from the configuration file or database. These rules cover conversions between multiple programming languages. Conversion rules applicable to the current programming language and the target programming language are filtered out from the loaded code conversion rules. Conversion rules are verified to ensure that the filtered conversion rules are complete and valid and can cover all syntax and semantic requirements of the current code block to be converted. These rules help the model better meet the syntax requirements of the target language during conversion, ensuring that the generated code is both correct and in line with the coding habits of the target language.

[0122] For example, assuming that the current programming language is Java and the target programming language is TypeScript, taking the conversion from Java to TypeScript as an example, the configuration file or database stores code conversion rules, including a set of conversion rules written in advance for common syntax differences between Java and TypeScript. The code conversion rules are loaded, and the conversion rules from Java to TypeScript are selected to verify whether these rules cover the conversion of common syntax structures such as class definition, method call, and variable declaration.

[0123] Specifically, the context information of the current code block to be converted is obtained from the code semantic network, such as dependencies, scopes, etc. According to the target conversion rules, the syntax and semantics of the current code block to be converted are converted into the syntax and semantics of the target programming language. Special constructs and language features in the source code are processed to ensure that the converted code runs correctly in the target programming language.

[0124] It can be seen from the above technical solutions of the embodiments of the present application that the generated TypeScript code not only meets the standards of the target language, but also retains the readability and maintainability of the original code through the formulation of conversion rules and the enrichment of context information in the embodiments of the present application, thereby facilitating subsequent code maintenance and upgrades.

[0125] In an exemplary embodiment, the above step S107 may include the following steps:

[0126] Get the project document corresponding to the code to be converted;

[0127] Based on the code semantic network and the project documents corresponding to the code to be converted, the current code block to be converted is expressed as a target programming language to obtain the target code corresponding to the current code block to be converted.

[0128] Among them, project documents are documents related to the code to be converted, including but not limited to design documents, API documents, user manuals, development guides, etc., which provide the functions, uses and contextual information of the code to help understand the structure and functions of the project.

[0129] Specifically, parse the project documents and extract the functional descriptions, design documents, API documents and other information related to the code to be converted. Verify the integrity of the documents to ensure that the project documents are complete and valid and can provide sufficient contextual information to support code conversion.

[0130] Specifically, the context information of the current code block to be converted is obtained from the code semantic network, such as dependencies, scopes, etc. The functional description, design documents, API documents and other information in the project documents are integrated into the code conversion process to ensure that the converted code meets the expected functions and designs. According to the syntax and semantic rules of the target programming language, the current code block to be converted is converted into the code of the target programming language.

[0131] If there is a lack of corresponding project documentation, you can generate function comments to help you better understand the business environment of the code and improve your ability to understand complex code blocks.

[0132] Specifically, in the absence of project documents corresponding to the code to be converted, the current code block to be converted is annotated based on the code semantic network; based on the code semantic network and the annotations of the code to be converted, the current code block to be converted is expressed in the target programming language to obtain the target code corresponding to the current code block to be converted.

[0133] Specifically, the context information of the current code block to be converted is obtained from the code semantic network, such as dependencies, scopes, etc. Based on the code semantic network, comments are automatically generated for the current code block to be converted, explaining the function and purpose of the code. The generated comment information is integrated into the code conversion process.

[0134] As can be seen from the above technical solutions of the embodiments of the present application, the embodiments of the present application ensure that the dependencies in the source code are fully preserved in the target code through the code semantic network and project documentation, avoiding functional loss or logical errors caused by incorrect dependency order. The project documentation provides detailed contextual information, which helps to generate more accurate target code. In the absence of project documentation, automatically generated comments provide additional contextual information.

[0135] S109, determining whether there are any elements in the code semantic network that have not been traversed.

[0136] Specifically, if the result of the judgment is yes, step S105 can be executed; otherwise, if the result of the judgment is no, step S1011 can be executed.

[0137] Specifically, when there are elements that have not been traversed in the code semantic network, the pre-order traversal is continued, the current code block to be converted is updated, and the target code corresponding to the updated current code block to be converted is determined, until there are no elements that have not been traversed in the code semantic network, and the pre-order traversal is ended.

[0138] S1011, end the pre-order traversal and obtain the target code corresponding to the code to be converted.

[0139] The target code corresponding to the code to be converted refers to the code after the code to be converted is converted into the target programming language.

[0140] Specifically, all converted code blocks are merged to form a complete target code, and the generated target code is saved to a file or returned to the user.

[0141] It can be seen from the above technical solutions of the embodiments of the present application that the embodiments of the present application ensure that the dependencies in the source code are fully preserved in the target code through pre-order traversal and dependency management, avoiding functional loss or logical errors caused by incorrect dependency order; based on the code semantic network and pre-order traversal, complex code blocks can be understood and converted more accurately, and the code semantic network provides detailed contextual environment information, which helps to generate more accurate target code. The code semantic network provides contextual environment information during the conversion process, enhances the ability to understand complex code blocks, and makes the conversion process more reliable and efficient. It reduces the workload and error rate of manual code conversion, reduces the risks that may be caused in the code conversion process, especially in the cross-language conversion of complex systems, and can better guarantee the quality of code conversion.

[0142] Corresponding to the code conversion methods provided in the above-mentioned embodiments, the embodiment of the present application also provides a code conversion device. Since the code conversion device provided in the embodiment of the present application corresponds to the code conversion methods provided in the above-mentioned embodiments, the implementation methods of the aforementioned code conversion methods are also applicable to the code conversion device provided in this embodiment and will not be described in detail in this embodiment.

[0143] See also Figure 5 , which is a schematic diagram of the structure of a code conversion device provided by an embodiment of the present application, the device has the function of implementing the code conversion method in the above method embodiment, and the function can be implemented by hardware, or by hardware executing corresponding software. Figure 5 As shown, the device may include:

[0144] An information acquisition module 510 is used to acquire the code to be converted and the target programming language;

[0145] A network construction module 520 is used to construct a code semantic network based on the code to be converted; the code semantic network indicates the elements in the code to be converted and the relationship between the elements;

[0146] The first traversal module 530 is used to perform a pre-order traversal on the code semantic network, and determine a current code block to be converted in the code to be converted based on a current traversal element and a traversal path corresponding to the current traversal element;

[0147] A language conversion module 540 is used to express the current code block to be converted into a target programming language based on a code semantic network, and obtain a target code corresponding to the current code block to be converted;

[0148] The second traversal module 550 is used to continue the pre-order traversal when there are elements that have not been traversed in the code semantic network, update the current code block to be converted, and determine the target code corresponding to the updated current code block to be converted, until there are no elements that have not been traversed in the code semantic network, then end the pre-order traversal and obtain the target code corresponding to the code to be converted.

[0149] In an exemplary embodiment, the language conversion module includes:

[0150] A current language module, used to determine the current programming language of the code to be converted;

[0151] A candidate instance module, for determining a candidate conversion instance in a code conversion instance based on a current programming language and a target programming language;

[0152] A similar instance module is used to determine a similar conversion instance corresponding to the current code block to be converted from the candidate conversion instances based on a preset similarity requirement;

[0153] The example guidance module is used to express the current code block to be converted into a target programming language based on the code semantic network, using similar conversion examples corresponding to the current code block to be converted as guidance, and obtain the target code corresponding to the current code block to be converted.

[0154] In an exemplary embodiment, the apparatus further comprises a direct conversion module for performing code conversion without example guidance in the absence of similar conversion examples, the direct conversion module comprising:

[0155] The direct conversion module is used to express the current code block to be converted into the target programming language based on the code semantic network to obtain the target code corresponding to the current code block to be converted when there is no similar conversion instance corresponding to the current code block to be converted in the code conversion instance.

[0156] In an exemplary embodiment, the apparatus further includes an instance adding module for accumulating code conversion instances during the conversion process of the code block, and the instance adding module includes:

[0157] The instance adding module is used to add the current code block to be converted and the target code corresponding to the current code block to be converted to the code conversion instance to update the code conversion instance.

[0158] In an exemplary embodiment, the language conversion module, or the example guidance module, or the direct conversion module includes:

[0159] A target rule module, for determining a target conversion rule in a code conversion rule based on a current programming language and a target programming language;

[0160] The rule guidance module is used to express the current code block to be converted into a target programming language based on the target conversion rules and the code semantic network, and obtain the target code corresponding to the current code block to be converted.

[0161] In an exemplary embodiment, the language conversion module includes:

[0162] A document acquisition module is used to obtain the project documents corresponding to the code to be converted;

[0163] The project guidance module is used to express the current code block to be converted into a target programming language based on the code semantic network and the project document corresponding to the code to be converted, and obtain the target code corresponding to the current code block to be converted.

[0164] In an exemplary embodiment, the apparatus further includes an annotation guidance conversion module for annotating the current code block to be converted to guide code conversion, and the annotation guidance conversion module includes:

[0165] A code annotation module is used to annotate the current code block to be converted based on the code semantic network in the absence of a project document corresponding to the code to be converted;

[0166] The annotation guidance module is used to express the current code block to be converted into a target programming language based on the code semantic network and the annotations of the code to be converted, and obtain the target code corresponding to the current code block to be converted.

[0167] In an exemplary embodiment, the language conversion module includes:

[0168] A context information module, used to determine the context information of the current code block to be converted based on the code semantic network;

[0169] The model conversion module is used to input the current code block to be converted, the target programming language and the context information into the pre-trained language model, so that the pre-trained language model can express the current code block to be converted into the target programming language and generate the target code corresponding to the current code block to be converted.

[0170] It should be noted that the device provided in the above embodiment, when implementing its functions, is only illustrated by the division of the above functional modules. In actual applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the device is divided into different functional modules to complete all or part of the functions described above. In addition, the device and method embodiments provided in the above embodiment belong to the same concept, and the specific implementation process is detailed in the method embodiment, which will not be repeated here.

[0171] An embodiment of the present application provides an electronic device, which includes a processor and a memory, wherein the memory stores at least one instruction or at least one program, and the at least one instruction or the at least one program is loaded and executed by the processor to implement any one of the code conversion methods provided in the above method embodiments.

[0172] The memory can be used to store software programs and modules. The processor executes various functional applications and data processing by running the software programs and modules stored in the memory. The memory can mainly include a program storage area and a data storage area, wherein the program storage area can store an operating system, application programs required for functions, etc.; the data storage area can store data created according to the use of the device, etc. In addition, the memory can include a high-speed random access memory and can also include a non-volatile memory, such as at least one disk storage device, a flash memory device, or other volatile solid-state storage devices. Accordingly, the memory can also include a memory controller to provide the processor with access to the memory.

[0173] The method embodiments provided in the embodiments of the present application may be executed in a computer terminal, a server or a similar computing device, that is, the above-mentioned electronic device may include a computer terminal, a server or a similar computing device. Figure 6 is a hardware structure block diagram of a computer device running a code conversion method provided by an embodiment of the present invention, such as Figure 6 As shown, the internal structure of the computer device may include but is not limited to: a processor, a network interface and a memory. The processor, network interface and memory in the computer device may be connected via a bus or other means. Figure 6 The example of connecting through bus is taken in the following.

[0174] Among them, the processor (or CPU (Central Processing Unit)) is the computing core and control core of the computer device. The network interface may optionally include a standard wired interface, a wireless interface (such as WI-FI, a mobile communication interface, etc.). Memory is a memory device in a computer device for storing programs and data. It is understandable that the memory here can be a high-speed RAM storage device or a non-volatile storage device (non-volatile memory), such as at least one disk storage device; optionally, it can also be at least one storage device located away from the aforementioned processor. The memory provides a storage space, which stores the operating system of the electronic device, which may include but is not limited to: Windows system (an operating system), Linux (an operating system), Android (Android, a mobile operating system) system, IOS (a mobile operating system) system, etc., and the present invention is not limited to this; and, in the storage space, one or more instructions suitable for being loaded and executed by the processor are also stored, and these instructions may be one or more computer programs (including program codes). In the embodiment of this specification, the processor loads and executes one or more instructions stored in the memory to implement the code conversion method provided in the above method embodiment.

[0175] An embodiment of the present application also provides a computer-readable storage medium, which can be set in an electronic device to store at least one instruction or at least one program related to implementing a code conversion method. The at least one instruction or the at least one program is loaded and executed by the processor to implement any one of the code conversion methods provided in the above method embodiments.

[0176] Optionally, in this embodiment, the above-mentioned storage medium may include but is not limited to: a USB flash drive, a read-only memory (ROM), a random access memory (RAM), a mobile hard disk, a magnetic disk or an optical disk, and other media that can store program codes.

[0177] It should be noted that the above-mentioned sequence of the embodiments of the present application is for description only and does not represent the advantages and disadvantages of the embodiments. The above-mentioned specific embodiments of this specification are described. Other embodiments are within the scope of the attached claims. In some cases, the actions or steps recorded in the claims can be performed in an order different from that in the embodiments and still achieve the desired results. In addition, the processes depicted in the drawings do not necessarily require the specific order or continuous order shown to achieve the desired results. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.

[0178] Each embodiment in this specification is described in a progressive manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the device embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.

[0179] A person skilled in the art will understand that all or part of the steps to implement the above embodiments may be accomplished by hardware or by instructing related hardware through a program, and the program may be stored in a computer-readable storage medium, and the above-mentioned storage medium may be a read-only memory, a disk or an optical disk, etc.

[0180] The above are only preferred embodiments of the present application and are not intended to limit the present application. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present application should be included in the protection scope of the present application.

Claims

1. A code conversion method, characterized in that: The method comprises: Get the code to be converted and the target programming language; Constructing a code semantic network based on the code to be converted; the code semantic network indicates elements in the code to be converted and the relationship between the elements; Performing a pre-order traversal on the code semantic network, and determining a current code block to be converted in the code to be converted based on a current traversal element and a traversal path corresponding to the current traversal element; Based on the code semantic network, the current code block to be converted is expressed as the target programming language to obtain the target code corresponding to the current code block to be converted; In the case that there are elements that have not been traversed in the code semantic network, the pre-order traversal is continued, the current code block to be converted is updated, and the target code corresponding to the updated current code block to be converted is determined, until there are no elements that have not been traversed in the code semantic network, the pre-order traversal is ended, and the target code corresponding to the code to be converted is obtained.

2. The code conversion method according to claim 1, characterized in that: The step of expressing the current code block to be converted into the target programming language based on the code semantic network to obtain the target code corresponding to the current code block to be converted includes: Determining the current programming language of the code to be converted; Determining a candidate conversion instance in a code conversion instance based on the current programming language and the target programming language; Based on a preset similarity requirement, determining a similar conversion instance corresponding to the current code block to be converted from the candidate conversion instances; Guided by similar conversion instances corresponding to the current code block to be converted, based on the code semantic network, the current code block to be converted is expressed as the target programming language to obtain the target code corresponding to the current code block to be converted.

3. The code conversion method according to claim 2, characterized in that: The method further comprises: When there is no similar conversion instance corresponding to the current code block to be converted in the code conversion instance, the current code block to be converted is expressed as the target programming language based on the code semantic network to obtain the target code corresponding to the current code block to be converted.

4. The code conversion method according to claim 3, characterized in that: The method further comprises: The current code block to be converted and the target code corresponding to the current code block to be converted are added to the code conversion instance to update the code conversion instance.

5. The code conversion method according to claim 4, characterized in that: The step of expressing the current code block to be converted into the target programming language based on the code semantic network to obtain the target code corresponding to the current code block to be converted includes: Determining a target conversion rule in a code conversion rule based on the current programming language and the target programming language; Based on the target conversion rule and the code semantic network, the current code block to be converted is expressed as the target programming language to obtain the target code corresponding to the current code block to be converted.

6. The code conversion method according to claim 1, characterized in that: The step of expressing the current code block to be converted into the target programming language based on the code semantic network to obtain the target code corresponding to the current code block to be converted includes: Obtaining the project document corresponding to the code to be converted; Based on the code semantic network and the project document corresponding to the code to be converted, the current code block to be converted is expressed in the target programming language to obtain the target code corresponding to the current code block to be converted.

7. The code conversion method according to claim 6, characterized in that: The method further comprises: In the absence of a project document corresponding to the code to be converted, annotating the current code block to be converted based on the code semantic network; Based on the code semantic network and the annotations of the code to be converted, the current code block to be converted is expressed as the target programming language to obtain the target code corresponding to the current code block to be converted.

8. The code conversion method according to any one of claims 1 to 7, characterized in that: The step of expressing the current code block to be converted into the target programming language based on the code semantic network to obtain the target code corresponding to the current code block to be converted includes: Based on the code semantic network, determining context information of the current code block to be converted; The current code block to be converted, the target programming language and the context information are input into a pre-trained language model, so that the pre-trained language model expresses the current code block to be converted as the target programming language and generates a target code corresponding to the current code block to be converted.

9. A code conversion device, characterized in that: The device comprises: An information acquisition module, used to acquire the code to be converted and the target programming language; A network construction module, used to construct a code semantic network based on the code to be converted; the code semantic network indicates elements in the code to be converted and the relationship between the elements; A first traversal module is used to perform a pre-order traversal on the code semantic network, and determine a current code block to be converted in the code to be converted based on a current traversal element and a traversal path corresponding to the current traversal element; A language conversion module, used to express the current code block to be converted into the target programming language based on the code semantic network, and obtain the target code corresponding to the current code block to be converted; The second traversal module is used to continue the pre-order traversal, update the current code block to be converted, and determine the target code corresponding to the updated current code block to be converted when there are elements that have not been traversed in the code semantic network, until there are no elements that have not been traversed in the code semantic network, then end the pre-order traversal and obtain the target code corresponding to the code to be converted.

10. An electronic device, characterized in that: The invention comprises a processor and a memory, wherein the memory stores at least one instruction or at least one program, and the at least one instruction or at least one program is loaded and executed by the processor to implement the code conversion method according to any one of claims 1 to 8.

Citation Information

Patent Citations

  • Method for automatically converting codes between high-level programming languages

    CN112732264A

  • MDE-based closed world integrity constraint checking method in semantic network

    CN117786759A

  • Automatic code conversion and evaluation method and device from C language to RUST language

    CN118170672A

  • Translating between programming languages independently of sequence-to-sequence decoders

    US20230325164A1