Code rewriting method and device, computer equipment, storage medium and program product
By generating a target abstract syntax tree and utilizing preset transformation constraints, combined with large language model optimization, the problem of unexpected changes in code translation is solved, ensuring the correctness and accuracy of code translation.
Patent Information
- Application Number
- CN202511792493.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-12-01
- Publication Date
- 2026-02-24
AI Technical Summary
In the process of code translation, existing technologies may cause unexpected changes to key algorithm logic, making it impossible to guarantee the correctness of the code translation.
By generating a target abstract syntax tree and utilizing the knowledge tags of nodes and preset transformation constraints, the code is rewritten from the first programming language to the second programming language. Combined with large language model optimization and verification mechanisms, the syntactic compliance and semantic equivalence of the code are ensured.
Ensure that the converted code is functionally consistent with the original code, avoid logical errors caused by misunderstandings during manual conversion, and ensure that the generated target code conforms to the syntax rules of the target language, reducing syntax errors and guaranteeing the accuracy of the conversion results.
Smart Images

Figure CN121560337A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and in particular to a code rewriting method, apparatus, computer equipment, storage medium, and program product. Background Technology
[0002] Code translation refers to the technical process of converting source code from one programming language to another while maintaining its functional logic and semantic equivalence. Its core goal is to achieve cross-language functional transfer, rather than simple syntax replacement. The objective of code translation is to generate maintainable target code, preserving human-understandable elements such as variable naming, comments, and algorithm structure.
[0003] Current mainstream methods in code translation include rule-based engines, pure large language models, and traditional machine learning. However, with existing technologies, key algorithmic logic may undergo unexpected changes during the translation process, making it impossible to guarantee the accuracy of the code translation. Summary of the Invention
[0004] Therefore, it is necessary to provide a code rewriting method, apparatus, computer equipment, storage medium, and program product that can guarantee the correctness of code rewriting in response to the above-mentioned technical problems.
[0005] Firstly, this application provides a code rewriting method, including:
[0006] Obtain the source code file of the code to be rewritten; wherein the code to be rewritten is written in a first programming language;
[0007] Based on the source code file, a target abstract syntax tree is generated for the code to be rewritten; wherein, the abstract syntax tree includes nodes of different types, connecting edges between nodes, and knowledge labels for nodes; different types of nodes represent different attributes of the code to be rewritten, connecting edges between nodes represent grammatical relationships, and knowledge labels for nodes are used to describe the grammatical rules corresponding to the type of the node in a second programming language;
[0008] Based on the target abstract syntax tree and preset transformation constraints, the code to be rewritten is rewritten from the first programming language to a target code file in the second programming language.
[0009] In one embodiment, generating the target abstract syntax tree of the code to be rewritten based on the source code file includes:
[0010] Semantic extraction and syntactic analysis are performed on the source code file to obtain an initial abstract syntax tree; wherein, the initial abstract syntax tree includes nodes and connecting edges between nodes;
[0011] For each node in the initial abstract syntax tree, based on the node type, the syntax rules of the second programming language corresponding to the node type are queried from the cross-language knowledge base;
[0012] The syntax rules corresponding to each node are used as knowledge tags and added to the corresponding nodes in the initial abstract syntax tree to obtain the target abstract syntax tree of the code to be rewritten.
[0013] In one embodiment, the step of rewriting the code to be rewritten from the first programming language to a target code file in the second programming language based on the target abstract syntax tree and preset transformation constraints includes:
[0014] Hierarchical indentation is applied to the nesting relationships between nodes in the target syntax abstraction tree to obtain indented structured text;
[0015] According to the preset splicing rules, the knowledge tags of each node in the structured text are spliced together to obtain the target text;
[0016] Based on preset conversion constraints and the target text, the code to be rewritten is rewritten from the first programming language to a target code file in the second programming language.
[0017] In one embodiment, the step of rewriting the code to be rewritten from the first programming language to a target code file in the second programming language according to preset conversion constraints and the target text includes:
[0018] Based on preset transformation constraints, the pre-trained large language model is optimized to obtain the target large language model;
[0019] The target code is input into the target large language model to obtain the target code file.
[0020] In one embodiment, the method further includes:
[0021] The target code file is verified for at least one of the following: syntax compliance, semantic equivalence, and domain rule conformity, and the verification result is obtained.
[0022] If the verification result is that the verification fails, the preset transformation constraints are updated according to the verification result, and the operation of rewriting the code to be rewritten from the first programming language to the target code in the second programming language according to the target abstract syntax tree and the preset transformation constraints is returned.
[0023] In one embodiment, the method further includes:
[0024] If the verification result is successful, new transformation constraints are extracted based on the mapping relationship between the source code file and the target code file;
[0025] The preset transformation constraints are updated based on the new transformation constraints.
[0026] Secondly, this application also provides a code rewriting apparatus, comprising:
[0027] The file acquisition module is used to acquire the source code file of the code to be rewritten; wherein the code to be rewritten is code written in a first programming language;
[0028] A tree construction module is used to generate a target abstract syntax tree for the code to be rewritten based on the source code file; wherein, the abstract syntax tree includes nodes of different types, connecting edges between nodes, and knowledge labels of nodes; different types of nodes represent different attributes of the code to be rewritten, connecting edges between nodes represent grammatical relationships, and knowledge labels of nodes are used to describe the grammatical rules corresponding to the type of the node in a second programming language;
[0029] The code rewriting module is used to rewrite the code to be rewritten from the first programming language to a target code file in the second programming language based on the target abstract syntax tree and preset transformation constraints.
[0030] Thirdly, this application also provides a computer device, including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to perform the following steps:
[0031] Obtain the source code file of the code to be rewritten; wherein the code to be rewritten is written in a first programming language;
[0032] Based on the source code file, a target abstract syntax tree is generated for the code to be rewritten; wherein, the abstract syntax tree includes nodes of different types, connecting edges between nodes, and knowledge labels for nodes; different types of nodes represent different attributes of the code to be rewritten, connecting edges between nodes represent grammatical relationships, and knowledge labels for nodes are used to describe the grammatical rules corresponding to the type of the node in a second programming language;
[0033] Based on the target abstract syntax tree and preset transformation constraints, the code to be rewritten is rewritten from the first programming language to a target code file in the second programming language.
[0034] Fourthly, this application also provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, performs the following steps:
[0035] Obtain the source code file of the code to be rewritten; wherein the code to be rewritten is written in a first programming language;
[0036] Based on the source code file, a target abstract syntax tree is generated for the code to be rewritten; wherein, the abstract syntax tree includes nodes of different types, connecting edges between nodes, and knowledge labels for nodes; different types of nodes represent different attributes of the code to be rewritten, connecting edges between nodes represent grammatical relationships, and knowledge labels for nodes are used to describe the grammatical rules corresponding to the type of the node in a second programming language;
[0037] Based on the target abstract syntax tree and preset transformation constraints, the code to be rewritten is rewritten from the first programming language to a target code file in the second programming language.
[0038] Fifthly, this application also provides a computer program product, including a computer program that, when executed by a processor, performs the following steps:
[0039] Obtain the source code file of the code to be rewritten; wherein the code to be rewritten is written in a first programming language;
[0040] Based on the source code file, a target abstract syntax tree is generated for the code to be rewritten; wherein, the abstract syntax tree includes nodes of different types, connecting edges between nodes, and knowledge labels for nodes; different types of nodes represent different attributes of the code to be rewritten, connecting edges between nodes represent grammatical relationships, and knowledge labels for nodes are used to describe the grammatical rules corresponding to the type of the node in a second programming language;
[0041] Based on the target abstract syntax tree and preset transformation constraints, the code to be rewritten is rewritten from the first programming language to a target code file in the second programming language.
[0042] The aforementioned code rewriting method, apparatus, computer equipment, storage medium, and program product acquire the source code file of the code to be rewritten; wherein the code to be rewritten is written in a first programming language; based on the source code file, a target abstract syntax tree (AST) is generated for the code to be rewritten; wherein the AST includes different types of nodes, connecting edges between nodes, and knowledge tags for the nodes; different types of nodes represent different attributes of the code to be rewritten, connecting edges between nodes represent syntactic relationships, and knowledge tags for nodes describe the syntactic rules corresponding to the node type in a second programming language; based on the target AST and preset conversion constraints, the code to be rewritten is converted from the first programming language into a target code file in the second programming language. In this scheme, the target AST preserves the logical structure of the source code, and combined with preset conversion constraints, it ensures that the converted code is functionally consistent with the original code, avoiding logical errors caused by misunderstandings during manual conversion; the knowledge tags and preset conversion constraints clearly define the syntactic rules of the target language, and the generated target code naturally conforms to the syntactic specifications of the target language, reducing syntactic errors during manual writing and ensuring the accuracy of the conversion results. Attached Figure Description
[0043] To more clearly illustrate the technical solutions in the embodiments or related technologies of this application, the accompanying drawings used in the description of the embodiments or related technologies will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0044] Figure 1 This is a flowchart illustrating a code rewriting method in one embodiment;
[0045] Figure 2 This is a flowchart illustrating the process of constructing the target abstract syntax tree for the code to be rewritten in one embodiment;
[0046] Figure 3 This is a flowchart illustrating the process of generating target code files in one embodiment;
[0047] Figure 4 This is a flowchart illustrating the object code file verification process in one embodiment;
[0048] Figure 5 This is a schematic diagram of the process for updating preset transformation constraints in one embodiment;
[0049] Figure 6 This is a flowchart illustrating the code rewriting method in another embodiment;
[0050] Figure 7 This is a structural block diagram of a code rewriting device in one embodiment;
[0051] Figure 8 This is an internal structural diagram of a computer device in one embodiment. Detailed Implementation
[0052] To make the objectives, technical solutions, and advantages of this application clearer, the following detailed description is provided in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the scope of this application.
[0053] The code rewriting method provided in this application can be applied to application environments where code needs to be rewritten or translated. The code rewriting method provided in this application can be executed by a computer device, which can be a server or a terminal with powerful computing capabilities.
[0054] In one exemplary embodiment, such as Figure 1 As shown, a code rewriting method is provided. Taking the application of this method to a server as an example, the specific steps include:
[0055] S101, Obtain the source code file of the code to be modified.
[0056] The code to be rewritten is written in a first programming language, which can be any programming language, such as Java, Python, etc.
[0057] Optionally, the storage location of the source code file can be explicitly specified, and the file can be located using the file path. The file content can then be read using the corresponding tool or the file reading interface of the programming language to obtain the source code in string form. The source code must exist in plain text form (avoid binary format or encrypted content) and must completely contain the logic to be converted (such as functions, classes, statement blocks, etc.), without omitting dependent variable definitions, import statements, etc.
[0058] S102, Generate the target abstract syntax tree for the code to be rewritten based on the source code file.
[0059] The abstract syntax tree includes nodes of different types, connecting edges between nodes, and knowledge labels for nodes. Different types of nodes represent different attributes of the code to be rewritten, connecting edges between nodes represent syntactic relationships, and knowledge labels for nodes are used to describe the syntactic rules corresponding to the node type in the second programming language.
[0060] Optionally, a language-specific parser can be used, based on the source code file. Generate target abstract syntax tree .in, A collection of nodes (including attributes such as type and value); Let be the set of edges, representing the grammatical structure relationships.
[0061] S103, based on the target abstract syntax tree and preset transformation constraints, rewrite the code to be rewritten from the first programming language to the target code file in the second programming language.
[0062] Among them, preset conversion constraints are a set of rules and restrictions defined in cross-language code conversion. They are used to guide the conversion process from the first programming language (source language) to the second programming language (target language), ensuring that the converted code maintains the original logical function and semantic consistency while being syntactically correct. In the embodiments of this application, preset conversion constraints include, but are not limited to, syntax mapping rules, type conversion rules, semantic equivalence constraints, style specifications, etc. For example, Python's list [] corresponds to Java's ArrayList, and java.util.ArrayList needs to be imported.
[0063] Optionally, based on the characteristics of the second programming language, the necessary context information for code generation can be initialized, including necessary import or reference statements, declaration rules for global variables / constants, and code style constraints. Data structures are used to record declared variables, type aliases, import dependencies, etc., to avoid duplicate declarations or reference errors. Each node of the target abstract syntax tree is processed using a depth-first traversal (from the root node to child nodes), combining the node's knowledge tags and preset transformation constraints to generate corresponding grammatical fragments of the target language. The core is to convert general logical nodes into the target language's grammatical structure.
[0064] For each node, the corresponding transformation rule is retrieved from the preset transformation constraints. Based on the node attributes and knowledge tags, the syntactic elements of the second coding language are supplemented, namely function definitions and conditional statements. Furthermore, considering the semantic differences between the two languages, equivalent transformations are performed according to the preset transformation constraints. In addition, for nodes containing child nodes (such as function bodies containing multiple statement nodes), the transformation logic is recursively executed, concatenating the transformation results of child nodes into syntactic fragments of the parent node (such as statement blocks in function bodies being composed of multiple sub-statements). The syntactic fragments generated by the transformation of each node are concatenated according to the hierarchical relationship of the target abstract syntax tree to form a complete code structure, namely the target code file in the second programming language.
[0065] In the above code rewriting method, the source code file of the code to be rewritten is obtained; the code to be rewritten is written in a first programming language; based on the source code file, a target abstract syntax tree (AST) is generated for the code to be rewritten; the AST includes different types of nodes, connecting edges between nodes, and knowledge tags for the nodes; different types of nodes represent different attributes of the code to be rewritten, connecting edges between nodes represent syntactic relationships, and knowledge tags for nodes describe the syntactic rules corresponding to the node type in a second programming language; based on the target AST and preset conversion constraints, the code to be rewritten is converted from the first programming language to a target code file in the second programming language. This scheme preserves the logical structure of the source code in the target AST, and combined with preset conversion constraints, ensures that the converted code is functionally consistent with the original code, avoiding logical errors caused by misunderstandings during manual conversion; the knowledge tags and preset conversion constraints clearly define the syntactic rules of the target language, ensuring that the generated target code naturally conforms to the syntactic specifications of the target language, reducing syntactic errors during manual writing, and guaranteeing the accuracy of the conversion results.
[0066] Optionally, in one embodiment, such as Figure 2 As shown, a method for constructing the target abstract syntax tree of the code to be rewritten is provided, which specifically includes the following steps:
[0067] S201, semantic extraction and syntax analysis are performed on the source code file to obtain the initial abstract syntax tree.
[0068] The initial abstract syntax tree includes nodes and the connecting edges between nodes.
[0069] Optionally, a first programming language parser can be used to perform lexical and syntactic analysis on the source code, constructing an initial abstract syntax tree based on the source language's grammatical rules. Nodes represent syntactic elements of the source code, such as function definition nodes, variable assignment nodes, conditional statement nodes, etc., and contain the specific attributes of that element. Connecting edges represent the syntactic relationships between nodes, reflecting the nesting and inclusion logic of the code.
[0070] Furthermore, based on grammatical analysis, semantic information of nodes is extracted. This semantic information serves as additional attributes of the nodes, assisting in the accurate matching of subsequent cross-language rules.
[0071] S202, for each node in the initial abstract syntax tree, query the syntax rules of the second programming language corresponding to the node type from the cross-language knowledge base according to the node type.
[0072] The cross-language knowledge base stores the mapping relationship between the grammar rules of the first language and the second language. The core of it is the correspondence table between the source language node type and the target language grammar rules.
[0073] Optionally, for each node Execute query ,in, It is the node type of the node (such as "For loop", "MySQL connection call"); It retrieves syntax rules or templates related to node types from a cross-language knowledge base, and the retrieval results... .
[0074] S203, the syntax rules corresponding to each node are used as knowledge tags and added to the corresponding nodes in the initial abstract syntax tree to obtain the target abstract syntax tree of the code to be rewritten.
[0075] Optionally, the syntax rules corresponding to each node can be added as knowledge tags to the corresponding nodes in the initial abstract syntax tree to obtain the target abstract syntax tree of the code to be rewritten.
[0076] In this embodiment, the initial abstract syntax tree focuses on preserving the logical structure (nodes and connecting edges) of the source code, while the knowledge tags focus on the grammatical rules of the target language. This separation makes the conversion process more flexible. With the help of a cross-language knowledge base, grammatical rules can be customized for different node types. The knowledge tags of the target abstract syntax tree directly tell subsequent steps how the node should be written in the target language, reducing ambiguity in the conversion process.
[0077] Optionally, in an exemplary embodiment, such as Figure 3 As shown, a method for generating target code files is provided, which specifically includes the following steps:
[0078] S301, perform hierarchical indentation on the nesting relationships between nodes in the target syntax abstraction tree to obtain indented structured text.
[0079] Optionally, nodes in the target syntax abstraction tree are nested through connecting edges (e.g., function nodes contain conditional statement nodes, and conditional statement nodes contain assignment nodes). Hierarchical indentation aims to transform this nesting relationship into a text structure that conforms to the target language's coding style. The indentation amount is determined based on the nesting depth of the nodes (e.g., adding 4 spaces for each nesting level) to ensure that the hierarchical relationship between parent and child nodes is visually represented in the text. Nodes are temporarily represented in the text using node type + core attributes.
[0080] S302, according to the preset splicing rules, splice the knowledge tags of each node in the structured text to obtain the target text.
[0081] The preset splicing rules specify the splicing logic between node labels and parent / child node labels.
[0082] Optionally, each node in the target abstract tree carries a knowledge tag (describing the grammatical rules of the target language). The concatenation rules define how to combine the tag content hierarchically according to the node to form a text fragment that conforms to the grammar of the target language. The knowledge tags of each node are recursively concatenated according to the hierarchical order of the structured text. At this point, the target text is close to the target language code, but there may be unprocessed semantic details (such as type inference and dependency imports).
[0083] For example, the target text can be represented as: The Serialize operation abstracts and linearizes the text into indented structured text; [SEP] is the delimiter; This specifies the predefined concatenation rules. For example, given the following Java method, it needs to be translated into Python:
[0084] public int add(int a, int b) {
[0085] return a + b;
[0086] }
[0087] ASTS' is:
[0088] FunctionDeclaration
[0089] ├── ReturnType: int
[0090] ├── MethodName: add
[0091] ├── Parameters: [a, b]
[0092] └── Body: BinaryExpression (operator: +, operands: [a, b])
[0093] Knowledge tags are:
[0094] Knowledge:
[0095] - Function: Use 'def' instead of 'public'.
[0096] - ReturnType: Omit type declaration (Python dynamic typing).
[0097] - Optional: Add type hints (eg, "-> int").
[0098] Separate with the [SEP] delimiter and add task instructions:
[0099] FunctionDeclaration
[0100] ReturnType: int
[0101] MethodName: add
[0102] Parameters: [a, b]
[0103] Body: BinaryExpression(operator: +, operands: [a, b])
[0104] Knowledge:
[0105] - Function: Use 'def' instead of 'public'.
[0106] - ReturnType: Omit type declaration (Python dynamic typing).
[0107] - Optional: Add type hints (eg, "-> int").
[0108] [SEP]
[0109] Translate the above Java function to Python while following the knowledge rules.
[0110] S303, based on preset conversion constraints and target text, rewrite the code to be modified from the first programming language to a target code file in the second programming language.
[0111] Optionally, the target text needs to be validated and supplemented in conjunction with preset transformation constraints to ensure that the code meets the requirements of semantic equivalence and environment adaptation on the basis of syntactic correctness.
[0112] A pre-trained large language model can be optimized based on preset transformation constraints to obtain a target large language model. The target code is then input into the target large language model to obtain the target code file. That is, the target code file is generated using the pre-trained large language model under grammatical constraints: T=LLM_decoder(P, constraints=GrammarC∧KBC). Here, GrammarC comes from the target language grammar rules learned during the pre-training of the large language model, and KBC comes from the transformation rule constraints in the knowledge base.
[0113] In this embodiment, the nesting relationship of nodes in the target abstract tree is transformed into structured text through hierarchical indentation, making the logical hierarchy of the code clearly visible during the transformation process. Compared with flat text without indentation, hierarchical indentation can effectively prevent the loss of nesting relationships and ensure that the generated target code is consistent with the original code in terms of syntax block structure. The knowledge tags store the grammatical details of the target language, and the preset splicing rules define the combination logic of the tags, so that grammatically correct text fragments can be generated without manual intervention.
[0114] Optionally, in an implementation, such as Figure 4 As shown, a method for verifying target code files is provided, which specifically includes the following steps:
[0115] S401 verifies the target code file for at least one of the following: syntax compliance, semantic equivalence, and domain rule conformance, and obtains the verification result.
[0116] Optionally, the target code file can be verified to conform to the knowledge base rules from three perspectives: syntax compliance, semantic equivalence, and domain rule compliance. Syntax compliance verification involves using the target language's compiler and syntax checking tools to check for syntax errors. Semantic equivalence verification uses unit testing and behavior comparison to verify whether the target code's functionality is consistent with the source code. Domain rule compliance verification checks whether the target code conforms to the specifications or business rules of a specific domain.
[0117] S402, if the verification result is that the verification fails, then update the preset transformation constraints according to the verification result, and return to execute the operation of rewriting the code to be rewritten from the first programming language to the target code in the second programming language according to the target abstract syntax tree and the preset transformation constraints.
[0118] Optionally, if the verification passes, it means the target code file meets the preset standards and can be directly output or used. If the verification fails, it is necessary to analyze the reasons for the failure and locate the root cause of the problem. For example, syntax compliance failure may be due to missing syntax rules of the target language in the transformation constraints. Semantic equivalence failure may be due to incorrect semantic mapping rules. Domain rule failure may be due to domain-specific transformation rules not being included in the constraints.
[0119] Based on the reason for the verification failure, the preset transformation constraints are corrected or supplemented. Using the updated preset transformation constraints, the code generation process is re-executed based on the original target abstract syntax tree to obtain the corrected target code file.
[0120] In this embodiment, syntax compliance verification avoids low-level errors that prevent compilation / running; semantic equivalence verification ensures the functional consistency between the target code and the source code, preventing logical deviations caused by language differences; domain rule compliance verification ensures that the code adapts to specific business scenarios and meets industry standards or performance requirements. The feedback of verification failure directly points to defects in the transformation constraints (rather than accidental errors in a single transformation), and similar problems can be solved by updating the constraints.
[0121] Optionally, in one embodiment, such as Figure 5 As shown, a method for updating preset transformation constraints is provided, which specifically includes the following steps:
[0122] S501, if the verification result is successful, extract new transformation constraints based on the mapping relationship between the source code file and the target code file.
[0123] Optionally, for the verified source code files and target code files, a precise mapping relationship is established for each segment to identify the successful conversion pattern from source language structure to target language structure.
[0124] S502, Update the preset transformation constraints according to the new transformation constraints.
[0125] Optionally, the mapping relationships can be stored in a structured manner (e.g., key-value pairs), and the above mapping relationships can be summarized and generalized to extract new, reusable transformation constraints. Integration methods include, but are not limited to, adding new constraints, strengthening existing constraints, deduplication, and priority sorting.
[0126] In this embodiment, the initial preset constraints may only cover common scenarios, while actual code often contains personalized logic. By extracting new constraints from successful conversion cases, the system can gradually learn and adapt to these personalized scenarios, reducing the reliance on manually written rules. The constraints extracted based on the actual verified code are more in line with real development scenarios than the initial preset theoretical rules, ensuring that the updated preset conversion constraints are more accurate.
[0127] Figure 6 This is a flowchart illustrating the code rewriting method in another embodiment. Based on the above embodiments, this embodiment provides an optional example of the code rewriting method. (Combined with...) Figure 6 The specific implementation process is as follows:
[0128] S601, retrieve the source code file of the code to be modified.
[0129] The code to be rewritten is written in the first programming language.
[0130] S602 performs semantic extraction and syntax analysis on the source code file to obtain an initial abstract syntax tree.
[0131] The initial abstract syntax tree includes nodes and the connecting edges between nodes.
[0132] S603, for each node in the initial abstract syntax tree, queries the cross-language knowledge base for the syntax rules of the second programming language corresponding to the node type, based on the node type.
[0133] S604, the syntax rules corresponding to each node are used as knowledge tags and added to the corresponding nodes in the initial abstract syntax tree to obtain the target abstract syntax tree of the code to be rewritten.
[0134] S605 performs hierarchical indentation on the nesting relationships between nodes in the target syntax abstraction tree to obtain indented structured text.
[0135] S606: According to the preset splicing rules, the knowledge tags of each node in the structured text are spliced together to obtain the target text.
[0136] S607, optimize the pre-trained large language model according to the preset transformation constraints to obtain the target large language model.
[0137] S608 inputs the target code into the target large language model to obtain the target code file.
[0138] S609 verifies at least one of the following: syntax compliance, semantic equivalence, and domain rule conformance of the target code file, and obtains the verification result.
[0139] S610, if the verification result is that the verification fails, then update the preset transformation constraints according to the verification result and return to S607.
[0140] S611, if the verification result is successful, output the target code file.
[0141] The specific processes of S601-S611 described above can be found in the description of the above method embodiments. Their implementation principles and technical effects are similar, and will not be repeated here.
[0142] It should be understood that although the steps in the flowcharts of the embodiments described above are shown sequentially according to the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless explicitly stated herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least some steps in the flowcharts of the embodiments described above may include multiple steps or multiple stages. These steps or stages are not necessarily completed at the same time, but can be executed at different times. The execution order of these steps or stages is not necessarily sequential, but can be performed alternately or in turn with other steps or at least some of the steps or stages of other steps.
[0143] Based on the same inventive concept, this application also provides a code rewriting apparatus for implementing the code rewriting method described above. The solution provided by this apparatus is similar to the implementation scheme described in the above method; therefore, the specific limitations in one or more code rewriting apparatus embodiments provided below can be found in the limitations of the code rewriting method described above, and will not be repeated here.
[0144] In one exemplary embodiment, such as Figure 7 As shown, a code rewriting apparatus 700 is provided, including: a file acquisition module 710, a tree construction module 720, and a code rewriting module 730, wherein:
[0145] The file acquisition module 710 is used to acquire the source code file of the code to be rewritten; wherein the code to be rewritten is code written in the first programming language.
[0146] Tree building module 720 is used to generate a target abstract syntax tree for the code to be rewritten based on the source code file. The abstract syntax tree includes nodes of different types, connecting edges between nodes, and knowledge labels for nodes. Different types of nodes represent different attributes of the code to be rewritten, connecting edges between nodes represent grammatical relationships, and knowledge labels for nodes are used to describe the grammatical rules corresponding to the type of node in the second programming language.
[0147] The code rewriting module 730 is used to rewrite the code to be rewritten from the first programming language to the target code file in the second programming language based on the target abstract syntax tree and preset transformation constraints.
[0148] The aforementioned code rewriting device acquires the source code file of the code to be rewritten, wherein the code to be rewritten is written in a first programming language; based on the source code file, it generates a target abstract syntax tree (AST) for the code to be rewritten; wherein the AST includes nodes of different types, connecting edges between nodes, and knowledge tags for the nodes; different types of nodes represent different attributes of the code to be rewritten, connecting edges between nodes represent syntactic relationships, and knowledge tags for nodes describe the syntactic rules corresponding to the node type in a second programming language; based on the target AST and preset conversion constraints, the code to be rewritten is converted from the first programming language into a target code file in the second programming language. In this scheme, the target AST preserves the logical structure of the source code, and combined with preset conversion constraints, it ensures that the converted code is functionally consistent with the original code, avoiding logical errors caused by misunderstandings during manual conversion; the knowledge tags and preset conversion constraints clearly define the syntactic rules of the target language, and the generated target code naturally conforms to the syntactic specifications of the target language, reducing syntactic errors during manual writing and ensuring the accuracy of the conversion results.
[0149] In one embodiment, the tree building module 720 is specifically used for:
[0150] Semantic extraction and syntactic analysis are performed on the source code file to obtain an initial abstract syntax tree (API). The API includes nodes and connecting edges between nodes. For each node in the API, the syntax rules of the second programming language corresponding to the node type are queried from the cross-language knowledge base. The syntax rules corresponding to each node are added as knowledge tags to the corresponding nodes in the API to obtain the target API of the code to be rewritten.
[0151] In one embodiment, the code rewriting module 730 includes:
[0152] The first building unit is used to perform hierarchical indentation on the nesting relationships between nodes in the target syntax abstraction tree, resulting in indented structured text.
[0153] The second building unit is used to splice the knowledge tags of each node in the structured text according to the preset splicing rules to obtain the target text.
[0154] The code rewriting unit is used to rewrite the code to be rewritten from the first programming language to the target code file in the second programming language according to preset transformation constraints and target text.
[0155] In one embodiment, the code rewriting unit is specifically used for:
[0156] Based on preset transformation constraints, the pre-trained large language model is optimized to obtain the target large language model; the target code is input into the target large language model to obtain the target code file.
[0157] In one embodiment, the code rewriting apparatus 700 further includes a verification module for:
[0158] The target code file is validated for at least one of the following: syntax compliance, semantic equivalence, and domain rule compliance. If the validation result is that the validation fails, the preset transformation constraints are updated according to the validation result, and the operation of rewriting the code to be rewritten from the first programming language to the target code in the second programming language is returned to be executed according to the target abstract syntax tree and the preset transformation constraints.
[0159] In one embodiment, the code rewriting device 700 is further configured to:
[0160] If the verification result is successful, extract new transformation constraints based on the mapping relationship between the source code file and the target code file; update the preset transformation constraints based on the new transformation constraints.
[0161] Each module in the aforementioned code rewriting device 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 as software, so that the processor can call and execute the operations corresponding to each module.
[0162] In one exemplary embodiment, a computer device is provided, which may be a server, and its internal structure diagram may be as follows: Figure 8 As shown, this computer device includes a processor, memory, input / output (I / O) interfaces, and a communication interface. The processor, memory, and I / O interfaces are connected via a system bus, and the communication interface is also connected to the system bus via the I / O interfaces. The processor provides computational and control capabilities. The memory includes non-volatile storage media and internal memory. The non-volatile storage media stores the operating system, computer programs, and databases. The internal memory provides the environment for the operating system and computer programs stored in the non-volatile storage media to run. The I / O interfaces are used for exchanging information between the processor and external devices. The communication interface is used for communicating with external terminals via a network connection. When the computer program is executed by the processor, it implements a code rewriting method.
[0163] Those skilled in the art will understand that Figure 8The structure shown is merely a block diagram of a portion of the structure related to the present application and does not constitute a limitation on the computer device to which the present application is applied. Specific computer devices may include more or fewer components than those shown in the figure, or combine certain components, or have different component arrangements.
[0164] In one exemplary embodiment, a computer device is provided, including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to perform the following steps:
[0165] Obtain the source code file of the code to be rewritten; wherein, the code to be rewritten is written in the first programming language;
[0166] Based on the source code file, a target abstract syntax tree is generated for the code to be rewritten. The abstract syntax tree includes nodes of different types, connecting edges between nodes, and knowledge labels for nodes. Different types of nodes represent different attributes of the code to be rewritten, connecting edges between nodes represent syntactic relationships, and knowledge labels for nodes are used to describe the syntactic rules corresponding to the node type in the second programming language.
[0167] Based on the target abstract syntax tree and preset transformation constraints, the code to be rewritten is transformed from the first programming language into a target code file in the second programming language.
[0168] In one embodiment, when the processor executes a computer program to generate a target abstract syntax tree for the code to be rewritten based on the source code file, it also performs the following steps:
[0169] Semantic extraction and syntactic analysis are performed on the source code file to obtain an initial abstract syntax tree (API). The API includes nodes and connecting edges between nodes. For each node in the API, the syntax rules of the second programming language corresponding to the node type are queried from the cross-language knowledge base. The syntax rules corresponding to each node are added as knowledge tags to the corresponding nodes in the API to obtain the target API of the code to be rewritten.
[0170] In one embodiment, when the processor executes a computer program to rewrite the code to be rewritten from a first programming language to a target code file in a second programming language based on a target abstract syntax tree and preset transformation constraints, the following steps are also performed:
[0171] Hierarchical indentation is applied to the nested relationships between nodes in the target syntax abstract tree to obtain indented structured text; knowledge tags of each node in the structured text are concatenated according to preset concatenation rules to obtain target text; and the code to be rewritten is rewritten from the first programming language to the target code file in the second programming language according to preset transformation constraints and target text.
[0172] In one embodiment, when the processor executes a computer program to rewrite the code to be rewritten from a first programming language to a target code file in a second programming language according to preset transformation constraints and target text, the following steps are also performed:
[0173] Based on preset transformation constraints, the pre-trained large language model is optimized to obtain the target large language model; the target code is input into the target large language model to obtain the target code file.
[0174] In one embodiment, the processor, when executing a computer program, also performs the following steps:
[0175] The target code file is validated for at least one of the following: syntax compliance, semantic equivalence, and domain rule compliance. If the validation result is that the validation fails, the preset transformation constraints are updated according to the validation result, and the operation of rewriting the code to be rewritten from the first programming language to the target code in the second programming language is returned to be executed according to the target abstract syntax tree and the preset transformation constraints.
[0176] In one embodiment, the processor, when executing a computer program, also performs the following steps:
[0177] If the verification result is successful, extract new transformation constraints based on the mapping relationship between the source code file and the target code file; update the preset transformation constraints based on the new transformation constraints.
[0178] In one embodiment, a computer-readable storage medium is provided having a computer program stored thereon, the computer program performing the following steps when executed by a processor:
[0179] Obtain the source code file of the code to be rewritten; wherein, the code to be rewritten is written in the first programming language;
[0180] Based on the source code file, a target abstract syntax tree is generated for the code to be rewritten. The abstract syntax tree includes nodes of different types, connecting edges between nodes, and knowledge labels for nodes. Different types of nodes represent different attributes of the code to be rewritten, connecting edges between nodes represent syntactic relationships, and knowledge labels for nodes are used to describe the syntactic rules corresponding to the node type in the second programming language.
[0181] Based on the target abstract syntax tree and preset transformation constraints, the code to be rewritten is transformed from the first programming language into a target code file in the second programming language.
[0182] In one embodiment, when the processor executes a computer program to generate a target abstract syntax tree for the code to be rewritten based on the source code file, it also performs the following steps:
[0183] Semantic extraction and syntactic analysis are performed on the source code file to obtain an initial abstract syntax tree (API). The API includes nodes and connecting edges between nodes. For each node in the API, the syntax rules of the second programming language corresponding to the node type are queried from the cross-language knowledge base. The syntax rules corresponding to each node are added as knowledge tags to the corresponding nodes in the API to obtain the target API of the code to be rewritten.
[0184] In one embodiment, when the processor executes a computer program to rewrite the code to be rewritten from a first programming language to a target code file in a second programming language based on a target abstract syntax tree and preset transformation constraints, the following steps are also performed:
[0185] Hierarchical indentation is applied to the nested relationships between nodes in the target syntax abstract tree to obtain indented structured text; knowledge tags of each node in the structured text are concatenated according to preset concatenation rules to obtain target text; and the code to be rewritten is rewritten from the first programming language to the target code file in the second programming language according to preset transformation constraints and target text.
[0186] In one embodiment, when the processor executes a computer program to rewrite the code to be rewritten from a first programming language to a target code file in a second programming language according to preset transformation constraints and target text, the following steps are also performed:
[0187] Based on preset transformation constraints, the pre-trained large language model is optimized to obtain the target large language model; the target code is input into the target large language model to obtain the target code file.
[0188] In one embodiment, the processor, when executing a computer program, also performs the following steps:
[0189] The target code file is validated for at least one of the following: syntax compliance, semantic equivalence, and domain rule compliance. If the validation result is that the validation fails, the preset transformation constraints are updated according to the validation result, and the operation of rewriting the code to be rewritten from the first programming language to the target code in the second programming language is returned to be executed according to the target abstract syntax tree and the preset transformation constraints.
[0190] In one embodiment, the processor, when executing a computer program, also performs the following steps:
[0191] If the verification result is successful, extract new transformation constraints based on the mapping relationship between the source code file and the target code file; update the preset transformation constraints based on the new transformation constraints.
[0192] In one embodiment, a computer program product is provided, including a computer program that, when executed by a processor, performs the following steps:
[0193] Obtain the source code file of the code to be rewritten; wherein, the code to be rewritten is written in the first programming language;
[0194] Based on the source code file, a target abstract syntax tree is generated for the code to be rewritten. The abstract syntax tree includes nodes of different types, connecting edges between nodes, and knowledge labels for nodes. Different types of nodes represent different attributes of the code to be rewritten, connecting edges between nodes represent syntactic relationships, and knowledge labels for nodes are used to describe the syntactic rules corresponding to the node type in the second programming language.
[0195] Based on the target abstract syntax tree and preset transformation constraints, the code to be rewritten is transformed from the first programming language into a target code file in the second programming language.
[0196] In one embodiment, when the processor executes a computer program to generate a target abstract syntax tree for the code to be rewritten based on the source code file, it also performs the following steps:
[0197] Semantic extraction and syntactic analysis are performed on the source code file to obtain an initial abstract syntax tree (API). The API includes nodes and connecting edges between nodes. For each node in the API, the syntax rules of the second programming language corresponding to the node type are queried from the cross-language knowledge base. The syntax rules corresponding to each node are added as knowledge tags to the corresponding nodes in the API to obtain the target API of the code to be rewritten.
[0198] In one embodiment, when the processor executes a computer program to rewrite the code to be rewritten from a first programming language to a target code file in a second programming language based on a target abstract syntax tree and preset transformation constraints, the following steps are also performed:
[0199] Hierarchical indentation is applied to the nested relationships between nodes in the target syntax abstract tree to obtain indented structured text; knowledge tags of each node in the structured text are concatenated according to preset concatenation rules to obtain target text; and the code to be rewritten is rewritten from the first programming language to the target code file in the second programming language according to preset transformation constraints and target text.
[0200] In one embodiment, when the processor executes a computer program to rewrite the code to be rewritten from a first programming language to a target code file in a second programming language according to preset transformation constraints and target text, the following steps are also performed:
[0201] Based on preset transformation constraints, the pre-trained large language model is optimized to obtain the target large language model; the target code is input into the target large language model to obtain the target code file.
[0202] In one embodiment, the processor, when executing a computer program, also performs the following steps:
[0203] The target code file is validated for at least one of the following: syntax compliance, semantic equivalence, and domain rule compliance. If the validation result is that the validation fails, the preset transformation constraints are updated according to the validation result, and the operation of rewriting the code to be rewritten from the first programming language to the target code in the second programming language is returned to be executed according to the target abstract syntax tree and the preset transformation constraints.
[0204] In one embodiment, the processor, when executing a computer program, also performs the following steps:
[0205] If the verification result is successful, extract new transformation constraints based on the mapping relationship between the source code file and the target code file; update the preset transformation constraints based on the new transformation constraints.
[0206] It should be noted that the data involved in this application (including but not limited to data used for analysis, data stored, data displayed, etc.) are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of the relevant data must comply with relevant regulations.
[0207] Those skilled in the art will understand that all or part of the processes in 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. When executed, the computer program can include the processes of the embodiments described above. Any references to memory, 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, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetic random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. By way of illustration and not limitation, RAM can take many forms, such as Static Random Access Memory (SRAM) or Dynamic Random Access Memory (DRAM). The databases involved in the embodiments provided in this application may include at least one type of relational database and non-relational database. Non-relational databases may include, but are not limited to, blockchain-based distributed databases. The processors involved in the embodiments provided in this application may be general-purpose processors, central processing units, graphics processing units, digital signal processors, programmable logic devices, quantum computing-based data processing logic devices, etc., and are not limited to these.
[0208] The technical features of the above embodiments can be combined in any way. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0209] The embodiments described above are merely illustrative of several implementation methods of this application, and while the descriptions are specific and detailed, they should not be construed as limiting the scope of this patent application. It should be noted that those skilled in the art can make various modifications and improvements without departing from the concept of this application, and these all fall within the protection scope of this application. Therefore, the protection scope of this application should be determined by the appended claims.
Claims
1. A code rewriting method, characterized in that, The method includes: Obtain the source code file of the code to be rewritten; wherein the code to be rewritten is written in a first programming language; Based on the source code file, a target abstract syntax tree is generated for the code to be rewritten; wherein, the abstract syntax tree includes nodes of different types, connecting edges between nodes, and knowledge labels for nodes; different types of nodes represent different attributes of the code to be rewritten, connecting edges between nodes represent grammatical relationships, and knowledge labels for nodes are used to describe the grammatical rules corresponding to the type of the node in a second programming language; Based on the target abstract syntax tree and preset transformation constraints, the code to be rewritten is rewritten from the first programming language to a target code file in the second programming language.
2. The method according to claim 1, characterized in that, The step of generating the target abstract syntax tree of the code to be rewritten based on the source code file includes: Semantic extraction and syntactic analysis are performed on the source code file to obtain an initial abstract syntax tree; wherein, the initial abstract syntax tree includes nodes and connecting edges between nodes; For each node in the initial abstract syntax tree, based on the node type, the syntax rules of the second programming language corresponding to the node type are queried from the cross-language knowledge base; The syntax rules corresponding to each node are used as knowledge tags and added to the corresponding nodes in the initial abstract syntax tree to obtain the target abstract syntax tree of the code to be rewritten.
3. The method according to claim 1, characterized in that, The step of rewriting the code to be rewritten from the first programming language to a target code file in the second programming language based on the target abstract syntax tree and preset transformation constraints includes: Hierarchical indentation is applied to the nesting relationships between nodes in the target syntax abstraction tree to obtain indented structured text; According to the preset splicing rules, the knowledge tags of each node in the structured text are spliced together to obtain the target text; Based on preset conversion constraints and the target text, the code to be rewritten is rewritten from the first programming language to a target code file in the second programming language.
4. The method according to claim 3, characterized in that, The step of rewriting the code to be rewritten from the first programming language to a target code file in the second programming language according to preset conversion constraints and the target text includes: Based on preset transformation constraints, the pre-trained large language model is optimized to obtain the target large language model; The target code is input into the target large language model to obtain the target code file.
5. The method according to claim 1, characterized in that, The method further includes: The target code file is verified for at least one of the following: syntax compliance, semantic equivalence, and domain rule conformity, and the verification result is obtained. If the verification result is that the verification fails, the preset transformation constraints are updated according to the verification result, and the operation of rewriting the code to be rewritten from the first programming language to the target code in the second programming language according to the target abstract syntax tree and the preset transformation constraints is returned.
6. The method according to claim 5, characterized in that, The method further includes: If the verification result is successful, new transformation constraints are extracted based on the mapping relationship between the source code file and the target code file; The preset transformation constraints are updated based on the new transformation constraints.
7. A code rewriting device, characterized in that, The device includes: The file acquisition module is used to acquire the source code file of the code to be rewritten; wherein the code to be rewritten is code written in a first programming language; A tree construction module is used to generate a target abstract syntax tree for the code to be rewritten based on the source code file; wherein, the abstract syntax tree includes nodes of different types, connecting edges between nodes, and knowledge labels of nodes; different types of nodes represent different attributes of the code to be rewritten, connecting edges between nodes represent grammatical relationships, and knowledge labels of nodes are used to describe the grammatical rules corresponding to the type of the node in a second programming language; The code rewriting module is used to rewrite the code to be rewritten from the first programming language to a target code file in the second programming language based on the target abstract syntax tree and preset transformation constraints.
8. 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 6.
9. 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 6.
10. A computer program product, comprising a computer program, 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 6.