Application program code conversion method and device and computer equipment
By generating an abstract syntax tree and monitoring semantic features to perform code conversion actions, the problem of poor accuracy in complex code conversion in the existing technology is solved, and efficient and accurate code conversion under complex structures is achieved.
Patent Information
- Application Number
- CN202410726857.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-06
- Publication Date
- 2024-10-01
AI Technical Summary
Existing code conversion methods are difficult to achieve accurate conversion under complex code structures and rely on manual processing or complex regular expressions, resulting in poor conversion accuracy and large manpower and resource investment and risks.
By obtaining the syntax reference information of the original code and the target code, an abstract syntax tree is generated, and the semantic characteristics of the information nodes are monitored according to the predefined operation information, and code conversion actions are performed. The abstract syntax tree is used to deal with context sensitivity issues, and the monitoring and traversal are combined. In a combined manner, predefined operation information is defined to perform conversion actions.
It improves the accuracy of code conversion, reduces human resource investment, reduces conversion risks, and enables flexible and accurate code conversion under complex code structures.
Smart Images

Figure CN118733046A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of artificial intelligence technology, and in particular to an application code conversion method, apparatus, computer equipment, computer-readable storage medium, and computer program product. Background Art
[0002] As modern computer service architectures become increasingly complex, new platforms, frameworks, and technology stacks continue to emerge. To keep systems and software up-to-date and achieve better performance and security, a series of operations, including platform migration, version upgrades, technology stack conversions, and code refactoring and optimization, are often required.
[0003] While revising and refactoring existing code is relatively straightforward, changing the code language requires significant manpower and resources. Code conversion itself is challenging, and the compatibility and dependencies between existing code and new code or new technologies must be considered. Manual conversion inevitably introduces certain risks and uncertainties, requiring continuous testing to mitigate conversion risks and ensure consistent results.
[0004] However, current code conversion methods rely on manual processing or complex regular expressions, which are difficult to meet the demand for accurate conversion of complex code structures. In the case of complex code structures, the accuracy of code conversion is poor. Summary of the Invention
[0005] Based on this, it is necessary to provide an application code conversion method, device, computer equipment, computer-readable storage medium and computer program product that can improve the accuracy of code conversion when the code structure is complex to address the above technical problems.
[0006] In a first aspect, the present application provides an application code conversion method, comprising:
[0007] Obtaining original syntax reference information of the original code and target syntax reference information of the target code, and obtaining predefined operation information based on the target syntax reference information, wherein the original syntax reference information is reference information set for the programming language corresponding to the original code, the target syntax reference information is reference information set for the programming language corresponding to the target code, and the predefined operation information is used to represent the relationship between the state of the information node and the code conversion action;
[0008] generating an abstract syntax tree according to the original syntax reference information, wherein the abstract syntax tree includes at least one of the information nodes;
[0009] Traversing each of the information nodes in the abstract syntax tree, and monitoring semantic features of the information nodes according to the predefined operation information;
[0010] In a case where the semantic feature of the information node matches the preset feature in the predefined operation information, the code conversion action indicated by the predefined operation information is performed to convert the original code into the target code.
[0011] In one embodiment, generating an abstract syntax tree according to the original syntax reference information includes:
[0012] Extracting semantic features from the original grammatical reference information and generating a semantic parser;
[0013] The original code is parsed using the semantic parser to obtain an abstract syntax tree.
[0014] In one embodiment, the semantic parser includes a lexical analyzer and a syntax analyzer; and the using of the semantic parser to parse the original code to obtain an abstract syntax tree includes:
[0015] Splitting the original code using the lexical analyzer to obtain an original word set;
[0016] The syntax analyzer is used to associate and combine the elements in the original word set to generate an abstract syntax tree.
[0017] In one embodiment, the acquiring predefined operation information according to the target syntax reference information includes:
[0018] Acquire a predefined operation information set, wherein the predefined operation information set is used to record predefined operation information corresponding to different target codes;
[0019] When there is predefined operation information matching the target syntax reference information in the predefined operation information set, directly acquiring the predefined operation information according to the target code identifier in the target syntax reference information;
[0020] In a case where there is no predefined operation information matching the target syntax reference information in the predefined operation information set, predefined operation information is compiled according to the target syntax reference information, and the predefined operation information is written into the predefined operation information set.
[0021] In one embodiment, the method further comprises:
[0022] Monitoring the execution of the code conversion action indicated by the predefined operation information to obtain a monitoring record;
[0023] In a case where the monitoring record indicates that an error occurs in the execution of the code conversion action, the corresponding predefined operation information is updated according to the monitoring record and the target syntax reference information.
[0024] In one embodiment, the monitoring record includes an error code line identifier and exception information corresponding to the error code line; and executing the code conversion action indicated by the predefined operation information to convert the original code into the target code includes:
[0025] In the case where an error is detected in the execution of the code conversion action, the current code conversion action is skipped according to the error code line identifier, and the next code conversion action is continued to be executed.
[0026] In a second aspect, the present application further provides an application code conversion device, comprising:
[0027] an information acquisition module, configured to acquire original syntax reference information of the original code and target syntax reference information of the target code, and to acquire predefined operation information based on the target syntax reference information, wherein the original syntax reference information is reference information set for the programming language corresponding to the original code, the target syntax reference information is reference information set for the programming language corresponding to the target code, and the predefined operation information is used to represent the relationship between the state of the information node and the code conversion action;
[0028] a node analysis module, configured to generate an abstract syntax tree according to the original syntax reference information, wherein the abstract syntax tree includes at least one of the information nodes;
[0029] A node traversal module, configured to traverse each of the information nodes in the abstract syntax tree and monitor semantic features of the information nodes according to the predefined operation information;
[0030] The code conversion module is used to execute the code conversion action indicated by the predefined operation information when the semantic feature of the information node matches the preset feature in the predefined operation information, so as to convert the original code into the target code.
[0031] In one embodiment, the node analysis module includes:
[0032] A semantic analysis submodule, configured to extract semantic features from the original grammatical reference information and generate a semantic parser;
[0033] The code parsing submodule is used to parse the original code using the semantic parser to obtain an abstract syntax tree.
[0034] In one embodiment, the semantic parser includes a lexical analyzer and a syntax analyzer; the code parsing submodule includes:
[0035] A lexical analysis unit, configured to use the lexical analyzer to split the original code to obtain an original word set;
[0036] The syntax analysis unit is used to use the syntax analyzer to associate and combine the elements in the original word set to generate an abstract syntax tree.
[0037] In one embodiment, the information acquisition module includes:
[0038] An information aggregation submodule, configured to obtain a predefined operation information set, wherein the predefined operation information set is configured to record predefined operation information corresponding to different target codes;
[0039] an information acquisition submodule, configured to directly acquire the predefined operation information according to the target code identifier in the target syntax reference information when there is predefined operation information matching the target syntax reference information in the predefined operation information set;
[0040] The information generation submodule is used to write predefined operation information according to the target grammar reference information and write the predefined operation information into the predefined operation information set when there is no predefined operation information matching the target grammar reference information in the predefined operation information set.
[0041] In one embodiment, the apparatus further comprises:
[0042] an action monitoring module, configured to monitor the execution of the code conversion action indicated by the predefined operation information and obtain a monitoring record;
[0043] The error handling module is configured to update the corresponding predefined operation information according to the monitoring record and the target syntax reference information when the monitoring record indicates that an error occurs in the execution of the code conversion action.
[0044] In one embodiment, the monitoring record includes an error code line identifier and exception information corresponding to the error code line; the code conversion module is also used to skip the current code conversion action according to the error code line identifier when an error occurs in the execution of the code conversion action, and continue to execute the next code conversion action.
[0045] In a third aspect, the present application further provides a computer device comprising a memory and a processor, wherein the memory stores a computer program, and when the processor executes the computer program, the following steps are implemented:
[0046] Obtaining original syntax reference information of the original code and target syntax reference information of the target code, and obtaining predefined operation information based on the target syntax reference information, wherein the original syntax reference information is reference information set for the programming language corresponding to the original code, the target syntax reference information is reference information set for the programming language corresponding to the target code, and the predefined operation information is used to represent the relationship between the state of the information node and the code conversion action;
[0047] generating an abstract syntax tree according to the original syntax reference information, wherein the abstract syntax tree includes at least one of the information nodes;
[0048] Traversing each of the information nodes in the abstract syntax tree, and monitoring semantic features of the information nodes according to the predefined operation information;
[0049] In a case where the semantic feature of the information node matches the preset feature in the predefined operation information, the code conversion action indicated by the predefined operation information is performed to convert the original code into the target code.
[0050] In a fourth aspect, the present application further provides a computer-readable storage medium having a computer program stored thereon, wherein when the computer program is executed by a processor, the following steps are implemented:
[0051] Obtaining original syntax reference information of the original code and target syntax reference information of the target code, and obtaining predefined operation information based on the target syntax reference information, wherein the original syntax reference information is reference information set for the programming language corresponding to the original code, the target syntax reference information is reference information set for the programming language corresponding to the target code, and the predefined operation information is used to represent the relationship between the state of the information node and the code conversion action;
[0052] generating an abstract syntax tree according to the original syntax reference information, wherein the abstract syntax tree includes at least one of the information nodes;
[0053] Traversing each of the information nodes in the abstract syntax tree, and monitoring semantic features of the information nodes according to the predefined operation information;
[0054] In a case where the semantic feature of the information node matches the preset feature in the predefined operation information, the code conversion action indicated by the predefined operation information is performed to convert the original code into the target code.
[0055] In a fifth aspect, the present application further provides a computer program product, comprising a computer program, which, when executed by a processor, implements the following steps:
[0056] Obtaining original syntax reference information of the original code and target syntax reference information of the target code, and obtaining predefined operation information based on the target syntax reference information, wherein the original syntax reference information is reference information set for the programming language corresponding to the original code, the target syntax reference information is reference information set for the programming language corresponding to the target code, and the predefined operation information is used to represent the relationship between the state of the information node and the code conversion action;
[0057] generating an abstract syntax tree according to the original syntax reference information, wherein the abstract syntax tree includes at least one of the information nodes;
[0058] Traversing each of the information nodes in the abstract syntax tree, and monitoring semantic features of the information nodes according to the predefined operation information;
[0059] In a case where the semantic feature of the information node matches the preset feature in the predefined operation information, the code conversion action indicated by the predefined operation information is performed to convert the original code into the target code.
[0060] The above-mentioned application code conversion method, apparatus, computer device, computer-readable storage medium, and computer program product obtain the original syntax reference information of the original code and the target syntax reference information of the target code, thereby obtaining predefined operation information based on the target syntax reference information, and then generating an abstract syntax tree including at least one information node based on the original syntax reference information, thereby using the abstract syntax tree to achieve a deeper understanding of the code structure and effectively handle the problem of context sensitivity. Subsequently, each information node in the abstract syntax tree is traversed, and the semantic features of the information node are monitored based on the predefined operation information. When the semantic features of the information node match the preset features in the predefined operation information, the code conversion action indicated by the predefined operation information is executed to convert the original code into the target code. This solution establishes an abstract syntax tree to handle the problem of context sensitivity, adopts predefined specific conversion rules, rather than relying solely on pattern matching, and adopts a combination of monitoring and traversal, so that the predefined operation information is used to define the operation to be performed when the key node is monitored, which can improve the accuracy of code conversion in the case of complex code structure. BRIEF DESCRIPTION OF THE DRAWINGS
[0061] In order to more clearly illustrate the technical solutions in the embodiments of the present application or related technologies, the following briefly introduces the drawings required for use in the embodiments of the present application or related technical descriptions. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without paying any creative work.
[0062] FIG1 is a diagram illustrating an application environment of a method for converting application code according to an embodiment;
[0063] FIG2 is a schematic diagram of a flow chart of a method for converting application code according to an embodiment;
[0064] FIG3 is a flow diagram of step S206 of a method for converting application code in one embodiment;
[0065] FIG4 is a schematic flow chart of a method for converting application code according to another embodiment;
[0066] FIG5 is a block diagram of an application code conversion device according to an embodiment;
[0067] FIG6 is a diagram showing the internal structure of a computer device according to one embodiment;
[0068] FIG7 is a diagram showing the internal structure of a computer device in another embodiment. DETAILED DESCRIPTION
[0069] In order to make the purpose, technical solutions and advantages of this application more clear, the present application is further described in detail below with reference to the accompanying drawings and examples. It should be understood that the specific embodiments described herein are only used to explain this application and are not used to limit this application. It should be noted that in the embodiments of this application, certain software, groups, models and other existing solutions in the industry may be mentioned, which should be considered as exemplary. Their purpose is only to illustrate the feasibility of implementing the technical solution of this application, but does not mean that the applicant has or will necessarily use the solution.
[0070] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, storage, and display, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, use, and processing of the relevant data must comply with relevant regulations. The acquisition, storage, use, and processing of data in this application's technical solution comply with relevant national laws and regulations.
[0071] The application code conversion method provided in the embodiments of the present application can be applied in the application environment shown in Figure 1. Terminal 102 communicates with server 104 via a network. Terminal 102 can receive predefined operation information and send it to server 104 for storage in a data storage system. The data storage system can store data that server 104 needs to process. The data storage system can be integrated with server 104 or located in the cloud or on another network server. The data storage system can store data such as original syntax reference information for the source code and target syntax reference information for the target code, as well as predefined operation information. It can also be used to cache intermediate information such as the generated abstract syntax tree. Terminal 102 can be, but is not limited to, various personal computers, laptops, smartphones, tablets, IoT devices, and portable wearable devices. IoT devices can include smart speakers, smart TVs, smart air conditioners, smart car devices, projectors, and the like. Portable wearable devices can include smart watches, smart bracelets, head-mounted devices, and the like. Head-mounted devices can include virtual reality (VR) devices, augmented reality (AR) devices, smart glasses, and the like. The server 104 may be an independent physical server, a server cluster or a distributed system composed of multiple physical servers, or a cloud server that provides cloud computing services.
[0072] Current code conversion solutions are often based on established regular expressions or natural language processing technologies. These solutions have several drawbacks: Lack of grammatical structure understanding: Regular expression and natural language processing tools are typically based on pattern matching and lack a deep understanding of the code's grammatical structure. This leads to a lack of context sensitivity when handling complex code conversions. Difficulty in handling complexity: Regular expression and natural language processing tools can become complex and difficult to maintain when handling complex programming language structures. Code often contains nested structures, conditional statements, and loops, which are difficult to capture with simple pattern matching. Insufficient semantic understanding: Regular expression and natural language processing may not capture the semantic meaning of the code. Code conversion requires more semantic understanding to ensure that the converted code remains correct and retains the same functionality. In short, some existing solutions lack a deep understanding of the syntax and characteristics of the programming language itself and are difficult to scale. Solutions based on natural language processing may even rely on hardware and environment.
[0073] In an exemplary embodiment, as shown in FIG2 , a method for converting application code is provided, which is described by taking the method applied to the server 104 in FIG1 as an example, and includes the following steps S202 to S208 .
[0074] Step S202: obtaining original syntax reference information of the original code and target syntax reference information of the target code, and obtaining predefined operation information according to the target syntax reference information.
[0075] The original code and target code are codes written in different programming languages that require code conversion. For example, SAS code is converted from the original code to Python code (target code). Predefined operation information represents the relationship between the state of an information node and the code conversion action.
[0076] The original syntax reference information is the reference information set for the programming language corresponding to the original code, including all structures and rules related to the source language (such as SAS).
[0077] Exemplarily, the original syntax reference information may include: reserved words in a specific language; the grammatical rules of the language, such as how to define a data step, how to perform sorting operations, etc.; the data types supported in the language, such as numeric types and character types in SAS; common code structures in the language, such as data steps and process steps, etc.; control structures in the language, such as conditional statements and loop statements, etc.; predefined functions and procedures in the language, such as statistical functions and data management procedures in SAS, etc.; variable naming rules and usage methods; the meaning of the code in a specific context, such as a variable may have different meanings in a data step and in a process step.
[0078] The target syntax reference information is reference information set for the programming language corresponding to the target code, and refers to all structures and rules related to the target language (such as Python).
[0079] Exemplarily, the target syntax reference information may include: reserved words in the target language, such as def, import, for, while in Python; grammatical rules of the target language, such as how to define functions, how to perform sorting operations, etc.; data types supported in the target language, such as integers, floating-point numbers, strings, lists, dictionaries, etc. in Python; common code structures in the target language, such as function definition (def), class definition (class), etc.; control structures in the target language, such as conditional statements (if-elif-else), loop statements (for, while), etc.; predefined functions and modules in the target language, such as print(), len() in Python, and commonly used modules such as pandas, numpy, etc.; variable naming rules and usage methods in the target language; the meaning of code in a specific context, such as the meaning of a variable inside a function and in the global scope may be different.
[0080] For example, server 104 can first compile a SAS code programming syntax file (.g4 suffix) as a reference file for the original syntax. Some statements in the file share common meanings across programming languages and can be directly accessed by server 104 from the data storage system. Other, more specialized syntax can be compiled with reference to the official, precise definitions of the statements. Next, the data storage system can pre-store predefined operation information corresponding to multiple programming languages. Server 104 can then obtain the corresponding programming language based on the target syntax reference information and, based on the identifier corresponding to the programming language, request the predefined operation information from the data storage system.
[0081] Step S204: Generate an abstract syntax tree according to the original syntax reference information, wherein the abstract syntax tree includes at least one information node.
[0082] An abstract syntax tree (AST) is a tree-like data structure used to represent the grammatical structure of code. Each node represents a syntactic element in the code, and the entire tree shows the hierarchy and relationships of the code. The AST is an abstract representation that simplifies the grammatical information of program source code while preserving the structure and content of the code. It focuses on grammatical structure rather than specific grammatical details such as punctuation and spacing.
[0083] For example, the server 104 may extract semantic features from the original syntax reference information, generate a semantic parser, and use the semantic parser to parse the original code to obtain an abstract syntax tree. Furthermore, the server 104 may use the Antlr4 tool to parse the code to generate an abstract syntax tree.
[0084] The semantic parser may include a lexical analyzer and a syntax analyzer. For example, the server 104 may first use the lexical analyzer to split the original code into a set of original words, and then use the syntax analyzer to associate and combine the elements in the original word set to generate an abstract syntax tree. Lexical analysis refers to the process of converting a character sequence into a word (token) in computer science.
[0085] The lexer is typically used by the parser. Because the SAS language is complex, server 104 separates SAS statements into different types. For example, to parse the syntax of the SAS Data step statement, a separate parser file named DataStmt.g4 is created. Similarly, files like ProcStmt.g4, SetStmt.g4, and RunStmt.g4 are created. All parser and lexer files can be imported and aggregated into a single SAS.g4 file. The Antlr4 tool can then generate the corresponding lexer and parser based on this file.
[0086] Step S206: traverse each information node in the abstract syntax tree, and monitor the semantic features of the information node according to predefined operation information.
[0087] Exemplarily, the server 104 can use the Antlr4 tool to generate a customized listening component. There are two ways to traverse the AST: Listerner and Visitor. Among them, Listerner is used to automatically trigger predefined methods when traversing the abstract syntax tree (AST). Whenever the parser reaches a node of a specific type, the listener will listen and call the corresponding method for processing. The listener's methods are generally divided into the enter method when entering the node and the exit method when leaving the node. Visitor is a pattern used to manually control the process of traversing the abstract syntax tree (AST). Visitor defines a series of methods that users need to explicitly call to traverse and process syntax tree nodes. The visitor pattern provides greater flexibility, and users can fully control the traversal process and order, as well as the processing logic of the nodes.
[0088] Step S208 : When the semantic feature of the information node matches the preset feature in the predefined operation information, the code conversion action indicated by the predefined operation information is executed to convert the original code into the target code.
[0089] For example, when server 104 performs code conversion, it needs to traverse each statement in the SAS code. It can be seen that although Visitor is more flexible and can customize access to different nodes, using Visitor cannot fully traverse every statement, so it is not applicable to the current scenario. Instead, Listerner is more suitable. For example, server 104 can control Listerner to use ParseTreeWalker (parse tree traverser) to traverse the generated AST. If a specific node is traversed, an event corresponding to the predefined operation information will be triggered. When ParseTreeWalker traverses the syntax tree of the corresponding SAS statement type, SAS2PyListerner will listen for this type of SAS statement and enter the corresponding enter and exit methods. By defining the corresponding converter under the corresponding enter and exit methods, server 104 can convert the source code in the converter. The following code shows that the sort in proc_stmt is converted into a Python sort statement fragment. The converter fully utilizes the context information of the AST to perform the conversion. In addition, the server 104 may also introduce a DSL (Domain-Specific Language) as a transition between the upper and lower parts to avoid the maintenance problem caused by excessive coupling.
[0090] In the above-mentioned application code conversion method, by obtaining the original syntax reference information of the original code and the target syntax reference information of the target code, predefined operation information is obtained based on the target syntax reference information. Then, an abstract syntax tree including at least one information node is generated based on the original syntax reference information. This method uses the abstract syntax tree to achieve a deeper understanding of the code structure and effectively handles the problem of context sensitivity. Subsequently, each information node in the abstract syntax tree is traversed, and the semantic features of the information node are monitored based on the predefined operation information. When the semantic features of the information node match the preset features in the predefined operation information, the code conversion action indicated by the predefined operation information is executed to convert the original code into the target code. This solution establishes an abstract syntax tree to address the problem of context sensitivity, adopts predefined specific conversion rules rather than relying solely on pattern matching, and adopts a combination of monitoring and traversal. Predefined operation information is used to define the operation to be executed when a key node is monitored. This not only provides a deep understanding of the code syntax itself, but also makes code conversion more flexible and easier to deploy and implement, and can improve the accuracy of code conversion even in complex code structures.
[0091] In an exemplary embodiment, as shown in FIG3 , obtaining predefined operation information according to target grammar reference information in step S206 may include steps S302 to S306 .
[0092] Step S302: Acquire a predefined operation information set.
[0093] The predefined operation information set is used to record predefined operation information corresponding to different target codes. Exemplarily, the predefined operation information set is stored in a data storage system, and the server 104 can request the predefined operation information set from the data storage system.
[0094] Step S304: when there is predefined operation information matching the target syntax reference information in the predefined operation information set, the predefined operation information is directly acquired according to the target code identifier in the target syntax reference information.
[0095] Step S306 : if there is no predefined operation information matching the target grammar reference information in the predefined operation information set, compile predefined operation information according to the target grammar reference information and write the predefined operation information into the predefined operation information set.
[0096] Since each modification of the grammar file will modify the predefined operation information file, and frequent modification of the grammar file is normal for code conversion tasks, the server 104 can customize a SAS2PyListerner file and rewrite the corresponding enter and exit methods to specifically monitor various types of SAS statements in the AST. When a specific node is detected, the SAS2PyListerner will call the corresponding converter to perform code conversion.
[0097] In this embodiment, when there is predefined operation information that matches the target grammar reference information in the predefined operation information set, the predefined operation information is directly obtained according to the target code identifier in the target grammar reference information; and when there is no predefined operation information that matches the target grammar reference information in the predefined operation information set, the predefined operation information is written according to the target grammar reference information, and the predefined operation information is written into the predefined operation information set. By recording the predefined operation information of the same type and reusing the predefined operation information, the need to change the predefined operation information file when the grammar file is frequently modified is avoided.
[0098] In an exemplary embodiment, as shown in FIG4 , the above method may further include steps S402 to S404 , wherein:
[0099] Step S402: monitor the execution of the code conversion action indicated by the predefined operation information and obtain a monitoring record.
[0100] The monitoring record includes an error code line identifier and exception information corresponding to the error code line.
[0101] For example, since the SAS code statements to be converted span multiple lines, server 104 can declare global variables to store context variables for conversion, enhancing attention to context-sensitive information during code conversion. If the grammar file is incorrect or excludes some tokens, causing the lexer or parser to be unable to parse a line of source code to be converted, an error will be reported during the conversion process. Server 104 can also introduce a global error catcher to record the source code line number and exception information when a code conversion action fails.
[0102] Step S404: when the monitoring record indicates that an error occurs in the execution of the code conversion action, the corresponding predefined operation information is updated according to the monitoring record and the target syntax reference information.
[0103] Accordingly, the above step S208 may further include skipping the current transcoding action according to the error code line identifier and continuing to execute the next transcoding action when an error is detected in the execution of the transcoding action.
[0104] In another exemplary embodiment, server 104 first compiles a SAS code programming syntax file as a raw syntax reference file, obtains the corresponding programming language based on the target syntax reference information, and requests predefined operation information from the data storage system based on the identifier corresponding to the programming language. Next, server 104 extracts semantic features from the raw syntax reference information and generates a semantic parser. The semantic parser includes a lexical analyzer and a syntax analyzer. Code parsing is performed using the Antlr4 tool. Server 104 first uses the lexical analyzer to split the raw code into a set of raw words, and then uses the syntax analyzer to associate and combine elements in the raw word set to generate an abstract syntax tree.
[0105] Subsequently, the server 104 requests a predefined operation information set from the data storage system. If there is predefined operation information that matches the target syntax reference information in the predefined operation information set, the predefined operation information is directly obtained according to the target code identifier in the target syntax reference information. If there is no predefined operation information that matches the target syntax reference information in the predefined operation information set, the predefined operation information is written according to the target syntax reference information and written into the predefined operation information set.
[0106] Next, server 104 controls Listerner to traverse the generated AST through the parse tree traversal. If a specific node is traversed, an event corresponding to the predefined operation information will be triggered. When ParseTreeWalker traverses the syntax tree of the corresponding SAS statement type, server 104 listens to this type of SAS statement and enters the corresponding enter and exit methods. By defining corresponding converters under the corresponding enter and exit methods, server 104 can convert the source code in the converter. The following code shows how to convert the sorting in proc_stmt into a Python sorting statement fragment. The converter makes full use of the context information of AST to perform the conversion.
[0107] At the same time, server 104 introduces a global error catcher that records the source code line number and exception information when a transcoding action is executed abnormally. If the monitoring record indicates an error in the transcoding action execution, the corresponding predefined operation information is updated based on the monitoring record and target syntax reference information. If an error is detected in the transcoding action execution, the current transcoding action is skipped based on the error code line identifier and the next transcoding action is executed.
[0108] Finally, since the converted target code may have code format errors and cause abnormal operation, server 104 writes a source code grammar file based on Antlr4. Python's g4 grammar file has been officially provided, so after generating the corresponding grammar parsing file according to the above-mentioned process, the converted code is converted into AST again, and then traversed through AST, and formatted accordingly based on Python's code specifications. Since AST can accurately represent the relationship between classes, functions, and statements, server 104 makes corresponding adjustments to indentation, spaces, and line breaks to ensure that the code meets the specification requirements. Finally, it supports saving the converted code to a specified path or directly outputting it to the standard.
[0109] It should be understood that, although the various steps in the flowcharts involved in the various embodiments described above are displayed in sequence according to the instructions of the arrows, these steps are not necessarily executed in sequence in the order indicated by the arrows. Unless otherwise specified herein, there is no strict order restriction on the execution of these steps, and these steps can be executed in other orders. Moreover, at least a portion of the steps in the flowcharts involved in the various embodiments described above can include multiple steps or multiple stages, and these steps or stages are not necessarily executed and completed at the same time, but can be executed at different times, and the execution order of these steps or stages is not necessarily to be carried out in sequence, but can be executed in turn or alternately with other steps or at least a portion of steps or stages in other steps.
[0110] Based on the same inventive concept, embodiments of the present application also provide an application code conversion device for implementing the aforementioned application code conversion method. The solution provided by this device is similar to the solution described in the aforementioned method. Therefore, the specific limitations in one or more of the following application code conversion device embodiments can be found in the aforementioned limitations of the application code conversion method and will not be further elaborated here.
[0111] In an exemplary embodiment, as shown in FIG5 , an application code conversion apparatus is provided, comprising: an information acquisition module 502 , a node analysis module 504 , a node traversal module 506 , and a code conversion module 508 , wherein:
[0112] An information acquisition module 502 is configured to acquire original syntax reference information of the original code and target syntax reference information of the target code, and to acquire predefined operation information based on the target syntax reference information, wherein the original syntax reference information is reference information set for the programming language corresponding to the original code, the target syntax reference information is reference information set for the programming language corresponding to the target code, and the predefined operation information is used to represent the relationship between the state of the information node and the code conversion action;
[0113] A node analysis module 504 is configured to generate an abstract syntax tree according to the original syntax reference information, wherein the abstract syntax tree includes at least one information node;
[0114] The node traversal module 506 is used to traverse each information node in the abstract syntax tree and monitor the semantic features of the information node according to predefined operation information;
[0115] The code conversion module 508 is configured to execute the code conversion action indicated by the predefined operation information to convert the original code into the target code when the semantic feature of the information node matches the preset feature in the predefined operation information.
[0116] In one embodiment, the node analysis module 504 includes:
[0117] Semantic analysis submodule, used to extract semantic features from original grammatical reference information and generate a semantic parser;
[0118] The code parsing submodule is used to parse the original code using a semantic parser to obtain an abstract syntax tree.
[0119] In one embodiment, the semantic parser includes a lexical analyzer and a syntax analyzer; the code parsing submodule includes:
[0120] Lexical analysis unit, used to split the original code using a lexical analyzer to obtain an original word set;
[0121] The syntax analysis unit is used to use a syntax analyzer to associate and combine elements in the original word set to generate an abstract syntax tree.
[0122] In one embodiment, the information acquisition module 502 includes:
[0123] An information aggregation submodule is used to obtain a predefined operation information set, wherein the predefined operation information set is used to record predefined operation information corresponding to different target codes;
[0124] An information acquisition submodule, configured to directly acquire the predefined operation information according to the target code identifier in the target syntax reference information when there is predefined operation information matching the target syntax reference information in the predefined operation information set;
[0125] The information generation submodule is used to write predefined operation information according to the target grammar reference information and write the predefined operation information into the predefined operation information set when there is no predefined operation information matching the target grammar reference information in the predefined operation information set.
[0126] In one embodiment, the apparatus further comprises:
[0127] An action monitoring module is used to monitor the execution of the code conversion action indicated by the predefined operation information and obtain a monitoring record;
[0128] The error handling module is used to update the corresponding predefined operation information according to the monitoring record and the target syntax reference information when the monitoring record indicates that an error occurs in the execution of the code conversion action.
[0129] In one embodiment, the monitoring record includes an error code line identifier and exception information corresponding to the error code line; the code conversion module is also used to skip the current code conversion action according to the error code line identifier and continue to execute the next code conversion action when an error is detected in the execution of the code conversion action.
[0130] Each module in the aforementioned application code conversion device may be implemented in whole or in part through software, hardware, or a combination thereof. Each module may be embedded in or independent of a processor in a computer device in the form of hardware, or may be stored in a memory in the computer device in the form of software, so that the processor can call and execute the corresponding operations of each module.
[0131] In an exemplary embodiment, a computer device is provided, which may be a server. Its internal structure diagram may be as shown in FIG6 . The computer device includes a processor, memory, an input / output (I / O) interface, and a communication interface. The processor, memory, and I / O interface are connected via a system bus, and the communication interface is connected to the system bus via the I / O interface. The processor of the computer device is configured to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program, and a database. The internal memory provides an environment for the operation of the operating system and computer program in the non-volatile storage medium. The database of the computer device is configured to store data such as original syntax reference information of source code, target syntax reference information of target code, and predefined operation information. The I / O interface of the computer device is configured to exchange information between the processor and external devices. The communication interface of the computer device is configured to communicate with external terminals via a network connection. When executed by the processor, the computer program implements an application code conversion method.
[0132] In an exemplary embodiment, a computer device is provided. The computer device may be a terminal, and its internal structure may be shown in Figure 7. The computer device includes a processor, memory, an input / output interface, a communication interface, a display unit, and an input device. The processor, memory, and input / output interface are connected via a system bus, and the communication interface, display unit, and input device are connected to the system bus via the input / output interface. The processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operation of the operating system and computer program in the non-volatile storage medium. The input / output interface of the computer device is used to exchange information between the processor and external devices. The communication interface of the computer device is used to communicate with external terminals via wired or wireless means. The wireless means may be implemented via Wi-Fi, a mobile cellular network, near-field communication (NFC), or other technologies. When executed by the processor, the computer program implements an application code conversion method. The display unit of the computer device is used to produce a visually visible image and may be a display screen, a projection device, or a virtual reality imaging device. The display screen can be a liquid crystal display screen or an electronic ink display screen, and the input device of the computer device can be a touch layer covering the display screen, or a button, trackball or touchpad set on the computer device casing, or an external keyboard, touchpad or mouse.
[0133] Those skilled in the art will understand that the structure shown in FIG7 is merely a block diagram of a portion of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than shown in the figure, or combine certain components, or have a different arrangement of components.
[0134] In an exemplary embodiment, a computer device is provided, comprising a memory and a processor, wherein a computer program is stored in the memory, and the processor implements the following steps when executing the computer program: obtaining original syntax reference information of an original code and target syntax reference information of a target code, and obtaining predefined operation information based on the target syntax reference information, wherein the original syntax reference information is reference information set for a programming language corresponding to the original code, the target syntax reference information is reference information set for a programming language corresponding to the target code, and the predefined operation information is used to characterize the relationship between the state of an information node and a code conversion action; generating an abstract syntax tree based on the original syntax reference information, wherein the abstract syntax tree includes at least one information node; traversing each information node in the abstract syntax tree, and monitoring the semantic features of the information node based on the predefined operation information; and executing the code conversion action indicated by the predefined operation information to convert the original code into the target code when the semantic features of the information node match the preset features in the predefined operation information.
[0135] In one embodiment, when executing the computer program, the processor further implements the following steps: extracting semantic features from the original syntax reference information to generate a semantic parser; and using the semantic parser to parse the original code to obtain an abstract syntax tree.
[0136] In one embodiment, when executing the computer program, the processor further implements the following steps: using a lexical analyzer to split the original code to obtain an original word set; using a syntax analyzer to associate and combine elements in the original word set to generate an abstract syntax tree.
[0137] In one embodiment, when the processor executes the computer program, the following steps are also implemented: obtaining a predefined operation information set, wherein the predefined operation information set is used to record predefined operation information corresponding to different target codes; when there is predefined operation information matching the target syntax reference information in the predefined operation information set, directly obtaining the predefined operation information according to the target code identifier in the target syntax reference information; when there is no predefined operation information matching the target syntax reference information in the predefined operation information set, writing the predefined operation information according to the target syntax reference information, and writing the predefined operation information into the predefined operation information set.
[0138] In one embodiment, when the processor executes the computer program, the processor further implements the following steps: monitoring the execution status of the code conversion action indicated by the predefined operation information to obtain a monitoring record; when the monitoring record indicates that an error occurs in the execution status of the code conversion action, updating the corresponding predefined operation information based on the monitoring record and the target syntax reference information.
[0139] In one embodiment, when executing the computer program, the processor further implements the following steps: when an error is detected in the execution of the code conversion action, skipping the current code conversion action according to the error code line identifier and continuing to execute the next code conversion action.
[0140] In one embodiment, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the following steps are implemented: obtaining original syntax reference information of an original code and target syntax reference information of a target code, and obtaining predefined operation information based on the target syntax reference information, wherein the original syntax reference information is reference information set for a programming language corresponding to the original code, the target syntax reference information is reference information set for a programming language corresponding to the target code, and the predefined operation information is used to characterize the relationship between the state of an information node and a code conversion action; generating an abstract syntax tree based on the original syntax reference information, wherein the abstract syntax tree includes at least one information node; traversing each information node in the abstract syntax tree, and monitoring the semantic features of the information node based on the predefined operation information; and when the semantic features of the information node match the preset features in the predefined operation information, executing the code conversion action indicated by the predefined operation information to convert the original code into the target code.
[0141] In one embodiment, when the computer program is executed by a processor, the following steps are further implemented: extracting semantic features from the original syntax reference information to generate a semantic parser; and using the semantic parser to parse the original code to obtain an abstract syntax tree.
[0142] In one embodiment, when the computer program is executed by the processor, the following steps are further implemented: using a lexical analyzer to split the original code to obtain an original word set; using a syntax analyzer to associate and combine elements in the original word set to generate an abstract syntax tree.
[0143] In one embodiment, when the computer program is executed by the processor, the following steps are also implemented: obtaining a predefined operation information set, wherein the predefined operation information set is used to record predefined operation information corresponding to different target codes; when there is predefined operation information matching the target syntax reference information in the predefined operation information set, directly obtaining the predefined operation information according to the target code identifier in the target syntax reference information; when there is no predefined operation information matching the target syntax reference information in the predefined operation information set, writing the predefined operation information according to the target syntax reference information, and writing the predefined operation information into the predefined operation information set.
[0144] In one embodiment, when the computer program is executed by the processor, the following steps are further implemented: monitoring the execution status of the code conversion action indicated by the predefined operation information to obtain a monitoring record; and when the monitoring record indicates that an error occurs in the execution status of the code conversion action, updating the corresponding predefined operation information based on the monitoring record and the target syntax reference information.
[0145] In one embodiment, when the computer program is executed by the processor, the following steps are further implemented: when an error is detected in the execution of the code conversion action, the current code conversion action is skipped according to the error code line identifier, and the next code conversion action is continued.
[0146] In one embodiment, a computer program product is provided, comprising a computer program that, when executed by a processor, implements the following steps: obtaining original syntax reference information of an original code and target syntax reference information of a target code, and obtaining predefined operation information based on the target syntax reference information, wherein the original syntax reference information is reference information set for a programming language corresponding to the original code, the target syntax reference information is reference information set for a programming language corresponding to the target code, and the predefined operation information is used to characterize the relationship between the state of an information node and a code conversion action; generating an abstract syntax tree based on the original syntax reference information, wherein the abstract syntax tree includes at least one information node; traversing each information node in the abstract syntax tree, and monitoring semantic features of the information node based on the predefined operation information; and executing the code conversion action indicated by the predefined operation information to convert the original code into the target code when the semantic features of the information node match preset features in the predefined operation information.
[0147] In one embodiment, when the computer program is executed by a processor, the following steps are further implemented: extracting semantic features from the original syntax reference information to generate a semantic parser; and using the semantic parser to parse the original code to obtain an abstract syntax tree.
[0148] In one embodiment, when the computer program is executed by the processor, the following steps are further implemented: using a lexical analyzer to split the original code to obtain an original word set; using a syntax analyzer to associate and combine elements in the original word set to generate an abstract syntax tree.
[0149] In one embodiment, when the computer program is executed by the processor, the following steps are also implemented: obtaining a predefined operation information set, wherein the predefined operation information set is used to record predefined operation information corresponding to different target codes; when there is predefined operation information matching the target syntax reference information in the predefined operation information set, directly obtaining the predefined operation information according to the target code identifier in the target syntax reference information; when there is no predefined operation information matching the target syntax reference information in the predefined operation information set, writing the predefined operation information according to the target syntax reference information, and writing the predefined operation information into the predefined operation information set.
[0150] In one embodiment, when the computer program is executed by the processor, the following steps are further implemented: monitoring the execution status of the code conversion action indicated by the predefined operation information to obtain a monitoring record; and when the monitoring record indicates that an error occurs in the execution status of the code conversion action, updating the corresponding predefined operation information based on the monitoring record and the target syntax reference information.
[0151] In one embodiment, when the computer program is executed by the processor, the following steps are further implemented: when an error is detected in the execution of the code conversion action, the current code conversion action is skipped according to the error code line identifier, and the next code conversion action is continued.
[0152] Those skilled in the art will understand that all or part of the processes in the above-mentioned embodiments can be implemented by instructing the relevant hardware through a computer program. The computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. In particular, any reference to memory, database, or other media used in the embodiments provided in this application can include at least one of non-volatile memory and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetic random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. By way of illustration and not limitation, RAM can take various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM). The databases involved in the various embodiments provided herein may include at least one of a relational database and a non-relational database. Non-relational databases may include, but are not limited to, blockchain-based distributed databases. The processors involved in the various embodiments provided herein may be, but are not limited to, general-purpose processors, central processing units (CPUs), graphics processing units (GPUs), digital signal processors (DSPs), programmable logic devices (PLDs), quantum computing-based data processing logic devices, artificial intelligence (AI) processors, and the like.
[0153] The technical features of the above embodiments can be combined arbitrarily. In order to make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this application.
[0154] The above-described embodiments merely represent several implementation methods of the present application. While the descriptions are relatively specific and detailed, they should not be construed as limiting the scope of the present application. It should be noted that a person of ordinary skill in the art may make various modifications and improvements without departing from the spirit of the present application, and these modifications and improvements fall within the scope of protection of the present application. Therefore, the scope of protection of the present application shall be determined by the appended claims.
Claims
1. A method for converting application code, It is characterized in that The method comprises: Obtaining original syntax reference information of the original code and target syntax reference information of the target code, and obtaining predefined operation information according to the target syntax reference information, wherein the original syntax reference information is reference information set for the programming language corresponding to the original code, the target syntax reference information is reference information set for the programming language corresponding to the target code, and the predefined operation information is used to characterize the relationship between the state of the information node and the code conversion action; Generate an abstract syntax tree according to the original syntax reference information, wherein the abstract syntax tree includes at least one of the information nodes; Traversing each of the information nodes in the abstract syntax tree, and monitoring the semantic features of the information nodes according to the predefined operation information; In the case where the semantic feature of the information node matches the preset feature in the predefined operation information, the code conversion action indicated by the predefined operation information is performed to convert the original code into the target code.
2. The method according to claim 1, It is characterized in that The generating an abstract syntax tree according to the original syntax reference information includes: Extracting semantic features from the original grammatical reference information and generating a semantic parser; The original code is parsed using the semantic parser to obtain an abstract syntax tree.
3. The method according to claim 2, It is characterized in that The semantic parser includes a lexical analyzer and a grammatical analyzer; The using the semantic parser to parse the original code to obtain an abstract syntax tree includes: Use the lexical analyzer to split the original code to obtain an original word set; The syntax analyzer is used to associate and combine the elements in the original word set to generate an abstract syntax tree.
4. The method according to claim 1, It is characterized in that The acquiring predefined operation information according to the target syntax reference information includes: Acquire a predefined operation information set, wherein the predefined operation information set is used to record predefined operation information corresponding to different target codes; In the case where there is predefined operation information matching the target syntax reference information in the predefined operation information set, directly acquiring the predefined operation information according to the target code identifier in the target syntax reference information; In the case that there is no predefined operation information matching the target syntax reference information in the predefined operation information set, predefined operation information is compiled according to the target syntax reference information, and the predefined operation information is written into the predefined operation information set.
5. The method according to any one of claims 1 to 4, It is characterized in that The method further comprises: Monitoring the execution of the code conversion action indicated by the predefined operation information to obtain a monitoring record; In the case where the monitoring record indicates that an error occurs in the execution of the code conversion action, the corresponding predefined operation information is updated according to the monitoring record and the target syntax reference information.
6. The method according to claim 5, It is characterized in that The monitoring record includes an error code line identifier and exception information corresponding to the error code line; the executing the code conversion action indicated by the predefined operation information to convert the original code into the target code includes: When it is detected that an error occurs in the execution of the code conversion action, the current code conversion action is skipped according to the error code line identifier, and the next code conversion action is continued to be executed.
7. An application code conversion device, It is characterized in that The device comprises: An information acquisition module, used to acquire original syntax reference information of the original code and target syntax reference information of the target code, and acquire predefined operation information according to the target syntax reference information, wherein the original syntax reference information is reference information set for the programming language corresponding to the original code, the target syntax reference information is reference information set for the programming language corresponding to the target code, and the predefined operation information is used to characterize the relationship between the state of the information node and the code conversion action; A node analysis module, configured to generate an abstract syntax tree according to the original syntax reference information, wherein the abstract syntax tree includes at least one of the information nodes; A node traversal module, used for traversing each of the information nodes in the abstract syntax tree, and monitoring the semantic features of the information nodes according to the predefined operation information; The code conversion module is used to execute the code conversion action indicated by the predefined operation information to convert the original code into the target code when the semantic feature of the information node matches the preset feature in the predefined operation information.
8. The device according to claim 7, It is characterized in that The node analysis module includes: A semantic analysis submodule, used to extract semantic features from the original grammatical reference information and generate a semantic parser; The code parsing submodule is used to parse the original code using the semantic parser to obtain an abstract syntax tree.
9. The device according to claim 8, It is characterized in that The semantic parser includes a lexical analyzer and a syntax analyzer; the code parsing submodule includes: A lexical analysis unit, used for splitting the original code using the lexical analyzer to obtain an original word set; The syntax analysis unit is used to use the syntax analyzer to associate and combine the elements in the original word set to generate an abstract syntax tree.
10. The device according to claim 1, It is characterized in that The information acquisition module includes: An information aggregation submodule, used for acquiring a predefined operation information set, wherein the predefined operation information set is used for recording predefined operation information corresponding to different target codes; An information acquisition submodule, configured to directly acquire the predefined operation information according to the target code identifier in the target grammar reference information when there is predefined operation information matching the target grammar reference information in the predefined operation information set; The information generation submodule is used to write predefined operation information according to the target grammar reference information and write the predefined operation information into the predefined operation information set when there is no predefined operation information matching the target grammar reference information in the predefined operation information set.
11. The device according to any one of claims 7 to 10, It is characterized in that The device also includes: An action monitoring module, used to monitor the execution of the code conversion action indicated by the predefined operation information and obtain a monitoring record; The error handling module is used to update the corresponding predefined operation information according to the monitoring record and the target syntax reference information when the monitoring record indicates that an error occurs in the execution of the code conversion action.
12. The device according to claim 11, It is characterized in that The monitoring record includes an error code line identifier and exception information corresponding to the error code line; the code conversion module is also used to skip the current code conversion action according to the error code line identifier when an error occurs in the execution of the code conversion action, and continue to execute the next code conversion action.
13. A computer device comprising a memory and a processor, wherein the memory stores a computer program. It is characterized in that When the processor executes the computer program, the steps of the method according to any one of claims 1 to 6 are implemented.
14. A computer-readable storage medium having a computer program stored thereon, It is characterized in that When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 6 are implemented.
15. A computer program product comprising a computer program, It is characterized in that When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 6 are implemented.
Citation Information
Cited By
Programming language migration operation method and device
CN120950074A
