Code translation method and system, electronic equipment and computer readable storage medium

By performing lexical analysis, syntax analysis and segmentation of the source code, and combining with the code mapping library for code translation, the problems of low code translation efficiency and poor accuracy in the existing technology are solved, and efficient and accurate code translation effect is achieved.

CN119576333APending Publication Date: 2025-03-07DIGITAL CHINA FINANCIAL SOFTWARE LTD
View PDF 0 Cites 5 Cited by

Patent Information

Application Number
CN202411655647.8
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-11-19
Publication Date
2025-03-07

AI Technical Summary

Technical Problem

The existing code translation technology has problems such as difficult to read, manual adjustment, time-consuming and costly, and difficult project implementation. The complexity and flexibility of rule-based code translation are not enough to meet the needs of accurate translation.

Method used

By performing lexical analysis and syntactic analysis of the source code to be translated, an abstract syntax tree is generated, divided into multiple code blocks, and the mapping relationship in the code mapping library is used for code translation, and the translated code blocks are finally compiled into the target code of the target code language.

Benefits of technology

It improves the efficiency and accuracy of code translation, reduces the need for manual adjustment, ensures the integrity and accuracy of compilation, and is suitable for complex financial industry code translation needs.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119576333A_ABST
    Figure CN119576333A_ABST
Patent Text Reader

Abstract

The invention discloses a code translation method and system, electronic equipment and a computer readable storage medium, which are applied to a code translation model, and the code translation method comprises the following steps: generating an abstract syntax tree corresponding to a source code; segmenting the source code according to the code structure of the abstract syntax tree to obtain a plurality of code blocks; matching a corresponding code mapping relationship in a code mapping library; translating the plurality of code blocks into a plurality of target code blocks of a target code language according to a code mapping relationship; and compiling the plurality of target code blocks into target code in a target code language. According to the code translation model, the abstract syntax tree corresponding to the source code is firstly generated, then the whole source code is segmented into the multiple code blocks based on the abstract syntax tree, therefore, the whole task is segmented, then code translation is conducted through the code mapping relation in the code mapping library, the transfer speed and efficiency are improved, and the code translation efficiency is improved. And finally, compiling the plurality of translated target code blocks into a whole section of target code to ensure the integrity and accuracy of compiling.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computers and data processing, and in particular to a code translation method, system, electronic device and computer-readable storage medium. Background Art

[0002] Traditional code translation technology: Before the emergence of big models, code translation mainly relied on source-to-source compiler technology, which usually relied on manually written rewriting rules. However, this method has some defects, such as the conversion results are usually not readable, cannot follow the specifications of the target programming language, and require programmers to manually modify them to run accurately. The entire conversion process is not only time-consuming, but also requires programmers to master the expertise of both the source language and the target language. Therefore, the workload of using this method for code conversion projects is very large and practical application is difficult.

[0003] Rule-based code translation: Machine translation systems for the patent field, such as the Chinese-English patent text machine translation fusion system, combine rule-based translation methods and phrase-based statistical translation systems. The rule system is mainly responsible for the analysis and conversion of the source language, and determines the basic syntactic framework of the target language. The statistical translation system searches for suitable translation word forms based on the generated target syntactic structure during the target language generation phase, and generates the final candidate translation. This method is tested using automatic evaluation indicators, and the results show that the results of the fusion system are better than those of the single rule system and the statistical system, indicating the effectiveness and feasibility of the fusion method.

[0004] However, traditional code translation technology: 1. Lack of readability: The codes converted by traditional methods are often difficult to read and understand because they may not follow the best practices and specifications of the target programming language. 2. Manual adjustment requirements: The converted code often requires programmers to make a lot of manual adjustments to run correctly, which increases the workload and complexity. 3. Time-consuming and costly: Since it requires proficiency in both the source and target languages, this process is time-consuming and costly. 4. Difficult project implementation: Due to the above factors, the application of these methods in actual projects is limited.

[0005] Rule-based code translation has problems of complexity and flexibility: despite the integration of rule-based translation and statistical translation technology, it still cannot completely solve the complexity and flexibility problems in the field of code translation. And the precise translation requirements of technical terms and legal terms: the technical content and legal terms in patent documents need to be accurately translated, which places higher requirements on translators.

[0006] These shortcomings show that, despite the progress of existing technologies in some aspects, there are still significant challenges and limitations in the field of code translation. The emergence of large models provides new possibilities to solve these problems, but the above challenges also need to be overcome to achieve more efficient, accurate and reliable code translation. Summary of the invention

[0007] Embodiments of the present application provide a code translation method, system, electronic device, and computer-readable storage medium to achieve more efficient, accurate, and reliable code translation.

[0008] The present application provides a code translation method, which is applied to a code translation model, including:

[0009] Performing lexical analysis and syntactic analysis on the source code to be translated, and generating an abstract syntax tree corresponding to the source code;

[0010] Obtaining a code structure of the source code according to the abstract syntax tree, and segmenting the source code according to the code structure to obtain a plurality of code blocks;

[0011] According to the source code language and the target code language of the source code, matching the corresponding code mapping relationship in the code mapping library;

[0012] Translating the plurality of code blocks into a plurality of target code blocks of the target code language according to the code mapping relationship;

[0013] compiling a plurality of target code blocks into target codes in the target code language;

[0014] The code mapping library includes code translation mapping relationships between a plurality of different code languages, and the code mapping relationship is a code translation mapping relationship between the source code language and the target code language.

[0015] Optionally, before lexical analysis and syntactic analysis of the source code to be translated, it also includes:

[0016] receiving translation information input into a prompt template and source code to be translated;

[0017] The translation information includes code structure description information of the source code, business logic information, the target code language and output format information.

[0018] Optionally, the process of performing lexical analysis and syntactic analysis on the source code to be translated and generating an abstract syntax tree corresponding to the source code includes:

[0019] According to the code structure description information and the business logic information, lexical analysis and syntactic analysis are performed on the source code to be translated, and an abstract syntax tree corresponding to the source code is generated.

[0020] Optionally, the process of compiling the plurality of target code blocks into target codes in the target code language comprises:

[0021] Compile multiple target code blocks into target codes in the target code language according to the business logic information and the output format information.

[0022] Optionally, the code structure includes a first structure level, a second structure level and a third structure level, the second structure level is a sublevel of the first structure level, and the third structure level is a sublevel of the second structure level;

[0023] The process of segmenting the source code according to the code structure to obtain multiple code blocks includes:

[0024] According to the first structural level, dividing the source code into a plurality of first-level code blocks;

[0025] According to the second structural level, a plurality of second-level code blocks are divided from the plurality of first-level code blocks;

[0026] According to the third structural level, a plurality of third-level code blocks are cut out from the plurality of second-level code blocks.

[0027] The present application also provides a code translation system, which is applied to the code translation model, including:

[0028] A syntax tree generation module, used for performing lexical analysis and syntactic analysis on the source code to be translated, and generating an abstract syntax tree corresponding to the source code;

[0029] A code segmentation module, used to obtain a code structure of the source code according to the abstract syntax tree, and segment the source code according to the code structure to obtain multiple code blocks;

[0030] A code mapping module, used to match a corresponding code mapping relationship in a code mapping library according to a source code language and a target code language of the source code;

[0031] A code translation module, used for translating a plurality of code blocks into a plurality of target code blocks of the target code language according to the code mapping relationship;

[0032] A code compiling module, used for compiling a plurality of target code blocks into target codes of the target code language;

[0033] The code mapping library includes code translation mapping relationships between a plurality of different code languages, and the code mapping relationship is a code translation mapping relationship between the source code language and the target code language.

[0034] Optionally, also include:

[0035] An information receiving module, used for prompting and receiving the translation information input into the prompt template and the source code to be translated;

[0036] The translation information includes code structure description information of the source code, business logic information, the target code language and output format information.

[0037] Optionally, the syntax tree generation module is specifically used to perform lexical analysis and syntactic analysis on the source code to be translated according to the code structure description information and the business logic information, and generate an abstract syntax tree corresponding to the source code.

[0038] The present application also provides an electronic device, which includes a processor and a memory, wherein the memory stores program code, and when the program code is executed by the processor, the processor executes the aforementioned code translation method.

[0039] The present application also provides a computer-readable storage medium, including a program code. When the storage medium is run on an electronic device, the program code is used to enable the electronic device to execute the aforementioned code translation method.

[0040] The present application provides a code translation method, which is applied to a code translation model, and includes: performing lexical analysis and syntactic analysis on a source code to be translated, and generating an abstract syntax tree corresponding to the source code; obtaining a code structure of the source code according to the abstract syntax tree, and segmenting the source code according to the code structure to obtain a plurality of code blocks; matching corresponding code mapping relationships in a code mapping library according to a source code language and a target code language of the source code; translating a plurality of code blocks into a plurality of target code blocks of the target code language according to the code mapping relationship; and compiling a plurality of target code blocks into a target code of the target code language; wherein the code mapping library includes code translation mapping relationships between a plurality of different code languages, and the code mapping relationship is a code translation mapping relationship between the source code language and the target code language.

[0041] The code translation model of the embodiment of the present application first performs syntax and lexical analysis on the source code to obtain the corresponding abstract syntax tree, and then divides the entire source code into multiple code blocks based on the abstract syntax tree, thereby dividing the overall task, and then uses the code mapping relationship in the code mapping library to perform code translation, which improves the transfer speed and efficiency. Finally, the translated multiple target code blocks are compiled into a whole target code to ensure the integrity and accuracy of the compilation. BRIEF DESCRIPTION OF THE DRAWINGS

[0042] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the related technologies, the drawings required for use in the embodiments or the related technical descriptions are briefly introduced below. Obviously, the drawings described below are only the embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on the provided drawings without paying any creative work.

[0043] Figure 1 This is a schematic diagram of an application scenario in an embodiment of the present application;

[0044] Figure 2 This is a flowchart of a code translation method in an embodiment of the present application;

[0045] Figure 3 This is a schematic diagram of a source code in an embodiment of the present application;

[0046] Figure 4 This is a schematic diagram of a target code in an embodiment of the present application;

[0047] Figure 5 This is a schematic diagram of a prompt template in an embodiment of the present application;

[0048] Figure 6 This is a schematic diagram of a code translation model structure in an embodiment of the present application;

[0049] Figure 7 This is a schematic diagram of the structure of a code translation system in an embodiment of the present application;

[0050] Figure 8 A schematic diagram of the hardware structure of an electronic device in an embodiment of the present application. DETAILED DESCRIPTION

[0051] In order to make the purpose, technical scheme and advantages of the present application clearer, the technical scheme in the embodiment of the present application will be clearly and completely described below in conjunction with the drawings in the embodiment of the present application. Obviously, the described embodiment is only a part of the embodiment of the present application, rather than all the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in the field without making creative work are within the scope of protection of the present application. In the absence of conflict, the embodiments in the present application and the features in the embodiments can be arbitrarily combined with each other. In addition, although the logical order is shown in the flow chart, in some cases, the steps shown or described can be performed in an order different from that here.

[0052] The terms "first", "second", etc. in the specification and claims of this application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the numbers used in this way can be interchanged where appropriate, so that the embodiments of the invention described herein can be implemented in sequences other than those illustrated or described herein.

[0053] The following is a brief introduction to the design concept of the embodiment of the present application:

[0054] In the financial industry, the widespread use of C# code and the differences with other programming languages ​​(such as XML and Java) lead to inefficiencies and accuracy issues that may occur during code translation. Traditional manual translation often fails to fully and accurately complete the conversion of complex codes, especially when dealing with privatized industry-specific codes, where the mapping relationship is complex and prone to errors. In addition, although code generation based on big model technology has potential, the generation capability of big models depends on the accuracy and comprehensiveness of the provided prompt word templates. In the prior art, there is a lack of effective mapping of private domain codes in the financial industry and optimization strategies for big model prompt words, making it impossible to achieve efficient and accurate cross-language code translation.

[0055] To this end, the following technical issues need to be solved:

[0056] 1. How to sort out the mapping relationship between C# code and the target language (such as XML, Java) to ensure that the translated code meets the specific scenarios and business requirements of the financial industry and avoids logical and semantic deviations.

[0057] 2. How to configure and design effective large model prompt word templates to fully tap the capabilities of the large model and improve its accuracy and generation effect in the code translation process.

[0058] In response to the code translation needs of private domain systems in the financial industry, this solution proposes a code translation solution based on knowledge base and big model technology, focusing on solving the problem of accurate translation of C# code to XML and Java code, and improving the efficiency and accuracy of code translation in the financial industry.

[0059] The preferred embodiments of the present application are described below in conjunction with the drawings in the specification. It should be understood that the preferred embodiments described herein are only used to illustrate and explain the present application, and are not used to limit the present application. In addition, the embodiments and features in the embodiments of the present application may be combined with each other if there is no conflict.

[0060] like Figure 1 , which is a schematic diagram of an application scenario provided by an embodiment of the present application. In the schematic diagram of the application scenario, a terminal device 101 and a server 102 are included. The terminal device 101 and the server 102 communicate with each other through a communication network.

[0061] The terminal device 101 is an electronic device used by the target object, and the electronic device may be a personal computer, a mobile phone, a tablet computer, a notebook, an e-book reader, a vehicle-mounted terminal, etc. In addition, a code translation client may be installed on the terminal device 101, and the client may be a software (e.g., an APP, a browser, etc.), or a web page, a small program, etc. The target object may use the above-mentioned code translation-related client through the terminal device 101 to perform code translation-related operations.

[0062] The server 102 may be an independent physical server or an edge device 102 in the field of cloud computing. It may also be a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud storage, cloud functions, network services, cloud communications, middleware services, domain name services, security services, content delivery networks (English name: Content Delivery Network, abbreviated as CDN), as well as big data and artificial intelligence platforms.

[0063] There is no restriction on the number of the terminal devices 101 and / or servers 102 .

[0064] It should be noted that the code translation method in the embodiment of the present application can be executed by the terminal device 101 or the server 102 alone, or can be executed jointly by the terminal device 101 and the server 102. For example, when the terminal device 101 and the server 102 are executed jointly, the server 102 can receive the source code to be translated, and then generate an abstract syntax tree corresponding to the source code, segment the source code to obtain multiple code blocks, match the corresponding code mapping relationship in the code mapping library, and translate the multiple code blocks into multiple target code blocks of the target code language according to the code mapping relationship; compile the multiple target code blocks into the target code of the target code language, the terminal device 101 can receive the source code and send it to the server 102, and can also present the target code generated by the server to the user.

[0065] The following describes the code translation method provided by the exemplary embodiment of the present application in combination with the above-mentioned application scenarios and with reference to the accompanying drawings. It should be noted that the above-mentioned application scenarios are only shown to facilitate understanding of the spirit and principles of the present application, and the implementation of the present application is not subject to any limitations in this regard.

[0066] refer to Figure 2 As shown, a code translation method provided in an embodiment of the present application is applied to a code translation model, and the specific implementation process of the method is as follows:

[0067] S11: Perform lexical analysis and syntactic analysis on the source code to be translated, and generate an abstract syntax tree corresponding to the source code.

[0068] By performing lexical and syntactic analysis on the source code, the source code is parsed to identify and classify the various elements of the code, breaking it down into classes, methods, control structures (such as if, else, for, etc.) and basic statements.

[0069] The ultimate goal of parsing C# code is to generate an abstract syntax tree (AST) that can clearly show the hierarchical structure and logical relationship of the code. Through this syntax tree, key elements in the code, such as classes, methods, properties, and control flow structures, can be extracted, laying the foundation for subsequent code translation.

[0070] First, a syntax tree is constructed through lexical analysis and syntactic analysis. In the syntax tree, different types of nodes need to be identified. Common node types include:

[0071] ① Class: Class definition, including class name, modifiers, inheritance relationship, etc.

[0072] ②Method: Method definition, including method name, parameter list, return type, modifiers, etc.

[0073] ③Property: The property in a class, including its data type, accessor, etc.

[0074] ④Control Flow Structures: if-else, switch, for, while and other control structures.

[0075] ⑤Basic Statements: such as variable declaration, assignment, expression, method call, etc.

[0076] This meticulous code parsing process can provide effective support for accurate code translation.

[0077] S12: Obtain a code structure of the source code according to the abstract syntax tree, and segment the source code according to the code structure to obtain multiple code blocks.

[0078] After generating the abstract syntax tree of the source code, the structure of the abstract syntax tree itself can represent the code structure. Therefore, the source code can be segmented according to the code structure displayed by the abstract syntax tree to obtain multiple code blocks. Each code block corresponds to a certain function and structure. For example, several large code blocks can be segmented according to the code framework, and then multiple sub-code blocks can be segmented according to each function in the framework. By dividing the entire code into multiple code blocks, subsequent translation is facilitated, and the code translation model can translate each code better and more accurately, dividing the overall huge task into several small sub-tasks, with higher efficiency and accuracy.

[0079] S13: Matching corresponding code mapping relationships in a code mapping library according to the source code language and the target code language of the source code.

[0080] Pre-build mapping relationships between multiple code languages, for example, the code mapping relationship between C# language and XML language, the code mapping relationship between C# language and Java language, and obtain a code mapping library that includes code translation mapping relationships between multiple different code languages, so that translation between different language codes can be completed directly through mapping relationships.

[0081] For example, the code mapping relationship between C# language and XML language can be as follows:

[0082] There is a direct correspondence between the classes and objects in C# and the tags in XML. The mapping rules should follow the following specifications:

[0083] C# classes are mapped to XML top-level tags: When each C# class is translated into XML, the class name is mapped to the top-level tag of XML, indicating the beginning of the object.

[0084] Here is an example:

[0085] C# code:

[0086] public class Transaction

[0087] {

[0088] public string TransactionId{get;set;}

[0089] public decimal Amount{get;set;}

[0090] public DateTime Date{get;set;}

[0091] }

[0092] XML example

[0093] <transaction>

[0094] <transactionid>< / transactionid>

[0095] <amount>< / amount>

[0096] <date>< / date>

[0097] < / transaction>

[0098] The attributes in the C# class are mapped to XML sub-tags or tag attributes, depending on the business requirements:

[0099] Simple type attributes are mapped as sub-tags: C#'s basic data types (such as strings, integers, floating-point numbers, dates, etc.) are mapped as XML sub-tags, and the name of the sub-tag is consistent with the attribute name.

[0100] The C# code example is as follows:

[0101] public class Customer

[0102] {

[0103] public string Name{get;set;}

[0104] public int Age{get;set;}

[0105] }

[0106] Xml Example

[0107] <customer>

[0108] <name>< / name>

[0109] <age>< / age>

[0110] < / customer>

[0111] Complex type attributes are mapped as nested tags: If a C# class contains other class objects as attributes, the class objects corresponding to the attributes should be mapped as nested tags when mapped to XML.

[0112] C# code example:

[0113] public class Account

[0114] {

[0115] public string AccountId{get;set;}

[0116] public Customer AccountHolder{get;set;}

[0117] }

[0118] Xml Example

[0119] <account>

[0120] <accountid>< / accountid>

[0121] <accountholder>

[0122] <name>< / name>

[0123] <age>< / age>

[0124] < / accountholder>

[0125] < / account>

[0126] When mapping to XML, control structures in C# (such as if-else, switch, for loop, etc.) need to be converted into appropriate conditional semantic structures. If-else is mapped to conditional tags: Map the if-else structure to conditional tags in XML. The conditional expression can be used as an XML attribute or tag content.

[0127] C# code example:

[0128]

[0129] Xml Code Example

[0130]

[0131] Loop structures are mapped to repeated labels: Loop structures such as for and while should be mapped to repeated labels or list structures to indicate repeated processing of data items.

[0132] C# code example:

[0133] for(int i=0; i <transactions.Count;i++)

[0134] {

[0135] Console.WriteLine(transactions[i].TransactionId);

[0136] }

[0137] Xml Code Example

[0138] <transactions>

[0139] <transaction>

[0140] <transactionid>< / transactionid>

[0141] < / transaction>

[0142] < / transactions>

[0143] The translation rules of C# methods need to be processed according to business needs. Generally, the method itself is not directly mapped to XML, but the result or logic of the method call can be mapped to XML content or tags. The result of the method call is mapped to the tag content: If the C# method generates result data, the return value of the method can be mapped to the tag content or sub-tag when translated to XML.

[0144] C# code example:

[0145] public decimal GetBalance()

[0146] {

[0147] return balance;

[0148] }

[0149] Xml Code Example

[0150] <balance> 1000.00< / balance>

[0151] For complex business logic, the mapping process should convert the logical operations into business semantic descriptions in XML according to the specific needs of the industry. For example, logic such as transaction status, amount verification, and user identity authentication can be expressed through specific XML tags. Business validation logic is mapped to conditional or rule tags: Validation and inspection logic in the financial industry can be marked with conditional or rule tags in XML.

[0152] C# code example:

[0153] if(isVerified)

[0154] {

[0155] ProcessTransaction();

[0156] }

[0157] Xml Code Example

[0158] <verification>

[0159] <status> Verified< / status>

[0160] <action> ProcessTransaction< / action>

[0161] < / verification>

[0162] S14: translating the plurality of code blocks into a plurality of target code blocks of a target code language according to the code mapping relationship.

[0163] The multiple code blocks of the source code language are translated into multiple target code blocks of the target code language through the above code mapping relationship to obtain multiple target code blocks of the target code language. At this time, the multiple target code blocks have not yet been compiled to form a complete code language.

[0164] S15: Compile the multiple target code blocks into target codes of the target code language.

[0165] After obtaining multiple target code blocks translated into the target code language, the code translation model recompiles the scattered multiple target code blocks into a whole code and reconstructs the target code as a whole. The code translation model can use the business logic relationship between the target code blocks and the code structure information for compilation, so as to ensure that the compiled complete target code is accurate and can retain the original functions. At the same time, the code translation model can use the historical source code for compilation training in advance, so as to provide accurate compilation functions when used, for example, Figure 3 The source code shown can be transformed into Figure 4 The object code shown.

[0166] It can be seen that in the embodiment of the present application, the code translation model first performs syntax and lexical analysis on the source code to obtain the corresponding abstract syntax tree, and then divides the entire source code into multiple code blocks based on the abstract syntax tree, thereby dividing the overall task, and then uses the code mapping relationship in the code mapping library to perform code translation, which improves the transfer speed and efficiency, and finally compiles the translated multiple target code blocks into a whole target code to ensure the integrity and accuracy of the compilation.

[0167] In some embodiments of the present disclosure, before performing lexical analysis and syntactic analysis on the source code to be translated, step S10 may also be included: receiving translation information input into the prompt template and the source code to be translated;

[0168] The translation information includes the code structure description information of the source code, business logic information, target code language and output format information.

[0169] In order to effectively utilize the generation capability of large models, precise prompt templates are designed in code translation tasks so that users can accurately input relevant information of the source code and the required target code.

[0170] When designing a prompt template, you can follow the following principles:

[0171] Clarity: The prompt word should clearly describe the code structure, business logic and expected goals that need to be translated, and avoid vague expressions.

[0172] Context provision: Provide sufficient context information to ensure that the large model can accurately understand the business background and logical structure of the C# code.

[0173] Semantic consistency: The description in the template should maintain semantic consistency and avoid terms or expressions that do not match the target language.

[0174] Formatting requirements: Provide formatting guidance for the generated target code to ensure that the output code meets specific industry specifications or format requirements.

[0175] For example, Figure 5 As shown, in order to better guide the large model to complete the translation of C# code to XML, the template should contain the following parts:

[0176] (1) Code structure description: clearly indicate the structure of the C# code snippet that needs to be translated (such as class, method, property, etc.).

[0177] (2) Business logic interpretation: Provides the business background and logical functions of the code snippet to help the big model understand the semantics of the code.

[0178] (3) Target language requirements: Detailed description of the format and semantic requirements of the target language (such as XML) to ensure that the generated code complies with the specifications.

[0179] (4) Output format prompts: Guide large models to output code in a standardized format to ensure that the code is easy to read and maintain.

[0180] It is understandable that the prompt template will display different template styles according to the source code and the target code. At the same time, it can dynamically update the rich content according to the changes in the translation language and application scenarios in actual use. The prompt template can be updated through self-iteration during the use of the code compilation model, or it can be updated manually.

[0181] Furthermore, S11: the process of performing lexical analysis and syntactic analysis on the source code to be translated and generating an abstract syntax tree corresponding to the source code can be specifically performed according to the code structure description information and business logic information, performing lexical analysis and syntactic analysis on the source code to be translated and generating an abstract syntax tree corresponding to the source code.

[0182] The code structure description information and business logic information can help the code translation model to more accurately grasp the structure of the source code, better perform lexical analysis and syntactic analysis, and generate an accurate abstract syntax tree.

[0183] In some embodiments of the present disclosure, based on the aforementioned prompt template, S15 compiles multiple target code blocks into target codes of the target code language, which can be specifically compiled into target codes of the target code language according to the business logic information and the output format information. The code translation model can better determine the business logic between the target code blocks according to the business logic information, thereby better restoring the overall code by compiling, and ensuring the accuracy of the target code.

[0184] In some embodiments of the present disclosure, the code structure includes a first structure level, a second structure level, and a third structure level, the second structure level is a sub-level of the first structure level, and the third structure level is a sub-level of the second structure level;

[0185] Step S12 is a process of dividing the source code according to the code structure to obtain multiple code blocks, which may include S121 to S123;

[0186] S121: dividing the source code into a plurality of first-level code blocks according to the first structural level;

[0187] S122: dividing a plurality of first-level code blocks into a plurality of second-level code blocks according to the second structural level;

[0188] S123: According to the third structural level, a plurality of third-level code blocks are divided from the plurality of second-level code blocks.

[0189] The code can be segmented in a multi-level form to improve segmentation efficiency and the independence of each code block. For example, based on the syntax tree obtained by parsing, the code can be further logically segmented. The principle of segmentation is based on the hierarchical division of the code structure, which can be specifically segmented according to the following dimensions:

[0190] ① Class level segmentation: Each class is segmented as an independent unit.

[0191] ②Method level segmentation: The methods in each class are segmented as sub-units.

[0192] ③ Statement level segmentation: Each control structure and basic statement in the method is segmented as a basic unit to facilitate subsequent step-by-step translation.

[0193] Among them, class level segmentation can correspond to the first structural level, method level segmentation can correspond to the second structural level, and statement level segmentation can correspond to the third structural level.

[0194] The segmented code structure facilitates gradual translation, especially complex code blocks can be processed in segments to ensure the accuracy and controllability of the translation.

[0195] It should be noted that the final code blocks may include multiple first-level code blocks, multiple second-level code blocks and multiple third-level code blocks. It does not mean that all the upper-level code blocks are divided into lower-level code blocks after splitting. The upper-level code blocks may still include structures and frameworks that are not included in the lower-level code blocks, as well as logical index relationships.

[0196] In addition, see Figure 6 The figure shows a schematic diagram of the overall code translation model structure.

[0197] This application also discloses a code translation system, see Figure 7 As shown, it is applied to the code translation model, including:

[0198] A syntax tree generation module 61 is used to perform lexical analysis and syntactic analysis on the source code to be translated, and generate an abstract syntax tree corresponding to the source code;

[0199] A code segmentation module 62 is used to obtain a code structure of a source code according to an abstract syntax tree, and segment the source code according to the code structure to obtain a plurality of code blocks;

[0200] A code mapping module 63, used to match a corresponding code mapping relationship in a code mapping library according to a source code language and a target code language of the source code;

[0201] A code translation module 64, used for translating the plurality of code blocks into a plurality of target code blocks of a target code language according to the code mapping relationship;

[0202] A code compiling module 65, for compiling a plurality of target code blocks into target codes in a target code language;

[0203] The code mapping library includes code translation mapping relationships between multiple different code languages, and the code mapping relationship is a code translation mapping relationship between a source code language and a target code language.

[0204] It can be seen that in the embodiment of the present application, the code translation model first performs syntax and lexical analysis on the source code to obtain the corresponding abstract syntax tree, and then divides the entire source code into multiple code blocks based on the abstract syntax tree, thereby dividing the overall task, and then uses the code mapping relationship in the code mapping library to perform code translation, which improves the transfer speed and efficiency, and finally compiles the translated multiple target code blocks into a whole target code to ensure the integrity and accuracy of the compilation.

[0205] Among them, it also includes:

[0206] An information receiving module, used for prompting and receiving the translation information input into the prompt template and the source code to be translated;

[0207] The translation information includes the code structure description information of the source code, business logic information, target code language and output format information.

[0208] The syntax tree generation module 61 is specifically used to perform lexical analysis and syntactic analysis on the source code to be translated according to the code structure description information and the business logic information, and generate an abstract syntax tree corresponding to the source code.

[0209] The code translation module 64 is specifically used for compiling multiple target code blocks into target codes of the target code language, including:

[0210] The plurality of target code blocks are compiled into target codes of a target code language according to the business logic information and the output format information.

[0211] Wherein, the code structure includes a first structure level, a second structure level and a third structure level, the second structure level is a sub-level of the first structure level, and the third structure level is a sub-level of the second structure level;

[0212] The code segmentation module 62 is specifically used to segment a plurality of first-level code blocks from the source code according to a first structural level; to segment a plurality of second-level code blocks from a plurality of first-level code blocks according to a second structural level; and to segment a plurality of third-level code blocks from a plurality of second-level code blocks according to a third structural level.

[0213] In some possible implementations, the electronic device according to the present application may include at least a processor and a memory. The memory stores program code, and when the program code is executed by the processor, the processor executes the steps of the code translation method according to various exemplary implementations of the present application described in this specification. For example, the processor may execute the following steps: Figure 2 Follow the steps shown in .

[0214] Based on the same inventive concept, an electronic device is also provided in the embodiment of the present application. The electronic device can implement the functions of the aforementioned code translation method and device. Figure 8 , electronic equipment includes:

[0215] At least one processor 701, and a memory 702 connected to the at least one processor 701. The specific connection medium between the processor 701 and the memory 702 is not limited in the embodiment of the present application. Figure 8 In the example, the processor 701 and the memory 702 are connected via a bus 700. Figure 8 The connections between other components are shown in bold lines, and are not intended to be limiting. The bus 700 can be divided into an address bus, a data bus, a control bus, etc. For ease of illustration, Figure 8 Only one thick line is used in the figure, but it does not mean that there is only one bus or one type of bus. Alternatively, the processor 701 can also be called a controller, and there is no limitation on the name.

[0216] In the embodiment of the present application, the memory 702 stores instructions that can be executed by at least one processor 701. The at least one processor 701 can execute the code translation method discussed above by executing the instructions stored in the memory 702. The processor 701 can implement Figure 7 The functions of each module in the device shown.

[0217] Among them, the processor 701 is the control center of the device, and can use various interfaces and lines to connect the various parts of the entire control device. By running or executing instructions stored in the memory 702 and calling data stored in the memory 702, the various functions of the device and process data, the device can be monitored as a whole.

[0218] In one possible design, the processor 701 may include one or more processing units, and the processor 701 may integrate an application processor and a modem processor, wherein the application processor mainly processes an operating system, a user interface, and application programs, and the modem processor mainly processes wireless communications. It is understandable that the modem processor may not be integrated into the processor 701. In some embodiments, the processor 701 and the memory 702 may be implemented on the same chip, and in some embodiments, they may also be implemented separately on separate chips.

[0219] Processor 701 can be a general-purpose processor, such as a central processing unit (CPU), 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, a discrete hardware component, and can implement or execute the various methods, steps and logic block diagrams disclosed in the embodiments of the present application. A general-purpose processor can be a microprocessor or any conventional processor, etc. The steps of the code translation method disclosed in the embodiments of the present application can be directly embodied as a hardware processor to be executed, or a combination of hardware and software modules in the processor can be executed.

[0220] The memory 702 is a non-volatile computer-readable storage medium that can be used to store non-volatile software programs, non-volatile computer executable programs and modules. The memory 702 may include at least one type of storage medium, such as a flash memory, a hard disk, a multimedia card, a card-type memory, a random access memory (Random Access Memory, RAM), a static random access memory (Static Random Access Memory, SRAM), a programmable read-only memory (Programmable Read Only Memory, PROM), a read-only memory (Read Only Memory, ROM), an electrically erasable programmable read-only memory (Electrically Erasable Programmable Read-Only Memory, EEPROM), a magnetic memory, a disk, an optical disk, etc. The memory 702 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 702 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.

[0221] By designing and programming the processor 701, the code corresponding to the code translation method described in the above embodiment can be fixed into the chip, so that the chip can execute the code translation method when running. Figure 2The steps of the code translation method of the embodiment shown are as follows: How to design and program the processor 701 is a technique well known to those skilled in the art and will not be described in detail here.

[0222] Based on the same inventive concept, an embodiment of the present application further provides a computer-readable storage medium, which stores computer instructions. When the computer instructions are executed on a computer, the computer executes the code translation method discussed above.

[0223] In some possible implementations, various aspects of the code translation method provided by the present application may also be implemented in the form of a program product, which includes program code. When the program product is run on an apparatus, the program code is used to enable the control device to execute the steps of the code translation method according to various exemplary implementations of the present application described above in this specification.

[0224] 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.

[0225] 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 embodiments of 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 generate 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.

[0226] 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.

[0227] 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. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.

[0228] 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 translation method, characterized in that: Applied to code translation models, including: Performing lexical analysis and syntactic analysis on the source code to be translated, and generating an abstract syntax tree corresponding to the source code; Obtaining a code structure of the source code according to the abstract syntax tree, and segmenting the source code according to the code structure to obtain a plurality of code blocks; According to the source code language and the target code language of the source code, matching the corresponding code mapping relationship in the code mapping library; Translating the plurality of code blocks into a plurality of target code blocks of the target code language according to the code mapping relationship; compiling a plurality of target code blocks into target codes in the target code language; The code mapping library includes code translation mapping relationships between a plurality of different code languages, and the code mapping relationship is a code translation mapping relationship between the source code language and the target code language.

2. The code translation method according to claim 1, characterized in that: Before the lexical analysis and syntactic analysis of the source code to be translated, it also includes: receiving translation information input into a prompt template and source code to be translated; The translation information includes code structure description information of the source code, business logic information, the target code language and output format information.

3. The code translation method according to claim 2, characterized in that: The process of performing lexical analysis and syntactic analysis on the source code to be translated and generating an abstract syntax tree corresponding to the source code includes: According to the code structure description information and the business logic information, lexical analysis and syntactic analysis are performed on the source code to be translated, and an abstract syntax tree corresponding to the source code is generated.

4. The code translation method according to claim 2, characterized in that: The process of compiling a plurality of target code blocks into target codes in the target code language comprises: Compile multiple target code blocks into target codes in the target code language according to the business logic information and the output format information.

5. The code translation method according to any one of claims 1 to 4, characterized in that: The code structure includes a first structure level, a second structure level and a third structure level, the second structure level is a sub-level of the first structure level, and the third structure level is a sub-level of the second structure level; The process of segmenting the source code according to the code structure to obtain multiple code blocks includes: According to the first structural level, dividing the source code into a plurality of first-level code blocks; According to the second structural level, a plurality of second-level code blocks are divided from the plurality of first-level code blocks; According to the third structural level, a plurality of third-level code blocks are cut out from the plurality of second-level code blocks.

6. A code translation system, characterized in that: Applied to code translation models, including: A syntax tree generation module, used for performing lexical analysis and syntactic analysis on the source code to be translated, and generating an abstract syntax tree corresponding to the source code; A code segmentation module, used to obtain a code structure of the source code according to the abstract syntax tree, and segment the source code according to the code structure to obtain multiple code blocks; A code mapping module, used to match a corresponding code mapping relationship in a code mapping library according to a source code language and a target code language of the source code; A code translation module, used for translating a plurality of code blocks into a plurality of target code blocks of the target code language according to the code mapping relationship; A code compiling module, used for compiling a plurality of target code blocks into target codes of the target code language; The code mapping library includes code translation mapping relationships between a plurality of different code languages, and the code mapping relationship is a code translation mapping relationship between the source code language and the target code language.

7. The code translation system according to claim 6, characterized in that: Also includes: An information receiving module, used for prompting and receiving the translation information input into the prompt template and the source code to be translated; The translation information includes code structure description information of the source code, business logic information, the target code language and output format information.

8. The code translation system according to claim 7, characterized in that: The syntax tree generation module is specifically used to perform lexical analysis and syntactic analysis on the source code to be translated according to the code structure description information and the business logic information, and generate an abstract syntax tree corresponding to the source code.

9. An electronic device, characterized in that: The invention comprises a processor and a memory, wherein the memory stores program codes, and when the program codes are executed by the processor, the processor executes the code translation method according to any one of claims 1 to 5.

10. A computer-readable storage medium, characterized in that: The invention comprises a program code, and when the storage medium is run on an electronic device, the program code is used to enable the electronic device to execute the code translation method according to any one of claims 1 to 5.

Citation Information

Cited By

  • Code generation method, computing device, electronic device and storage medium

    CN119883939A

  • Code processing method and device, equipment, storage medium and program product

    CN120723248A

  • A code processing method, apparatus, device, storage medium, and program product

    CN120723248B

  • Software technology stack renovation method, system and equipment based on artificial intelligence and storage medium

    CN122086367A

  • Method and device for code conversion, equipment, storage medium and program product

    CN122489078A