A code conversion method, device and medium

The construction of an abstract syntax tree through preset code mapping relationships and adding attribute parameter information is solved, and the problem of inaccurate and inability to communicate between different programming languages ​​is achieved, achieving high accuracy and communicability of code conversion effects.

CN119781775APending Publication Date: 2025-04-08DAWNING INT INFORMATION IND CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411981034.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-30
Publication Date
2025-04-08

AI Technical Summary

Technical Problem

In the prior art, code conversion tools between different programming languages ​​are often inaccurate or incomplete due to complex language structures, and the converted code cannot communicate with codes in other languages.

Method used

The abstract syntax tree is constructed through preset code mapping relationships, traversing the syntax tree to obtain code snippets of the target language, and adding attribute parameter information to ensure data format compatibility, realizing code conversion and communication between different languages.

Benefits of technology

It improves the accuracy and completeness of code conversion, so that the converted code can communicate effectively with code in other languages, and enhances the maintainability and scalability of the system.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119781775A_ABST
    Figure CN119781775A_ABST
Patent Text Reader

Abstract

The embodiment of the present application provides a code conversion method, device and medium to solve the problem of inaccurate code conversion and inability to communicate. The method includes: constructing an abstract syntax tree based on a second language based on a first language code to be converted and a preset code mapping relationship; the code mapping relationship is: a mapping relationship between the first language and the second language for a set code; traversing the abstract syntax tree based on the grammatical rules of the second language to obtain at least one code fragment of the second language; for any of the code fragments, after determining the attribute parameter information of the target type data in the code fragment, adding the attribute code generated based on the attribute parameter information to the code fragment to obtain the code of the second language; the attribute parameter information is used to convert the target type data into a data form that can be used by the code of the first language or the second language.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the technical field of programming language conversion, and in particular to a code conversion method, device and medium. Background Art

[0002] In the prior art, code conversion tools between different programming languages ​​often produce inaccurate or incomplete conversion results due to the complex language structure. For example, when converting C to Java, due to the significant differences between C and Java in syntax and syntax, C allows low-level operations such as pointer operations and memory management, while Java prohibits direct memory operations, resulting in the inability to correctly handle the above code conversion during the conversion process. In addition, since different programming languages ​​cannot communicate directly with each other, the converted code cannot communicate with the code of other languages. Summary of the invention

[0003] The embodiments of the present application provide a code conversion method, device and medium to solve the problem of inaccurate code conversion and inability to communicate.

[0004] In a first aspect, an embodiment of the present application provides a code conversion method, including:

[0005] Based on the first language code to be converted and a preset code mapping relationship, constructing an abstract syntax tree based on the second language; the code mapping relationship is: a mapping relationship between the first language and the second language for the set code;

[0006] Traversing the abstract syntax tree based on the grammatical rules of the second language to obtain at least one code snippet in the second language;

[0007] For any one of the code snippets, after determining the attribute parameter information of the target type data in the code snippet, the attribute code generated based on the attribute parameter information is added to the code snippet to obtain the code in the second language; the attribute parameter information is used to convert the target type data into a data form that can be used by the code in the first language or the second language.

[0008] Based on the above scheme, a mapping relationship between the first language and the second language for the set code is pre-constructed, and an abstract syntax tree is constructed based on the mapping relationship and the first language code to be converted, and then the syntax tree is traversed to obtain the code fragment of the second language. Furthermore, the attribute code corresponding to the target type data parameter information is added to the code fragment to obtain the code of the second language. The present application converts the set code through a preset code mapping relationship to ensure the accuracy of the code conversion. Furthermore, the attribute parameter information is used to convert the target type data into a data type that can be used by the code of the first language or the second language, so that the converted code can communicate with the code of the other language.

[0009] In a possible implementation, constructing an abstract syntax tree based on the second language based on the first language code to be converted and a preset code mapping relationship includes:

[0010] Recognize the first language code based on lexical recognition rules to obtain grammatical elements;

[0011] Based on the code mapping relationship and the syntax elements, an abstract syntax tree based on the second language is constructed.

[0012] Based on the above scheme, the present application performs lexical analysis on the code of the first language through lexical recognition rules, and then can identify basic language elements, so as to construct an abstract syntax tree according to the mapping relationship between syntax elements and codes, thereby avoiding the tedious operation of manual conversion.

[0013] In a possible implementation, constructing an abstract syntax tree based on a second language based on the code mapping relationship and the syntax element includes:

[0014] Based on the code mapping relationship, convert the set code in the first language code to obtain an initial conversion code;

[0015] An abstract syntax tree of a second language is constructed according to the initial conversion code and the syntax elements.

[0016] Based on the above scheme, the set code is converted based on the code mapping relationship, so that the constructed abstract syntax tree is more accurate and complete.

[0017] In a possible implementation, traversing the abstract syntax tree based on the grammar rules of the second language to obtain at least one code snippet in the second language includes:

[0018] For any structure in the first language code, perform the following operations:

[0019] Determine at least one tree node corresponding to the structure in the abstract syntax tree;

[0020] Based on the at least one tree node and the grammatical rule of the second language, a code snippet in the second language corresponding to the structure is determined.

[0021] Through the above scheme, the syntax tree-based conversion method can accurately capture the structure and semantics of the first language and generate code that conforms to the specifications of the second language, which can reduce the errors that may occur in manual conversion and has high conversion accuracy.

[0022] In a possible implementation, the attribute parameter information includes a storage data format, serialization parameters, and deserialization parameters of the data;

[0023] The serialization parameters are used to convert the target type data into a data form usable by the code of the first language, and the deserialization parameters are used to convert the target type data into a data form usable by the code of the second language.

[0024] Based on the above solution, by adding data storage format, serialization parameters and deserialization parameters in the code, the target type data can be converted into a data form that can be used by the code of the first language or the second language, thereby realizing communication and interaction between codes.

[0025] In a possible implementation manner, the first language code is obtained in the following manner:

[0026] Get the code file to be converted;

[0027] A header file in the code file to be converted is determined, and based on the compiler identifying the header file, a first language code to be converted is obtained.

[0028] Based on the above solution, after the compiler identifies and expands the file, the first language code obtained is more complete, thereby making the converted code more complete and accurate.

[0029] In a possible implementation, the method further includes:

[0030] Before constructing an abstract syntax tree based on the second language, the name and storage path of each structure in the first language code are written into the first language code respectively;

[0031] And, for any code snippet in the second language, the method further includes:

[0032] Determine the target storage path in the code snippet and the target class name corresponding to the structure;

[0033] The file name of the code snippet is set to the target class name, and the file of the code snippet is saved in the target storage path.

[0034] Based on the above solution, by saving the generated second language code snippet file in the corresponding target storage path, the code file can be directly accessed and used by other codes.

[0035] In a second aspect, an embodiment of the present application provides a code conversion device, including:

[0036] A first processing module is used to construct an abstract syntax tree based on the second language based on the first language code to be converted and a preset code mapping relationship; the code mapping relationship is: a mapping relationship between the first language and the second language for a set code;

[0037] Traversing the abstract syntax tree based on the grammatical rules of the second language to obtain at least one code snippet in the second language;

[0038] The second processing module is used to determine the attribute parameter information of the target type data in any one of the code snippets, and then add the attribute code generated based on the attribute parameter information to the code snippet to obtain the code in the second language; the attribute parameter information is used to convert the target type data into a data form that can be used by the code in the first language or the second language.

[0039] In a possible implementation, when the first processing module constructs an abstract syntax tree based on the second language based on the first language code to be converted and a preset code mapping relationship, it is specifically used to:

[0040] Recognize the first language code based on lexical recognition rules to obtain grammatical elements;

[0041] Based on the code mapping relationship and the syntax elements, an abstract syntax tree based on the second language is constructed.

[0042] In a possible implementation, when the first processing module constructs an abstract syntax tree based on the second language based on the code mapping relationship and the syntax element, it is specifically configured to:

[0043] Based on the code mapping relationship, convert the set code in the first language code to obtain an initial conversion code;

[0044] An abstract syntax tree of a second language is constructed according to the initial conversion code and the syntax elements.

[0045] In a possible implementation, the first processing module, when traversing the abstract syntax tree based on the grammatical rules of the second language to obtain at least one code snippet in the second language, is specifically configured to:

[0046] For any structure in the first language code, perform the following operations:

[0047] Determine at least one tree node corresponding to the structure in the abstract syntax tree;

[0048] Based on the at least one tree node and the grammatical rule of the second language, a code snippet in the second language corresponding to the structure is determined.

[0049] In a possible implementation, the attribute parameter information includes a storage data format, serialization parameters, and deserialization parameters of the data;

[0050] The serialization parameters are used to convert the target type data into a data form usable by the code of the first language, and the deserialization parameters are used to convert the target type data into a data form usable by the code of the second language.

[0051] In a possible implementation manner, the first processing module is further configured to:

[0052] The first language code is obtained by:

[0053] Get the code file to be converted;

[0054] A header file in the code file to be converted is determined, and based on the compiler identifying the header file, a first language code to be converted is obtained.

[0055] In a possible implementation, the second processing module is further configured to:

[0056] Before constructing an abstract syntax tree based on the second language, the name and storage path of each structure in the first language code are written into the first language code respectively;

[0057] And, for any code snippet in the second language, the second processing module is further used to:

[0058] Determine the target storage path in the code snippet and the target class name corresponding to the structure;

[0059] The file name of the code snippet is set to the target class name, and the file of the code snippet is saved in the target storage path.

[0060] In a third aspect, an embodiment of the present application provides an execution device, including:

[0061] A memory for storing program instructions;

[0062] The processor is used to obtain the program instructions in the memory and execute the method described in the first aspect and different implementation methods of the first aspect according to the obtained program instructions.

[0063] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, wherein the computer-readable storage medium includes computer instructions. When the computer instructions are executed by a computer, the method described in the first aspect and different implementation methods of the first aspect are implemented.

[0064] In addition, for the technical effects brought about by any implementation method of the second to fourth aspects, please refer to the first aspect and the technical effects brought about by different implementation methods of the first aspect, and will not be repeated here. BRIEF DESCRIPTION OF THE DRAWINGS

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

[0066] Figure 1 A flow chart of a code conversion method provided by an embodiment of the present application;

[0067] Figure 2 A schematic diagram of a mapping relationship provided in an embodiment of the present application;

[0068] Figure 3 A flowchart for converting C language code into Java code provided in an embodiment of the present application;

[0069] Figure 4 A schematic diagram of a code conversion device provided in an embodiment of the present application;

[0070] Figure 5 A schematic diagram of an execution device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0071] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. The components of the embodiments of the present application described and shown in the drawings here can be arranged and designed in various different configurations.

[0072] Therefore, the following detailed description of the embodiments of the present application provided in the accompanying drawings is not intended to limit the scope of the present application for which protection is sought, but merely represents selected embodiments of the present application. Based on the embodiments of the present application, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of the present application.

[0073] It should be noted that relational terms such as "first" and "second" are used only to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or device including the elements.

[0074] In order to help those skilled in the art better understand the technical solution of the present application, some concepts involved in the present application are introduced below.

[0075] Abstract Syntax Tree (AST): also known as syntax tree or syntax tree, is an abstract representation of the syntax structure of the source code. As an important intermediate representation of a program, the syntax tree contains complete source program information. Its basic formation process can be roughly divided into three steps, namely, firstly, lexical analysis of the source program code, grammatical analysis of the word stream generated by the lexical analysis, and finally semantic analysis to form a syntax tree of the source code.

[0076] Lexical analysis: Lexical analysis refers to the process of converting a character sequence into a word sequence. The program or function that performs lexical analysis is called a lexical analyzer (Lexer). Lexical analyzers generally exist in the form of functions for syntax analysis to call. Lexical analysis provides symbolic nodes for the generation of abstract syntax numbers. Specifically, the program source code is input as a character stream in sequence, and the read-in character sequence is scanned and analyzed, and finally converted into a word sequence output, where "word" is the smallest unit that makes up the program.

[0077] Syntactic analysis: Syntactic analysis refers to the logical analysis of a program. Based on the word sequence generated by lexical analysis, the basic grammatical structure of the program, such as methods, variables, expressions, and various components of the program, is analyzed according to the grammatical rules of a given language to be analyzed. A parser usually appears as a component of a compiler or interpreter. Its function is to perform grammar checking and construct a data structure composed of input words.

[0078] Semantic analysis: Semantic analysis mainly conducts logical analysis on the program, and determines whether the source program is semantically accurate by performing context-related verification on the source program.

[0079] At present, the existing technologies for code conversion mainly include the following:

[0080] Source-to-source compilers (Transpilers) are a type of compiler that can convert code from one high-level programming language to another high-level programming language. The tool generates an intermediate representation by parsing the source code and then generates the target code according to the syntax and semantics of the target language.

[0081] Low-Level Virtual Machine (LLVM): LLVM is a modular and reusable compiler and toolchain technology library that supports multiple programming languages ​​and architectures. By converting source code into LLVM intermediate representation (IR), LLVM's optimization and code generation modules can be used to generate target code for different platforms. Although LLVM is mainly used to generate machine code, it can also be used for conversion between high-level languages.

[0082] ANTLR (Another Tool for Language Recognition): ANTLR is a powerful parser generator that supports lexical analysis and syntax analysis of multiple languages. By using ANTLR, developers can define the grammatical rules of the source language and the target language and generate the corresponding parsers for code conversion.

[0083] However, existing code conversion tools have the following problems:

[0084] First, due to the significant differences in syntax and the above between different languages, such as some languages ​​are allowed low-level operations, such as pointer operations and memory management, while other languages ​​prohibit direct memory operations, which makes it difficult to directly map these features during the conversion process, and there is no direct corresponding mechanism to the processing instructions in some languages. Therefore, the code conversion work cannot correctly handle the above operations, resulting in inaccurate or incomplete conversion results. Secondly, the design of existing conversion tools is usually based on fixed syntax and rules, and lacks a flexible expansion mechanism. When new language features or syntax are introduced, the tool needs to be modified and refactored, resulting in poor maintainability and scalability of the tool. In addition, after the existing code tools convert the code, the converted code cannot communicate with other code.

[0085] In view of the above problems, the embodiments of the present application provide a code conversion method, device and medium, a mapping relationship between a first language and a second language is pre-constructed for setting codes, and an abstract syntax tree is constructed based on the mapping relationship and the first language code to be converted, and then the syntax tree is traversed to obtain the code fragment of the second language. Further, the attribute code corresponding to the target type data parameter information is added to the code fragment to obtain the code of the second language. The present application converts the set code through a preset code mapping relationship, which can ensure the accuracy of the code conversion. Furthermore, the attribute parameter information is used to convert the target type data into a data type that can be used by the code of the first language or the second language, so that the converted code can communicate with the code of other languages. In addition, the present application constructs an abstract syntax tree through lexical recognition rules and grammar rules, so it can be expanded according to demand to handle more load language structures and specific library calls, and has strong scalability.

[0086] The first language in the present application may be C language, the second language may be Java language, or the first language may be Java language and the second language may be C language, or the first language and the second language may be other programming languages, which is not specifically limited in the present application.

[0087] See also Figure 1 As shown, it is a flowchart of a code conversion method provided by an embodiment of the present application, and the specific process is as follows:

[0088] Step S101: construct an abstract syntax tree based on a second language based on the first language code to be converted and a preset code mapping relationship.

[0089] The code mapping relationship is: a mapping relationship between the first language and the second language for a set code.

[0090] In some embodiments, taking the first language as C and the second language as Java as an example, the code mapping relationship is the mapping relationship between C and Java for setting codes. For example, the int type in C has a mapping relationship with the Interger class in Java, the structure struct in C has a mapping relationship with the class class in Java, the pointer type in C has a mapping relationship with the reference type in Java, and so on. Through the mapping relationship, the code involving pointer operations and memory management in C can be correctly converted and executed in Java.

[0091] like Figure 2 As shown, some setting codes with mapping relationships between C language and Java language are given as examples in this application. In addition, more mapping relationships between codes can be included, and this application does not give too many examples for this.

[0092] In a possible implementation, the first language code may be obtained in the following manner:

[0093] A code file to be converted is obtained, a header file in the code file to be converted is determined, and the header file is identified based on a compiler to obtain a first language code to be converted.

[0094] As an example, obtain the C language file to be converted. Since most files contain header files, if the conversion is based only on the file name of the header file, the content in the header file cannot be determined. Therefore, a compiler may be needed to identify multiple files. Specifically, gcc can be used to expand all header files, and then generate an intermediate file with a .i suffix. At this time, all header files are expanded in the intermediate file with a .i suffix, which contains all the information of the code to be converted, and is the C language code that needs to be converted.

[0095] In some embodiments, when constructing an abstract syntax tree based on the second language based on the first language code to be converted and a preset code mapping relationship, it can be achieved in the following way: the first language code is identified based on lexical recognition rules to obtain grammatical elements.

[0096] As an example, the lexical recognition rules are first used to recognize the first language code. Specifically, the lex tool can be used to parse the intermediate file ending with .i generated in the previous step, and then the grammatical elements of the code in the intermediate file can be identified. For example, language elements can include keywords, identifiers, operators, variable names, capacities, etc. For example, the C language code is int test

[11] ; then it is recognized by the lex tool, and the grammatical elements obtained are:

[0097] int: type

[0098] test: variable name

[0099] []: Specific symbol []

[0100] 01: Capacity

[0101] ;: terminator, stop when encountering this

[0102] Furthermore, an abstract syntax tree based on the second language can be constructed based on the code mapping relationship and the syntax elements. Specifically, based on the code mapping relationship, the set code in the first language code can be converted to obtain an initial conversion code; and an abstract syntax tree of the second language can be constructed based on the initial conversion code and the syntax elements.

[0103] For example, taking C as the first language and Java as the second language, the int type in C language can be converted into the Integer class in Java language, struct A in C language can be converted into class A in Java language, and the pointer type in C language can be converted into a reference type in Java language.

[0104] As an example, int in C language code has a mapping relationship with Interger in Java code, and int needs to be converted into Interger during code conversion. Further, an abstract syntax tree of the second language can be constructed based on the initial conversion code obtained after the code mapping relationship conversion and the syntax elements.

[0105] Continuing with the above example, the syntax elements included in int test

[11] in C language are: type, variable name, specific symbol, capacity and terminal symbol. A syntax analysis is performed to determine that a syntax conversion is required for a statement such as type, variable name, capacity and terminal symbol. In the present application, a syntax analysis can be performed using the yacc tool, and a syntax structure can be constructed based on the above syntax elements to obtain an abstract syntax tree based on Java. The obtained abstract syntax tree based on Java contains the syntax information of the C language code.

[0106] Step S102: traverse the abstract syntax tree based on the grammatical rules of the second language to obtain at least one code snippet in the second language.

[0107] In a possible implementation, for any structure in the first language code, the following operations are performed: determining at least one tree node corresponding to the structure in the abstract syntax tree, and determining a code snippet of the second language corresponding to the structure based on the at least one tree node and a grammar rule of the second language.

[0108] Specifically, after the abstract syntax tree is generated, the abstract syntax tree can be traversed to process the nodes therein one by one, thereby converting the grammatical structure of the first language into an equivalent structure of the second language.

[0109] In some embodiments, since the grammatical rules of the first language and the grammatical rules of the second language may be different, after conversion into an equivalent structure of the second language, a code snippet of the second language may be generated based on the grammatical rules and the equivalent structure of the second language. For example, if the abstract syntax tree only retains important information of the code in the second language, and the grammatical rules of the second language also include other rules, then the code snippet needs to be further constructed according to the other rules.

[0110] Continuing with the above example, after constructing the abstract syntax tree with the syntax element type, variable name, capacity, terminal symbol and mapping relationship corresponding to the C language code int test

[11] , the tree nodes involved in int test

[11] in the abstract syntax tree are determined, and then the involved tree nodes are converted and processed through the syntax rules of the Java language to obtain the corresponding Java code. As an example, the obtained Java code snippet is shown as follows:

[0111] Private static final Integer TEST_DRFAULT=11;

[0112] Public Integer test.

[0113] Step S103, for any code snippet, after determining the attribute parameter information of the target type data in the code snippet, the attribute code generated based on the attribute parameter information is added to the code snippet to obtain the code in the second language.

[0114] In some embodiments, the attribute parameter information is used to convert the target type data into a data form that can be used by the code of the first language or the second language. Specifically, the attribute parameter information includes the storage data format of the data, serialization parameters, and deserialization parameters. Among them, the serialization parameters are used to convert the target type data into a data form that can be used by the code of the first language, and the deserialization parameters are used to convert the target type data into a data form that can be used by the code of the second language.

[0115] In this application, in order to achieve communication between C language code and Java code, three member functions can be added according to the member type of the class after generating the Java code snippet: serialization, deserialization and obtaining class size. Through the above functions, Java objects can be converted into a data format that can be passed to the C language, or the serialized data obtained by the C language can be restored to Java objects.

[0116] Continuing with the above example, after generating the Integer class, three member functions need to be added according to the member type of the Integer class: serialization parameters, deserialization parameters, and storage data format. Taking the target type data as the variable value as an example, the storage data format of 11 in the converted code snippet may exist in multiple forms when stored, such as 64-bit or 128-bit storage, or other storage forms. Therefore, it is necessary to determine the storage data format of the variable value. In addition, it is also necessary to determine the serialization parameters and deserialization parameters of the variable value. The Java side can serialize the class information into a data form that can be recognized by the C language through serialization, and deserialize the class information into a data form that can be recognized by Java through deserialization.

[0117] Based on the above scheme, a mapping relationship between the first language and the second language for the set code is pre-constructed, and an abstract syntax tree is constructed based on the mapping relationship and the first language code to be converted, and then the syntax tree is traversed to obtain the code fragment of the second language. Furthermore, the attribute code corresponding to the target type data parameter information is added to the code fragment to obtain the code of the second language. The present application converts the set code through a preset code mapping relationship to ensure the accuracy of the code conversion. Furthermore, the attribute parameter information is used to convert the target type data into a data type that can be used by the code of the first language or the second language, so that the converted code can communicate with the code of the other language.

[0118] In a possible implementation, before constructing an abstract syntax tree based on the second language, the name and storage path of each structure in the first language code can be written into the first language code. Further, after executing step S103, for any code fragment in the second language, step S104 can also be executed:

[0119] Step S104, determining the target storage path in the code snippet and the target class name corresponding to the structure, setting the file name of the code snippet as the target class name, and saving the file of the code snippet in the target storage path.

[0120] As an example, although the expanded intermediate file ending with .i contains code information, the package path and import package name required by the Java code file do not exist in the intermediate file, so the package path and the package name used in the Java code file also need to be written in the .i intermediate file. Among them, the import package name refers to the name of the structure in the C language. Furthermore, when the lexical recognition rules are used to identify and construct the abstract syntax tree, the package path and import package name can be saved in the abstract syntax tree. It should be noted that when constructing the abstract syntax tree, the structure name needs to be converted into the name of a class that can be recognized by the Java language.

[0121] Furthermore, after obtaining multiple Java language code snippets, the class name and storage path corresponding to the structure in the code snippet can be determined, and then the class name is used as the file name of the code snippet and saved in the corresponding storage path to facilitate direct access and use by other Java classes.

[0122] The code conversion method proposed in the present application can automatically parse the first language code into an abstract syntax tree through lexical analysis and grammatical analysis, avoiding the tedious operation of manual conversion, with a high degree of automation. In addition, the conversion method based on the syntax tree can accurately capture the structure and semantics of the first language, and then generate code that conforms to the language specification of the second language, reduce conversion errors, and improve the accuracy of conversion. In addition, the present application can expand lexical recognition rules and grammatical rules according to demand, and then handle more complex language structures and specific library calls, with strong extensibility. In addition, through serialization and deserialization, the generated code can communicate with the first language code or the second language code.

[0123] like Figure 3 As shown, it is a flowchart of converting C language code into Java code provided by an embodiment of the present application, and the specific steps are as follows:

[0124] S301, expanding the header file in the C code file to be converted based on the compiler to obtain an intermediate file.

[0125] S302, writing the storage path and the name of the structure into an intermediate file to obtain a code file to be converted.

[0126] S303, parsing the code file to be converted according to the lex recognition rule and the yacc grammar recognition rule to obtain an abstract syntax tree.

[0127] Among them, Lex is responsible for lexical analysis and identifying basic grammatical elements, such as keywords, identifiers, operators, etc.; yacc builds grammatical structures based on these lexical units. Through the cooperation of lex and yacc, basic types, structures and other elements in C code can be identified and converted into corresponding Java elements. For example, the int type in C language can be directly mapped to int in Java, and the structure struct can be identified as class in Java. Through the conversion, a complete syntax tree will eventually be generated, which contains all the syntax information of the original C code.

[0128] In this step, the basic data types (such as int, float, char, etc.) and complex data types (such as pointers, arrays, structures, etc.) of the C language can be mapped to the corresponding data types (such as int, float, char, Array, Object, etc.) in the Java language.

[0129] S304, traverse the abstract syntax tree based on the grammatical rules of the Java language to obtain Java code.

[0130] Since the grammatical rules of the Java language are different from those of the C language, when traversing the abstract syntax tree, it is necessary to process based on the grammatical rules of the Java language to ensure that the obtained Java code can be executed correctly.

[0131] S305, adding attribute parameter information to the Java code.

[0132] In some embodiments, the attribute parameter information includes serialization, deserialization, and obtaining class size. In order to achieve communication between C code and Java code, three additional member functions can be added to the generated Java class according to the member type of the class: serialization, deserialization, and obtaining class size. These functions are used to convert Java objects into data formats that can be passed to C, and to restore serialized data obtained from C to Java objects.

[0133] S306, setting the class name in the Java code as the file name, and storing the Java code file in a corresponding storage path.

[0134] Based on the same technical concept, see Figure 4 The above is a code conversion device 400 provided in an embodiment of the present application. The device 400 can implement any step in the above code conversion method, and in order to avoid repetition, it is not described here. The device 400 includes a first processing module 401 and a second processing module 402.

[0135] The first processing module 401 is used to construct an abstract syntax tree based on the second language based on the first language code to be converted and a preset code mapping relationship; the code mapping relationship is: a mapping relationship between the first language and the second language for a set code;

[0136] Traversing the abstract syntax tree based on the grammatical rules of the second language to obtain at least one code snippet in the second language;

[0137] The second processing module 402 is used to determine the attribute parameter information of the target type data in any one of the code snippets, and then add the attribute code generated based on the attribute parameter information to the code snippet to obtain the code in the second language; the attribute parameter information is used to convert the target type data into a data form that can be used by the code in the first language or the second language.

[0138] In a possible implementation, when constructing an abstract syntax tree based on the second language based on the first language code to be converted and a preset code mapping relationship, the first processing module 401 is specifically configured to:

[0139] Recognize the first language code based on lexical recognition rules to obtain grammatical elements;

[0140] Based on the code mapping relationship and the syntax elements, an abstract syntax tree based on the second language is constructed.

[0141] In a possible implementation, when constructing an abstract syntax tree based on the second language based on the code mapping relationship and the syntax element, the first processing module 401 is specifically configured to:

[0142] Based on the code mapping relationship, convert the set code in the first language code to obtain an initial conversion code;

[0143] An abstract syntax tree of a second language is constructed according to the initial conversion code and the syntax elements.

[0144] In a possible implementation, the first processing module 401, when traversing the abstract syntax tree based on the grammatical rules of the second language to obtain at least one code snippet in the second language, is specifically configured to:

[0145] For any structure in the first language code, perform the following operations:

[0146] Determine at least one tree node corresponding to the structure in the abstract syntax tree;

[0147] Based on the at least one tree node and the grammatical rule of the second language, a code snippet in the second language corresponding to the structure is determined.

[0148] In a possible implementation, the attribute parameter information includes a storage data format, serialization parameters, and deserialization parameters of the data;

[0149] The serialization parameters are used to convert the target type data into a data form usable by the code of the first language, and the deserialization parameters are used to convert the target type data into a data form usable by the code of the second language.

[0150] In a possible implementation, the first processing module 401 is further configured to:

[0151] The first language code is obtained by:

[0152] Get the code file to be converted;

[0153] A header file in the code file to be converted is determined, and based on the compiler identifying the header file, a first language code to be converted is obtained.

[0154] In a possible implementation, the second processing module 402 is further configured to:

[0155] Before constructing an abstract syntax tree based on the second language, the name and storage path of each structure in the first language code are written into the first language code respectively;

[0156] And, for any code snippet in the second language, the second processing module 402 is further used to:

[0157] Determine the target storage path in the code snippet and the target class name corresponding to the structure;

[0158] The file name of the code snippet is set to the target class name, and the file of the code snippet is saved in the target storage path.

[0159] Based on the same technical concept, see Figure 5 As shown, an execution device 500 provided in an embodiment of the present application can implement any step of the code conversion method discussed above, please refer to Figure 5 The device 500 includes a memory 501 and a processor 502 .

[0160] The memory 501 is used to store program instructions;

[0161] The processor 502 is used to call the program instructions stored in the memory and execute the above code conversion method according to the obtained program.

[0162] In the embodiment of the present application, the processor 502 may be a general-purpose processor, a digital signal processor, an application-specific integrated circuit, a field programmable gate array or other programmable logic device, a discrete gate or transistor logic device, or a discrete hardware component, and may implement or execute the methods, steps, and logic block diagrams disclosed in the embodiment of the present application. The general-purpose processor may be a microprocessor or any conventional processor, etc. The steps of the method disclosed in the embodiment of the present application may be directly embodied as being executed by a hardware processor, or may be executed by a combination of hardware and software modules in the processor.

[0163] Memory 501, as a non-volatile computer-readable storage medium, can be used to store non-volatile software programs, non-volatile computer executable programs and modules. Memory 501 may include at least one type of storage medium, such as flash memory, hard disk, multimedia card, card-type memory, random access memory (Random Access Memory, RAM), static random access memory (Static Random Access Memory, SRAM), programmable read-only memory (Programmable Read Only Memory, PROM), read-only memory (Read Only Memory, ROM), electrically erasable programmable read-only memory (Electrically Erasable Programmable Read-Only Memory, EEPROM), magnetic storage, disk, optical disk, etc. Memory 501 is any other medium that can be used to carry or store a desired program code in the form of an instruction or data structure and can be accessed by a computer, but is not limited thereto. The memory 501 in the embodiment of the present application can also be a circuit or any other device that can realize a storage function, for storing program instructions and / or data.

[0164] Based on the same technical concept, the embodiment of the present application provides a computer-readable storage medium, including computer program instructions, which, when executed on a computer, causes the computer to execute the code conversion method discussed above. Since the principle of solving the problem by the above-mentioned computer-readable storage medium is similar to that of the code conversion method, the implementation of the above-mentioned computer-readable storage medium can refer to the implementation of the method, and the repeated parts will not be repeated.

[0165] Based on the same technical concept, the embodiment of the present application provides a computer program product, including program code, which, when executed on a computer, enables the computer to execute the code conversion method discussed above. Since the principle of solving the problem by the above-mentioned computer program product is similar to that of the code conversion method, the implementation of the above-mentioned computer program product can refer to the implementation of the method, and the repeated parts will not be repeated.

[0166] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems, or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment in combination with software and hardware. Moreover, the present application may adopt the form of a computer program product implemented in one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that include computer-usable program code.

[0167] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0168] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.

[0169] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process in the computer or other programmable device. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.

[0170] Obviously, those skilled in the art can make various changes and modifications to the present application without departing from the spirit and scope of the present application. Thus, if these modifications and variations of the present application fall within the scope of the claims of the present application and their equivalents, the present application is also intended to include these modifications and variations.

Claims

1. A code conversion method, characterized in that: include: Based on the first language code to be converted and the preset code mapping relationship, construct an abstract syntax tree based on the second language; The code mapping relationship is: a mapping relationship between the first language and the second language for a set code; Traversing the abstract syntax tree based on the grammatical rules of the second language to obtain at least one code snippet in the second language; For any of the code snippets, after determining the attribute parameter information of the target type data in the code snippet, adding the attribute code generated based on the attribute parameter information to the code snippet to obtain the code in the second language; The attribute parameter information is used to convert the target type data into a data form that can be used by the code in the first language or the second language.

2. The method according to claim 1, characterized in that The step of constructing an abstract syntax tree based on the second language based on the first language code to be converted and a preset code mapping relationship includes: Recognize the first language code based on lexical recognition rules to obtain grammatical elements; Based on the code mapping relationship and the syntax elements, an abstract syntax tree based on the second language is constructed.

3. The method according to claim 2, characterized in that The constructing an abstract syntax tree based on the second language based on the code mapping relationship and the syntax element includes: Based on the code mapping relationship, convert the set code in the first language code to obtain an initial conversion code; An abstract syntax tree of a second language is constructed according to the initial conversion code and the syntax elements.

4. The method according to claim 1, characterized in that The traversing the abstract syntax tree based on the grammar rules of the second language to obtain at least one code snippet in the second language includes: For any structure in the first language code, perform the following operations: Determine at least one tree node corresponding to the structure in the abstract syntax tree; Based on the at least one tree node and the grammatical rule of the second language, a code snippet in the second language corresponding to the structure is determined.

5. The method according to claim 1, characterized in that The attribute parameter information includes the data storage format, serialization parameters and deserialization parameters; The serialization parameters are used to convert the target type data into a data form usable by the code of the first language, and the deserialization parameters are used to convert the target type data into a data form usable by the code of the second language.

6. The method according to claim 1, characterized in that The first language code is obtained by: Get the code file to be converted; A header file in the code file to be converted is determined, and based on the compiler identifying the header file, a first language code to be converted is obtained.

7. The method according to any one of claims 1 to 6, characterized in that: The method further comprises: Before constructing an abstract syntax tree based on the second language, the name and storage path of each structure in the first language code are written into the first language code respectively; And, for any code snippet in the second language, the method further includes: Determine the target storage path in the code snippet and the target class name corresponding to the structure; The file name of the code snippet is set to the target class name, and the file of the code snippet is saved in the target storage path.

8. A code conversion device, characterized in that: include: A first processing module, configured to construct an abstract syntax tree based on a second language based on a first language code to be converted and a preset code mapping relationship; The code mapping relationship is: a mapping relationship between the first language and the second language for a set code; Traversing the abstract syntax tree based on the grammatical rules of the second language to obtain at least one code snippet in the second language; A second processing module is used for determining the attribute parameter information of the target type data in any one of the code snippets, and then adding the attribute code generated based on the attribute parameter information to the code snippet to obtain the code in the second language; The attribute parameter information is used to convert the target type data into a data form that can be used by the code in the first language or the second language.

9. An execution device, characterized in that: include: A memory for storing program instructions; A processor, configured to obtain program instructions in the memory and execute the method according to any one of claims 1 to 7 according to the obtained program instructions.

10. A computer-readable storage medium, characterized in that: The computer-readable storage medium includes computer instructions, and when the computer instructions are executed by a computer, the method according to any one of claims 1 to 7 is implemented.