Code automatic standardization method, device, storage medium, and program product

By automatically standardizing Java code, using syntax parsing and abstract syntax tree technology, combining the standard rule library and the optimization of the rule library mapping table, the existing tools have solved the shortcomings in code specifications and optimization, and achieved the improvement of code quality and maintainability.

CN119473310BActive Publication Date: 2025-04-11BEIJING VRV SOFTWARE CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202510054603.8
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-01-14
Publication Date
2025-04-11
Estimated Expiration
2045-01-14

AI Technical Summary

Technical Problem

Existing Java code inspection tools still have shortcomings in terms of comprehensive specification coverage and intelligent and accurate automatic optimization, resulting in a decrease in code readability, maintenance and development efficiency.

Method used

Through automated specification code, syntax parsing is used to generate an abstract syntax tree, traversing the tree structure to identify node types, collect node data, build a mapping table for the specification rule library and optimization rule library, judge whether there are exceptions in the code, and optimize the exception node data, and finally generate code that complies with the specification.

Benefits of technology

Improves code quality, consistency and maintainability, reduces development and maintenance costs, ensures that the code complies with high-quality writing standards, and improves code readability, maintainability and scalability.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119473310B_ABST
    Figure CN119473310B_ABST
Patent Text Reader

Abstract

The disclosed embodiment discloses a method, device, storage medium, and program product for automatic code standardization. The method includes: obtaining the code to be standardized; parsing the code to be standardized to generate an abstract syntax tree; traversing the abstract syntax tree to obtain the type of each node; collecting node data according to the type; constructing multiple mapping tables, wherein the multiple mapping tables store the standard rules in the preset standard rule library and the optimization rules in the preset optimization rule library, and establish a mapping relationship between the standard rules and the optimization rules; based on the type, the standard rule library, and the multiple mapping tables, judging whether the node data is abnormal; if so, optimizing the abstract syntax tree based on the multiple mapping tables and the abnormal node data; generating code that meets the specification based on the optimized abstract syntax tree. The method can improve the quality, consistency, and maintainability of the code by automatically standardizing the code, while reducing the development and maintenance costs.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present disclosure relates to the field of software engineering, and in particular to a method, device, storage medium, and program product for automatic code standardization. Background Art

[0002] In the field of software development, Java is widely used due to its cross-platform nature, robustness, and rich ecosystem. However, as projects expand and team members increase, the standardization of code becomes particularly important. Irregular Java code not only affects the readability and maintainability of the code, but may also lead to debugging difficulties and reduced development efficiency. Although there are some code checking tools, they are still insufficient in terms of comprehensive coverage of specifications, intelligence and accuracy of automatic optimization. Summary of the invention

[0003] In view of this, the embodiments of the present disclosure provide a method, device, storage medium, and program product for automatic code standardization, which can improve code quality, consistency, and maintainability by automatically standardizing code, while reducing development and maintenance costs.

[0004] In a first aspect, the present disclosure provides a method for automatic code standardization, which adopts the following technical solution:

[0005] Get the code to be standardized;

[0006] Performing syntax analysis on the code to be standardized to generate an abstract syntax tree;

[0007] Traversing the abstract syntax tree to obtain the type of each node;

[0008] Collect node data according to the type;

[0009] Constructing a plurality of mapping tables, wherein the plurality of mapping tables store the standard rules in the preset standard rule base and the optimization rules in the preset optimization rule base, and establishing a mapping relationship between the standard rules and the optimization rules;

[0010] Based on the type, the specification rule base and the plurality of mapping tables, determining whether the node data is abnormal;

[0011] If yes, optimizing the abstract syntax tree based on the multiple mapping tables and the abnormal node data;

[0012] Generates code that complies with the specification based on the optimized abstract syntax tree.

[0013] Optionally, the performing syntax parsing on the code to be standardized to generate an abstract syntax tree includes:

[0014] Decomposing the code to be standardized into multiple lexical units;

[0015] Based on the preset grammatical rules, grammar analysis is performed on each lexical unit;

[0016] When it is determined that the lexical unit conforms to the grammatical rule, creating a normal node based on the lexical unit;

[0017] When the lexical unit cannot be parsed, creating an unknown node based on the lexical unit;

[0018] When it is determined that the lexical unit does not conform to the grammatical rule, creating an error node based on the lexical unit;

[0019] When missing content between tokens is detected, an empty node is created based on the position of the missing content;

[0020] Generate an abstract syntax tree based on all the nodes created.

[0021] Optionally, the grammatical analysis of each lexical unit based on preset grammatical rules includes:

[0022] Constructing a buffer, wherein the buffer downloads the disassembled lexical units according to a preset number and stores the downloaded lexical units in the order of disassembly;

[0023] Reading a lexical unit from the buffer, and deleting the lexical unit from the buffer after the lexical unit is read;

[0024] Performing grammatical analysis on the read lexical unit based on the grammatical rules;

[0025] When the number of lexical units stored in the buffer is less than a preset number threshold;

[0026] Based on the preset number and the number of lexical units, the disassembled lexical units continue to be downloaded until all the disassembled lexical units are downloaded.

[0027] Optionally, the types include variable declaration nodes, method declaration nodes, control structure nodes, unknown nodes, error nodes and empty nodes.

[0028] Optionally, the constructing of multiple mapping tables includes:

[0029] constructing a plurality of mapping tables based on the number of standard rule sets contained in the standard rule base;

[0030] Determine a mapping relationship between each standard rule in the standard rule base and each optimization rule in the optimization rule base;

[0031] Convert each specification rule into a first key-value pair, and convert the optimization rule having a mapping relationship with the specification rule into a second key-value pair;

[0032] Based on the mapping relationship, the first key-value pair and the second key-value pair are combined into a mapping entry and stored in a corresponding mapping table.

[0033] Optionally, judging whether the node data is abnormal based on the type, the specification rule base and the multiple mapping tables includes:

[0034] Based on the type of the node, determining an available specification rule set from a plurality of specification rule sets;

[0035] Based on the available specification rule set, determining an available mapping table from a plurality of preset mapping tables;

[0036] Matching the specification rules stored in the available mapping table with the node data;

[0037] If a match is successful with any of the specification rules stored in the available mapping table, it is determined that the node data is abnormal;

[0038] If all the specification rules stored in the available mapping table fail to match, it is determined that there is no abnormality in the node data.

[0039] Optionally, the optimizing the abstract syntax tree based on the multiple mapping tables and abnormal node data includes:

[0040] When it is determined that the node data is abnormal, based on the specification rule that successfully matches the node data, determining an available optimization rule from the available mapping table;

[0041] The abstract syntax tree is optimized based on the available optimization rules and the abnormal node data.

[0042] In a second aspect, the embodiment of the present disclosure also provides a system for automatic code standardization, which adopts the following technical solution:

[0043] A code acquisition module is used to acquire the code to be standardized;

[0044] A syntax parsing module, used for parsing the code to be standardized and generating an abstract syntax tree;

[0045] A type acquisition module, used to traverse the abstract syntax tree and obtain the type of each node;

[0046] A data collection module, used for collecting node data according to the type;

[0047] A mapping table construction module, used to construct a plurality of mapping tables, wherein the plurality of mapping tables store the standard rules in a preset standard rule base and the optimization rules in a preset optimization rule base, and establish a mapping relationship between the standard rules and the optimization rules;

[0048] An abnormality judgment module, used to judge whether the node data has an abnormality based on the type, the specification rule base and the multiple mapping tables; if so, execute the syntax tree optimization module;

[0049] A syntax tree optimization module, configured to optimize the abstract syntax tree based on the multiple mapping tables and abnormal node data;

[0050] The code generation module is used to generate code that complies with the specification based on the optimized abstract syntax tree.

[0051] In a third aspect, the embodiments of the present disclosure further provide a computer device, which adopts the following technical solution:

[0052] The computer device comprises:

[0053] at least one processor; and,

[0054] a memory communicatively connected to the at least one processor; wherein,

[0055] The memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to perform any of the above-mentioned code automatic standardization methods.

[0056] In a fourth aspect, an embodiment of the present disclosure further provides a computer-readable storage medium, which stores computer instructions, and the computer instructions are used to enable a computer to execute any of the above-mentioned code automatic standardization methods.

[0057] In a fifth aspect, an embodiment of the present disclosure further provides a computer program product, including a computer program / instruction, which implements the steps of any of the above methods when executed by a processor.

[0058] The automatic code standardization method provided by the embodiment of the present disclosure realizes programmatic access to various parts of the code by parsing the code to be standardized and generating an abstract syntax tree, thereby providing a solid foundation for automated standardization. This method helps to ensure the consistency of the code and avoids problems caused by inconsistent developer styles. By traversing the abstract syntax tree and identifying the type of each node, specific elements in the code, such as variable declarations and function calls, can be accurately located, which is crucial for standard inspection and optimization. At the same time, collecting node data can obtain specific information of the code, such as variable names and function parameters, which provides a basis for judging whether the code complies with the specification and optimizing. Through the preset standard rule library, it is possible to automatically judge whether there is a problem with the code, and a mapping table is also applied in the judgment process to improve the judgment efficiency. For node data that meets the specification, there is no need to modify it, which reduces unnecessary code intervention and maintains the original meaning and function of the code. For code that does not meet the specification, it will be modified according to the optimization rule library to improve the code quality, ensure the consistency of optimization, and follow the best practices. Generate code that meets the specification through the optimized abstract syntax tree, avoiding direct modification of the original code to avoid affecting the internal logic of the code. This method is faster and more consistent than manual inspection through comprehensive specification detection and automatic optimization. It can find potential problems, ensure that the code meets high-quality writing standards, and improve the readability, maintainability and scalability of the code. This method reduces the time developers spend manually checking and modifying code specification issues, allowing them to focus more on the implementation of business logic and accelerate the software development cycle. In team development, it ensures that all members' codes follow the same specifications to facilitate code integration and team collaboration. Code that complies with the specifications is easier to understand and modify during subsequent maintenance, reducing the difficulty and cost of maintenance. This method has a wide range of applicability and can adapt to different code logics. Especially when applied to Java code specifications, it can cover various problems in Java code more and more comprehensively by continuously maintaining and updating the specification rule base and optimization rule base.

[0059] The above description is only an overview of the technical solution of the present invention. In order to more clearly understand the technical means of the present invention, it can be implemented in accordance with the contents of the specification. In order to make the above and other purposes, features and advantages of the present invention more obvious and easy to understand, the following preferred embodiments are specifically cited and described in detail with reference to the accompanying drawings. BRIEF DESCRIPTION OF THE DRAWINGS

[0060] In order to more clearly illustrate the technical solutions of the embodiments of the present disclosure, the drawings required for use in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present disclosure. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.

[0061] Figure 1 A flowchart of a method for automatic code standardization provided by an embodiment of the present disclosure;

[0062] Figure 2 A schematic diagram of a flow chart of a method for generating an abstract syntax tree provided in an embodiment of the present disclosure;

[0063] Figure 3 A flowchart of a lexical unit caching method provided by an embodiment of the present disclosure;

[0064] Figure 4 A flowchart of a method for constructing a mapping table provided in an embodiment of the present disclosure;

[0065] Figure 5 A principle block diagram of a code automatic standardization system provided by an embodiment of the present disclosure;

[0066] Figure 6 A schematic diagram of the structure of a computer device provided in an embodiment of the present disclosure. DETAILED DESCRIPTION

[0067] The embodiments of the present disclosure are described in detail below with reference to the accompanying drawings.

[0068] It should be clear that the following embodiments of the present disclosure are described by specific specific examples, and those skilled in the art can easily understand other advantages and effects of the present disclosure from the contents disclosed in this specification. Obviously, the described embodiments are only a part of the embodiments of the present disclosure, rather than all of the embodiments. The present disclosure can also be implemented or applied through other different specific embodiments, and the details in this specification can also be modified or changed in various ways based on different viewpoints and applications without departing from the spirit of the present disclosure. It should be noted that the following embodiments and features in the embodiments can be combined with each other in the absence of conflict. Based on the embodiments in the present disclosure, all other embodiments obtained by ordinary technicians in the field without making creative work are within the scope of protection of the present disclosure.

[0069] It should be noted that various aspects of the embodiments within the scope of the appended claims are described below. It should be apparent that the aspects described herein may be embodied in a wide variety of forms, and any specific structure and / or function described herein is merely illustrative. Based on the present disclosure, it should be understood by those skilled in the art that an aspect described herein may be implemented independently of any other aspect, and two or more of these aspects may be combined in various ways. For example, any number of aspects described herein may be used to implement the device and / or practice the method. In addition, other structures and / or functionalities other than one or more of the aspects described herein may be used to implement this device and / or practice this method.

[0070] It should also be noted that the illustrations provided in the following embodiments are only schematic illustrations of the basic concept of the present disclosure. The drawings only show components related to the present disclosure rather than being drawn according to the number, shape and size of components in actual implementation. In actual implementation, the type, quantity and proportion of each component may be changed arbitrarily, and the component layout may also be more complicated.

[0071] Additionally, in the following description, specific details are provided to facilitate a thorough understanding of the examples. However, it will be understood by those skilled in the art that the aspects described may be practiced without these specific details.

[0072] Reference Figure 1 The present invention provides a method for automatic code standardization, comprising the following steps:

[0073] S1: Get the code to be standardized;

[0074] S2: Perform syntax analysis on the standard code and generate an abstract syntax tree;

[0075] S3: traverse the abstract syntax tree and obtain the type of each node;

[0076] S4: Collect node data according to type;

[0077] S5: construct multiple mapping tables, wherein the multiple mapping tables store the standard rules in the preset standard rule base and the optimization rules in the preset optimization rule base, and establish a mapping relationship between the standard rules and the optimization rules;

[0078] S6: Based on the type, the specification rule base and the multiple mapping tables, determine whether the node data is abnormal; if not, execute S7; if yes, execute S8;

[0079] S7: No processing of node data;

[0080] S8: Optimize the abstract syntax tree based on multiple mapping tables and abnormal node data;

[0081] S9: Generate code that complies with the specification based on the optimized abstract syntax tree.

[0082] The disclosed automatic code standardization method realizes programmatic access to various parts of the code by parsing the code to be standardized and generating an abstract syntax tree, providing a solid foundation for automated standardization. This method helps to ensure the consistency of the code and avoids problems caused by inconsistent developer styles. By traversing the abstract syntax tree and identifying the type of each node, specific elements in the code, such as variable declarations and function calls, can be accurately located, which is crucial for standard inspection and optimization. At the same time, collecting node data can obtain specific information of the code, such as variable names and function parameters, which provides a basis for judging whether the code complies with the specification and optimizing. Through the preset standard rule library, it is possible to automatically judge whether there is a problem with the code, and a mapping table is also applied in the judgment process to improve the judgment efficiency. For node data that meets the specification, there is no need to modify it, which reduces unnecessary code intervention and maintains the original meaning and function of the code. For code that does not meet the specification, it will be modified according to the optimization rule library to improve the code quality, ensure the consistency of optimization, and follow the best practices. Generate code that meets the specification through the optimized abstract syntax tree, avoiding direct modification of the original code to avoid affecting the internal logic of the code. This method is faster and more consistent than manual inspection through comprehensive specification detection and automatic optimization. It can find potential problems, ensure that the code meets high-quality writing standards, and improve the readability, maintainability and scalability of the code. This method reduces the time developers spend manually checking and modifying code specification issues, allowing them to focus more on the implementation of business logic and accelerate the software development cycle. In team development, it ensures that all members' codes follow the same specifications to facilitate code integration and team collaboration. Code that complies with the specifications is easier to understand and modify during subsequent maintenance, reducing the difficulty and cost of maintenance. This method has a wide range of applicability and can adapt to different code logics. Especially when applied to Java code specifications, it can cover various problems in Java code more and more comprehensively by continuously maintaining and updating the specification rule base and optimization rule base.

[0083] In S1, a code input module and a syntax analysis module are set up, where the code input module is mainly responsible for receiving Java code data from different sources, including supporting the following three code sources: one is the local file system, where users can specify the Java code files to be detected through the file path; the second is the version control system (such as Git), where users obtain Java code in a specific code base by specifying the warehouse address and branch information; the third is the development environment, where users directly input Java code snippets or files through an integrated development environment (IDE) or other development tools. In short, users submit Java code data to the system's code input module through development environment plug-ins or command line tools. The code input module, as an input interface, is responsible for receiving Java code data and passing it to the syntax analysis module as the code to be standardized to ensure the integrity of the Java code data.

[0084] In S2, the syntax analysis module uses Java compiler technology (such as JDT tools) to perform syntax analysis on the standard code and build an abstract syntax tree (AST) based on grammatical rules for downward recursive analysis. Figure 2 The flowchart of the abstract syntax tree generation method shown in the figure, "parsing the standard code and generating an abstract syntax tree" includes the following steps:

[0085] S21: Decompose the code to be standardized into multiple lexical units;

[0086] S22: performing grammatical analysis on each lexical unit based on preset grammatical rules;

[0087] S23: when it is determined that the lexical unit complies with the grammatical rule, a normal node is created based on the lexical unit;

[0088] S24: when the lexical unit cannot be parsed, creating an unknown node based on the lexical unit;

[0089] S25: when it is determined that the lexical unit does not conform to the grammatical rule, creating an error node based on the lexical unit;

[0090] S26: when it is detected that there is a lack of content between lexical units, an empty node is created based on the position of the missing content;

[0091] S27: Generate an abstract syntax tree based on all created nodes.

[0092] In S21, a lexical analyzer is used to scan the code to be standardized, and the code to be standardized is broken down into multiple lexical units, including but not limited to keywords (such as class, public), identifiers (variable names, class names), operators (+, -, *), and separators ({,}, ;).

[0093] In S22, a parser is used to parse the lexical unit sequence into a tree structure according to preset grammatical rules. During the parsing process, corresponding nodes are created according to the grammatical rules. For example, in Java code, the declaration of a class is the starting point of the abstract syntax tree. When the parser detects the keyword class, it confirms the start of parsing a class. At this time, the parser creates a ClassDeclaration node and uses it as the root node of the abstract syntax tree. Then, the parser continues to read the class name (such as MyClass) and creates an Identifier node as the name of the class. After matching the curly braces {, the internal parsing phase of the class is entered. The task of the parser is to parse the introduction, methods, variables, etc. of the class one by one. The parser will parse the lexical units inside the class one by one according to the preset grammatical rules. For example, when parsing a statement such as int num;, the parser will first identify the keyword int as the data type of the variable, then identify the identifier num as the variable name, and finally match the semicolon; as the end of the statement. The parser will create corresponding nodes according to the grammatical rules. For example, the statement int num; will generate a VariableDeclaration node (variable declaration node), which contains two child nodes: one is the Type node representing the data type (value is int), and the other is the Identifier node representing the variable name (value is num). These child nodes are connected to the parent node VariableDeclaration through edges to form a hierarchical structure. This process will recursively process various nested grammatical structures in the standard code (such as inner structures such as statement blocks in method declarations) and continuously build a complete abstract syntax tree.

[0094] For more complex statements (such as method calls), the parser needs to parse the nested structure more deeply. For example, when parsing a statement like deviceService.delete(deviceId), the parser will first identify the object reference deviceService, then identify the method call operator. and the method name delete, and finally parse the parameter list in the parentheses. Each part will generate a corresponding node according to the grammatical rules and establish a reasonable connection relationship. The parser will create a MethodInvocation node and parse the object reference deviceService and the method name delete in turn. For the parameter list, the parser will recursively parse each parameter and create a corresponding node for each parameter. These nodes will be connected to the MethodInvocation node through edges to form a complete tree structure.

[0095] Through the above steps, the parser can parse lexical units one by one based on the preset grammar rules and recursively construct a complete abstract syntax tree. This process not only covers simple grammatical structures (such as variable declarations), but also supports complex nested structures (such as method declarations and method calls), and finally forms a hierarchical tree representation.

[0096] In S23-S26, due to the diversity of code types, the initial preset grammar rules may not fully cover all types of codes, resulting in unparseable lexical units, such as unknown elements, in the grammatical parsing process. In order to avoid affecting the generation of the abstract syntax tree, for such unparseable lexical units, it is constructed as an unknown node and exists in the abstract syntax tree in an unknown state. When a lexical unit that does not conform to the grammatical rules is detected, such as an extra symbol or an unmatched bracket, it is constructed as an error node. When it is detected that there is a lack of content between lexical units, such as a missing colon, an empty node is constructed under the generated node according to the position of the missing content. This empty node will exist in the abstract syntax tree in an inserted manner to represent the missing content. When parsing a lexical unit, if no problem is found, that is, it is confirmed that the lexical unit conforms to the grammatical rules, a normal node can be created directly based on the lexical unit.

[0097] In S27, starting from the starting point of syntax analysis (such as program entry), the parser recursively builds subtrees. Each node can contain zero or more child nodes, which together form a hierarchical structure. Ultimately, all created nodes will be organized according to this hierarchical relationship to represent the structure of the entire code, namely the abstract syntax tree.

[0098] When encountering a syntax error, the traditional abstract syntax tree generation method often directly interrupts the parsing process, resulting in the inability to generate a complete abstract syntax tree. In contrast, the code automatic standardization method disclosed in the present invention switches to a fault-tolerant parsing mode when encountering a problematic lexical unit. In this mode, the problematic lexical unit will be regarded as a special "unknown node", "error node" or "empty node" and recorded in the abstract syntax tree. During the recording process, these nodes are marked accordingly according to the syntax parsing results to facilitate subsequent analysis of the abstract syntax tree. The parser will then continue to process the subsequent code and generate an incomplete but usable abstract syntax tree containing error prompt information. This approach makes it possible to clearly point out which specific parts of the code have suspected syntax errors when performing operations such as code analysis, thereby helping developers to locate and fix these problems more effectively.

[0099] Furthermore, when parsing a lexical unit to generate an abstract syntax tree, frequently obtaining the next lexical unit from the disassembly result of the lexical analyzer will result in certain performance overhead. In order to solve this problem, the automatic code standardization method disclosed in the present invention establishes a lexical word cache mechanism, referring to Figure 3 The flowchart of the lexical unit caching method shown in the figure, "Parsing each lexical unit based on the preset grammatical rules" includes the following steps:

[0100] S221: constructing a buffer, the buffer downloads the disassembled lexical units according to a preset number, and stores the downloaded lexical units in the disassembly order;

[0101] S222: Reading a lexical unit from the buffer, and after the lexical unit is read, deleting the lexical unit from the buffer;

[0102] S223: Performing grammatical analysis on the read lexical unit based on grammatical rules;

[0103] S224: When the number of lexical units stored in the cache is less than a preset number threshold, based on the preset number and the number of lexical units, continue downloading the disassembled lexical units until all the disassembled lexical units are downloaded.

[0104] In S221-S223, a buffer with a certain memory space is created, and the number of lexical units that can be stored, i.e., the preset number, is evaluated according to the allocated memory space. The buffer initially downloads a preset number of lexical units from the lexical analyzer, and stores the downloaded lexical units in a preset data structure (such as a queue) according to their original disassembly order to form a lexical unit cache.

[0105] The parser reads lexical units in the order in which they are stored in the cache. After each lexical unit is read, it is deleted from the cache to free up the cache's memory space. At the same time, the lexical units read are parsed based on grammatical rules.

[0106] In S224, the number of lexical units stored in the cache is monitored in real time. When the number of lexical units is less than the preset number threshold, it means that the lexical units in the cache are about to be exhausted, and part of the memory space has been released at this time, which can be used to store new lexical units. At this time, the difference between the preset number and the current number of lexical units is calculated. The difference represents the number of lexical units that can be newly stored. According to this difference, the corresponding number of lexical units are continuously downloaded from the lexical analyzer for storage. This process will continue until all the disassembled lexical units are downloaded. Through this method, the number of visits to the lexical analyzer is significantly reduced, the performance overhead is saved, and the generation efficiency of the abstract syntax tree is improved. In addition, since the preset number threshold is lower than the preset number, the frequency of downloading lexical units from the lexical analyzer is further reduced, thereby more effectively reducing the number of visits to the lexical analyzer.

[0107] Furthermore, in programming languages, there are often some recurring grammatical patterns, such as loop statement structures in Java. For these common grammatical patterns, their corresponding parsing states can be recorded in the cache. The next time a similar grammatical pattern is encountered, the parser can directly use the parsing state in the cache to build the corresponding node faster, thereby reducing repeated matching calculation processes and further improving efficiency.

[0108] In S3 and S4, the abstract syntax tree is traversed using a depth-first search or breadth-first search algorithm. For each traversed node, its type is obtained, and the node data of the node is collected and stored according to the type to form node data. For Java code, the types of nodes include but are not limited to variable declaration nodes, method declaration nodes, control structure nodes, unknown nodes, error nodes, and empty nodes. For example, for variable declaration nodes, the collected node data includes the data type of the variable (such as int, String, etc.), variable name (i.e. identifier), modifiers (such as public, private, etc., if any), and location information in the code (such as line number, column number range, etc., for subsequent positioning). For method declaration nodes, the collected node data includes return type, method name, parameter list (including parameter data type and parameter name, etc.), access modifiers, and location information in the class. For control structure nodes, the collected node data includes nesting depth (indicating the nesting level of the control structure node in the AST), style (such as indentation method, bracket usage, etc.), and location information. For unknown nodes, the collected node data includes node type (i.e., an "unknown" tag), location information, source code snippet (including the source code snippet that caused the unknown node, which is helpful for analysis and debugging), and context information (including information about the previous and next nodes, which helps understand the context of the unknown node). For error nodes, the collected node data includes error type (referring to the specific error type of the tag, such as syntax error, semantic error, etc.), error message (referring to a detailed error description, explaining the cause of the error), location information, related code (source code snippet that caused the error), and context information (including information about the relevant correct node or nearby nodes, which helps diagnose the problem). For empty nodes, the collected node data includes node type (i.e., an "empty" tag), location information, comment description (referring to the explanation that the node is empty), and context information (including information about the previous and next nodes, to maintain the integrity of the AST). For these special types of nodes, their node data mainly focuses on providing enough information to diagnose and handle problems in the parsing or compilation process, while maintaining the integrity and context of the AST. By including location information, source code snippets, and error messages, these nodes help developers better understand and fix problems in the code.

[0109] When determining the specific type of the problem node (i.e., unknown node, error node, and empty node), it can be quickly obtained only through the relevant tags of the abstract syntax tree. For normal nodes, it is necessary to determine according to the preset naming rules. For example, when traversing the abstract syntax tree and obtaining the node data of a normal node, check whether the node data contains a subnode representing the data type (for example, whether there is a node representing keywords such as int, double, etc.), and whether it contains a subnode representing the identifier as a variable name. At the same time, it is also necessary to confirm whether the grammatical structure of the node conforms to the rules of variable declaration in Java, that is, whether the overall connection relationship of the node conforms to the pattern of "[modifier] data type variable name [= initial value]", where the content in the square brackets is optional. If all these conditions are met, the node is determined to be a variable declaration node. That is, when traversing the node, first determine whether the node contains a preset tag. If it does, determine that the node is a problem node, and determine the specific type of the problem node according to the preset tag, that is, determine whether it is an unknown node, an error node, or an empty node. If the preset mark is not included, the node is determined to be a normal node, and a grammatical structure check is performed on the normal node based on a preset naming rule to further determine the specific type of the normal node.

[0110] In S5-S8, in order to quickly determine whether there is an abnormality in the node data and quickly find the optimization rules suitable for the abnormal node data, the automatic code standardization method disclosed in the present invention introduces the concept of a mapping table, which is used to shorten the query time and improve the overall speed of code standardization. Figure 4 The flowchart of the mapping table construction method shown in the figure includes the following steps:

[0111] S51: construct multiple mapping tables based on the number of standard rule sets included in the standard rule base;

[0112] S52: Determine a mapping relationship between each standard rule in the standard rule base and each optimization rule in the optimization rule base;

[0113] S53: converting each standard rule into a first key-value pair, and converting the optimization rule having a mapping relationship with the standard rule into a second key-value pair;

[0114] S54: Based on the mapping relationship, the first key-value pair and the second key-value pair are combined into a mapping entry and stored in a corresponding mapping table.

[0115] In S51, multiple specification rules are preset, and according to the different types of nodes applicable to each specification rule, these specification rules are divided into different specification rule sets, and these specification rule sets constitute a specification rule base. A mapping table is constructed for each specification rule set, which can be achieved by defining a mapping table structure. In Java, the mapping table can be implemented using the Map interface, such as using HashMap or TreeMap.

[0116] In S52, a corresponding optimization rule is configured for each standard rule in advance. The standard rule is responsible for detecting whether the node data is abnormal and providing specific abnormal information when an abnormality is found. The optimization rule is responsible for correcting these abnormalities. Therefore, each standard rule has an optimization rule with a mapping relationship therewith.

[0117] In S53, each standard rule is converted into a first key-value pair, where the key is the identifier of the standard rule and the value is the specific content of the standard rule. For example, for a variable naming rule set, each standard rule in the set is converted into a key-value pair, and the key-value pair is called a first key-value pair, where the key is the rule description (such as "the first letter of the variable name is lowercase, and the first letter of subsequent words is uppercase"), and the value is the corresponding specific regular expression (used to match and check whether the variable name is compliant) or a detailed logical judgment code snippet (if the regular expression is not easy to accurately describe the complex situation). For method-related rules (such as method overloading rules, method naming conventions, etc.), a standard rule set is also formed separately, and the standard rules in these standard rule sets are converted into key-value pairs.

[0118] At the same time, the optimization rule having a mapping relationship with the standard rule is converted into a second key-value pair, where the key is an identifier of the optimization rule and the value is the specific content of the optimization rule.

[0119] In S54, the first key-value pair and the second key-value pair having a mapping relationship are combined into mapping entries, and the mapping entries are stored in corresponding mapping tables. In this way, multiple mapping tables store the standard rules in the preset standard rule library and the optimization rules in the preset optimization rule library, and establish a mapping relationship between the standard rules and the optimization rules, that is, the mapping table can reflect the mapping relationship between the stored standard rules and the optimization rules in the form of mapping entries, which helps to quickly complete the abnormal detection and optimization of node data.

[0120] Furthermore, based on the type of the node, an available standard rule set is determined from multiple standard rule sets, and based on the available standard rule set, an available mapping table is determined from multiple preset mapping tables, and the standard rules stored in the available mapping table are matched with the node data; if the match with any standard rule stored in the available mapping table is successful, it is determined that the node data has an abnormality; if the match with all standard rules stored in the available mapping table fails, it is determined that the node data does not have an abnormality. In this way, the type of the node can be fully utilized to find the available standard rule set corresponding to the type, and then the mapping table of the available standard rule set is determined as the available mapping table, and then the matching with the node data can be completed by using only the standard rules stored in the available mapping table. This method does not need to match the node data with all standard rules, and can reduce a certain amount of calculation.

[0121] When it is determined that the node data is abnormal, based on the specification rules that successfully match the node data, the available optimization rules are determined from the available mapping table, and the abstract syntax tree is optimized based on the available optimization rules and the abnormal node data. This method can quickly find the required available optimization rules through the mapping entries of the available mapping table, and then quickly locate the abnormal node position of the abstract syntax tree through the abnormal node data, and use the available optimization rules to optimize the abnormal node to make it meet the specifications.

[0122] It can be seen that the above method can effectively reduce the number of matches with various rules, reduce the workload of calculations, shorten the time for code standardization, and improve the efficiency of code detection and optimization.

[0123] In S9, the optimized abstract syntax tree is converted back into new code, and the generated code is compliant with the specification. For example, when a variable declaration node is detected, its variable name is checked to see if it complies with the naming convention. If it is found that it does not comply with this specification rule, relevant information such as the location of the illegal code and the type of violation is recorded, and the optimization rule corresponding to the specification rule is quickly queried. A new variable name is generated using the optimization rule, and the node position that needs to be modified is recorded. The AST is modified based on the changed node position and the new variable name.

[0124] In the traditional method, the optimized code snippet is directly generated by using the available optimization rules and abnormal node data, and then the optimized code snippet is used to replace the abnormal part of the original code to be standardized, or the original code to be standardized is directly modified by using the available optimization rules and abnormal node data. However, it is very important to maintain the consistency and traceability of the code structure, especially in complex projects, where there are often a large number of mutual references and dependencies between codes. The direct replacement and direct modification of the code may destroy the structural relevance of the original code, resulting in the destruction of the logical relationship between the code elements. The code automatic standardization method disclosed in the present invention regenerates the code by operating the node attributes and structure of the abstract syntax tree, which can ensure that the optimized code still maintains the original overall structural framework and the logical relationship between each code element is preserved. This method not only facilitates subsequent code review and version control operations, but also helps developers understand the differences and logical evolution before and after optimization when tracing the code change process, ensuring the controllability and transparency of the optimization operation. By operating the abstract syntax tree, developers can also achieve precise control and batch optimization of the code. For example, when modifying a variable declaration, only the node attributes that do not conform to the naming convention (such as modifying the variable name) need to be operated without affecting other code logic that is not related to it. At the same time, for codes with a large number of similar violations (such as multiple variable names that do not comply with naming conventions), we can operate on the corresponding nodes in batches by traversing the AST and modify them uniformly according to the optimization strategy, so as to achieve code optimization more efficiently and accurately. This method effectively avoids the problems of erroneous modification or omission that may be caused by directly replacing the code, and ensures the accuracy and consistency of the optimization operation.

[0125] Furthermore, the grammatical analysis results, anomaly detection and optimization results of each lexical unit are recorded in the log, and the compliant code and log are output to the user in the form of display in the development environment, saving as a new file or integrating with the version control system. The log is visualized to facilitate users to improve grammatical rules, standard rules and optimization rules.

[0126] Reference Figure 5 The present disclosure provides a code automatic standardization system, comprising:

[0127] The code acquisition module 101 is used to acquire the code to be standardized;

[0128] Syntax parsing module 102, used to perform syntax parsing on the standard code and generate an abstract syntax tree;

[0129] A type acquisition module 103 is used to traverse the abstract syntax tree and acquire the type of each node;

[0130] A data collection module 104, used to collect node data according to type;

[0131] A mapping table construction module 105 is used to construct a plurality of mapping tables, wherein the plurality of mapping tables store the standard rules in the preset standard rule base and the optimization rules in the preset optimization rule base, and establish a mapping relationship between the standard rules and the optimization rules;

[0132] The abnormality judgment module 106 is used to judge whether the node data is abnormal based on the type and the preset standard rule library; if so, the syntax tree optimization module 107 is executed;

[0133] A syntax tree optimization module 107, used to optimize the abstract syntax tree based on a preset optimization rule base and abnormal node data;

[0134] The code generation module 108 is used to generate code that complies with the specification based on the optimized abstract syntax tree.

[0135] The various variations and specific examples of the automatic code standardization method provided above are also applicable to the automatic code standardization system provided in the present disclosure. Through the above detailed description of the automatic code standardization method, those skilled in the art can clearly know the implementation method of the automatic code standardization system. For the sake of brevity of the specification, it will not be described in detail here.

[0136] The computer device according to the embodiment of the present disclosure includes a memory and a processor. The memory is used to store non-temporary computer-readable instructions. Specifically, the memory may include one or more computer program products, and the computer program product may include various forms of computer-readable storage media, such as volatile memory and / or non-volatile memory. The volatile memory may include, for example, a random access memory (RAM) and / or a cache memory (cache), etc. The non-volatile memory may include, for example, a read-only memory (ROM), a hard disk, a flash memory, etc.

[0137] The processor may be a central processing unit (CPU) or other forms of processing units with data processing capabilities and / or instruction execution capabilities, and may control other components in the computer device to perform desired functions. In one embodiment of the present disclosure, the processor is used to run the computer-readable instructions stored in the memory, so that the computer device performs all or part of the steps of the automatic code specification method of each embodiment of the present disclosure.

[0138] Those skilled in the art should be able to understand that in order to solve the technical problem of how to obtain a good user experience, the present embodiment may also include well-known structures such as a communication bus and an interface, and these well-known structures should also be included in the protection scope of the present disclosure.

[0139] like Figure 6A schematic diagram of the structure of a computer device provided in an embodiment of the present disclosure is shown, which is a schematic diagram of the structure of a computer device suitable for implementing the embodiment of the present disclosure. Figure 6 The computer device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present disclosure.

[0140] like Figure 6 As shown, the computer device may include a processor (e.g., a central processing unit, a graphics processing unit, etc.), which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) or a program loaded from a storage device into a random access memory (RAM). In the RAM, various programs and data required for the operation of the computer device are also stored. The processor, ROM, and RAM are connected to each other via a bus. An input / output (I / O) interface is also connected to the bus.

[0141] Typically, the following devices can be connected to the I / O interface: input devices such as sensors or visual information acquisition devices; output devices such as display screens; storage devices such as tapes, hard disks, etc.; and communication devices. The communication device can allow the computer device to communicate with other devices (such as edge computing devices) wirelessly or by wire to exchange data. Figure 6 A computer device having various devices is shown, but it should be understood that it is not required to implement or possess all of the devices shown. More or fewer devices may be implemented or possessed instead.

[0142] In particular, according to an embodiment of the present disclosure, the process described above with reference to the flowchart can be implemented as a computer software program. For example, an embodiment of the present disclosure includes a computer program product, which includes a computer program carried on a non-transitory computer-readable medium, and the computer program contains program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from a network through a communication device, or installed from a storage device, or installed from a ROM. When the computer program is executed by a processor, all or part of the steps of the automatic code specification method of the embodiment of the present disclosure are executed.

[0143] For detailed description of this embodiment, reference may be made to the corresponding descriptions in the aforementioned embodiments, which will not be repeated here.

[0144] According to the computer-readable storage medium of the embodiment of the present disclosure, non-transitory computer-readable instructions are stored thereon. When the non-transitory computer-readable instructions are executed by a processor, all or part of the steps of the automatic code specification method of each embodiment of the present disclosure are executed.

[0145] The above-mentioned computer-readable storage media include, but are not limited to: optical storage media (e.g., CD-ROM and DVD), magneto-optical storage media (e.g., MO), magnetic storage media (e.g., magnetic tape or mobile hard disk), media with built-in rewritable non-volatile memory (e.g., memory card) and media with built-in ROM (e.g., ROM box).

[0146] For detailed description of this embodiment, reference may be made to the corresponding descriptions in the aforementioned embodiments, which will not be repeated here.

[0147] The basic principles of the present disclosure are described above in conjunction with specific embodiments. However, it should be noted that the advantages, strengths, effects, etc. mentioned in the present disclosure are only examples and not limitations, and it cannot be considered that these advantages, strengths, effects, etc. are required by each embodiment of the present disclosure. In addition, the specific details disclosed above are only for the purpose of illustration and ease of understanding, and are not limitations. The above details do not limit the present disclosure to the necessity of adopting the above specific details to be implemented.

[0148] In the present disclosure, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply that there is any such actual relationship or order between these entities or operations. The block diagrams of the devices, devices, equipment, and systems involved in the present disclosure are only illustrative examples and are not intended to require or imply that they must be connected, arranged, and configured in the manner shown in the block diagram. As will be appreciated by those skilled in the art, these devices, devices, equipment, and systems can be connected, arranged, and configured in any manner. Words such as "including", "comprising", "having", etc. are open words, referring to "including but not limited to", and can be used interchangeably with them. The words "or" and "and" used here refer to the words "and / or" and can be used interchangeably with them, unless the context clearly indicates otherwise. The words "such as" used here refer to the phrase "such as but not limited to", and can be used interchangeably with them.

[0149] Additionally, as used herein, "or" used in a list of items beginning with "at least one" indicates a separate list, so that, for example, a list of "at least one of A, B, or C" means A or B or C, or AB or AC or BC, or ABC (i.e., A and B and C). Furthermore, the word "exemplary" does not mean that the example described is preferred or better than other examples.

[0150] It should also be noted that in the system and method of the present disclosure, each component or each step can be decomposed and / or recombined. Such decomposition and / or recombination should be regarded as equivalent solutions of the present disclosure.

[0151] Various changes, substitutions, and modifications of the techniques described herein may be made without departing from the teachings defined by the appended claims. Furthermore, the scope of the claims of the present disclosure is not limited to the specific aspects of the processes, machines, manufactures, compositions of events, means, methods, and actions described above. Currently existing or later to be developed processes, machines, manufactures, compositions of events, means, methods, or actions that perform substantially the same functions or achieve substantially the same results as the corresponding aspects described herein may be utilized. Thus, the appended claims include such processes, machines, manufactures, compositions of events, means, methods, or actions within their scope.

[0152] The above description of the disclosed aspects is provided to enable any person skilled in the art to make or use the present disclosure. Various modifications to these aspects will be readily apparent to those skilled in the art, and the general principles defined herein may be applied to other aspects without departing from the scope of the present disclosure. Therefore, the present disclosure is not intended to be limited to the aspects shown herein, but rather to the widest scope consistent with the principles and novel features disclosed herein.

[0153] The above description has been given for the purpose of illustration and description. In addition, this description is not intended to limit the embodiments of the present disclosure to the forms disclosed herein. Although multiple example aspects and embodiments have been discussed above, those skilled in the art will recognize certain variations, modifications, changes, additions and sub-combinations thereof.

Claims

1. A method for automatic code standardization, characterized in that: include: Get the code to be standardized; Performing syntax analysis on the code to be standardized to generate an abstract syntax tree; The step of parsing the code to be standardized to generate an abstract syntax tree includes: Decomposing the code to be standardized into multiple lexical units; Based on the preset grammatical rules, grammar analysis is performed on each lexical unit; When it is determined that the lexical unit conforms to the grammatical rule, creating a normal node based on the lexical unit; When the lexical unit cannot be parsed, creating an unknown node based on the lexical unit; When it is determined that the lexical unit does not conform to the grammatical rule, creating an error node based on the lexical unit; When missing content between tokens is detected, an empty node is created based on the position of the missing content; Generate an abstract syntax tree based on all created nodes; The step of parsing the code to be standardized to generate an abstract syntax tree also includes: Mark the nodes accordingly according to the grammar parsing results; Traverse the abstract syntax tree to obtain the type of each node; wherein the type includes a variable declaration node, a method declaration node, a control structure node, an unknown node, an error node, and an empty node; The traversing of the abstract syntax tree to obtain the type of each node includes: When traversing a node, determine whether the node contains a preset tag; If it is included, the node is determined to be a problem node, and the specific type of the problem node is determined according to the preset mark; If the preset mark is not included, the node is determined to be a normal node, and the grammatical structure of the normal node is checked based on the preset naming rule to determine the specific type of the normal node; Collect node data according to the type; Wherein, collecting node data according to the type includes: For unknown nodes, the collected node data includes the corresponding labeled node type, location information, source code snippet, and context information; For error nodes, the collected node data includes the marked error type, error message, location information, related codes, and context information; For empty nodes, the collected node data includes the corresponding marked node type, location information, annotation description and context information; Constructing a plurality of mapping tables, wherein the plurality of mapping tables store the standard rules in the preset standard rule base and the optimization rules in the preset optimization rule base, and establishing a mapping relationship between the standard rules and the optimization rules; Based on the type, the specification rule base and the plurality of mapping tables, determining whether the node data is abnormal; If yes, optimizing the abstract syntax tree based on the multiple mapping tables and the abnormal node data; Generates code that complies with the specification based on the optimized abstract syntax tree.

2. The automatic code standardization method according to claim 1, characterized in that: The grammatical analysis of each lexical unit based on the preset grammatical rules includes: Constructing a buffer, wherein the buffer downloads the disassembled lexical units according to a preset number and stores the downloaded lexical units in the order of disassembly; Reading a lexical unit from the buffer, and deleting the lexical unit from the buffer after the lexical unit is read; Performing grammatical analysis on the read lexical unit based on the grammatical rules; When the number of lexical units stored in the buffer is less than a preset number threshold, based on the preset number and the number of lexical units, the disassembled lexical units continue to be downloaded until all the disassembled lexical units are downloaded.

3. The automatic code standardization method according to claim 1, characterized in that: The constructing of multiple mapping tables includes: constructing a plurality of mapping tables based on the number of standard rule sets contained in the standard rule base; Determine a mapping relationship between each standard rule in the standard rule base and each optimization rule in the optimization rule base; Convert each specification rule into a first key-value pair, and convert the optimization rule having a mapping relationship with the specification rule into a second key-value pair; Based on the mapping relationship, the first key-value pair and the second key-value pair are combined into a mapping entry and stored in a corresponding mapping table.

4. The automatic code standardization method according to claim 3, characterized in that: The determining whether the node data is abnormal based on the type, the specification rule base and the plurality of mapping tables includes: Based on the type of the node, determining an available specification rule set from a plurality of specification rule sets; Determining an available mapping table from a plurality of mapping tables based on the available specification rule set; Matching the specification rules stored in the available mapping table with the node data; If a match is successful with any of the specification rules stored in the available mapping table, it is determined that the node data is abnormal; If all the specification rules stored in the available mapping table fail to match, it is determined that there is no abnormality in the node data.

5. The automatic code standardization method according to claim 4, characterized in that: The optimizing the abstract syntax tree based on the multiple mapping tables and the abnormal node data includes: When it is determined that the node data is abnormal, based on the specification rule that successfully matches the node data, determining an available optimization rule from the available mapping table; The abstract syntax tree is optimized based on the available optimization rules and the abnormal node data.

6. A computer device, characterized in that: The computer device comprises: at least one processor; and, a memory communicatively connected to the at least one processor; wherein, The memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the code automatic standardization method described in any one of claims 1-5.

7. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer instructions, which are used to enable a computer to execute the automatic code standardization method described in any one of claims 1-5.

8. A computer program product comprising computer instructions, characterized in that: When the computer instruction is executed by a processor, the steps of the method according to any one of claims 1 to 5 are implemented.

Citation Information

Patent Citations

  • Analyzing method of abstract syntax tree and computer program product

    CN114443041A

  • Method for normalizing code and electronic equipment

    CN116415552A