Code language conversion method and device, electronic equipment, storage medium and computer program product
By leveraging a large language model and dependency graph to optimize the conversion process from C to Rust, the problem of poor C2Rust conversion quality is solved, and efficient and secure code migration is achieved.
Patent Information
- Application Number
- CN202511317009.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-09-15
- Publication Date
- 2025-10-17
- Estimated Expiration
- 2045-09-15
AI Technical Summary
In the existing technology, C2Rust has poor standardization in converting C language code into Rust language code, resulting in the quality of the converted Rust language code being difficult to meet actual needs.
The source files are obtained by output logs based on C language projects. The large language model is used to convert basic conversion units such as structures, enumerations, functions, macros, and global variables. A dependency graph is constructed for priority sorting, mapping relationships are obtained, and compilation and testing are performed to improve conversion quality.
It realizes the automatic conversion of C language code into standardized Rust language code, improves migration efficiency and security, reduces the error rate, and realizes large-scale, low-error, controllable and safe human-computer collaborative code conversion.
Smart Images

Figure CN120803467A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present disclosure relates to the technical field of code language conversion, and more particularly, to a code language conversion method and device, an electronic device, a storage medium, and a computer program product. BACKGROUND
[0002] With increasing emphasis on information security and improvement of software engineering quality standards, the Rust language has gradually become a popular choice to replace the C language due to its memory safety, concurrent safety, and other characteristics. However, a large number of infrastructure and underlying systems are currently written in C language, and if a manual migration method is used, it will consume high costs and have low migration efficiency, and new errors are also likely to be introduced.
[0003] C2Rust is a tool for converting C code to Rust code, and its core goal is to maintain the original semantics rather than generating idiomatic or safe Rust code. However, there are some limitations in converting C language code to Rust language code using C2Rust. For example, the generated Rust code usually contains a large number of unsafe blocks that need to be manually refactored to improve safety; C2Rust does not support some features of the C language, such as variable-length arrays, bit fields, and variadic functions; the support for the build system of complex projects is limited, and the generated files (Makefile) need to be manually adjusted; macro expansion can result in generated code that is lengthy and difficult to maintain.
[0004] Therefore, the specification of C2Rust in the related art for converting C language code to Rust language code is poor, which makes the quality of the Rust language code obtained by conversion difficult to meet actual needs. SUMMARY
[0005] The present disclosure provides a code language conversion method, device, electronic device, storage medium, and computer program product to at least solve the problem in the related art that the specification of C2Rust for converting C language code to Rust language code is poor, which makes the quality of the Rust language code obtained by conversion difficult to meet actual needs.
[0006] According to a first aspect of an embodiment of the present disclosure, a code language conversion method is provided, including: obtaining a C language source file based on an output log of a C language project; extracting different types of basic conversion units from the C language source file, wherein the types of the basic conversion units include at least one of a structure, an enumeration, a function, a macro, and a global variable; and converting each type of basic conversion unit using a large language model to obtain a Rust language file corresponding to each type of basic conversion unit.
[0007] Optionally, before converting each type of basic conversion unit by using a large language model to obtain a Rust language file corresponding to each type of basic conversion unit, the method further comprises: constructing a dependency graph between the different types of basic conversion units; and performing conversion priority sorting on the different types of basic conversion units based on the dependency graph to obtain a sorting result, wherein the simpler the corresponding dependency relationship is, the higher the conversion priority of the corresponding basic conversion unit is; and the converting each type of basic conversion unit by using the large language model comprises: converting each type of basic conversion unit based on the sorting result by using the large language model to obtain a Rust language file corresponding to each type of basic conversion unit.
[0008] Optionally, when the basic conversion unit includes a target function and the target function references other basic conversion units, the conversion method further comprises: when the other basic conversion units referenced by the target function have been converted into corresponding Rust language files, obtaining a mapping relationship between the other basic conversion units and the corresponding Rust language files, wherein the other basic conversion units include at least one of the structs, the enumerations, and the functions; and the converting each type of basic conversion unit by using the large language model comprises: converting the target function into a corresponding Rust language file based on the mapping relationship by using the large language model.
[0009] Optionally, after converting each type of basic conversion unit by using a large language model to obtain a Rust language file corresponding to each type of basic conversion unit, the method further comprises: compiling and testing the Rust language file corresponding to the function; and when the compiling and testing fails, modifying the Rust language file corresponding to the function by using the large language model and recompiling and testing the modified Rust language file until the recompiling and testing succeeds.
[0010] Optionally, the extracting different types of basic conversion units from the C language source file comprises: performing abstract syntax tree (AST) analysis on the C language source file to extract the different types of basic conversion units.
[0011] According to a second aspect of the embodiments of the present disclosure, a code language conversion apparatus is provided, comprising: a source file acquisition module configured to acquire a C language source file based on an output log of a C language project; a conversion unit extraction module configured to extract different types of basic conversion units from the C language source file, wherein the types of the basic conversion units comprise at least one of a structure, an enumeration, a function, a macro, and a global variable; and a language conversion module configured to convert each type of the basic conversion units respectively by using a large language model to obtain a Rust language file corresponding to each type of the basic conversion units.
[0012] Optionally, the code language conversion apparatus further comprises: a dependency graph construction module configured to construct a dependency graph between the different types of the basic conversion units; and a sorting module configured to sort the different types of the basic conversion units in conversion priority based on the dependency graph to obtain a sorting result, wherein the simpler the corresponding dependency relationship is, the higher the conversion priority of the corresponding basic conversion unit is; and the language conversion module is configured to convert each type of the basic conversion units respectively by using the large language model based on the sorting result to obtain the Rust language file corresponding to each type of the basic conversion units.
[0013] Optionally, the code language conversion apparatus further comprises: a mapping relationship acquisition module configured to acquire a mapping relationship between the other basic conversion units and the corresponding Rust language files in a case where the other basic conversion units referenced by the target function have been converted into the corresponding Rust language files, wherein the other basic conversion units comprise at least one of the structure, the enumeration, and the function; and the language conversion module is configured to convert the target function into the corresponding Rust language file by using the large language model based on the mapping relationship.
[0014] Optionally, the code language conversion apparatus further comprises: a compilation and testing module configured to compile and test the Rust language file corresponding to the function; and a correction module configured to correct the Rust language file corresponding to the function by using the large language model and recompile and test the corrected Rust language file until the recompilation and testing are successful in a case where the compilation and testing fail.
[0015] Optionally, the conversion unit extraction module is configured to perform abstract syntax tree (AST) analysis on the C language source file to extract the different types of the basic conversion units.
[0016] According to a third aspect of the embodiments of the present disclosure, an electronic device is provided, comprising: a processor; a memory for storing instructions executable by the processor; wherein the processor is configured to execute the instructions to implement the code language conversion method according to the present disclosure.
[0017] According to a fourth aspect of the embodiments of the present disclosure, a computer-readable storage medium is provided, when instructions in the computer-readable storage medium are executed by a processor of an electronic device, the electronic device can execute the code language conversion method according to the present disclosure.
[0018] According to a fifth aspect of the embodiments of the present disclosure, a computer program product is provided, comprising a computer program, when the computer program is executed by a processor, the code language conversion method according to the present disclosure is implemented.
[0019] The embodiments of the present disclosure provide at least the following beneficial effects: In the present disclosure, unlike the conversion method based on syntax rules, by fully utilizing the powerful processing capability of large language models for code, the C language code can be automatically converted into standardized Rust language code, realizing large-scale, low-error, controllable and safe human-machine collaborative code conversion, which can improve the efficiency and safety of migrating old C projects to standardized modern languages.
[0020] It should be understood that the above general description and the following detailed description are only exemplary and explanatory, and cannot limit the present disclosure. BRIEF DESCRIPTION OF DRAWINGS
[0021] The accompanying drawings incorporated in and forming a part of the specification illustrate embodiments consistent with the present disclosure and serve to explain the principles of the present disclosure, and do not constitute an undue limitation on the present disclosure.
[0022] Figure 1 is a flowchart showing a code language conversion method according to an exemplary embodiment of the present disclosure; Figure 2 is a dependency relationship diagram showing different types of basic conversion units according to an exemplary embodiment of the present disclosure; Figure 3 is a specific implementation flowchart showing a code language conversion method according to an exemplary embodiment of the present disclosure; Figure 4 is a block diagram showing a code language conversion device according to an exemplary embodiment of the present disclosure; Figure 5 is a block diagram showing an electronic device according to an exemplary embodiment of the present disclosure. DETAILED DESCRIPTION
[0023] In order for the ordinary person in the art to better understand the technical solutions of the present disclosure, the technical solutions in the embodiments of the present disclosure will be clearly and completely described below with reference to the drawings.
[0024] It should be noted that the terms "first", "second", etc. in the specification and claims of the present disclosure and the above-described drawings are used to distinguish similar objects, and do not necessarily have to be used to describe a specific order or sequence. It should be understood that the data thus used can be interchanged under appropriate circumstances, so that the embodiments of the present disclosure described herein can be implemented in an order other than those illustrated or described herein. The embodiments described in the following embodiments do not represent all embodiments consistent with the present disclosure. Rather, they are merely examples of devices and methods consistent with some aspects of the present disclosure as detailed in the appended claims.
[0025] It should be noted herein that "at least one of a plurality of items" appearing in the present disclosure means that the three types of alternatives including "any one of the plurality of items", "a combination of any two or more of the plurality of items", and "all of the plurality of items" are included. For example, "including at least one of A and B" includes the following three alternatives: (1) including A; (2) including B; (3) including A and B. For another example, "performing at least one of step one and step two" means the following three alternatives: (1) performing step one; (2) performing step two; (3) performing step one and step two.
[0026] Figure 1 is a flowchart showing a conversion method of a code language according to an exemplary embodiment of the present disclosure.
[0027] Referring to Figure 1 In step 101, the C language source file can be acquired based on the output log of the C language project. Exemplarily, the path information of the C language source file can be extracted from the output log of the C language project, and then the C language source file can be read from the path indicated by the path information.
[0028] According to an exemplary embodiment of the present disclosure, the output log of a C language project can be constructed using the make command, and then the output log of the C language project and a preset prompt word can be input into a large language model to obtain a C language file of a specified type. The preset prompt word can be used to indicate that a C language file of a specified type is obtained based on the output log of the C language project. Exemplarily, after the output log of the C language project and the preset prompt word are input into the large language model, the large language model can output the path information of the C language file of the specified type, and then the C language file of the specified type can be read from the path indicated by the path information. The C language file of the specified type can include, but is not limited to: C language source files, C language library files, C language executable files, etc.
[0029] For example, the output log of the constructed C language project can be input into the large language model through the following prompt words: Based on the input log, please analyze and generate binary files, including library files (.a or .so) and executable files (no extension), and output detailed information for each binary file in the following format: { "bin":"binary_name", # The name of the generated binary file, the binary file is a library file or an executable file "type":"exe or lib", # The type of binary file, `exe` indicates an executable file, `lib` indicates a library file "src": ["path / source1.c", "path / source2.c"], # The storage path of the source file required to generate the current binary file. Relative paths can be used "lib": ["lib1.a","lib2.a"] # Generate the library files required for the current binary file }".
[0030] By feeding the output log of the constructed C language project into the large language model through prompt words, the entire C language project structure can be obtained, including but not limited to: source files, library files, executable files, etc. Next, the C language source files involved in the project compilation can be automatically converted, and the compilation parameters of each C language source file can be analyzed and recorded.
[0031] It should be noted that the large language model in the present disclosure can be a large neural network based on deep learning, and it can be obtained by training a large amount of text data. The large language model has the ability to understand and generate natural language, and can be used in text generation, translation, question and answer, etc. task scenarios, and it also has strong semantic understanding ability and context processing ability.
[0032] The large language model in the present disclosure can be obtained by training in the following way: A C language source file sample can be obtained, which can correspond to a Rust language file label. Then, the C language source file sample can be input into the large language model to obtain a converted Rust language file. Next, the loss can be calculated based on the converted Rust language file and the Rust language file label. Then, the parameters of the large language model can be adjusted based on the loss for training.
[0033] In step 102, different types of basic conversion units can be extracted from the C language source file, wherein the types of basic conversion units can include at least one of struct, enumeration, function, macro, and global variable.
[0034] In step 103, the large language model can be used to convert each type of basic conversion unit to obtain a Rust language file corresponding to each type of basic conversion unit.
[0035] It should be noted that the large language model has a limit on the input length, so in the present disclosure, different types of basic conversion units can be extracted from the C language source file, and then each type of basic conversion unit can be input into the large language model for conversion. In this way, the phenomenon of inputting a large amount of data into the large language model at one time can be avoided, and the efficiency and accuracy of code language conversion can be improved.
[0036] According to the exemplary embodiments of the present disclosure, the C language source file can be subjected to Abstract Syntax Tree (AST) analysis to extract different types of basic conversion units. That is, in the present disclosure, the C language source file can be subjected to AST analysis by Clang to extract all basic conversion units such as struct, enumeration, function, macro, and global variable.
[0037] It should be noted that the Abstract Syntax Tree (AST) can be an intermediate representation generated after the source code is subjected to lexical analysis and syntax analysis, which can represent the structure and semantics of the source code in a tree form, rather than simply preserving the text form of the source code. For example, the expression "(a+b) For example, the expression (a * (b + c)) can be parsed by the syntax analyzer. The syntax analyzer can analyze that this is a multiplication expression, in which the left sub-tree is an addition expression: (a + b); and the right sub-tree is a variable c. In this way, by constructing the abstract syntax tree, the compiler can clearly understand the structure and hierarchical relationship of the source code.
[0038] According to an example embodiment of the present disclosure, before converting each type of basic conversion unit by using the large language model, a dependency graph between different types of basic conversion units can also be constructed. Then, based on the dependency graph, the conversion priorities of different types of basic conversion units can be sorted to obtain a sorting result, in which the simpler the corresponding dependency relationship is, the higher the conversion priority of the corresponding basic conversion unit is. That is, in the present disclosure, based on the dependency graph, the reference relationship of different types of basic conversion units can be topologically sorted, and the conversion priority of the basic conversion unit without subsequent dependency relationship can be the highest. Next, based on the sorting result of the conversion priority, the large language model can be used to convert each type of basic conversion unit to obtain the corresponding Rust language file of each type of basic conversion unit.
[0039] An example, Figure 2 is a dependency graph between different types of basic conversion units according to an example embodiment of the present disclosure. Referring to Figure 2 , a total of 4 basic conversion units are shown, which are A, B, C, and D. And A depends on B; B depends on C and D at the same time; C and D do not depend on any basic conversion unit. At this time, since C and D do not depend on any basic conversion unit, the dependency relationship corresponding to C and D is the simplest, and C and D can be converted to code language first. Next, the code language conversion can be performed on B with a more complex dependency relationship. Finally, the code language conversion can be performed on A with the most complex dependency relationship.
[0040] When converting the basic conversion unit to the code language, for the C language macro, the large language model can be used to convert it to a constant or a function in the form of Rust language; for the C language structure and the C language enumeration, the large language model can be used to map them to the corresponding Rust language file.
[0041] According to an example embodiment of the present disclosure, in the case where the basic conversion unit includes a target function, the target function references other basic conversion units, and the other basic conversion units referenced by the target function have been converted to the corresponding Rust language file, the mapping relationship between the other basic conversion units and the corresponding Rust language file can be obtained. The other basic conversion units can include at least one of a structure, an enumeration, and a function. Next, the large language model can be used to convert the target function to the corresponding Rust language file based on the obtained mapping relationship.
[0042] In addition, a mapping relationship between the definition of the C language function and the definition of the converted Rust language function can also be recorded for use next time as a context of the large language model. It should be noted that the function itself can include both the function definition and the entity operation logic. Moreover, the function definition can include but is not limited to the name of the function, the input parameter, and the output parameter.
[0043] According to the example embodiment of the present disclosure, the Rust language file corresponding to the function can also be compiled and tested. Moreover, in the case of failed compilation and testing, the Rust language file corresponding to the function can be corrected by using the large language model and re-compiled and tested after correction, until the re-compiled and tested are successful.
[0044] It should be noted that in the present disclosure, in addition to correcting the Rust language file corresponding to the function by using the large language model, the correction can also be made by artificial intervention, and the like. The present disclosure does not limit the specific correction method, and the foregoing embodiment is only an example of illustration.
[0045] Figure 3 is a specific implementation flowchart showing a code language conversion method according to an example embodiment of the present disclosure.
[0046] Referring to Figure 3 In step 301, the output log of the C language project is built by the make command.
[0047] In step 302, the output log of the C language project and the preset prompt word are input into the large language model to obtain a specified type of C language file. For example, the specified type of C language file can include but is not limited to a C language source file, a C language library file, a C language executable file, and the like, and the extracted C language file can be in a json format.
[0048] In step 303, the C language source file is subjected to abstract syntax tree (AST) analysis to extract different types of basic conversion units, such as a structure, an enumeration, a function, a macro, and a global variable.
[0049] In step 304, a dependency relationship graph between different types of basic conversion units is built.
[0050] In step 305, based on the dependency relationship graph, different types of basic conversion units are subjected to conversion priority sorting, wherein the simpler the corresponding dependency relationship, the higher the conversion priority of the corresponding basic conversion unit.
[0051] In step 306 , the C language macro is converted into a constant or function in the Rust language using the large language model.
[0052] In step 307, the large language model is used to map the C language structure and C language enumeration to corresponding Rust language files.
[0053] In step 308 , the C language function is converted into a Rust language function using the large language model.
[0054] For example, if a function references other basic conversion units, and the other basic conversion units referenced by the function have been converted to corresponding Rust language files, the mapping relationship between the other basic conversion units and the corresponding Rust language files can be obtained. Next, the large language model can be used to convert the function into the corresponding Rust language file based on the obtained mapping relationship.
[0055] In step 309, the Rust language file corresponding to the function is compiled and tested.
[0056] In step 3010, in the event of compilation and testing failure, the Rust language file corresponding to the function is corrected using the large language model and the corrected Rust language file is recompiled and tested until the recompilation and testing are successful.
[0057] In step 3011, it is checked whether all functions have been converted. If all functions have been converted, it is determined that the C language project conversion is complete; otherwise, for the functions that have not been converted, the process returns to step 308.
[0058] Figure 4 2 is a block diagram illustrating a code language conversion apparatus 400 according to an exemplary embodiment of the present disclosure.
[0059] Reference Figure 4 The code language conversion device 400 may include a source file acquisition module 401 , a conversion unit extraction module 402 and a language conversion module 403 .
[0060] The source file acquisition module 401 may acquire the C language source file based on the output log of the C language project.
[0061] The conversion unit extraction module 402 can extract different types of basic conversion units from the C language source file, wherein the type of the basic conversion unit can include at least one of: structure, enumeration, function, macro, and global variable.
[0062] The language conversion module 403 can convert each type of basic conversion unit using a large language model to obtain a Rust language file corresponding to each type of basic conversion unit.
[0063] According to an example embodiment of the present disclosure, the conversion unit extraction module 402 can perform AST analysis on the C language source file to extract different types of basic conversion units.
[0064] According to an example embodiment of the present disclosure, the code language conversion device 400 can further include a dependency graph construction module and a sorting module.
[0065] The dependency graph construction module can construct a dependency graph between different types of basic conversion units. Then, the sorting module can sort the conversion priorities of different types of basic conversion units based on the dependency graph to obtain a sorting result, wherein the simpler the corresponding dependency relationship is, the higher the conversion priority of the corresponding basic conversion unit is. Next, the language conversion module 403 can convert each type of basic conversion unit using a large language model based on the sorting result of the conversion priority to obtain a Rust language file corresponding to each type of basic conversion unit.
[0066] According to an example embodiment of the present disclosure, the code language conversion device 400 can further include a mapping relationship acquisition module.
[0067] In the case where the basic conversion unit includes a target function, the target function references other basic conversion units, and the other basic conversion units referenced by the target function have been converted into corresponding Rust language files, the mapping relationship acquisition module can acquire a mapping relationship between the other basic conversion units and the corresponding Rust language files. The other basic conversion units can include at least one of a structure, an enumeration, and a function. Next, the language conversion module 403 can convert the target function into a corresponding Rust language file based on the acquired mapping relationship using a large language model.
[0068] According to an example embodiment of the present disclosure, the code language conversion device 400 can further include a compilation and testing module and a correction module.
[0069] The compilation and testing module can compile and test the Rust language file corresponding to the function. And in the case where the compilation and testing fails, the correction module can correct the Rust language file corresponding to the function using a large language model and recompile and test the corrected Rust language file until the recompilation and testing succeeds.
[0070] Figure 5 is a block diagram illustrating an electronic device 500 according to an example embodiment of the present disclosure.
[0071] Referring to Figure 5 The electronic device 500 includes at least one memory 501 having instructions stored therein and at least one processor 502 that executes the instructions to perform a method of converting a code language according to an exemplary embodiment of the present disclosure.
[0072] As an example, the electronic device 500 can be a PC computer, a tablet device, a personal digital assistant, a smart phone, or other devices capable of executing the above instructions. Here, the electronic device 500 is not necessarily a single electronic device, but can be a collection of any devices or circuits capable of executing the above instructions (or instruction sets) individually or jointly. The electronic device 500 can also be a part of an integrated control system or a system manager, or can be configured as a portable electronic device that interfaces with a local or remote (e.g., via wireless transmission).
[0073] In the electronic device 500, the processor 502 can include a central processing unit (CPU), a graphics processing unit (GPU), a programmable logic device, a dedicated processor system, a microcontroller, or a microprocessor. As an example and not a limitation, the processor can also include an analog processor, a digital processor, a microprocessor, a multi-core processor, a processor array, a network processor, etc.
[0074] The processor 502 can execute instructions or codes stored in the memory 501, and the memory 501 can also store data. The instructions and data can also be transmitted and received via a network through a network interface device, which can use any known transmission protocol.
[0075] The memory 501 can be integrated with the processor 502, for example, by arranging RAM or flash memory within an integrated circuit microprocessor, etc. In addition, the memory 501 can include a separate device, such as an external disk drive, a storage array, or other storage devices that can be used by any database system. The memory 501 and the processor 502 can be operatively coupled or can communicate with each other, for example, through an I / O port, a network connection, etc., so that the processor 502 can read files stored in the memory.
[0076] In addition, the electronic device 500 can also include a video display (such as a liquid crystal display) and a user interaction interface (such as a keyboard, a mouse, a touch input device, etc.). All components of the electronic device 500 can be connected to each other via a bus and / or a network.
[0077] According to an example embodiment of the disclosure, a computer-readable storage medium can also be provided, which, when instructions in the computer-readable storage medium are executed by a processor of an electronic device, enables the electronic device to perform the code language conversion method described above. Examples of the computer-readable storage medium here include a read-only memory (ROM), a random access programmable read-only memory (PROM), an electrically erasable programmable read-only memory (EEPROM), a random access memory (RAM), a dynamic random access memory (DRAM), a static random access memory (SRAM), a flash memory, a non-volatile memory, a CD-ROM, a CD-R, a CD+R, a CD-RW, a CD+RW, a DVD-ROM, a DVD-R, a DVD+R, a DVD-RW, a DVD+RW, a DVD-RAM, a BD-ROM, a BD-R, a BD-R LTH, a BD-RE, a Blu-ray or an optical disc memory, a hard disk drive (HDD), a solid state drive (SSD), a card memory (such as a multimedia card, a secure digital (SD) card, or an extreme digital (XD) card), a magnetic tape, a floppy disk, a magneto-optical data storage device, an optical data storage device, a hard disk, a solid state disk, and any other device configured to store a computer program and any associated data, data files, and data structures in a non-transitory manner and provide the computer program and any associated data, data files, and data structures to a processor or computer so that the processor or computer can execute the computer program. The computer program in the computer-readable storage medium described above can be run in an environment deployed in a computer device such as a client, a host, an agent device, a server, etc., and in addition, in one example, the computer program and any associated data, data files, and data structures are distributed over a networked computer system so that the computer program and any associated data, data files, and data structures are stored, accessed, and executed by one or more processors or computers in a distributed manner.
[0078] According to an example embodiment of the disclosure, a computer program product can also be provided, which includes a computer program that, when executed by a processor, implements the code language conversion method according to the disclosure.
[0079] The code language conversion method, device, electronic device, storage medium, and computer program product according to the disclosure are different from the conversion mode based on syntax rules. By fully utilizing the powerful processing capability of a large language model for code, the disclosure can automatically convert C language code into standardized Rust language code, realizing large-scale, low-error, controllable, and safe human-machine collaborative code conversion, which can improve the efficiency and safety of migrating old C projects to standardized modern languages.
[0080] According to the example embodiments of the present disclosure, by extracting different types of basic conversion units from the C language source file and inputting each type of basic conversion unit into a large language model for conversion respectively, the phenomenon of inputting a large amount of data all at once into the large language model can be avoided, and thus the efficiency and accuracy of code language conversion can be improved.
[0081] Other embodiments of the present disclosure will be apparent to those skilled in the art from consideration of the specification and practice of the features disclosed herein. It is intended that the present disclosure cover any and all variations of the present disclosure that come within the scope of the present disclosure along with its general principles and features. The specification and examples are to be considered exemplary only, with the true scope and spirit of the present disclosure being indicated by the following claims.
[0082] It is to be understood that the present disclosure is not limited to the precise construction described above and shown in the drawings, and that various modifications and changes can be made without departing from the scope thereof. The scope of the present disclosure is limited only by the claims appended hereto.
Claims
1. A method for converting a code language, characterized in that: include: Based on the output log of the C language project, obtain the C language source file; Extracting different types of basic conversion units from the C language source file, wherein the type of the basic conversion unit includes at least one of a structure, an enumeration, a function, a macro, and a global variable; The large language model is used to convert each type of basic conversion unit separately to obtain the Rust language file corresponding to each type of basic conversion unit.
2. The conversion method according to claim 1, wherein: Before using the large language model to convert each type of basic conversion unit and obtain the Rust language file corresponding to each type of basic conversion unit, the following is also included: Constructing a dependency graph between the different types of basic conversion units; Based on the dependency graph, performing conversion priority sorting on the different types of basic conversion units to obtain a sorting result, wherein the simpler the corresponding dependency, the higher the conversion priority of the corresponding basic conversion unit; The large language model is used to convert each type of basic conversion unit respectively, and the Rust language file corresponding to each type of basic conversion unit is obtained, including: The large language model is used to perform conversion on each type of basic conversion unit based on the sorting result to obtain a Rust language file corresponding to each type of basic conversion unit.
3. The conversion method according to claim 1, wherein: In a case where the basic conversion unit includes a target function and the target function references other basic conversion units, the conversion method further includes: In a case where the other basic conversion units referenced by the target function have been converted into corresponding Rust language files, obtaining a mapping relationship between the other basic conversion units and the corresponding Rust language files, wherein the other basic conversion units include at least one of the structure, the enumeration, and the function; The large language model is used to convert each type of basic conversion unit respectively, and the Rust language file corresponding to each type of basic conversion unit is obtained, including: The target function is converted into a corresponding Rust language file based on the mapping relationship using the large language model.
4. The conversion method according to claim 1, wherein: After using the large language model to convert each type of basic conversion unit and obtain the Rust language file corresponding to each type of basic conversion unit, it also includes: Compile and test the Rust language file corresponding to the function; In the event of compilation and testing failure, the large language model is used to correct the Rust language file corresponding to the function and the corrected Rust language file is recompiled and tested until the recompilation and testing are successful.
5. The conversion method according to claim 1, wherein: The extracting different types of basic conversion units from the C language source file includes: An abstract syntax tree (AST) analysis is performed on the C language source file to extract the different types of basic conversion units.
6. A code language conversion device, characterized in that: include: The source file acquisition module is configured to acquire the C language source file based on the output log of the C language project; a conversion unit extraction module configured to extract different types of basic conversion units from the C language source file, wherein the type of the basic conversion unit includes at least one of a structure, an enumeration, a function, a macro, and a global variable; The language conversion module is configured to use the large language model to convert each type of basic conversion unit separately to obtain the Rust language file corresponding to each type of basic conversion unit.
7. An electronic device, characterized in that: include: processor; a memory for storing instructions executable by the processor; The processor is configured to execute the instructions to implement the code language conversion method according to any one of claims 1 to 5.
8. A computer-readable storage medium, characterized in that When the instructions in the computer-readable storage medium are executed by a processor of an electronic device, the electronic device is enabled to execute the code language conversion method according to any one of claims 1 to 5.
9. A computer program product comprising a computer program, characterized in that When the computer program is executed by a processor, the code language conversion method according to any one of claims 1 to 5 is implemented.
Citation Information
Patent Citations
Programming language conversion method and device, equipment and storage medium
CN118940720A
Code conversion method and device and computer readable storage medium
CN119847537A
Context aware transcoding and optimization based on neural network
CN120162048A