Code language conversion method and device, electronic equipment, storage medium and computer program product
By analyzing C language projects and constructing dependency graphs using a large language model, the problem of poor standardization in C2Rust tool conversion was solved, achieving efficient and safe C to Rust code conversion, and improving code quality and migration efficiency.
Patent Information
- Application Number
- CN202511317009.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-09-15
- Publication Date
- 2025-11-21
- Estimated Expiration
- 2045-09-15
AI Technical Summary
In existing technologies, C2Rust tools have poor standardization in converting C language code to Rust language code, resulting in Rust language code of quality that is difficult to meet actual needs.
By analyzing the output logs of C language projects based on a large language model, basic transformation units such as structures, enumerations, functions, macros, and global variables are extracted. A dependency graph is constructed to prioritize transformations. The large language model is then used to perform transformations, obtain mapping relationships, and compile and test the code until successful.
It enables the automatic conversion of C language code into standardized Rust language code, improving migration efficiency and security, reducing error rate, and achieving large-scale, low-error, and controllable human-computer collaborative code conversion.
Smart Images

Figure CN120803467B_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:
[0020] In the present disclosure, unlike the conversion method based on syntax rules, by fully utilizing the powerful processing capability of large language models on 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.
[0021] 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
[0022] 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.
[0023] Figure 1 is a flowchart showing a code language conversion method according to an exemplary embodiment of the present disclosure;
[0024] Figure 2 is a dependency relationship diagram showing between different types of basic conversion units according to an exemplary embodiment of the present disclosure;
[0025] Figure 3 is a specific implementation flowchart showing a code language conversion method according to an exemplary embodiment of the present disclosure;
[0026] Figure 4 is a block diagram showing a code language conversion device according to an exemplary embodiment of the present disclosure;
[0027] Figure 5is a block diagram illustrating an electronic device according to an exemplary embodiment of the present disclosure. DETAILED DESCRIPTION
[0028] In order for those skilled 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.
[0029] It should be noted that the terms "first", "second", and the like 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 that 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.
[0030] It should be noted that "at least one of a plurality of items" appearing in the present disclosure means that three types of alternatives are included, i.e., "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". For example, "including at least one of A and B" includes the following three alternatives: (1) including A; (2) including B; and (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; and (3) performing step one and step two.
[0031] Figure 1 is a flowchart illustrating a code language conversion method according to an exemplary embodiment of the present disclosure.
[0032] 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. For example, 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.
[0033] According to the example embodiments of the present disclosure, the output log of the C language project can be built through a 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 instruct the large language model to obtain the C language file of the specified type based on the output log of the C language project. For example, 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 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, a C language source file, a C language library file, a C language executable file, and the like.
[0034] For example, the output log of the built C language project can be input into the large language model through the following prompt word:
[0035] "According to the input log, please analyze the generated binary files, including library files (.a or.so) and executable files (no extension), and output the detailed information of each binary file in the following format:
[0036] {
[0037] "bin":"binary_name", # The name of the generated binary file, which is a library file or an executable file
[0038] "type":"exe or lib", # The type of the binary file, "exe" indicates an executable file, and "lib" indicates a library file
[0039] "src": ["path / source1.c","path / source2.c"], # The storage path of the source file required for generating the current binary file, which can use a relative path
[0040] "lib": ["lib1.a","lib2.a"] # Library files required for generating the current binary file
[0041] }”.
[0042] In this way, by inputting the output log of the built C language project into the large language model through the prompt word, the architecture of the entire C language project can be obtained, such as including but not limited to source files, library files, executable files, and the like. Next, the C language source files participating in the project compilation can be automatically converted, and the compilation parameters of each C language source file can be analyzed and recorded.
[0043] 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.
[0044] The large language model in the present disclosure can be obtained by training in the following way:
[0045] A C language source file sample is 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 the 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.
[0046] 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 structure, enumeration, function, macro, and global variable.
[0047] 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.
[0048] 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.
[0049] 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 structure, enumeration, function, macro, and global variable.
[0050] 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.
[0051] 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.
[0052] 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.
[0053] 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.
[0054] 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.
[0055] 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.
[0056] According to the example embodiments 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 recompiled and tested again until the recompilation and testing are successful.
[0057] 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 in a manual intervention manner, and the like. The present disclosure does not limit the specific correction manner, and the foregoing embodiments are only an example illustration.
[0058] Figure 3 is a specific implementation flowchart showing a code language conversion method according to an example embodiment of the present disclosure.
[0059] Referring to Figure 3 In step 301, the output log of the C language project is built by a make command.
[0060] 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 C language file of a specified type. For example, the C language file of the specified type 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.
[0061] In step 303, an abstract syntax tree (AST) analysis is performed on the C language source file to extract different types of basic conversion units, such as a structure, an enumeration, a function, a macro, and a global variable.
[0062] In step 304, a dependency relationship graph between different types of basic conversion units is built.
[0063] In step 305, based on the dependency relationship graph, different types of basic conversion units are prioritized for conversion, wherein the simpler the corresponding dependency relationship is, the higher the conversion priority of the corresponding basic conversion unit is.
[0064] In step 306, the C language macro is converted into a constant or a function in the form of Rust language using a large language model.
[0065] In step 307, the C language structure and the C language enumeration are mapped into corresponding Rust language files using a large language model.
[0066] In step 308, the C language function is converted into a Rust language function using a large language model.
[0067] For example, in the case where the function references other basic conversion units and the other basic conversion units referenced by the function have been converted into corresponding Rust language files, a mapping relationship between the other basic conversion units and the corresponding Rust language files can be obtained. Next, the function can be converted into a corresponding Rust language file based on the obtained mapping relationship using a large language model.
[0068] In step 309, the Rust language file corresponding to the function is compiled and tested.
[0069] In step 3010, in the case where the compilation and testing fail, the Rust language file corresponding to the function is modified using a large language model, and the modified Rust language file is recompiled and tested until the recompilation and testing are successful.
[0070] In step 3011, it is checked whether all functions have been converted. In the case where 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, step 308 is executed.
[0071] Figure 4 FIG. 4 is a block diagram of a code language conversion apparatus 400 according to an example embodiment of the present disclosure.
[0072] Referring to FIG. 4, Figure 4 The code language conversion apparatus 400 can include a source file acquisition module 401, a conversion unit extraction module 402, and a language conversion module 403.
[0073] The source file acquisition module 401 can acquire a C language source file based on an output log of a C language project.
[0074] The conversion unit extraction module 402 can extract different types of basic conversion units from the C language source file, wherein the types of the basic conversion units can include at least one of a structure, an enumeration, a function, a macro, and a global variable.
[0075] 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.
[0076] 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.
[0077] 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.
[0078] 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.
[0079] According to an example embodiment of the present disclosure, the code language conversion device 400 can further include a mapping relationship acquisition module.
[0080] 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.
[0081] 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.
[0082] 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 is successful.
[0083] Figure 5 is a block diagram illustrating an electronic device 500 according to an example embodiment of the present disclosure.
[0084] 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.
[0085] 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).
[0086] 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.
[0087] 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.
[0088] 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.
[0089] 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.
[0090] 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 read-only memory (ROM), random access programmable read-only memory (PROM), electrically erasable programmable read-only memory (EEPROM), random access memory (RAM), dynamic random access memory (DRAM), static random access memory (SRAM), flash memory, non-volatile memory, CD-ROM, CD-R, CD+R, CD-RW, CD+RW, DVD-ROM, DVD-R, DVD+R, DVD-RW, DVD+RW, DVD-RAM, BD-ROM, BD-R, BD-R LTH, BD-RE, Blu-ray or optical disc memory, a hard disk drive (HDD), a solid state drive (SSD), a card-type 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.
[0091] According to an example embodiment of the disclosure, a computer program product can also be provided, including a computer program that, when executed by a processor, implements the code language conversion method according to the disclosure.
[0092] According to the code language conversion method, device, electronic device, storage medium, and computer program product of the disclosure, unlike the conversion method based on syntax rules, the disclosure can automatically convert C language code into standardized Rust language code by fully utilizing the powerful processing capability of a large language model on code, realizing large-scale, low-error, controllable, and safe human-machine collaborative code conversion, and can improve the efficiency and safety of migrating old C projects to standardized modern languages.
[0093] 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.
[0094] 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. The present disclosure is intended to cover any variations, uses, or adaptations of the present disclosure following, in general, the principles of the present disclosure and including such features to the extent that they are not disclosed in the prior art. The specification and examples are to be regarded as illustrative only, and the true scope and spirit of the present disclosure are indicated by the following claims.
[0095] It should be understood that the present disclosure is not limited to the precise structures as herein described and illustrated in the drawings, and that various modifications and changes can be made without departing from its scope. The scope of the present disclosure is limited only by the claims that follow.
Claims
1. A conversion method of a code language, characterized by, The method comprises the following steps: Based on the output log of the C language project, the C language source file is obtained; Different types of basic conversion units are extracted 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; A large language model is used to convert each type of basic conversion unit to obtain a corresponding Rust language file for each type of basic conversion unit; Wherein, based on the output log of the C language project, the C language source file is obtained, comprising: The output log of the C language project and the preset prompt word are input into the large language model to obtain the path information of the C language source file output by the large language model; The C language source file is read from the path indicated by the path information.
2. The conversion method of claim 1, wherein, Before converting each type of basic conversion unit using a large language model to obtain a corresponding Rust language file for each type of basic conversion unit, the method further comprises: A dependency graph between the different types of basic conversion units is constructed; Based on the dependency graph, the different types of basic conversion units are prioritized for conversion to obtain a sorting result, wherein the simpler the corresponding dependency relationship, 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 based on the sorting result to obtain a corresponding Rust language file for each type of basic conversion unit. In the case that the basic conversion unit includes a target function and the target function references other basic conversion units, the conversion method further comprises:
3. The conversion method of claim 1, wherein, In the case that the other basic conversion units referenced by the target function have been converted into corresponding Rust language files, the mapping relationship between the other basic conversion units and the corresponding Rust language files is obtained, 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 the target function into a corresponding Rust language file based on the mapping relationship. After converting each type of basic conversion unit using a large language model to obtain a corresponding Rust language file for each type of basic conversion unit, the method further comprises: The Rust language file corresponding to the function is compiled and tested; 4. The conversion method of claim 1, wherein, In the case of failed compilation and testing, the large language model is used to modify the Rust language file corresponding to the function and recompile and test the modified Rust language file until the recompilation and testing are successful. The different types of basic conversion units are extracted from the C language source file, comprising: 5. The conversion method of claim 1, wherein, Performing abstract syntax tree (AST) analysis on the C language source file to extract the different types of basic conversion units.
6. A conversion apparatus of a code language characterized by comprising: Comprise: 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 include at least one of a structure, an enumeration, a function, a macro, and a global variable; a language conversion module configured to 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. Wherein the source file acquisition module is configured to: input the output log of the C language project and a preset prompt word into the large language model to obtain path information of the C language source file output by the large language model; read the C language source file from the path indicated by the path information.
7. An electronic device, comprising: Comprise: a processor; a memory for storing instructions executable by the processor; wherein the processor is configured to execute the instructions to implement the conversion method of the code language as claimed in 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 the processor of the electronic device, the electronic device can execute the conversion method of the code language as claimed in any one of claims 1 to 5.
9. A computer program product comprising a computer program, characterized in that, The computer program is executed by the processor to implement the conversion method of the code language as claimed in any one of claims 1 to 5.
Citation Information
Patent Citations
Context aware transcoding and optimization based on neural network
CN120162048A