Compiler-based automatic code upgrade method and apparatus, and storage medium
By automatically upgrading the code through the compiler, the problem of time-consuming, labor-intensive, and error-prone migration of old code to the new standard is solved, achieving efficient and automated code migration and code quality improvement.
Patent Information
- Authority / Receiving Office
- WO · WO
- Patent Type
- Applications
- Current Assignee / Owner
- Filing Date
- 2025-05-22
- Publication Date
- 2026-03-19
AI Technical Summary
In existing technologies, migrating old code to new programming standards requires manual migration, which is time-consuming, labor-intensive, and prone to errors, especially in large and complex codebases where the risk is high.
The compiler automatically upgrades the code, including lexical analysis, syntax analysis, and semantic analysis to generate an abstract syntax tree and build a new standard syntax rule mapping table. It automatically traverses the abstract syntax tree to perform node matching and transformation, generating source code that conforms to the new standard.
It reduces manual intervention, increases the automation of code migration, saves manpower and time costs, and generates code that is more in line with the syntax and naming conventions of the new standard, improving readability and maintainability.
Smart Images

Figure CN2025096565_19032026_PF_FP_ABST
Abstract
Description
Method, device and storage medium for automatically upgrading code based on a compiler
[0001] The present application claims priority from the Chinese patent application No. 202411279576.6 filed on September 12, 2024, and entitled "Method, device and storage medium for automatically upgrading code based on a compiler", the content of which is incorporated herein by reference in its entirety. TECHNICAL FIELD
[0002] The present application relates to the technical field of computer code upgrading, for example to a method, device and storage medium for automatically upgrading code based on a compiler. BACKGROUND
[0003] In the field of software development, with the continuous progress of technology and the continuous evolution of programming languages, new programming standards (such as new versions of languages such as C++, Java, Python, etc.) are constantly being introduced, providing developers with more rich, efficient and secure programming tools and features. These new standards often include improvements in syntax, performance optimization, addition of new libraries and APIs, better concurrency support, improvements in memory management mechanisms, and enhancements in security. However, due to historical legacy issues, compatibility considerations, resource limitations or developer habits, many projects are still using code written in older programming standards. Old code often appears to be inadequate when facing the advantages brought by new standards. In order to take advantage of the features provided by the new standard, developers need to manually migrate the 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, as well as familiarity with all the changes in the new standard, which puts high demands on the skills and experience of developers. In addition, manual migration can 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 section is only used to strengthen the understanding of the background of the present application, and therefore can include information that does not constitute prior art known to those of ordinary skill in the art. SUMMARY
[0005] In order to have a basic understanding of some aspects of the disclosed embodiments, a brief summary is given below. The summary is not a general review, nor is it intended to determine key / important components or delineate the scope of protection of these embodiments, but as a prelude to the detailed description that follows.
[0006] The method for automatically upgrading code based on a compiler provided by the embodiments of the present disclosure comprises:
[0007] The front end performs lexical analysis, syntax analysis, generates an abstract syntax tree, and performs semantic analysis on a high-level programming language to generate an intermediate representation;
[0008] The back end optimizes the intermediate representation, constructs a mapping table of syntax rules of the new standard, traverses the abstract syntax tree, queries whether the nodes of the abstract syntax tree can be queried in the mapping table, outputs the mapped string for the queried nodes, and normally outputs the code conforming to the semantics of the nodes for the unqueried nodes.
[0009] The lexical analysis includes receiving an input source code text, a 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 taking the tokens as the input of subsequent syntax analysis. The tokens include keyword, identifier type information, and identifier name, and literal value information.
[0010] In some embodiments, the syntax analysis includes using a syntax analyzer to construct an abstract syntax tree according to the syntax rules of the language, using an AST as a tree representation of the source code structure, reflecting the syntax structure of the program, and the nodes of the abstract syntax tree representing different construction blocks of the program. The abstract syntax tree serves as the basis for subsequent semantic analysis and optimization.
[0011] In some embodiments, the semantic analysis includes using a semantic analyzer to traverse the AST, checking the semantic correctness of the program, including type checking, scope resolution, and variable binding, to ensure that the program conforms to the semantic rules of the language on the basis of being grammatically correct, and outputting an AST with type information, scope information, and semantic attributes for subsequent optimization and code generation.
[0012] In some embodiments, the generation of the intermediate representation includes converting the AST into a general intermediate representation that is independent of a specific source language or target machine for subsequent optimization and code generation.
[0013] In some embodiments, the construction of the mapping table of syntax rules of the new standard, the traversal of the abstract syntax tree, the query of whether the nodes of the abstract syntax tree can be queried in the mapping table, the output of the mapped string for the queried nodes, and the normal output of the code conforming to the semantics of the nodes for the unqueried nodes include:
[0014] For the new standard of a programming language, a mapping table is constructed from the nodes of the abstract syntax tree to the new standard of the high-level programming language. The key in the mapping table is the node of the abstract syntax tree, and the value in the mapping table is a specific string that conforms to the code of the new standard.
[0015] traverse the abstract syntax tree, query in the mapping table of the new standard, if the node conforms to the new standard, mark the node, to check whether each node of the abstract syntax tree conforms to the syntax rule of the new standard;
[0016] traverse the matched abstract syntax tree, and generate source code conforming to the new standard by using a code generator, the code generator serializes and formats the nodes in the abstract syntax tree according to the syntax and style requirements of the new standard;
[0017] output the source code under the new standard, and output the code generated string to a file.
[0018] The device for automatically upgrading code based on a compiler provided by the embodiments of the present disclosure comprises:
[0019] The front-end module is configured to perform lexical analysis, syntax analysis, and semantic analysis on a high-level programming language, and generate an abstract syntax tree and an intermediate representation.
[0020] The back-end module is configured to optimize the intermediate representation, build a mapping table of syntax rules of the new standard, traverse the abstract syntax tree, query whether the node of the abstract syntax tree can be queried in the mapping table, output a string mapped by the queried node, and normally output code conforming to the semantics of the node for the unqueried node.
[0021] The lexical analysis comprises receiving an input source code text, reading the source code text by a lexical analyzer, dividing the source code text into a series of tokens according to the lexical rules of the language, and outputting a token sequence, and taking the token as the input of subsequent syntax analysis, the token comprising keyword, type information of identifier, and name of identifier, and specific numerical value information of literal.
[0022] In some embodiments, the back-end module is configured to optimize the intermediate representation, select instructions, schedule instructions, and schedule registers, and generate an executable program.
[0023] For the new standard of the programming language, a mapping table from the node of the abstract syntax tree to the new standard of the high-level programming language is built, the key in the mapping table is the node of the abstract syntax tree, and the value in the mapping table is a specific string, the specific string conforming to the code of the new standard.
[0024] traverse the abstract syntax tree, query in the mapping table of the new standard, if the node conforms to the new standard, mark the node, to check whether each node of the abstract syntax tree conforms to the syntax rule of the new standard;
[0025] Traverse the matched abstract syntax tree, and generate source code conforming to the new standard using a code generator, which serializes and formats the nodes in the abstract syntax tree according to the syntax and style requirements of the new standard;
[0026] Output the source code under the new standard, and output the code generated string to a file.
[0027] The storage medium provided by the embodiments of the present disclosure stores program instructions, and the program instructions perform the method of automatically upgrading code based on a compiler in the above embodiments when running.
[0028] The enhanced method, device and storage medium for automatically upgrading code based on a compiler provided by the embodiments of the present disclosure can achieve the following technical effects:
[0029] By automatically traversing the abstract syntax tree (AST) of the original code and mapping 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 code from the old standard to the new standard is improved. This not only saves a lot of manpower and time cost, but also reduces migration problems caused by human errors. Moreover, the code of the new standard often has a clearer syntax structure and more standardized naming conventions, and the code generated by automatic conversion can better meet these requirements, thereby improving the readability and maintainability of the code.
[0030] The foregoing general description and the following description are only exemplary and explanatory, and are not used to limit the present application. BRIEF DESCRIPTION OF DRAWINGS
[0031] One or more embodiments are exemplarily illustrated by corresponding drawings, which do not constitute limitations on the embodiments, elements with the same reference numerals in the drawings are shown as similar elements, the drawings do not constitute proportional limits, and wherein:
[0032] FIG. 1 is a flow diagram of a method for automatically upgrading code based on a compiler according to an embodiment of the present disclosure;
[0033] FIG. 2 is a flow diagram of a front end according to an embodiment of the present disclosure;
[0034] FIG. 3 is a flow diagram of another method for automatically upgrading code based on a compiler according to an embodiment of the present disclosure;
[0035] FIG. 4 is a diagram of an effect according to an embodiment of the present disclosure;
[0036] FIG. 5 is a structural diagram of a device for automatically upgrading code based on a compiler according to an embodiment of the present disclosure;
[0037] Fig. 6 is a structural schematic diagram of a system for automatically upgrading code based on a compiler according to an embodiment of the present disclosure. DETAILED DESCRIPTION
[0038] In order to enable a more detailed understanding of the features and technical content of the embodiments of the present disclosure, the implementation of the embodiments of the present disclosure will be described in detail below with reference to the accompanying drawings, which are only used for reference and do not limit the embodiments of the present disclosure. In the following technical description, in order to facilitate explanation, a plurality of details are provided to provide a sufficient understanding of the disclosed embodiments. However, one or more embodiments can still be implemented without these details. In other cases, well-known structures and devices can be simplified to facilitate the drawings.
[0039] The terms "first", "second", and the like in the embodiments of the present disclosure are used to distinguish similar objects, and do not necessarily have to describe a specific order or sequence. It should be understood that the data thus used can be interchanged under appropriate circumstances, so that the embodiments of the present disclosure described herein can be implemented. In addition, the terms "include" and "have" and any variations thereof are intended to cover non-exclusive inclusion.
[0040] Unless otherwise specified, the term "a plurality of" means two or more.
[0041] In the embodiments of the present disclosure, the character " / " represents an "or" relationship between the objects before and after it. For example, A / B represents: A or B.
[0042] The term "and / or" is a description of the association relationship between objects, which means that there can be three relationships. For example, A and / or B means: A or B, or, A and B, the three relationships.
[0043] The term "corresponding" can refer to an association relationship or a binding relationship. A and B correspond to each other means that there is an association relationship or a binding relationship between A and B.
[0044] The method, device, equipment and storage medium provided by the embodiments of the present disclosure for automatically upgrading code based on a compiler will be described below with reference to the accompanying drawings.
[0045] Fig. 1 is a flowchart of a method for automatically upgrading code based on a compiler according to an embodiment of the present disclosure. As shown in Fig. 1, the method for automatically upgrading code based on a compiler can include:
[0046] S01, the front end performs lexical analysis, syntax analysis, generates an abstract syntax tree, and performs semantic analysis on the high-level programming language, and generates an intermediate representation;
[0047] S02, the back end optimizes the intermediate representation, builds a mapping table of the syntax rules of the new standard, traverses the abstract syntax tree, queries whether the node of the abstract syntax tree can be queried in the mapping table, for the queried node, outputs the mapped string, and for the unqueried node, normally outputs the code conforming to the semantics of the node.
[0048] The lexical analysis includes receiving an input source code text, a 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 taking the token as the input of subsequent syntax analysis. The token includes the type information of the keyword, identifier and the name of the identifier, and the specific numerical value information of the literal.
[0049] In some embodiments, the above syntax analysis includes using a syntax analyzer to construct an abstract syntax tree according to the syntax rules of the language, using the AST as a tree representation of the source code structure, reflecting the syntax structure of the program, and the nodes of the abstract syntax tree representing different construction blocks of the program. The abstract syntax tree serves as the basis for subsequent semantic analysis and optimization.
[0050] In some embodiments, semantic analysis includes using a semantic analyzer to traverse the AST, checking the semantic correctness of the program, including type checking, scope resolution, variable binding, to ensure that the program meets the semantic rules of the language on the basis of being grammatically correct, and outputting the AST with type information, scope information and semantic attributes., used as necessary semantic information for subsequent optimization and code generation.
[0051] In some embodiments, the generation of the intermediate representation includes converting the AST into a general intermediate representation that does not depend on a specific source language or target machine for subsequent optimization and code generation.
[0052] In some embodiments, the above-mentioned construction of a mapping table of the syntax rules of the new standard, traversal of the abstract syntax tree, query of whether the node of the abstract syntax tree can be queried in the mapping table, output of the mapped string for the queried node, and normal output of the code conforming to the semantics of the node for the unqueried node, include:
[0053] For the new standard of the programming language, a mapping table is constructed from the nodes of the abstract syntax tree to the new standard of the high-level programming language. The key in the mapping table is the node of the abstract syntax tree, and the value in the mapping table is a specific string that conforms to the code of the new standard.
[0054] The abstract syntax tree is traversed, and the mapping table of the new standard is queried. If the node conforms to the new standard, the node is marked to check whether each node of the abstract syntax tree conforms to the syntax rules of the new standard.
[0055] Traverse the matched abstract syntax tree, and generate source code conforming to the new standard using a code generator, which serializes and formats the nodes in the abstract syntax tree according to the syntax and style requirements of the new standard;
[0056] Output the source code under the new standard, and output the code generated string to a file.
[0057] Figure 2 is a flowchart of a front end provided by an embodiment of the present disclosure, which further describes the process executed by the front end in Figure 1 in combination with Figure 2.
[0058] The flow of the front end is as follows:
[0059] 1. Lexical analysis: input the 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 token sequence, each token contains type (such as keyword, identifier, etc.) and possible value (such as the name of the identifier, the specific value of the literal), these tokens are used as the input of the subsequent syntax analysis.
[0060] 2. Syntax analysis: the syntax analyzer (Parser) constructs an abstract syntax tree (AST) according to the syntax rules of the language. AST is a tree representation of the structure of the source code, reflecting the syntax structure of the program. The nodes of the abstract syntax tree represent different construction blocks of the program (such as expressions, statements, declarations, etc.). The abstract syntax tree serves as the basis for subsequent semantic analysis and optimization.
[0061] 3. Semantic analysis: the semantic analyzer traverses the AST, checks the semantic correctness of the program, including type checking, scope resolution, variable binding, etc. It ensures that the program is not only syntactically correct, but also meets the semantic rules of the language, and outputs an AST with type information, scope information, etc. The enhanced AST provides necessary semantic information for subsequent optimization and code generation.
[0062] 4. Intermediate representation generation: convert the AST into a more general intermediate representation (IR), which is independent of the specific source language or target machine, and is convenient for subsequent optimization and code generation.
[0063] Based on the above-mentioned compiler front end for code processing to generate an abstract syntax tree, the present application traverses the abstract syntax tree for a specified new standard, at which time the nodes of the abstract syntax tree are expressions, statements or declarations independent of the specific standard, and combines each node with the syntax rules of the new standard to generate code conforming to the new standard.
[0064] FIG. 3 is a flow diagram of another method for automatic code upgrading based on a compiler according to an embodiment of the present disclosure. FIG. 3 further describes the process performed by the front end in FIG. 1.
[0065] 1. Input original source code, which is provided by a user and used by a compiler.
[0066] 2. Front-end processing. The compiler front end performs lexical analysis, syntax analysis, and semantic analysis on the source code, generates an abstract syntax tree, and confirms that the code is correct in syntax and semantics.
[0067] 3. Construct a mapping table of syntax rules of a new standard. For a new standard of a programming language, construct a mapping table from nodes of an abstract syntax tree to a high-level programming language, where the key is a node of an abstract syntax tree, such as a loop statement, a variable declaration, or an assignment expression, and the value is a specific string, which is code conforming to the new standard.
[0068] 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 the mapping table of the new standard. If the node conforms to the new standard, mark the node.
[0069] 5. Code generation. Traverse the matched abstract syntax tree and use a code generator to generate source code conforming 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.
[0070] 6. Output source code under the new standard. Output the string generated by the code to a file.
[0071] In the present disclosure, first, the source code is subjected to lexical analysis, syntax analysis, and semantic analysis, an abstract syntax tree is generated, a mapping table of syntax rules of a new standard is then constructed, the abstract syntax tree is traversed, it is queried whether a node of the abstract syntax tree can be queried in the mapping table, for a queried node, its mapped string is output, and for a node that is not queried, code conforming to the semantics of the node is normally output. By automatically traversing the abstract syntax tree (AST) of the original code and performing node matching and conversion according to the mapping table of syntax rules of the new standard, the need for manual intervention is greatly reduced, and the degree of automation of code migration from an old standard to a new standard is improved. This not only saves a large amount of manpower and time cost, but also reduces migration problems caused by human errors. Moreover, the code of the new standard often has a clearer syntax structure and more standardized naming conventions, and the automatically converted code can better meet these requirements, thereby improving the readability and maintainability of the code.
[0072] In one specific example, taking C++ programming language as an example, a compiler takes Clang as an example, and upgrades source code from C++98 standard to C++11 standard. The input source code main.cpp is as follows:
[0073] The for loop is used in the above code, the array defined in the function is looped, and each member in the array is output. In C++11, the for loop in the range can be used to traverse the array, and the auto keyword is used to automatically derive the type of the loop variable.
[0074] According to the method provided by the application, first, the syntax rule mapping table of C++11 standard is constructed, including the for loop in the range compilation, for the new features of the for loop in the range compilation, the key is the for loop statement ForStmt in the AST expression, and the value is the corresponding for loop string in the range, in the form of for (auto elem:arr).
[0075] Then the code is subjected to lexical analysis, syntax analysis, and abstract syntax tree generation, and then semantic analysis is performed to confirm that the code is correct in syntax and semantics. The abstract syntax tree generated according to the input source code contains for loop statement node ForStmt, variable definition statement VarDecl, assignment statement InitListExpr, etc.
[0076] The abstract syntax tree is traversed, and each node of the abstract syntax tree is matched. If the corresponding node is found in the C++11 syntax rule mapping table, the node is marked, and the node is marked as C++11 standard. If no corresponding node is found, the node is skipped. The abstract syntax tree contains the for loop statement ForStmt, and the C++11 syntax rule mapping table also includes ForStmt, so the ForStmt node is marked.
[0077] After marking the nodes in the abstract syntax tree, code generation is started. For the nodes marked with C++11 standard, the ForStmt corresponding string stored in the syntax rule mapping table is output, and for the nodes without marking, the standard code generator is used to generate code.
[0078] The code output by the application is as follows:
[0079] The for loop statement in the code is changed from the conventional for loop to the for loop in the range, and the specific embodiment effect diagram is shown in FIG. 4.
[0080] Fig. 5 is a structural schematic diagram of an apparatus for automatic code upgrading based on a compiler according to an embodiment of the present disclosure. As shown in Fig. 5, the apparatus for automatic code upgrading based on a compiler can include:
[0081] a front-end module 501 configured to perform lexical analysis, syntax analysis, generate an abstract syntax tree, and perform semantic analysis on a high-level programming language, and generate an intermediate representation;
[0082] a back-end module 502 configured to optimize the intermediate representation, build a mapping table of syntax rules of a new standard, traverse the abstract syntax tree, query whether a node of the abstract syntax tree can be queried in the mapping table, output a string mapped to the node for a queried node, and normally output code conforming to semantics of the node for an unqueried node.
[0083] The lexical analysis includes receiving an input source code text, a lexical analyzer reading the source code text, dividing the source code text into a series of tokens according to lexical rules of a language, and outputting a token sequence, and taking the tokens as input for subsequent syntax analysis. The tokens include type information of keywords and identifiers, and names of the identifiers and specific numerical value information of literal quantities.
[0084] In some embodiments, the syntax analysis includes using a syntax analyzer to build an abstract syntax tree according to syntax rules of a language, using an AST as a tree-like representation of a structure of source code, reflecting a syntax structure of a program, and nodes of the abstract syntax tree representing different construction blocks of the program. The abstract syntax tree serves as a basis for subsequent semantic analysis and optimization.
[0085] In some embodiments, the semantic analysis includes using a semantic analyzer to traverse the AST, checking semantic correctness of the program, including type checking, scope resolution, and variable binding, to ensure that the program conforms to semantic rules of a language on the basis of being correct in syntax, and outputting the AST with type information, scope information, and semantic attributes for use as necessary semantic information for subsequent optimization and code generation.
[0086] In some embodiments, the generation of the intermediate representation includes converting the AST into a general intermediate representation that is independent of a specific source language or a target machine for subsequent optimization and code generation.
[0087] In some embodiments, the above-mentioned construction of the mapping table of syntax rules of the new standard, the traversal of the abstract syntax tree, the query of whether a node of the abstract syntax tree can be queried in the mapping table, the output of a string mapped to the node for a queried node, and the normal output of code conforming to semantics of the node for an unqueried node include:
[0088] For the new standard of the programming language, a mapping table from the node of the abstract syntax tree to the new standard of the high-level programming language is constructed, the key in the mapping table is the node of the 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.
[0089] The abstract syntax tree is traversed, and the mapping table of the new standard is queried. If the node conforms to the new standard, the node is marked to check whether each node of the abstract syntax tree conforms to the syntax rule of the new standard.
[0090] The matched abstract syntax tree is traversed, and the code generator is used to generate source code conforming 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.
[0091] The source code under the new standard is output, and the string generated by the code is output to a file.
[0092] It should be noted that, for the foregoing method embodiments, in order to simply describe, they are all expressed as a series of action combinations, but those skilled in the art should know that the present disclosure is not limited by the action sequence described, because according to the present disclosure, certain steps can be performed in other sequences or simultaneously. Secondly, those skilled in the art should know 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.
[0093] In combination with FIG. 6, the present embodiment provides a system 600 for automatic code upgrading based on a compiler, including a processor 604 and a memory 601. Optionally, the system can also include a communication interface 602 and a bus 603. The processor 604, the communication interface 602, and the memory 601 can complete mutual communication through the bus 603. The communication interface 602 can be used for information transmission. The processor 604 can call the logical instructions in the memory 601 to execute the method for automatic code upgrading based on the compiler in the above-mentioned embodiments.
[0094] In addition, the logical instructions in the memory 601 described above can be implemented in the form of a software functional unit and sold or used as an independent product, which can be stored in a computer readable storage medium.
[0095] The memory 601 can be used to store software programs, computer executable programs, such as program instructions / modules corresponding to the method in the embodiments 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, implements the method for automatically upgrading code based on the compiler in the above embodiments.
[0096] The memory 601 can include a program storage area and a data storage area, wherein the program storage area can store an operating system and an application program required by at least one function; the data storage area can store data created according to the use of the terminal device, etc. In addition, the memory 601 can include a high-speed random access memory, and can also include a non-volatile memory.
[0097] The embodiments of the present disclosure provide a computer readable storage medium, which stores computer executable instructions, and the computer executable instructions are configured to execute the method for automatically upgrading code based on the compiler.
[0098] The computer readable storage medium described above can be a transitory computer readable storage medium or a non-transitory computer readable storage medium.
[0099] The technical solutions of the embodiments of the present disclosure can be embodied in the form of a software product. The computer software product is stored in a storage medium, and includes one or more instructions to make a computer device (which can be a personal computer, a server, or a network device, etc.) execute all or part of the steps of the method of the embodiments of the present disclosure. The aforementioned storage medium can be a non-transitory storage medium, including: a U disk, a mobile hard disk, a read-only memory (ROM, Read-Only Memory), a random access memory (RAM, Random Access Memory), a magnetic disk or an optical disk, and various media that can store program codes, or can be a transitory storage medium.
[0100] The above description and drawings suffice to fully enable one skilled in the art to practice the embodiments of the present disclosure. Other embodiments can include structural, logical, electrical, process, and other changes. The embodiments are illustrative only. Individual components and functions are optional unless explicitly required, and the order of operations can be varied. Portions and features of some embodiments can be included in, or substituted for, those of other embodiments. As used in the description of the embodiments, the article "a" is intended to include one or more items unless the context clearly indicates otherwise. Likewise, the term "and / or" as used in the application refers to any and all possible combinations of one or more of the associated listed items. Additionally, as used in this application, the term "comprises / comprising" its variants such as "comprises / comprising" and / or "comprises / comprising" and the like indicate the presence of stated features, integers, steps, operations, elements, and / or components but do not preclude the presence or addition of one or more other features, integers, steps, operations, elements, components, and / or groups thereof. An element procured in the statement "comprising a..." does not, without more constraints, exclude the presence of additional identical elements in the process, method, article, or apparatus including the element. In this document, each embodiment focuses on the differences from other embodiments, and the same or similar parts between various embodiments can be referred to each other.
[0101] For the methods, products, and the like disclosed by the embodiments, if they correspond to the method part disclosed by the embodiments, the relevant part can be referred to the description of the method part.
[0102] Those skilled in the art can understand that the units and algorithm steps of each example described in combination with the embodiments disclosed herein can be realized by electronic hardware or a combination of computer software and electronic hardware. Whether the functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of the embodiments of the present disclosure. Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working process of the above-described system, device and unit can refer to the corresponding process in the foregoing method embodiments, which will not be repeated here. In the embodiments disclosed herein, the disclosed methods, 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 the units can be only a logical function division, and there can be another division manner in actual implementation; for example, a plurality of units or components can be combined or integrated into another system, or some features can be ignored or not executed. In addition, the displayed or discussed mutual coupling or direct coupling or communication connection between each other can be indirect coupling or communication connection through some interface, device or unit, and can be electrical, mechanical or other forms. The units illustrated as separate components can be or can not be physically separate, and the components illustrated as units can be or can not be physical units, that is, they can be located in one place, or can be distributed on a plurality of network units. Some or all of the units can be selected according to actual needs to implement the embodiments. In addition, each functional unit in the embodiments of the present disclosure can be integrated in one processing unit, or each unit can exist physically independently, or two or more units can be integrated in one unit.
[0103] The computer program instructions can also be loaded onto a computer, other programmable data processing apparatus, or other processing device to cause a series of operational steps to be performed on the computer, other programmable apparatus or other processing device to produce a computer implemented process such that the instructions which execute on the computer or other programmable apparatus provide processes for implementing the functions / acts specified in the flowchart and / or block diagram block or blocks.
Claims
1. A method for automatic code upgrade based on compiler, characterized in that, The method comprises: front-end lexical analysis, syntax analysis, abstract syntax tree generation and semantic analysis of a high-level programming language to generate an intermediate representation; The back-end optimizes the intermediate representation, constructs a mapping table of syntax rules of a new standard, traverses the abstract syntax tree, queries whether a node of the abstract syntax tree can be queried in the mapping table, outputs a mapped string of the node for a queried node, and normally outputs code conforming to semantics of the node for an unqueried node.
2. The method of claim 1, wherein, The lexical analysis comprises receiving an input source code text, reading the source code text by a lexical analyzer, segmenting the source code text into a series of tokens according to lexical rules of a language, and outputting a token sequence, and taking the tokens as input of subsequent syntax analysis, the tokens comprising keyword, type information and name of an identifier, and specific numerical value information of a literal.
3. The method of claim 2, wherein, The syntax analysis comprises constructing an abstract syntax tree according to syntax rules of a language by using a syntax analyzer, taking the abstract syntax tree as a tree representation of a source code structure, reflecting a syntax structure of a program, and taking a node of the abstract syntax tree as a different construction block of the program, and taking the abstract syntax tree as a basis for subsequent semantic analysis and optimization.
4. The method of claim 3, wherein, The semantic analysis comprises traversing the AST by using a semantic analyzer, checking semantic correctness of the program, including type checking, scope resolution and variable binding, to ensure that the program conforms to semantic rules of the language on the basis of being correct in syntax, and outputting the AST with type information, scope information and semantic attributes for being necessary semantic information for subsequent optimization and code generation.
5. The method of claim 1, wherein, The generation of the intermediate representation comprises converting the AST into a general intermediate representation, which is independent of a specific source language or a target machine, for subsequent optimization and code generation.
6. The method of claim 1, wherein, The construction of the mapping table of syntax rules of the new standard, the traversal of the abstract syntax tree, the query of whether a node of the abstract syntax tree can be queried in the mapping table, the output of a mapped string of the node for a queried node, and the normal output of code conforming to semantics of the node for an unqueried node comprise: For a new standard of a programming language, a mapping table from a node of the abstract syntax tree to the new standard of the high-level programming language is constructed, the key in the mapping table is the node of the abstract syntax tree, and the value in the mapping table is a specific string conforming to code of the new standard; The abstract syntax tree is traversed, and the new standard mapping table is queried, if the node conforms to the new standard, the node is marked, to check whether each node of the abstract syntax tree conforms to syntax rules of the new standard; the matched abstract syntax tree is traversed, and a code generator is used to generate source code conforming to the new standard, the code generator serializes and formats the nodes in the abstract syntax tree according to syntax and style requirements of the new standard; The source code under the new standard is output, and the string output by the code generation is output to a file.
7. An apparatus for automatic code upgrade based on a compiler, the apparatus comprising: The device comprises a front-end module configured to perform lexical analysis, syntax analysis, abstract syntax tree generation and semantic analysis of a high-level programming language to generate an intermediate representation; A back-end module is configured to optimize the intermediate representation, select instructions, schedule instructions, schedule registers, and generate an executable program. The back-end module is further configured to build a mapping table of syntax rules of the new standard, traverse the abstract syntax tree, query whether a node of the abstract syntax tree can be queried in the mapping table, output a string mapped to the node for a queried node, and normally output code conforming to semantics of the node for an unqueried node.
8. A non-transitory computer-readable storage medium having stored thereon computer instructions, wherein, The computer instructions are configured to cause the computer to perform the method according to any one of claims 1 to 6.
Citation Information
Patent Citations
Automatic code conversion method and device, code converter and medium
CN110471666A
Code updating method and device based on data standardization and terminal equipment
CN110609693A
LLVM-based ST language compiling method and system and compiler
CN110825384A
Text programming language compiling method based on decoupling architecture
CN116501330A
Method and device for automatically upgrading codes based on compiler and storage medium
CN119311289A