Method and device for automatically upgrading codes based on compiler and storage medium

Through the compiler-based automatic code upgrade method, the time-consuming and labor-intensive and error risks of old code migration to the new standard are solved, efficient automated migration is achieved, and the readability and maintainability of the code is improved.

CN119311289APending Publication Date: 2025-01-14SHANDONG INSPUR SCI RES INST CO LTD
View PDF 0 Cites 6 Cited by

Patent Information

Application Number
CN202411279576.6
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-09-12
Publication Date
2025-01-14

AI Technical Summary

Technical Problem

Code from old programming standards needs to be processed manually when migrating to new standards, which is time-consuming and error-prone, especially in large and complex code bases, which are risky and difficult to take advantage of the features of the new standards.

Method used

Automatic code upgrade through compiler-based methods, including lexical analysis, syntax analysis, generation of abstract syntax trees, semantic analysis, construction of grammar rule mapping tables for new standards, and traversing the abstract syntax trees for node matching and conversion, generating a compliant standard standard source code.

Benefits of technology

It greatly reduces the need for manual intervention, increases the degree of automation of code migration from old standards to new standards, saves labor and time costs, and produces codes that are clearer, readable and maintainable.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119311289A_ABST
    Figure CN119311289A_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of computer code upgrading, and discloses an automatic code upgrading method and device based on a compiler and a storage medium. The method comprises the steps that firstly, lexical analysis, grammatical analysis and semantic analysis are conducted on a source code, an abstract syntax tree is generated, then a new standard grammatical rule mapping table is constructed, and a new standard grammatical rule mapping table is generated; and traversing the abstract syntax tree, querying whether the nodes of the abstract syntax tree can be queried in the mapping table, outputting the character strings mapped to the queried nodes, and normally outputting codes conforming to node semantics for the nodes which are not queried. The abstract syntax tree of the original code is automatically traversed, and node matching and conversion are performed according to the syntax rule mapping table of the new standard, so that the need of manual intervention is greatly reduced, the automation degree of code migration from an old standard to the new standard is improved, a large amount of manpower and time cost is saved, and the migration problem caused by human errors is reduced.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the technical field of computer code upgrade, for example, to a method, device and storage medium for automatic code upgrade based on a compiler. Background Art

[0002] In the field of software development, with the continuous advancement of technology and the continuous evolution of programming languages, new programming standards (such as new versions of C++, Java, Python and other languages) are constantly being introduced, providing developers with richer, more efficient and secure programming tools and features. These new standards often include syntax improvements, performance optimizations, new libraries and APIs, better concurrency support, memory management mechanism improvements and security enhancements. However, due to various reasons such as historical issues left over from the project, compatibility considerations, resource limitations or developer habits, many projects are still using code written in older programming standards.

[0003] Old code often seems powerless when faced with the advantages of new standards. In order to take advantage of the features provided by the new standard, developers need to manually migrate old code to the new standard, which is not only time-consuming and labor-intensive, but also prone to errors. Manual migration requires developers to have a deep understanding of the old code and be familiar with all the changes in the new standard, which places high demands on developers' skills and experience. In addition, manual migration may also introduce new errors, especially when dealing with large and complex code bases, this risk is particularly prominent.

[0004] It should be noted that the information disclosed in the above background technology section is only used to enhance the understanding of the background of the present application, and therefore may include information that does not constitute the prior art known to ordinary technicians in the field. Summary of the invention

[0005] In order to provide a basic understanding of some aspects of the disclosed embodiments, a brief summary is given below. The summary is not an extensive review, nor is it intended to identify key / critical elements or delineate the scope of protection of these embodiments, but rather serves as a prelude to the detailed description that follows.

[0006] The method for automatically upgrading code based on a compiler provided in an embodiment of the present disclosure includes: The front end performs lexical analysis and grammatical analysis on high-level programming languages ​​to generate an abstract syntax tree, and performs semantic analysis to generate an intermediate representation; The backend optimizes the intermediate representation, builds a new standard grammar rule mapping table, traverses the abstract syntax tree, queries whether the nodes of the abstract syntax tree can be found in the mapping table, outputs the strings mapped to the found nodes, and outputs the codes that conform to the semantics of the nodes that are not found.

[0007] The lexical analysis includes receiving input source code text, the lexical analyzer reads the source code text, divides it into a series of tokens according to the lexical rules of the language, outputs a token sequence, and uses the tokens as input for subsequent syntax analysis. The tokens include keywords, type information of identifiers, names of identifiers, and specific numerical information of literals.

[0008] In some embodiments, the above-mentioned syntax analysis includes using a syntax analyzer to construct an abstract syntax tree according to the syntax rules of the language, using AST as a tree representation of the source code structure to reflect the syntax structure of the program, the nodes of the abstract syntax tree represent different building blocks of the program, and the abstract syntax tree serves as the basis for subsequent semantic analysis and optimization.

[0009] In some embodiments, the above-mentioned semantic analysis includes using a semantic analyzer to traverse the AST and check the semantic correctness of the program, including type checking, scope resolution, and variable binding, to ensure that the program complies with the semantic rules of the language on the basis of syntactically correctness, and output an AST with semantic attributes of type information and scope information to be used as necessary semantic information for subsequent optimization and code generation.

[0010] In some embodiments, the generation of the intermediate representation includes converting the AST into a universal intermediate representation, where the universal intermediate representation does not depend on a specific source language or target machine, so as to be used for subsequent optimization and code generation.

[0011] In some embodiments, the above-mentioned construction of a new standard grammar rule mapping table traverses the abstract syntax tree to query whether the node of the abstract syntax tree can be queried in the mapping table, and for the queried node, outputs the string to which it is mapped, and for the node that is not queried, normally outputs the code that conforms to the node semantics, including: For the new standard of programming language, a mapping table from the node of abstract syntax tree to the new standard of high-level programming language is constructed, wherein the key in the mapping table is the node of abstract syntax tree, and the value in the mapping table is a specific string, and the specific string conforms to the code of the new standard; Traverse the abstract syntax tree and query the mapping table of the new standard. If the node meets the new standard, mark the node to check whether each node of the abstract syntax tree meets the grammatical rules of the new standard. Traverse the matched abstract syntax tree and use the code generator to generate source code that conforms to the new standard. The code generator serializes and formats the nodes in the abstract syntax tree according to the syntax and style requirements of the new standard. Output the source code under the new standard and output the strings generated by the code to a file.

[0012] The apparatus for automatically upgrading code based on a compiler provided in an embodiment of the present disclosure includes: The front-end module is used to perform lexical analysis and syntax analysis on high-level programming languages, generate abstract syntax trees, and perform semantic analysis to generate intermediate representations; The back-end module is used to optimize the intermediate representation, build a new standard grammar rule mapping table, traverse the abstract syntax tree, query whether the nodes of the abstract syntax tree can be queried in the mapping table, output the string mapped to the queried nodes, and output the code that conforms to the node semantics for the nodes that are not queried.

[0013] The lexical analysis includes receiving input source code text, the lexical analyzer reads the source code text, divides it into a series of tokens according to the lexical rules of the language, outputs a token sequence, and uses the tokens as input for subsequent syntax analysis. The tokens include keywords, type information of identifiers, names of identifiers, and specific numerical information of literals.

[0014] In some embodiments, the backend optimizes the intermediate representation, performs instruction selection, instruction scheduling, register scheduling, and generates an executable program, including: For the new standard of programming language, a mapping table from the node of abstract syntax tree to the new standard of high-level programming language is constructed, wherein the key in the mapping table is the node of abstract syntax tree, and the value in the mapping table is a specific string, and the specific string conforms to the code of the new standard; Traverse the abstract syntax tree and query the mapping table of the new standard. If the node meets the new standard, mark the node to check whether each node of the abstract syntax tree meets the grammatical rules of the new standard. Traverse the matched abstract syntax tree and use the code generator to generate source code that conforms to the new standard. The code generator serializes and formats the nodes in the abstract syntax tree according to the syntax and style requirements of the new standard. Output the source code under the new standard and output the strings generated by the code to a file.

[0015] The storage medium provided by the embodiment of the present disclosure stores program instructions, and is characterized in that when the program instructions are run, the method for automatically upgrading codes based on a compiler in the above embodiment is executed.

[0016] The enhanced compiler-based automatic code upgrade method, device, and storage medium provided in the embodiments of the present disclosure can achieve the following technical effects: By automatically traversing the abstract syntax tree (AST) of the original code and matching and converting nodes according to the syntax rule mapping table of the new standard, the need for manual intervention is greatly reduced, and the degree of automation of migrating codes from the old standard to the new standard is improved. This can not only save a lot of manpower and time costs, but also reduce migration problems caused by human errors. Moreover, the codes of the new standard often have clearer syntax structures and more standardized naming conventions. The code generated by automatic conversion can better meet these requirements, thereby improving the readability and maintainability of the code.

[0017] The above general description and the following description are exemplary and explanatory only and are not intended to limit the present application. BRIEF DESCRIPTION OF THE DRAWINGS

[0018] One or more embodiments are exemplarily described by corresponding drawings, which do not limit the embodiments. Elements with the same reference numerals in the drawings are shown as similar elements, and the drawings do not constitute a scale limitation, and wherein: Figure 1 It is a flowchart of a method for automatically upgrading codes based on a compiler provided in an embodiment of the present disclosure; Figure 2 It is a schematic diagram of a front-end process provided by an embodiment of the present disclosure; Figure 3 It is a flowchart of another method for automatically upgrading codes based on a compiler provided by an embodiment of the present disclosure; Figure 4 This is a schematic diagram of an effect provided by an embodiment of the present disclosure; Figure 5 It is a structural diagram of a device for automatically upgrading codes based on a compiler provided in an embodiment of the present disclosure; Figure 6 It is a structural diagram of a system for automatically upgrading codes based on a compiler provided in an embodiment of the present disclosure. DETAILED DESCRIPTION

[0019] In order to be able to understand the features and technical contents of the embodiments of the present disclosure in more detail, the implementation of the embodiments of the present disclosure is described in detail below in conjunction with the accompanying drawings. The attached drawings are for reference only and are not used to limit the embodiments of the present disclosure. In the following technical description, for the convenience of explanation, a full understanding of the disclosed embodiments is provided through multiple details. However, one or more embodiments can still be implemented without these details. In other cases, to simplify the drawings, well-known structures and devices can be simplified for display.

[0020] The terms "first", "second", etc. in the embodiments of the present disclosure are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the terms used in this way can be interchanged where appropriate, so as to describe the embodiments of the present disclosure described herein. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions.

[0021] Unless otherwise stated, the term "plurality" means two or more.

[0022] In the embodiment of the present disclosure, the character " / " indicates that the preceding and following objects are in an "or" relationship. For example, A / B indicates: A or B.

[0023] The term "and / or" is a description of the association relationship between objects, indicating that three relationships can exist. For example, A and / or B means: A or B, or, A and B.

[0024] The term "correspondence" may refer to an association relationship or a binding relationship. The correspondence between A and B means that there is an association relationship or a binding relationship between A and B.

[0025] The following describes the method, apparatus, device and storage medium for automatic code upgrade based on a compiler provided by the embodiments of the present disclosure in conjunction with the accompanying drawings.

[0026] Figure 1 is a flowchart of a method for automatically upgrading code based on a compiler provided by an embodiment of the present disclosure, such as Figure 1 As shown, the method for automatically upgrading the code based on the compiler may include: S01, the front end performs lexical analysis and syntax analysis on the high-level programming language, generates an abstract syntax tree, and performs semantic analysis to generate an intermediate representation; S02, the backend optimizes the intermediate representation, builds a new standard grammar rule mapping table, traverses the abstract syntax tree, queries whether the nodes of the abstract syntax tree can be queried in the mapping table, outputs the strings mapped to the queried nodes, and outputs the codes that conform to the node semantics normally for the nodes that are not queried.

[0027] The lexical analysis includes receiving input source code text, the lexical analyzer reads the source code text, divides it into a series of tokens according to the lexical rules of the language, outputs a token sequence, and uses the tokens as input for subsequent syntax analysis. The tokens include keywords, type information of identifiers, names of identifiers, and specific numerical information of literals.

[0028] In some embodiments, the above-mentioned syntax analysis includes using a syntax analyzer to construct an abstract syntax tree according to the syntax rules of the language, using AST as a tree representation of the source code structure to reflect the syntax structure of the program, the nodes of the abstract syntax tree represent different building blocks of the program, and the abstract syntax tree serves as the basis for subsequent semantic analysis and optimization.

[0029] In some embodiments, semantic analysis includes using a semantic analyzer to traverse the AST and check the semantic correctness of the program, including type checking, scope resolution, and variable binding, to ensure that the program complies with the semantic rules of the language on the basis of being syntactically correct, and output an AST with semantic attributes of type information and scope information to be used as necessary semantic information for subsequent optimization and code generation.

[0030] In some embodiments, the generation of the intermediate representation includes converting the AST into a generic intermediate representation that is not dependent on a specific source language or target machine for subsequent optimization and code generation.

[0031] In some embodiments, the above-mentioned construction of a new standard grammar rule mapping table traverses the abstract syntax tree to query whether the node of the abstract syntax tree can be queried in the mapping table, and for the queried node, outputs the string to which it is mapped, and for the node that is not queried, normally outputs the code that conforms to the node semantics, including: For the new standard of programming language, a mapping table from the node of abstract syntax tree to the new standard of high-level programming language is constructed, wherein the key in the mapping table is the node of abstract syntax tree, and the value in the mapping table is a specific string, and the specific string conforms to the code of the new standard; Traverse the abstract syntax tree and query the mapping table of the new standard. If the node meets the new standard, mark the node to check whether each node of the abstract syntax tree meets the grammatical rules of the new standard. Traverse the matched abstract syntax tree and use the code generator to generate source code that conforms to the new standard. The code generator serializes and formats the nodes in the abstract syntax tree according to the syntax and style requirements of the new standard. Output the source code under the new standard and output the strings generated by the code to a file.

[0032] Figure 2 is a schematic diagram of a front-end process provided by an embodiment of the present disclosure, combined with Figure 2 ,right Figure 1 The process performed by the middle front end is further described.

[0033] The front-end process, the specific steps are as follows: 1. Lexical analysis: Input source code text, the lexical analyzer (Lexer) reads the source code, divides it into a series of tokens according to the lexical rules of the language, and outputs a sequence of tokens. Each token contains a type (such as a keyword, identifier, etc.) and a possible value (such as the name of the identifier, the specific value of the literal). These tokens serve as input for subsequent syntax analysis.

[0034] 2. Syntax analysis: The parser builds an abstract syntax tree (AST) according to the grammatical rules of the language. AST is a tree representation of the source code structure, reflecting the grammatical structure of the program. The nodes of the abstract syntax tree represent different building blocks of the program (such as expressions, statements, declarations, etc.). The abstract syntax tree serves as the basis for subsequent semantic analysis and optimization.

[0035] 3. Semantic analysis: The semantic analyzer traverses the AST to check the semantic correctness of the program, including type checking, scope resolution, variable binding, etc. It ensures that the program is syntactically correct and also complies with the semantic rules of the language, and outputs an AST with semantic attributes such as type information and scope information. This enhanced AST provides the necessary semantic information for subsequent optimization and code generation.

[0036] 4. Intermediate representation generation: Convert AST into a more general intermediate representation (IR) that does not depend on a specific source language or target machine, facilitating subsequent optimization and code generation.

[0037] Based on the above-mentioned compiler front end generating an abstract syntax tree for code processing, the present invention traverses the abstract syntax tree for a specified new standard. At this time, the nodes of the abstract syntax tree are expressions, statements or declarations that are not related to the specific standard. Each node therein is combined with the grammatical rules of the new standard to generate code that conforms to the new standard.

[0038] Figure 3 is a flowchart of another method for automatically upgrading code based on a compiler provided by an embodiment of the present disclosure, combined with Figure 3 ,right Figure 1 The process performed by the middle front end is further described.

[0039] 1. Input the original source code, which is provided by the user for the compiler to use.

[0040] 2. Front-end processing: The compiler front-end performs lexical analysis and syntax analysis on the source code, generates an abstract syntax tree, and performs semantic analysis to confirm that the code is syntactically and semantically correct.

[0041] 3. Build a grammar rule mapping table for the new standard. For the new standard of programming languages, build a mapping table from the nodes of the abstract syntax tree to the high-level programming language, where the key is the node of the abstract syntax tree, such as loop statements, variable declarations, assignment expressions, etc. The value is a specific string, which is the code that complies with the new standard.

[0042] 4. Node matching. Traverse the abstract syntax tree and check whether each node of the abstract syntax tree conforms to the syntax rules of the new standard. Specifically, query in the mapping table of the new standard, and if the node conforms to the new standard, mark the node.

[0043] 5. Code generation. Traverse the matched abstract syntax tree and use the code generator to generate source code that complies with the new standard. The code generator serializes and formats the nodes in the abstract syntax tree according to the syntax and style requirements of the new standard.

[0044] 6. Output the source code under the new standard. Output the string generated by the code to a file.

[0045] In the present disclosure, the source code is first subjected to lexical analysis, syntax analysis, and semantic analysis to generate an abstract syntax tree, and then a syntax rule mapping table of the new standard is constructed, and the abstract syntax tree is traversed to query whether the nodes of the abstract syntax tree can be queried in the mapping table. For the queried nodes, the strings mapped to them are output, and for the nodes that are not queried, the code that conforms to the semantics of the nodes is output normally. By automatically traversing the abstract syntax tree (AST) of the original code and matching and converting the nodes according to the syntax rule mapping table of the new standard, the need for manual intervention is greatly reduced, and the degree of automation of migrating the code from the old standard to the new standard is improved. This can not only save a lot of manpower and time costs, but also reduce migration problems caused by human errors. Moreover, the code of the new standard often has a clearer syntax structure and a more standardized naming convention. The code generated by automatic conversion can better meet these requirements, thereby improving the readability and maintainability of the code.

[0046] In a specific example, taking the C++ programming language as an example, the compiler is Clang, and the source code is upgraded from the C++98 standard to the C++11 standard. The input source code main.cpp is as follows: #include <iostream> int main() { int arr[] = {1, 2, 3, 4, 5}; int size = sizeof(arr) / sizeof(arr[0]); for(int i = 0; i <size; ++i) { std::cout< <arr[i]<<" "; } std::cout< <std::endl; return 0; } The above code uses a for loop to loop over the array defined in the function and output each member in the array. In C++11, you can use a for loop in a range to traverse an array and use the auto keyword to automatically deduce the type of the loop variable.

[0047] According to the method provided by the present invention, a grammar rule mapping table of the C++11 standard is first constructed, including for loop compilation within a range. For the new feature of for loop compilation within a range, the key is the for loop statement ForStmt in the AST expression, and the value is the corresponding for loop string within the range, in the form of for(auto elem : arr).

[0048] After that, the code is analyzed lexically and syntactically to generate an abstract syntax tree, and then semantic analysis is performed to confirm that the code is correct in both syntax and semantics. The abstract syntax tree generated based on the input source code contains the for loop statement node ForStmt, the variable definition statement VarDecl, the assignment statement InitListExpr, etc.

[0049] Traverse the abstract syntax tree and match each node of the abstract syntax tree. If the corresponding node is found in the C++11 grammar rule mapping table, mark the node as C++11 standard. If the corresponding node is not found, skip the node. The abstract syntax tree here contains the for loop statement ForStmt, and the C++11 grammar rule mapping table also includes ForStmt, so the ForStmt node is marked here.

[0050] After marking the nodes in the abstract syntax tree, code generation begins. For nodes marked as using the C++11 standard for code generation, the string corresponding to ForStmt stored in the grammar rule mapping table is output. For nodes without marking, the standard code generator is used to generate code.

[0051] The code output using the present invention is as follows: #include <iostream> int main() { int arr[] = {1, 2, 3, 4, 5}; / / C++11 range-based for loop for(auto&elem : arr) { std::cout< <elem<<" "; } std::cout< <std::endl; return 0; } The for loop statement is changed from a conventional for loop to a range for loop. The specific embodiment effect diagram is as follows: Figure 4 shown.

[0052] Figure 5 is a schematic diagram of a structure of a device for automatically upgrading code based on a compiler provided in an embodiment of the present disclosure, such as Figure 5 As shown, the device for automatically upgrading the code based on the compiler may include: The front-end module 501 is used to perform lexical analysis and grammatical analysis on the high-level programming language, generate an abstract syntax tree, and perform semantic analysis to generate an intermediate representation; The back-end module 502 is used to optimize the intermediate representation, build a grammar rule mapping table of the new standard, traverse the abstract syntax tree, query whether the nodes of the abstract syntax tree can be queried in the mapping table, and output the string mapped to the queried nodes. For the nodes that are not queried, the code that conforms to the node semantics is output normally.

[0053] The lexical analysis includes receiving input source code text, the lexical analyzer reads the source code text, divides it into a series of tokens according to the lexical rules of the language, outputs a token sequence, and uses the tokens as input for subsequent syntax analysis. The tokens include keywords, type information of identifiers, names of identifiers, and specific numerical information of literals.

[0054] In some embodiments, the above-mentioned syntax analysis includes using a syntax analyzer to construct an abstract syntax tree according to the syntax rules of the language, using AST as a tree representation of the source code structure to reflect the syntax structure of the program, the nodes of the abstract syntax tree represent different building blocks of the program, and the abstract syntax tree serves as the basis for subsequent semantic analysis and optimization.

[0055] In some embodiments, semantic analysis includes using a semantic analyzer to traverse the AST and check the semantic correctness of the program, including type checking, scope resolution, and variable binding, to ensure that the program complies with the semantic rules of the language on the basis of being syntactically correct, and output an AST with semantic attributes of type information and scope information to be used as necessary semantic information for subsequent optimization and code generation.

[0056] In some embodiments, the generation of the intermediate representation includes converting the AST into a generic intermediate representation that is not dependent on a specific source language or target machine for subsequent optimization and code generation.

[0057] In some embodiments, the above-mentioned construction of a new standard grammar rule mapping table traverses the abstract syntax tree to query whether the node of the abstract syntax tree can be queried in the mapping table, and for the queried node, outputs the string to which it is mapped, and for the node that is not queried, normally outputs the code that conforms to the node semantics, including: For the new standard of programming language, a mapping table from the node of abstract syntax tree to the new standard of high-level programming language is constructed, wherein the key in the mapping table is the node of abstract syntax tree, and the value in the mapping table is a specific string, and the specific string conforms to the code of the new standard; Traverse the abstract syntax tree and query the mapping table of the new standard. If the node meets the new standard, mark the node to check whether each node of the abstract syntax tree meets the grammatical rules of the new standard. Traverse the matched abstract syntax tree and use the code generator to generate source code that conforms to the new standard. The code generator serializes and formats the nodes in the abstract syntax tree according to the syntax and style requirements of the new standard. Output the source code under the new standard and output the strings generated by the code to a file.

[0058] It should be noted that, for the aforementioned method embodiments, for the sake of simplicity of description, they are all expressed as a series of action combinations, but those skilled in the art should be aware that the present disclosure is not limited by the order of the actions described, because according to the present disclosure, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all optional embodiments, and the actions and modules involved are not necessarily required by the present disclosure.

[0059] Combination Figure 6 As shown, the embodiment of the present disclosure provides a system 600 for automatically upgrading code based on a compiler, including a processor 604 and a memory 601. Optionally, the system may also include a communication interface 602 and a bus 603. Among them, the processor 604, the communication interface 602, and the memory 601 can communicate with each other through the bus 603. The communication interface 602 can be used for information transmission. The processor 604 can call the logic instructions in the memory 601 to execute the method for automatically upgrading code based on the compiler in the above embodiment.

[0060] In addition, the logic instructions in the memory 601 described above can be implemented in the form of software functional units and can be stored in a computer-readable storage medium when sold or used as an independent product.

[0061] The memory 601 is a computer-readable storage medium that can be used to store software programs and computer executable programs, such as program instructions / modules corresponding to the method in the embodiment of the present disclosure. The processor 604 executes the function application and data processing by running the program instructions / modules stored in the memory 601, that is, the method for automatic code upgrade based on the compiler in the above embodiment is implemented.

[0062] The memory 601 may include a program storage area and a data storage area, wherein the program storage area may store an operating system and an application required for at least one function; the data storage area may store data created according to the use of the terminal device, etc. In addition, the memory 601 may include a high-speed random access memory and may also include a non-volatile memory.

[0063] An embodiment of the present disclosure provides a computer-readable storage medium storing computer-executable instructions, wherein the computer-executable instructions are configured to execute a method for automatically upgrading codes based on a compiler.

[0064] The computer-readable storage medium mentioned above may be a transient computer-readable storage medium or a non-transitory computer-readable storage medium.

[0065] The technical solution of the embodiment of the present disclosure can be embodied in the form of a software product, which is stored in a storage medium and includes one or more instructions for enabling a computer device (which may be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the method of the embodiment of the present disclosure. The aforementioned storage medium may be a non-transient storage medium, including: a USB flash drive, a mobile hard disk, a read-only memory (ROM, Read-Only Memory), a random access memory (RAM, Random Access Memory), a disk or an optical disk, and other media that can store program codes, or a transient storage medium.

[0066] The above description and the accompanying drawings fully illustrate the embodiments of the present disclosure so that those skilled in the art can practice them. Other embodiments may include structural, logical, electrical, process and other changes. The embodiments represent possible variations only. Unless explicitly required, individual components and functions are optional, and the order of operations may vary. Parts and features of some embodiments may be included in or replace parts and features of other embodiments. As used in the description of the embodiments, unless the context clearly indicates otherwise, the singular forms "a", "an" and "the" are intended to include plural forms as well. Similarly, the term "and / or" as used in this application refers to any and all possible combinations of one or more associated listings. In addition, when used in this application, the term "comprise" and its variants "comprises" and / or including (comprising) refer to the presence of stated features, wholes, steps, operations, elements, and / or components, but do not exclude the presence or addition of one or more other features, wholes, steps, operations, elements, components and / or groups of these. In the absence of further restrictions, an element defined by the sentence "comprising a ..." does not exclude the existence of other identical elements in the process, method or device comprising the element. In this article, each embodiment may focus on the differences from other embodiments, and the same or similar parts between the embodiments may refer to each other. For the methods, products, etc. disclosed in the embodiments, if they correspond to the method part disclosed in the embodiments, then the relevant parts may refer to the description of the method part.

[0067] Those skilled in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software may depend on the specific application and design constraints of the technical solution. Technicians can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of the embodiments of the present disclosure. Technicians can clearly understand that for the convenience and simplicity of description, the specific working process of the systems, devices and units described above can refer to the corresponding process in the aforementioned method embodiment, and will not be repeated here.

[0068] In the embodiments disclosed herein, the disclosed methods and products (including but not limited to devices, equipment, etc.) can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of units can be only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. In addition, the coupling or direct coupling or communication connection between each other shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms. The units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the units may be selected according to actual needs to implement this embodiment. In addition, each functional unit in the embodiment of the present disclosure may be integrated in a processing unit, or each unit may exist physically alone, or two or more units may be integrated in one unit.

[0069] The flowchart and block diagram in the accompanying drawings show the possible architecture, functions and operations of the system, method and computer program product according to the embodiment of the present disclosure. In this regard, each box in the flowchart or block diagram can represent a module, a program segment or a part of the code, and a part of the module, program segment or code contains one or more executable instructions for realizing the specified logical function. In some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two consecutive boxes can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, which can depend on the functions involved. In the description corresponding to the flowchart and block diagram in the accompanying drawings, the operations or steps corresponding to different boxes can also occur in a different order from the order disclosed in the description, and sometimes there is no specific order between different operations or steps. For example, two consecutive operations or steps can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, which can depend on the functions involved. Each block in the block diagrams and / or flowcharts, and combinations of blocks in the block diagrams and / or flowcharts, may be implemented by a dedicated hardware-based system that performs the specified functions or actions, or may be implemented by a combination of dedicated hardware and computer instructions.< / iostream> < / iostream>

Claims

1. A method for automatic code upgrade based on a compiler, characterized in that: The method comprises: The front end performs lexical analysis and grammatical analysis on high-level programming languages ​​to generate an abstract syntax tree, and performs semantic analysis to generate an intermediate representation; The backend optimizes the intermediate representation, builds a new standard grammar rule mapping table, traverses the abstract syntax tree, queries whether the nodes of the abstract syntax tree can be found in the mapping table, outputs the strings mapped to the found nodes, and outputs the codes that conform to the semantics of the nodes that are not found.

2. The method according to claim 1, characterized in that The lexical analysis includes receiving input source code text, the lexical analyzer reading the source code text, dividing it into a series of tokens according to the lexical rules of the language, and outputting a token sequence, and using the tokens as input for subsequent syntax analysis, the tokens including keywords, type information of identifiers, names of identifiers, and specific numerical information of literals.

3. The method according to claim 2, characterized in that The syntax analysis includes using a syntax analyzer to build an abstract syntax tree according to the syntax rules of the language, using AST as a tree representation of the source code structure to reflect the syntax structure of the program, the nodes of the abstract syntax tree represent different building blocks of the program, and the abstract syntax tree serves as the basis for subsequent semantic analysis and optimization.

4. The method according to claim 3, characterized in that The semantic analysis includes using a semantic analyzer to traverse the AST and check the semantic correctness of the program, including type checking, scope resolution, and variable binding, to ensure that the program complies with the semantic rules of the language on the basis of syntactically correctness, and output an AST with semantic attributes of type information and scope information to be used as necessary semantic information for subsequent optimization and code generation.

5. The method according to claim 1, characterized in that The generation of the intermediate representation includes converting the AST into a universal intermediate representation, where the universal intermediate representation does not depend on a specific source language or target machine, so as to be used for subsequent optimization and code generation.

6. The method according to claim 1, characterized in that The method of constructing a new standard grammar rule mapping table, traversing the abstract syntax tree, querying whether the nodes of the abstract syntax tree can be found in the mapping table, outputting the strings mapped to the found nodes, and outputting the codes that conform to the node semantics normally for the unfinished nodes, includes: For the new standard of programming language, a mapping table from the node of abstract syntax tree to the new standard of high-level programming language is constructed, wherein the key in the mapping table is the node of abstract syntax tree, and the value in the mapping table is a specific string, and the specific string conforms to the code of the new standard; Traverse the abstract syntax tree and query the mapping table of the new standard. If the node meets the new standard, mark the node to check whether each node of the abstract syntax tree meets the grammatical rules of the new standard. Traverse the matched abstract syntax tree and use the code generator to generate source code that conforms to the new standard. The code generator serializes and formats the nodes in the abstract syntax tree according to the syntax and style requirements of the new standard. Output the source code under the new standard and output the strings generated by the code to a file.

7. A device for automatically upgrading code based on a compiler, characterized in that: The device comprises: The front-end module is used to perform lexical analysis and syntax analysis on high-level programming languages, generate abstract syntax trees, and perform semantic analysis to generate intermediate representations; The back-end module is used to optimize the intermediate representation, select instructions, schedule instructions, schedule registers, and generate executable programs; The back-end module is also used to build a grammar rule mapping table of the new standard, traverse the abstract syntax tree, query whether the nodes of the abstract syntax tree can be queried in the mapping table, and for the queried nodes, output the strings they are mapped to, and for the nodes that are not queried, output the code that conforms to the node semantics normally.

8. A non-transitory computer-readable storage medium storing computer instructions, characterized in that: The computer instructions are used to cause the computer to execute the method according to any one of claims 1 to 6.

Citation Information

Cited By

  • Data multi-protocol adaptive analysis method and system and storage medium

    CN120455570A

  • Automatic inference method and device for variable types of low-code platform

    CN121766459A

  • A method and apparatus for automatic inference of variable types in a low-code platform

    CN121766459B

  • Code conversion method and device, electronic equipment and computer readable storage medium

    CN122311091A

  • Code conversion method and device, electronic equipment and computer readable storage medium

    CN122311091B