Method for translating code by language model

By extracting fragments from the code and integrating the conversion results after meeting the defined requirements, the problem of language models generating errors in code conversion is solved, achieving code conversion with higher accuracy and reliability.

CN121764482APending Publication Date: 2026-03-31ROBERT BOSCH GMBH
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-09-29
Publication Date
2026-03-31

AI Technical Summary

Technical Problem

Existing language models are prone to errors during code conversion, especially in security-related systems, and are difficult to verify effectively within an acceptable timeframe, resulting in code quality that does not meet expectations.

Method used

By extracting code snippets from the code, transforming and verifying them using a language model, the transformation results are only integrated into the original code if the defined requirements are met, and the output of the language model can be iteratively improved.

Benefits of technology

It improves the accuracy and reliability of code conversion, reduces the occurrence of errors, ensures that the conversion results meet semantic consistency and syntactic correctness, and satisfy security and quality standards.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121764482A_ABST
    Figure CN121764482A_ABST
Patent Text Reader

Abstract

The invention relates to a method (100) for converting a code by means of a language model (50), comprising the following steps:-extracting (101) a code snippet to be converted from the code; -transforming (102) the extracted code snippets by means of a language model (50); -checking (103) the translated code snippets on the basis of at least one defined requirement; and if the result of the verification (103) indicates that the at least one defined requirement is met, integrating (104) the translated code snippets into the code. The invention further relates to a computer program, an apparatus and a storage medium for this purpose.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to a method for converting code using a language model. Furthermore, this invention relates to computer programs, apparatus, and storage media used for this purpose. Background Technology

[0002] A Large Language Model (LLM) can perform tasks on existing program code with the help of corresponding text prompts. For example, it can improve code, eliminate errors, or translate code from one programming language to another. However, the results provided by an LLM are often incorrect or do not meet the expected quality standards (keyword: illusion). For example, refactoring is supposed to improve the quality of code structure while maintaining behavior (e.g., in terms of readability or maintainability). But if refactoring is performed by an LLM, the behavior often changes—making it no longer a refactoring. In this way, subtle errors are introduced in some cases, which later cause problems and must be corrected again in a costly manner. The larger the input (the code to be processed in this case), the more frequently the LLM provides such incorrect results.

[0003] Especially in security-related systems, ensuring correctness is a critical challenge. This technology cannot be used in the development of such systems without established safeguards.

[0004] For an LLM to correctly perform the tasks described above, it needs a certain amount of context. This includes, for example, declarations of program structures used in the corresponding code. Therefore, simply providing the functions involved is insufficient—the types, variables, and other declarations used within those functions must also be provided. This significantly increases the required context input size, potentially by an order of magnitude. Consequently, the LLM reaches its limits more quickly and cannot focus on the truly relevant parts, which typically consist of only a few lines of code. This, in turn, increases the error rate.

[0005] On the other hand, functions often contain complex control structures (such as nested loops and branches), which can cause expansion problems when verified using formal methods. Therefore, in many cases, formal methods cannot provide verification results within an acceptable timeframe (e.g., a few minutes). Summary of the Invention

[0006] The subject matter of this invention is a method having the features of claim 1, a computer program having the features of claim 10, an apparatus having the features of claim 11, and a computer-readable storage medium having the features of claim 12. Other features and details of the invention derive from the corresponding dependent claims, the description, and the drawings. Herein, the features and details described in conjunction with the method according to the invention also apply in conjunction with the computer program according to the invention, the apparatus according to the invention, and the computer-readable storage medium according to the invention, and vice versa, so that mutual reference is always possible in all aspects of the disclosure of this invention.

[0007] The subject of this invention is, in particular, a method for transforming code using a language model, the method comprising the following steps:

[0008] - Extract code snippets to be transformed from the code, where the code snippets are determined manually or automatically, for example, based on errors during code execution, or in order to perform code snippet refactoring, such as to improve the readability or maintainability of the code snippets.

[0009] - Transform the extracted code snippet through a language model, wherein the extracted code snippet is improved within the scope of the transformation, for example, in at least one feature, such as conciseness or maintainability, and / or at least one error in the extracted code snippet is eliminated, and / or the extracted code snippet is translated from one programming language to another.

[0010] - The transformed code snippet is examined based on at least one defined requirement, wherein preferably at least one or more examination methods can be performed, wherein, for example, the following can be examined: whether errors in the original extracted code snippet still exist and / or whether the transformed code snippet causes an error message and / or whether the transformed code snippet produces the same output as the original extracted code snippet.

[0011] If the test results indicate that at least one of the defined requirements is met, then the transformed code snippet is integrated into the code, that is, in particular, into the original code.

[0012] In extracting code snippets for conversion from the original code, it is preferable not to simply extract, for example, the relevant lines, but also the necessary context. Therefore, the extracted code snippets must, in particular, contain relevant code and be translatable. According to the invention, the extracted code snippets can thus be isolated into a translatable form and converted separately by the language model. This is particularly advantageous when the amount of code from which code snippets are extracted is very large. Because the focus is on that specific domain, this isolation allows for more targeted handling and reduction of errors. Formal verification specifically ensures that the conversion meets at least one defined requirement and generates correct code. Only when the verification is successful is the converted code snippet preferably integrated into the original code, thereby improving the accuracy of the entire conversion process.

[0013] At least one of the defined requirements may include, for example, semantic consistency and / or equivalence, i.e., the same behavior as the original extracted code snippet, syntactic correctness, and / or at least one rule-based code constraint on the code (e.g., based on code standards or other contexts).

[0014] Furthermore, it is possible that if the result of the verification indicates that at least one of the defined requirements is not met, the transformation and verification steps are repeated until the result of the verification indicates that at least one of the defined requirements is met. Therefore, by repeatedly performing the transformation and verification, it can be ensured that the transformed code snippet conforms to at least one defined requirement. This particularly improves the reliability of the resulting code and can reduce errors that may arise due to insufficient transformation results.

[0015] Alternatively, it is also conceivable that the transformations, performed separately, may further include the following steps:

[0016] - Determine a text prompt, the text prompt including the respective previous results of the transformation and the test, wherein a correction of the respective previous results of the transformation is initiated through the language model based on the text prompt.

[0017] This, in particular, enables the language model to iteratively improve its output. Furthermore, the combination of the transformed code snippets and the results of verification allows for incremental fine-tuning of the language model. This can lead to higher accuracy and reliability during transformation.

[0018] Furthermore, within the scope of this invention, extraction may include the following steps:

[0019] - Determine the abstract syntax tree (AST) of the code;

[0020] - Especially the abstract syntax tree determined by analyzing the contextual information in the code of the extracted code snippets;

[0021] - Based on the analysis results, at least one supplement is inserted into the extracted code snippet, specifically to provide contextual information that enables isolated translation, verification, and / or execution of the extracted code snippet in a coded sense.

[0022] Abstract syntax trees (ASTs) are data structures that can be used to represent the abstract syntactic constructions of program code. They are preferably tree structures that represent code in a hierarchical manner and allow for analysis and processing of the code at an abstract level. An AST is generated, for example, by the parser of a compiler or interpreter, and specifically includes all information about the structure of the code, including the arrangement of expressions, statements, and operators.

[0023] Optionally, it can be specified that analyzing the determined abstract syntax tree includes the following steps:

[0024] - Analyze the abstract syntax tree to determine the nodes in the abstract syntax tree assigned to the extracted code snippets;

[0025] - Determine the parent node of the identified node in the extracted code snippet;

[0026] - Identify the nodes in the abstract syntax tree that are located below the parent node and do not belong to the identified nodes of the extracted code fragment.

[0027] This specifically enables the language model to accurately determine the code to be converted, thereby qualitatively improving the conversion. Furthermore, the analysis of the abstract syntax tree allows for a better understanding of the context of the code segment to be converted, which also improves the accuracy of the conversion.

[0028] Alternatively, it is also conceivable to insert at least one supplement into the extracted code snippet, including at least one of the following steps:

[0029] - Insert manually generated code so that the runtime behavior of the extracted code snippets is adapted to the code, especially the original code;

[0030] - Insert declarations from the code related to the extracted code snippet.

[0031] This allows the extracted code snippets to be precisely adapted to the original behavior in the code's environment, enabling more accurate transformations through language models and allowing analysis through static or dynamic methods.

[0032] In another possibility, it can be specified that the method is executed automatically, and that the at least one requirement includes at least one requirement from a standard, particularly MISRA-C. MISRA-C is specifically a C programming standard for the automotive industry developed by the UK-based MISRA (Motor Industry Software Reliability Association). Including standards such as MISRA-C ensures, in particular, that the resulting code also meets common safety and quality specifications. This improves the reliability and safety of the resulting code.

[0033] Furthermore, extraction can be performed based on errors in the technical system. In this case, the at least one defined requirement may involve at least eliminating the errors in the technical system. In other words, the corresponding code snippet is extracted, which causes the error in the technical system, and the at least one defined requirement can be used to verify whether the error has been eliminated.

[0034] The subject of this invention is also a computer program, and more particularly a computer program product, which contains instructions that, when executed by a computer, cause the computer to perform the method according to the invention. Therefore, the computer program according to the invention provides the same advantages as those already described in detail with reference to the method according to the invention.

[0035] The subject of this invention is also an apparatus for performing data processing, configured to execute the method according to the invention. For example, a computer may be provided as the apparatus, executing a computer program according to the invention. The computer may have at least one processor for executing the computer program. A non-volatile data memory may also be provided, in which the computer program is stored, and which can be read from the non-volatile data memory by the processor for execution.

[0036] The subject of this invention can also be a computer-readable storage medium having a computer program according to the invention and / or including instructions that, when executed by a computer, cause the computer to perform the method according to the invention. This storage medium can, for example, be configured as a data storage device, such as a hard disk and / or non-volatile memory and / or a memory card. The storage medium can, for example, be integrated into a computer.

[0037] Furthermore, the method according to the invention can also be implemented as a computer-based method. Alternatively or additionally, at least one of the disclosed method steps can be computer-based and / or can be executed automatically. Attached Figure Description

[0038] Other advantages, features, and details of the invention will become apparent from the following description, in which embodiments of the invention are described in detail with reference to the accompanying drawings. Here, features mentioned in the claims and in the specification are important to the invention, either individually or in any combination. Wherein:

[0039] Figure 1 The diagram illustrates a method, technical system, language model, apparatus, storage medium, and computer program according to embodiments of the present invention.

[0040] Figure 2 A schematic diagram of an abstract syntax tree according to an embodiment of the present invention is shown.

[0041] Figure 3 A schematic diagram of an abstract syntax tree according to an embodiment of the present invention is shown.

[0042] Figure 4 A schematic diagram of an abstract syntax tree according to an embodiment of the present invention is shown.

[0043] Figure 5 A schematic diagram of an abstract syntax tree according to an embodiment of the present invention is shown.

[0044] Figure 6 A schematic diagram of an abstract syntax tree according to an embodiment of the present invention is shown. Detailed Implementation

[0045] exist Figure 1 The diagram schematically illustrates a method 100, a technical system 11, a language model 50, an apparatus 10, a storage medium 15, and a computer program 20 according to an embodiment of the present invention.

[0046] Figure 1 Specifically, a method 100 for converting code using a language model 50 is shown. In a first step 101, a code segment to be converted is extracted from the code. In a second step 102, the extracted code segment is converted using the language model 50. In a third step 103, the converted code segment is tested based on at least one defined requirement. In a fourth step 104, if the result of test 103 indicates that at least one defined requirement is met, the converted code segment is integrated into the code.

[0047] Figure 2 This shows node 2 of the abstract syntax tree 1, which is determined for the relevant lines of code. The set K includes these determined nodes 2.

[0048] Figure 3 The common parent node p of the determined node 2 is shown.

[0049] Figure 4The diagram schematically shows node 2' whose entire subtree is not in K. The set N includes these nodes 2'.

[0050] Figure 5 The diagram illustrates the mappings between subtrees p and q, and between q and r. Specifically, p is the subtree of the original code, q is the subtree of the isolated code, and r is the subtree of the transformed code.

[0051] Figure 6 The code obtained based on the subtree is shown. In this case, specifically, r is inserted instead of p, and y is inserted instead of x. Similarly, the node from r is preferably replaced by the subtree of p (not shown).

[0052] In the following detailed description, refer again Figures 2 to 6 .

[0053] According to an embodiment of the invention, the code portion related to the change is isolated before actual processing. Then, in particular, a language model-based transformation is performed. Subsequently, the result is preferably verified based on at least one defined requirement, i.e., guaranteed by means of a formal method. Finally, the changed, i.e., in particular the transformed code fragment, is preferably incorporated into the original code.

[0054] The problem (commonly known as code) is specifically simplified into code snippets related to changes. This allows the language model to focus more on these code snippets and thus provide better results. Formal methods, i.e., verification, are particularly advantageous due to the low complexity of the code snippets and avoid expansion problems.

[0055] The input data is preferably translatable code, and the modification tasks that should be performed on specific parts of the code, i.e., code fragments, by the language model. Furthermore, a formal method or executable tool for implementing this method is preferably provided, which can check the quality of the results.

[0056] The lines involved in the code snippet are preferably extracted and supplemented by analyzing the abstract syntax tree 1 (AST) of the code to form valid code that includes these lines again. To do this, the AST 1 can first be determined for a given (total) input code. Subsequently, nodes 2 of the AST 1 belonging to the lines of the code snippet can be determined. These nodes specifically constitute a set K (see...). Figure 2 Subsequently, preferably, the (first) common parent node p of all nodes in K is determined (see...). Figure 3 The parent node is preferably a statement (i.e., preferably an expression). Subsequently, the node 2' of the abstract syntax tree below the parent node p is preferably determined, from which the entire subtree is no longer in K (see...). Figure 4These elements or nodes 2' specifically constitute set N, and are preferably replaced by manually generated code in the steps described below. Then, preferably, functions with corresponding interfaces are generated around the code attached to the parent node p. The code belonging to node 2 attached to p can then be inserted into N up to node 2'. At their positions, manually generated code is preferably inserted, which marks the arrival of that position at runtime, for example by setting variables to unique constant values. Furthermore, any necessary declarations can be inserted, which appear before the functions in the thus generated code.

[0057] Then, preferably, a language model-based transformation is performed on the extracted code snippets that are thus isolated.

[0058] Subsequently, the result of the language model-based conversion is preferably examined. If the examination fails, this is preferably fed back to the language model 50, prompting a correction. The response of the language model 50 can then be used to re-execute the language model-based conversion.

[0059] If the verification is successful, the changes made by language model 50 to the isolated code segment are preferably applied to the original code, i.e., integrated into the original code. Here, the abstract syntax tree 1 of the original function, node p, the abstract syntax tree 1' of the extracted function, and the abstract syntax tree 1 of the function modified by language model 50 are preferably calculated (see...). Figure 5 Furthermore, preferably, the node q in abstract syntax tree 1' corresponding to node p in abstract syntax tree 1 and the node r in abstract syntax tree 1" corresponding to node q in abstract syntax tree 1' are determined (see [link to abstract syntax tree 1]). Figure 5 (yellow nodes). This is done, for example, by node type and position in the abstract syntax tree. Subsequently, the commonalities and differences between subtrees p and q can be determined (see...). Figure 5 (Left side). Here, the mapping Tba from the inserted placeholder, i.e., the artificially generated code, to the original code (from abstract syntax tree 1), can be determined. Furthermore, the commonalities and differences between subtrees q and r can be determined (see...). Figure 5 (Right side). Thus, in particular, the mapping Tcb from placeholders in abstract syntax tree 1” to placeholders in abstract syntax tree 1' can be determined. Then, the transformed code can be generated from abstract syntax tree 1 by traversing and resolving each node n by applying the function Tcb(Tba(n)), provided that the function is defined there (see...). Figure 6 ).

[0060] The method according to an embodiment of the present invention is described below with reference to examples.

[0061] Given the following C code:

[0062]

[0063] For example, MISRA-C requires that the types on both sides of an operator be the same during mathematical operations. In this example, `val` is an unsigned integer, but the literal 0 is an integer (by default). To eliminate this problem, it is preferable to isolate the corresponding lines. Starting from the expression `val != 0`, the `if` statement can be determined as node `p`. The entire `then` block and the `else` blocks ("more code B" and "more code C") can be determined as set `N`. Now, it is preferable to generate a new function that exactly includes this code:

[0064]

[0065] Preferably, statements about `ret` are generated for the nodes in N. These statements, especially in the case of formal verification, help characterize the behavior. Furthermore, declarations of `val` can be generated. A language model-based transformation at this node can lead to the following results:

[0066]

[0067]

[0068] Now, we can verify and conclude that the code no longer includes the original problem. In the next step, we can now incorporate the code changes into the original code:

[0069]

[0070] The above description of the embodiments is merely illustrative of the invention. Of course, the various features of the embodiments can be freely combined with each other without departing from the scope of the invention, provided it is technically reasonable.

Claims

1. A method (100) for converting code by means of a language model (50), the method comprising the following steps: - extracting (101) a code fragment to be converted from the code; - converting (102) the extracted code fragment by means of the language model (50); - checking (103) the converted code fragment on the basis of at least one defined requirement; - integrating (104) the converted code fragment into the code if the result of the check (103) indicates that the at least one defined requirement is fulfilled.

2. The method (100) according to claim 1, characterized in that the steps of converting (102) and checking (103) are re-performed if the result of the check (103) indicates that the at least one defined requirement is not fulfilled until the result of the check (103) indicates that the at least one defined requirement is fulfilled.

3. The method (100) according to claim 2, characterized in that the respectively re-performed conversion (102) furthermore comprises the following step: - determining a textual hint, the textual hint comprising the respectively previous result of the conversion (102) and the check (103), wherein a correction of the respectively previous result of the conversion (102) is initiated by means of the language model (50) on the basis of the textual hint.

4. The method (100) according to any one of the preceding claims, characterized in that the at least one defined requirement comprises semantic consistency and / or equivalence, syntactic correctness and / or at least one rule-based restriction of the code.

5. The method (100) according to any one of the preceding claims, characterized in that the extraction (101) comprises the following steps: - determining an abstract syntax tree (1) of the code, - analyzing the determined abstract syntax tree (1), - inserting at least one supplement into the extracted code fragment on the basis of the result of the analysis.

6. The method (100) according to claim 5, characterized in that analyzing the determined abstract syntax tree (1) comprises the following steps: - analyzing the abstract syntax tree (1) in order to determine nodes (2) of the abstract syntax tree (1) which are assigned to the extracted code fragment; - determining parent nodes (p) of the determined nodes (2) of the extracted code fragment; - determining nodes (2) of the abstract syntax tree (1) which are located below the parent nodes (p) and which do not belong to the determined nodes (2) of the extracted code fragment.

7. The method (100) according to claim 5 or 6, characterized in that inserting at least one supplement into the extracted code fragment comprises at least one of the following steps: - inserting artificially generated code in order to adapt the runtime behavior of the extracted code fragment to the code; - inserting a declaration from the code which is related to the extracted code fragment.

8. The method (100) according to any one of the preceding claims, characterized in that the method (100) is performed automatically and the at least one requirement comprises at least one requirement from a standard, in particular MISRA-C.

9. The method (100) according to any one of the preceding claims, characterized in that the extraction (101) is performed based on an error in a technical system (11), and the at least one defined requirement relates at least to eliminating the error in the technical system (11).

10. A computer program (20) comprising instructions which, when the computer program (20) is executed by a computer (10), cause the computer (10) to carry out the method (100) according to any one of the preceding claims.

11. An apparatus (10) for carrying out data processing, the apparatus being set up to carry out the method (100) according to any one of claims 1 to 9.

12. A computer-readable storage medium (15) comprising instructions which, when executed by a computer (10), cause the computer (10) to carry out the method (100) according to any one of claims 1 to 9.