Code repairing method and device based on abstract syntax tree and large model
By combining abstract syntax trees and large models, we have achieved automated and precise code repair, solving the problems of low efficiency and weak generalization in existing technologies, improving the efficiency and accuracy of code repair, and adapting to the ever-changing needs of complex projects.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-12-04
- Publication Date
- 2026-04-07
AI Technical Summary
Existing technologies are inefficient and have weak generalization ability in code repair, making it difficult to handle deep semantic problems. Furthermore, the repair process relies on preset rules, resulting in insufficient accuracy and an inability to cope with unknown errors in complex projects.
By using methods based on abstract syntax trees and large models, code syntax checking, compilation verification, and runtime verification are performed. Combined with preset rules and the diagnosis and repair of large language models, automated and accurate code repair is achieved.
It improves the efficiency and accuracy of code repair, can handle deep semantic errors and unknown runtime errors, enhances the tool's generalization ability and practicality, and improves the development efficiency and code quality of software projects.
Smart Images

Figure CN121807356A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of computer programming, and in particular to a code repair method and device based on an abstract syntax tree and a large model. BACKGROUND
[0002] In the process of modern software development, code writing, compiling and running constitute the basic and key workflow. With the continuous improvement of the scale and complexity of software systems, the amount of code grows exponentially, and the errors exposed in the compiling or running stage are increasingly frequent. Traditional development mode relies on developers to manually respond to error information fed back by the compiler or runtime environment, and to achieve functional correctness and system stability by manually locating, analyzing and modifying the problem code. In recent years, some static analysis tools and intelligent prompt plug-ins have been gradually introduced into the development environment to improve the efficiency and limitations of manual code repair.
[0003] However, the existing technology, whether using manual positioning repair or using basic intelligent tools for prompting, analysis and repair, generally has the problems of low efficiency and weak generalization ability. On the one hand, most tools can only identify surface syntax errors and are difficult to handle deep semantic problems involving type systems, scope logic or API misuse. On the other hand, the repair process highly depends on preset rules or templates, resulting in insufficient repair accuracy or introducing new errors. In addition, for error types not covered, existing methods often directly give up because they cannot be repaired, thereby limiting the practicality in real complex projects. SUMMARY
[0004] Therefore, the present application provides a code repair method and device based on an abstract syntax tree and a large model, which mainly aims to solve the technical problem of low practicality caused by low code repair efficiency and weak generalization ability in the prior art.
[0005] According to a first aspect of the present application, a code repair method based on an abstract syntax tree and a large model is provided, comprising: performing syntax checking on the to-be-repaired code and repairing syntax problems existing in the to-be-repaired code to generate preliminary repair code, wherein the syntax problems include syntax errors and / or programming language type mismatches; performing compilation verification on the preliminary repair code, when detecting that the preliminary repair code has a compilation error, obtaining compilation error information, and matching the compilation error information based on a preset error rule knowledge base to obtain a corresponding repair rule; repairing the preliminary repair code using the repair rule until the preliminary repair code passes the compilation verification to obtain target repair code, and performing running verification on the target repair code, wherein the repair rule is used to modify an abstract syntax tree of the preliminary repair code. When the target repair code running error is detected, running error information is acquired, and a preset large language model is called based on the running error information for diagnosis and repair until the target repair code passes running verification.
[0006] Optionally, the syntax of the to-be-repaired code is checked, and a syntax problem existing in the to-be-repaired code is repaired to generate preliminary repair code, including: parsing the to-be-repaired code to generate a corresponding initial abstract syntax tree; analyzing the initial abstract syntax tree to identify a problem node existing the syntax problem; modifying the problem node according to a preset syntax correction rule set to correct the initial abstract syntax tree; restoring the corrected initial abstract syntax tree to code to obtain preliminary repair code.
[0007] Optionally, the error rule knowledge base is configured to be extended by writing repair rule code, wherein the repair rule includes an error matching condition and an abstract syntax tree modification logic, the error matching condition is associated with the compilation error information, and the abstract syntax tree modification logic is used to define a modification manner of a target node in the abstract syntax tree.
[0008] Optionally, the preliminary repair code is repaired by using the repair rule, including: parsing the preliminary repair code to generate a corresponding abstract syntax tree, wherein the abstract syntax tree includes a plurality of nodes; locating an error node causing a compilation error in the abstract syntax tree and analyzing context information of the error node, wherein the context information includes ancestor nodes and descendant nodes of the error node; judging whether the repair rule is applicable to an error scenario of the preliminary repair code based on the context information; when the repair rule is applicable to the error scenario of the preliminary repair code, performing a modification operation on the abstract syntax tree according to the repair rule, and restoring the modified abstract syntax tree to executable code.
[0009] Optionally, the modification operation includes at least one of the following: modifying an attribute, a value, or a type of the error node; changing the ancestor node or the descendant node; adding or deleting a sub-tree in the abstract syntax tree.
[0010] Optionally, the calling a preset large language model based on the running error information for diagnosis and repair until the target repair code passes the running verification comprises: assembling an initial prompt word based on the running error information, an error code segment of the target repair code, and code context information of the error code segment; sending the initial prompt word to a preset large language model, and receiving repair content information returned by the large language model, wherein the repair content information comprises repair suggestion code and repair description; modifying the target repair code according to the repair content information, generating a to-be-verified code, and re-performing running verification on the to-be-verified code until the to-be-verified code passes the running verification.
[0011] Optionally, the re-performing running verification on the to-be-verified code until the to-be-verified code passes the running verification comprises: when the to-be-verified code does not pass the running verification, obtaining new running error information; assembling a new prompt word based on the initial prompt word, the new running error information, and a current state of the to-be-verified code, sending the new prompt word to the large language model, and receiving new repair content information returned by the large language model; modifying the to-be-verified code according to the new repair content information, and repeatedly performing running verification by using a multi-round iteration strategy until the to-be-verified code passes the running verification or a preset iteration termination condition is reached.
[0012] According to a second aspect of the present application, a code repair device based on an abstract syntax tree and a large model is provided, comprising: a syntax checking module configured to perform syntax checking on to-be-repaired code and repair syntax problems existing in the to-be-repaired code to generate preliminary repair code, wherein the syntax problems comprise syntax errors and / or programming language type mismatches; a compilation verification module configured to perform compilation verification on the preliminary repair code, obtain compilation error information when detecting that the preliminary repair code has compilation errors, and match the compilation error information based on a preset error rule knowledge base to obtain corresponding repair rules; a syntax tree modification module configured to repair the preliminary repair code by using the repair rules until the preliminary repair code passes the compilation verification to obtain target repair code, and perform running verification on the target repair code, wherein the repair rules are used to modify an abstract syntax tree of the preliminary repair code; a large model runtime repair module configured to obtain running error information when detecting that the target repair code has running errors, and call a preset large language model based on the running error information for diagnosis and repair until the target repair code passes the running verification.
[0013] According to a third aspect of the present application, a storage medium is provided, which stores a computer program, and the program is executed by a processor to implement the above-mentioned code repair method based on abstract syntax tree and large model.
[0014] According to a fourth aspect of the present application, a computer device is provided, which comprises a memory, a processor, and a computer program stored in the memory and executable on the processor, and the processor executes the program to implement the above-mentioned code repair method based on abstract syntax tree and large model.
[0015] The present application provides a code repair method and device based on abstract syntax tree and large model. Firstly, the method can automatically and efficiently handle the superficial syntax errors and type mismatch problems through pre-positioned syntax checking and repair, laying a foundation for subsequent deep repair and improving the initial code quality from the source. Secondly, for deeper semantic errors in the compilation stage, the repair method based on abstract syntax tree is adopted to match the compilation error information with the preset rule knowledge base and directly modify at the abstract syntax tree level, realizing accurate and lossless repair of the code structure, overcoming the insufficient repair accuracy of the prior art, and effectively handling complex semantic problems involving type system, scope, etc., improving the accuracy and reliability of repair. Finally, for errors occurring in the running stage, a large language model is called to diagnose and repair, ensuring that the repair process will not be interrupted due to unknown error types, enhancing the generalization ability and practicality of the tool, and thus being able to cope with the changing needs in complex projects. The above-mentioned method improves the efficiency of code error repair, and through the combination of abstract syntax tree analysis and large language model repair, the processing capability and repair accuracy for deep semantic errors and unknown running errors are enhanced, fundamentally improving the quality, reliability of the code and the development efficiency of the software project.
[0016] The above description is only a summary of the technical solutions of the present application. In order to more clearly understand the technical means of the present application, the specific embodiments of the present application can be implemented in accordance with the content of the description, and in order to make the above and other purposes, features and advantages of the present application more obvious and easy to understand, the following specific embodiments of the present application are described. BRIEF DESCRIPTION OF DRAWINGS
[0017] The drawings described herein are used to provide further understanding of the present application, and form a part of the present application. The schematic embodiments of the present application and their descriptions are used to explain the present application, and do not constitute an improper limitation on the present application. In the drawings: Figure 1 A flowchart of a code repair method based on abstract syntax tree and large model provided by an embodiment of the present application is shown; Figure 2 A flowchart of another code repair method based on abstract syntax tree and large model provided by an embodiment of the present application is shown; Figure 3 Fig. 2 shows a schematic diagram of the principle of code repair based on an abstract syntax tree structure in another code repair method based on an abstract syntax tree and a large model provided by an embodiment of the present application; Figure 4 Fig. 3 shows a schematic diagram of the specific content of code repair operation based on an abstract syntax tree structure in another code repair method based on an abstract syntax tree and a large model provided by an embodiment of the present application; Figure 5 Fig. 4 shows a schematic diagram of the structure of a code repair device based on an abstract syntax tree and a large model provided by an embodiment of the present application; Figure 6 Fig. 5 shows a schematic diagram of the device structure of a computer device provided by an embodiment of the present application. DETAILED DESCRIPTION
[0018] Exemplary embodiments of the present application will be described in greater detail below with reference to the accompanying drawings. Although exemplary embodiments of the present application are shown in the drawings, it should be understood that the present application can be implemented in various forms and should not be limited by the embodiments set forth herein. Rather, these embodiments are provided so that the present application can be more thoroughly understood and so that the scope of the present application can be completely conveyed to those skilled in the art.
[0019] An embodiment of the present application provides a code repair method based on an abstract syntax tree and a large model, as shown in Fig. 1, the method comprises the following steps: Figure 1 101, performing syntax checking on the code to be repaired, and repairing the syntax problems existing in the code to be repaired to generate preliminary repair code, wherein the syntax problems include syntax errors and / or programming language type mismatches.
[0020] The syntax checking is a process of parsing and verifying the source code text by using a syntax rule set of a specific programming language, and the core content lies in identifying whether the code structure conforms to the language intended specification, such as whether the parentheses are matched, whether the statement end symbol is correct, whether the keyword is used appropriately, etc.; the syntax error is a structural defect that causes the code to be unable to be correctly parsed by a standard parser; the programming language type mismatch belongs to a syntax problem at the semantic level, which means that in the operations such as assignment, parameter passing or operation, the actual data type of the program entity is inconsistent with the data type expected by the context, for example, assigning a string type data to an integer variable; the preliminary repair code refers to the code version generated after automatic correction, which conforms to the specification at the syntax level and is ready for the next compilation link.
[0021] Specifically, this step begins in the pre-compilation repair phase. The system first receives the input code to be repaired, and then initiates an automated syntax checking process. This process is usually implemented by calling the official or standard parser of the corresponding programming language. The system scans the code text, accurately locating syntax errors and type mismatches that violate language specifications. After identifying the problematic code snippets, the system will automatically correct the problems by calling a preset set of syntax correction rules. Correction operations may include completing missing symbols, correcting erroneous keywords, adjusting statement structure, or performing type conversions. Finally, the system outputs preliminary repair code that has eliminated the identified syntax problems, laying the foundation for subsequent compilation and in-depth analysis.
[0022] In this embodiment, code error repair is pre-emptively and automatically implemented. By systematically checking and repairing basic syntax and type issues before the compilation stage, a large number of shallow errors can be eliminated, significantly reducing the number of failures in the subsequent compilation process. This improves the efficiency of the overall repair process and not only creates a clean code environment for subsequent more complex compilation and runtime repairs, but also enhances the development experience and the initial quality of the code.
[0023] 102. Perform compilation verification on the preliminary repair code. When a compilation error is detected in the preliminary repair code, obtain the compilation error information and match the compilation error information based on the preset error rule knowledge base to obtain the corresponding repair rule.
[0024] Among them, compilation verification refers to the process of taking the initially repaired code as input and calling the compiler of the programming language to compile it. The purpose is to verify whether the code conforms to all the static semantic rules of the language, in addition to being syntactically correct. Compilation error information is the standardized output generated by the compiler during the compilation process when it detects that the code violates the language specification (such as undeclared variables, mismatched function parameters, type incompatibility, etc.), which includes the error type, location and description. The error rule knowledge base is a pre-built and extensible set of rules, in which each repair rule associates a specific compilation error pattern or class with a modification program for the abstract syntax tree (AST) designed to fix that error.
[0025] Specifically, this step is the post-compilation repair operation. When the system performs compilation verification on the initial repair code and confirms failure, it will automatically capture the compilation error information output by the compiler. Then, the system uses the error information as a query key to search and match in the preset error rule knowledge base to find the corresponding repair rule. The matching process aims to locate a pre-programmed repair logic in the knowledge base that is specifically designed to solve this type of error. Moreover, the repair logic is not a simple text replacement, but is designed as a sequence of computer instructions that can manipulate the code abstract syntax tree, providing a direct basis for the next step of precise code modification.
[0026] In the embodiments of the application, intelligent and rule-based automatic repair of compilation errors is implemented. Through establishing an accurate mapping between compilation error information and modification rules of an abstract syntax tree, the system can directly accurately intervene in deep semantic errors (such as type mismatch and scope error). This step is based on a repair mechanism of a rule knowledge base, improves the automation degree and reliability of the repair process, avoids the low efficiency and subjective differences of manual repair, and at the same time provides a reusable and extensible systematic solution for processing specific types of compilation errors that repeatedly occur in complex projects.
[0027] 103. Repairing the preliminary repair code by using the repair rules until the preliminary repair code passes the compilation verification to obtain target repair code, and performing running verification on the target repair code, wherein the repair rules are used to modify an abstract syntax tree of the preliminary repair code.
[0028] The abstract syntax tree (AST) is a tree representation of the abstract syntax structure of the source code, each node represents a construction (such as an operator, an identifier, or a literal) in the source code, thereby accurately expressing the logical level and relationship of the code. The modification of the abstract syntax tree means that the specific nodes in the abstract syntax tree are added, deleted, replaced, or attribute adjusted without destroying the overall structure of the code, so as to realize accurate transformation of the code. The target repair code is a code version without compilation errors obtained by modifying the abstract syntax tree and successfully passing the compilation verification. The running verification means executing the target repair code in a runtime environment to verify whether the function correctness and behavior meet the expectations.
[0029] Specifically, this step is to perform the operation of post-compilation repair. After the system obtains the repair rules matched with the compilation errors, instead of directly modifying the code text, the system first parses the preliminary repair code into its corresponding abstract syntax tree, and then according to the logic defined by the repair rules, accurately locates the node causing the error in the abstract syntax tree, and performs a series of structured modifications. The modification process is iterative. After the abstract syntax tree is modified by applying the rules and restored to code, the system will re-initiate the compilation verification. If the compilation still fails, the cycle of parsing, matching, modifying, and verifying will be repeated until the code is successfully compiled. At this time, the stable version generated is the target repair code. Finally, the system will start the running verification process to execute the target repair code.
[0030] In the embodiments of the present application, the accurate, structured and automated correction of code errors is realized through the repair mechanism based on the abstract syntax tree. Since the abstract syntax tree completely retains the semantic structure of the code, modifying the abstract syntax tree can fundamentally avoid the format damage or secondary syntax errors that may be introduced by traditional text replacement, thereby improving the accuracy of repair. The structured modification provided in this step ensures that the code intent can be maintained during the repair process, significantly improving the quality and reliability of the repair operation. At the same time, the compilation is used as one of the termination conditions of iterative repair, forming a closed-loop system that can be verified, ensuring that the final output target repair code has a good executable basis.
[0031] 104. When a running error of the target repair code is detected, running error information is obtained, and a preset large language model is called based on the running error information for diagnosis and repair until the target repair code passes the running verification.
[0032] Wherein, the running error refers to a failure that occurs in the actual execution process of the target repair code after compilation, which is usually caused by logical defects, resource conflicts, abnormal inputs or runtime environment inconsistencies; the running error information refers to the context data captured by the system when the code execution fails, which usually includes exception types, stack traces, error messages and variable states; the preset large language model refers to an artificial intelligence model with code understanding and generation capabilities, which is pre-trained on a large amount of code and text data and is integrated as an intelligent engine for diagnosis and repair in the present scheme.
[0033] Specifically, this step is the core process of large model repair. When the system detects that the target repair code fails to execute in the running verification phase, it automatically captures detailed running error information. Then the system assembles the error information, related code segments and their execution context into a structured prompt word, and calls the preset large language model. Based on its powerful code reasoning ability, the large language model analyzes and diagnoses the problem and generates corresponding repair suggestions or repaired code. The system receives the suggestions and applies them to the current code to generate a new version to be verified, and then restarts the running verification process. This cycle of diagnosis, repair and verification is automatically iterated until the code successfully passes the running verification, marking the final completion of the entire repair process.
[0034] In the embodiments of the present application, an intelligent repair backup mechanism with strong generalization ability and deep reasoning ability is introduced for the system. By calling a large language model, the system can break through the limitations of traditional rule libraries and deal with complex runtime logic errors and environment dependency problems that cannot be captured in the compilation phase. The interactive repair based on the large model significantly improves the processing capability of the tool for unknown error types and complex error scenarios, greatly enhances the robustness and practicality of the entire repair system, extends the boundary of automatic repair from the static syntax and compilation level to the dynamic running behavior level, realizes the full life cycle coverage of code errors from generation to running, and finally significantly improves the overall quality and reliability of the software.
[0035] The present application provides a code repair method based on abstract syntax tree and large model. First, the initial code quality is improved from the source by automatically and efficiently processing the surface syntax errors and type mismatch problems through pre-positioned syntax checking and repair for the code, laying a foundation for subsequent deep repair. Second, for the deeper semantic errors in the compilation phase, an abstract syntax tree-based repair method is used to match the compilation error information with the preset rule knowledge base and directly modify it at the abstract syntax tree level, realizing accurate and lossless repair of the code structure, overcoming the lack of repair precision in the prior art, and effectively handling complex semantic problems involving type systems, scopes, etc., improving the accuracy and reliability of the repair. Finally, for errors occurring in the running phase, a large language model is called to diagnose and repair, ensuring that the repair process will not be interrupted due to unknown error types, enhancing the generalization ability and practicality of the tool, and thus being able to cope with the changing needs in complex projects. The above method improves the efficiency of code error repair, enhances the processing capability and repair precision for deep semantic errors and unknown running errors through the combination of abstract syntax tree analysis and large language model repair, and fundamentally improves the quality, reliability of the code and the development efficiency of the software project.
[0036] The embodiments of the present application provide another code repair method based on abstract syntax tree and large model, as shown in Figure 2 The method comprises the following steps: 201, syntax checking of the code to be repaired, and repairing the syntax problems to generate a preliminary repair code.
[0037] Specifically, the code to be repaired is parsed to generate a corresponding initial abstract syntax tree; the initial abstract syntax tree is analyzed to identify problem nodes with syntax problems; the problem nodes are modified according to a preset syntax correction rule set to modify the initial abstract syntax tree; and the modified initial abstract syntax tree is restored to code to obtain a preliminary repair code.
[0038] In the embodiments of the present application, the operation process of the pre-compilation repair sub-module based on the abstract syntax tree is specifically described. The system first uses a standard parser of a preset programming language to perform lexical analysis and syntax analysis on the code to be repaired, constructs an initial abstract syntax tree that fully reflects the hierarchical structure of the code, and then traverses and analyzes the initial abstract syntax tree. According to the syntax specification of the programming language, the system accurately identifies the problem nodes with syntax errors or type mismatches. Then the system calls a preset syntax correction rule set, which contains repair logic for various common syntax problems. The system matches and applies the corresponding correction rules according to the type of the identified problem nodes, modifies the type, attributes, values or subtree structure of the nodes to correct the initial abstract syntax tree, and finally restores the corrected abstract syntax tree that has eliminated the identified syntax problems to the preliminary repair code that meets the text format requirements through a code generator, thereby completing the automatic repair before compilation and laying a foundation for subsequent compilation verification.
[0039] 202. Compiling and verifying the preliminary repair code. When there is a compilation error, obtain the compilation error information.
[0040] In the embodiments of the present application, the system takes the preliminary repair code obtained through pre-compilation repair as input and automatically calls the compiler corresponding to the programming language to perform the compilation operation. The purpose of the compilation verification process is to verify whether the code after eliminating the basic syntax problems completely complies with all static semantic rules of the programming language. When the compiler detects any violation of language specifications during the compilation process, such as an undeclared identifier, type mismatch, function signature error, etc., the system will immediately capture the structured compilation error information output by the compiler. The compilation error information usually includes error type description, specific location positioning of the error in the code and related context prompts. Obtaining the above compilation error information provides necessary input and basis for subsequent precise matching in the error rule knowledge base and triggering the corresponding automatic repair process.
[0041] 203. Matching the compilation error information based on the preset error rule knowledge base to obtain the corresponding repair rule.
[0042] Specifically, the error rule knowledge base is configured to be extended by writing repair rule code, wherein the repair rule includes an error matching condition and an abstract syntax tree modification logic. The error matching condition is associated with the compilation error information, and the abstract syntax tree modification logic is used to define the modification method of the target node in the abstract syntax tree.
[0043] In the embodiments of the present application, the error rule knowledge base is configured to support functional expansion by writing repair rule codes. Each repair rule in the error rule knowledge base includes two key components in structure, i.e., an error matching condition and an abstract syntax tree modification logic. The error matching condition is configured to be associated with specific compilation error information and serves as a trigger condition of the rule, responsible for accurate retrieval and matching of error information fed back by the compiler in the knowledge base. The abstract syntax tree modification logic defines how to programmatically modify the abstract syntax tree of the target code to repair errors after successful matching. The abstract syntax tree modification logic specifically specifies the target node to be located, the required modification operation (such as changing node attributes, adjusting subtree structure, adding or deleting nodes, etc.), and the code semantic constraints that must be followed during modification. By encapsulating the error pattern and the abstract syntax tree modification logic as an extensible rule in code form, the repair capability of the system can continuously adapt to new error types and complex code scenarios, significantly improving the scalability and long-term applicability of the tool in actual development environments.
[0044] 204. repairing the preliminary repair code by using the repair rule until the preliminary repair code passes the compilation check to obtain the target repair code.
[0045] Specifically, the preliminary repair code is parsed to generate a corresponding abstract syntax tree, wherein the abstract syntax tree includes a plurality of nodes; an error node causing a compilation error is located in the abstract syntax tree, and context information of the error node is analyzed, wherein the context information includes ancestor nodes and descendant nodes of the error node; it is determined whether the repair rule is applicable to the error scenario of the preliminary repair code based on the context information; when the repair rule is applicable to the error scenario of the preliminary repair code, a modification operation is performed on the abstract syntax tree according to the repair rule, and the modified abstract syntax tree is restored to executable code.
[0046] Further, the modification operation includes at least one of the following: modifying the attributes, values, or types of the error node; changing the ancestor nodes or descendant nodes; adding or deleting subtrees in the abstract syntax tree.
[0047] In the embodiments of the present application, the detailed process of the post-compilation repair submodule based on the abstract syntax tree for error repair is specifically described. The system first parses the preliminary repair code to generate the corresponding abstract syntax tree representation. The tree structure is composed of multiple nodes representing various syntax elements of the code. Then the system accurately locates the error node causing the compilation error in the abstract syntax tree and deeply analyzes the context information of the error node, including ancestor nodes and descendant nodes, to fully understand the syntax environment in which the error is located. Based on the comprehensive analysis of the context, the system determines whether the current repair rule is applicable to this specific error scenario. This judgment mechanism ensures the accuracy and safety of the repair operation. When the rule is confirmed to be applicable, the system performs modification operations on the abstract syntax tree according to the repair rule. These operations include but are not limited to: modifying the attributes, values or types of the error node to correct the underlying error; changing the related ancestor nodes or descendant nodes to maintain the consistency of the syntax tree; adding new sub-trees to the abstract syntax tree to supplement the necessary code structure, or deleting redundant and invalid sub-trees. Finally, the system restores the modified abstract syntax tree to executable code, completing the repair operation. The complete abstract syntax tree operation process ensures the accuracy of the repair and the structural integrity of the code.
[0048] In another embodiment, when the system does not retrieve a repair rule matching the current compilation error information in the error rule knowledge base, it will automatically switch to a large language model repair mode. In this repair mode, the system first assembles a structured compilation error repair prompt based on the compilation error information, the preliminary repair code segment that triggered the error, and the complete syntax context. Then the prompt is sent to the preset large language model to obtain the repair suggestion code or repair scheme description generated by the large language model for the current compilation error. The modified code is then recompiled and verified. If the modified code still has compilation errors, the system will start a multi-round iterative repair mechanism based on the previous interaction history, new compilation error information, and current code state to assemble new prompts containing more rich context. The large language model is called again to obtain updated repair suggestions, and the modification and compilation verification are continued. This process will be repeated until the code successfully passes the compilation verification or the preset iteration termination condition is reached.
[0049] Another embodiment of the compilation verification stage repair is provided in the embodiments of the present application. When the preset rule knowledge base cannot cover a specific compilation error, the system can seamlessly switch to an intelligent repair mode driven by a large language model. By combining the compilation error information, the code context, the code understanding and generation capabilities of the large language model, the system can effectively handle very rare errors, complex semantic errors or compilation problems specific to new programming paradigms. Based on the hybrid repair strategy combining rules and models, the system not only retains the accuracy and efficiency of rule repair, but also provides strong generalization ability and adaptability through the large language model, thereby significantly improving the practicality and robustness of the tool in diversified and dynamic software development scenarios, and maximizing the success rate of repair in the compilation verification stage.
[0050] 205. Perform a running verification on the target repair code, and obtain running error information when a running error is detected.
[0051] In the embodiments of the present application, after the target repair code passes the compilation verification, the system executes the target repair code in a real runtime environment to verify whether the function correctness and behavior logic of the target repair code meet the expectations. The running verification process is implemented by executing a preset test case or starting a program to run. The system monitors the execution state and output result of the code in real time. When an exception occurs, an assertion fails, an unexpected output is generated, or a runtime error (such as null pointer reference, array out-of-bounds, resource allocation failure, etc.) is triggered during the running of the code, the system immediately captures the running error. The running error information obtained by the system is a structured data set, which usually includes the exception type, detailed error description, complete stack trace information, variable state snapshot at the time of error occurrence, and related environment context. Comprehensive and accurate running error information provides indispensable diagnostic basis and context basis for subsequent large language model repair process.
[0052] 206. Diagnose and repair based on the running error information by calling a preset large language model until the target repair code passes the running verification.
[0053] Specifically, based on the running error information, the error code segment of the target repair code, and the code context information of the error code segment, an initial prompt word is assembled. The initial prompt word is sent to a preset large language model, and repair content information returned by the large language model is received, wherein the repair content information includes repair suggestion code and repair description. The target repair code is modified according to the repair content information to generate a to-be-verified code, and the to-be-verified code is re-verified by running until the to-be-verified code passes the running verification.
[0054] Furthermore, when the code to be verified fails the runtime verification, new runtime error information is obtained; based on the initial prompt word, the new runtime error information, and the current state of the code to be verified, a new prompt word is assembled, the new prompt word is sent to the large language model, and the new repair content information returned by the large language model is received; the code to be verified is modified according to the new repair content information, and the runtime verification is repeated, using a multi-round iterative strategy until the code to be verified passes the runtime verification or reaches the preset iteration termination condition.
[0055] In this embodiment, the large model repair submodule includes two mechanisms: single-interaction and multi-round iteration. Specifically, after the large model repair process is triggered, the system first assembles a structured initial prompt word based on the runtime error information, the target code segment where the error occurred, and its complete code context information. The initial prompt word provides sufficient diagnostic background for the large language model. Subsequently, the system sends the initial prompt word to the preset large language model. After analyzing the large language model based on its powerful code understanding and generation capabilities, it returns repair content information. The information can be directly usable repair suggestion code or a text description of the repair solution. Based on this, the system modifies the target repair code, generates code to be verified, and immediately runs the verification to determine whether the repair is successful.
[0056] If the repair fails, the system will initiate a multi-round iterative strategy. This involves establishing and maintaining a multi-round dialogue context with the large language model. After obtaining new runtime error information, the system combines the historical background in the initial prompts, the new error information, and the current state of the code to be verified to assemble new prompts with richer context. These new prompts are then sent to the large language model to obtain updated repair content, and the modifications and verification are applied again. This cycle of diagnosis, repair, and verification is automatically repeated, allowing the large language model to understand the evolution of the repair process and reason based on a more complete context. This enables the system to make more accurate inferences based on the continuously enriched dialogue history. The multi-round iteration will continue until the code to be verified successfully passes the runtime verification or reaches the preset iteration termination condition (such as reaching the maximum number of rounds). This multi-round iterative mechanism based on accumulated dialogue history ensures that the repair process is both intelligent and robust, and can effectively handle complex runtime errors.
[0057] This invention provides another code repair method based on abstract syntax trees and large models. This method achieves automated and precise code repair by manipulating abstract syntax trees, and its repair mechanism is as follows: Figure 3 and Figure 4 As shown, where, Figure 3 This paper systematically demonstrates the code repair principle based on the abstract syntax tree (AST) structure, revealing the complete process from source code to AST, and then from modifying the tree structure to finally generating the repaired code. Figure 4Thus, the specific content and repair effect of the repair operation are intuitively displayed from the code text level.
[0058] Specifically, the repair process starts with parsing the code to be repaired into an initial abstract syntax tree, and the tree structure accurately reflects the syntax hierarchy of the code, wherein each node corresponds to a syntax element (such as a method declaration, a variable definition, an assignment expression, etc.). The system locates the problem nodes and their associated nodes that cause the compilation or running errors by analyzing the tree structure. In the repair stage, a series of structured modification operations (including adding new function nodes to complete the missing logic, deleting redundant or error nodes to eliminate abnormalities, adjusting node attributes to correct type errors, and restructuring sub-trees to optimize code structure) are performed on the abstract syntax tree level. Finally, the corrected abstract syntax tree is restored to executable code that conforms to the programming specification through a code generator. The repair method based on the abstract syntax tree directly operates the syntax tree structure on the one hand, ensuring that all modifications conform to the syntax specifications of the programming language, fundamentally avoiding format errors or semantic deviations that may be introduced by text replacement. On the other hand, the modification based on the tree structure can accurately locate the error sources, implement deep repair of complex errors (such as scope problems, type mismatches, API misuse, etc.), and maintain the integrity of the overall code structure.
[0059] Further, as a specific implementation of the method, the embodiment of the present application provides a code repair device based on an abstract syntax tree and a large model, as shown in Figure 1 Figure 5 The device includes a syntax checking module 301, a compilation verification module 302, a syntax tree modification module 303, and a large model runtime repair module 304.
[0060] The syntax checking module 301 is configured to perform syntax checking on the code to be repaired and repair syntax problems existing in the code to be repaired to generate preliminary repair code, wherein the syntax problems include syntax errors and / or programming language type mismatches. The compilation verification module 302 is configured to perform compilation verification on the preliminary repair code, obtain compilation error information when detecting that the preliminary repair code has a compilation error, and match the compilation error information based on a preset error rule knowledge base to obtain corresponding repair rules. The syntax tree modification module 303 is configured to repair the preliminary repair code using the repair rules until the preliminary repair code passes the compilation verification to obtain target repair code, and perform running verification on the target repair code, wherein the repair rules are used to modify the abstract syntax tree of the preliminary repair code. The large model runtime repair module 304 is configured to obtain running error information when detecting that the target repair code has a running error, and call a preset large language model based on the running error information for diagnosis and repair until the target repair code passes the running verification.
[0061] In a specific application scenario, the syntax checking module 301 is specifically configured to parse the to-be-repaired code to generate a corresponding initial abstract syntax tree; analyze the initial abstract syntax tree to identify a problem node that has a syntax problem; modify the problem node according to a preset syntax correction rule set to correct the initial abstract syntax tree; and restore the corrected initial abstract syntax tree to code to obtain a preliminary repair code.
[0062] In a specific application scenario, the error rule knowledge base in the compilation verification module 302 is configured to be extended by writing repair rule code, wherein the repair rule includes an error matching condition and an abstract syntax tree modification logic, the error matching condition is associated with the compilation error information, and the abstract syntax tree modification logic is used to define a modification manner for a target node in the abstract syntax tree.
[0063] In a specific application scenario, the syntax tree modification module 303 can be specifically configured to parse the preliminary repair code to generate a corresponding abstract syntax tree, wherein the abstract syntax tree includes a plurality of nodes; locate an error node that causes a compilation error in the abstract syntax tree and analyze context information of the error node, wherein the context information includes ancestor nodes and descendant nodes of the error node; determine whether the repair rule is applicable to an error scenario of the preliminary repair code based on the context information; when the repair rule is applicable to the error scenario of the preliminary repair code, perform a modification operation on the abstract syntax tree according to the repair rule, and restore the modified abstract syntax tree to executable code.
[0064] In a specific application scenario, the modification operation in the syntax tree modification module 303 includes at least one of the following: modifying an attribute, a value, or a type of the error node; changing the ancestor node or the descendant node; adding or deleting a sub-tree in the abstract syntax tree.
[0065] In a specific application scenario, the large model runtime repair module 304 is specifically configured to assemble an initial prompt word based on the running error information, the error code segment of the target repair code, and the code context information of the error code segment; send the initial prompt word to a preset large language model, and receive repair content information returned by the large language model, wherein the repair content information includes repair suggestion code and repair description; modify the target repair code according to the repair content information to generate a to-be-verified code, and re-perform running verification on the to-be-verified code until the to-be-verified code passes the running verification.
[0066] In a specific application scenario, the large model runtime repair module 304 can also be used to obtain new running error information when the to-be-verified code fails the running verification; assemble a new prompt word based on the initial prompt word, the new running error information and the current state of the to-be-verified code, send the new prompt word to the large language model, and receive the new repair content information returned by the large language model; modify the to-be-verified code according to the new repair content information, and repeatedly perform the running verification, using a multi-round iteration strategy, until the to-be-verified code passes the running verification or a preset iteration termination condition is reached.
[0067] It should be noted that other corresponding descriptions of the various functional units involved in the code repair device based on the abstract syntax tree and the large model provided in this embodiment can refer to the corresponding descriptions in Figure 1 and Figure 2 , which will not be repeated here.
[0068] Based on the above methods as shown in Figure 1 and Figure 2 , accordingly, the present embodiment also provides a storage medium having a computer program stored thereon, which is executed by a processor to implement the above-mentioned code repair method based on the abstract syntax tree and the large model.
[0069] Based on such understanding, the technical solutions of the present application can be embodied in the form of a software product. The software product to be identified can be stored in a non-volatile storage medium (which can be a CD-ROM, a U disk, a mobile hard disk, etc.), and includes a plurality of instructions for causing a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the code repair method based on the abstract syntax tree and the large model of each implementation scenario of the present application.
[0070] Based on the above methods as shown in Figure 1 and Figure 2 , and Figure 5 the code repair device based on the abstract syntax tree and the large model, in order to achieve the above-mentioned purpose, as shown in Figure 6 , the present embodiment further provides an entity device for code repair based on the abstract syntax tree and the large model. The device includes a communication bus, a processor, a memory and a communication interface, and can also include an input / output interface and a display device, wherein the communication between each functional unit can be completed through the bus. The memory stores a computer program, and the processor is used to execute the program stored in the memory, and execute the code repair method based on the abstract syntax tree and the large model in the above-mentioned embodiments.
[0071] Optionally, the entity device can also include a user interface, a network interface, a camera, a radio frequency (RF) circuit, a sensor, an audio circuit, a WI-FI module, and the like. The user interface can include a display screen, an input unit such as a keyboard, and the like. Optionally, the user interface can also include a USB interface, a card reader interface, and the like. The network interface can optionally include a standard wired interface, a wireless interface (such as a WI-FI interface), and the like.
[0072] Those skilled in the art can understand that the entity device structure provided by the embodiment based on the abstract syntax tree and the large model does not constitute a limitation on the entity device, and can include more or fewer components, or combine certain components, or different component arrangements.
[0073] The storage medium can also include an operating system and a network communication module. The operating system is a program for managing the hardware of the entity device and the to-be-identified software resources, supporting the running of information processing programs and other to-be-identified software and / or programs. The network communication module is used to realize communication between the components in the storage medium and communication with other hardware and software in the information processing entity device.
[0074] Through the above description of the embodiments, those skilled in the art can clearly understand that the present application can be implemented by means of software and a necessary general hardware platform, or by hardware. By applying the technical solutions of the present application, firstly, through the pre-positioned syntax checking and repair of the code, the surface syntax errors and type mismatch problems can be automatically and efficiently processed, laying a foundation for subsequent deep repair and improving the initial code quality from the source; secondly, for deeper semantic errors in the compilation stage, a repair method based on the abstract syntax tree is adopted, the compilation error information is matched with the preset rule knowledge base, and the modification is directly made at the abstract syntax tree level, realizing accurate and lossless repair of the code structure, overcoming the insufficient repair precision of the prior art, and effectively processing complex semantic problems involving the type system, scope, and the like, improving the accuracy and reliability of the repair; finally, for errors occurring in the running stage, a large language model is called to diagnose and repair, ensuring that the repair process will not be interrupted due to unknown error types, enhancing the generalization ability and practicality of the tool, and thus being able to cope with the changing needs in complex projects. The above method improves the efficiency of code error repair, and through the combination of abstract syntax tree analysis and large language model repair, the processing capability and repair precision for deep semantic errors and unknown running errors are enhanced, fundamentally improving the quality, reliability of the code, and the development efficiency of the software project.
[0075] Those skilled in the art can understand that the modules or flows in the drawings are not necessarily required for implementing the present application. Those skilled in the art can understand that the modules in the devices in the implementation scenarios can be distributed in the devices in the implementation scenarios according to the description of the implementation scenarios, or can be changed to be located in one or more devices different from the implementation scenarios. The modules in the above implementation scenarios can be combined into one module, or can be further split into multiple sub-modules.
[0076] The above application numbers are only for description, and do not represent the advantages and disadvantages of the implementation scenarios. The above disclosure is only some specific implementation scenarios of the present application, but the present application is not limited thereto, and any variations that can be thought of by those skilled in the art should fall within the protection scope of the present application.
Claims
1. A code repair method based on abstract syntax trees and large models, characterized in that, include: Perform a syntax check on the code to be repaired, and fix the syntax problems in the code to be repaired to generate preliminary repair code. The syntax problems include syntax errors and / or programming language type mismatch. The preliminary repair code is compiled and verified. When a compilation error is detected in the preliminary repair code, the compilation error information is obtained, and the compilation error information is matched based on a preset error rule knowledge base to obtain the corresponding repair rule. The repair rules are used to repair the initial repair code until the initial repair code passes compilation verification to obtain the target repair code, and the target repair code is run and verified. The repair rules are used to modify the abstract syntax tree of the initial repair code. When an error is detected in the target repair code, the error information is obtained, and a preset large language model is called based on the error information to perform diagnosis and repair until the target repair code passes the runtime verification.
2. The method according to claim 1, characterized in that, The process of performing a syntax check on the code to be repaired, fixing the syntax problems in the code to be repaired, and generating preliminary repair code includes: The code to be repaired is parsed to generate the corresponding initial abstract syntax tree; Analyze the initial abstract syntax tree to identify problem nodes where the syntax problem exists; The problem nodes are modified according to a preset set of syntax correction rules to correct the initial abstract syntax tree; The corrected initial abstract syntax tree is restored to code, resulting in preliminary repaired code.
3. The method according to claim 1, characterized in that, The error rule knowledge base is configured to be extended by writing repair rule code. The repair rule includes error matching conditions and abstract syntax tree modification logic. The error matching conditions are associated with the compilation error information, and the abstract syntax tree modification logic is used to define the modification method of the target node in the abstract syntax tree.
4. The method according to claim 1, characterized in that, The process of repairing the initial repair code using the repair rules includes: The initial repair code is parsed to generate a corresponding abstract syntax tree, wherein the abstract syntax tree includes multiple nodes; Locate the error node causing the compilation error in the abstract syntax tree, and analyze the context information of the error node, wherein the context information includes the ancestor node and descendant node of the error node; Based on the context information, determine whether the repair rule is applicable to the error scenario of the initial repair code; When the repair rule applies to the error scenario of the initial repair code, the abstract syntax tree is modified according to the repair rule, and the modified abstract syntax tree is restored to executable code.
5. The method according to claim 4, characterized in that, The modification operation includes at least one of the following: Modify the attributes, values, or type of the faulty node; Modify the ancestor node or the descendant node; Add or delete subtrees in the abstract syntax tree.
6. The method according to claim 1, characterized in that, The process of calling a preset large language model based on the runtime error information for diagnosis and repair, until the target repair code passes runtime verification, includes: Based on the runtime error information, the error code fragment of the target repair code, and the code context information of the error code fragment, assemble the initial prompt words; The initial prompt word is sent to a preset large language model, and the repair content information returned by the large language model is received, wherein the repair content information includes repair suggestion code and repair description; The target repair code is modified according to the repair content information to generate code to be verified, and the code to be verified is run again until the code to be verified passes the run verification.
7. The method according to claim 6, characterized in that, The step of re-running the verification of the code to be verified until the code to be verified passes the verification includes: When the code to be verified fails the runtime verification, obtain new runtime error information; Based on the initial prompt word, the new runtime error information, and the current state of the code to be verified, a new prompt word is assembled, the new prompt word is sent to the large language model, and the new repair content information returned by the large language model is received; The code to be verified is modified according to the newly repaired information, and the verification is repeated. A multi-round iterative strategy is adopted until the code to be verified passes the verification or reaches the preset iteration termination condition.
8. A code repair device based on abstract syntax trees and large models, characterized in that, include: The syntax checking module is used to perform syntax checks on the code to be repaired, and to fix the syntax problems existing in the code to be repaired, generating preliminary repair code. The syntax problems include syntax errors and / or programming language type mismatch. The compilation verification module is used to perform compilation verification on the preliminary repair code. When a compilation error is detected in the preliminary repair code, the module obtains the compilation error information and matches the compilation error information based on a preset error rule knowledge base to obtain the corresponding repair rule. The syntax tree modification module is used to repair the preliminary repair code using the repair rules until the preliminary repair code passes the compilation verification to obtain the target repair code, and to run and verify the target repair code, wherein the repair rules are used to modify the abstract syntax tree of the preliminary repair code; The large model runtime repair module is used to obtain runtime error information when the target repair code is detected to call a preset large language model for diagnosis and repair based on the runtime error information, until the target repair code passes the runtime verification.
9. A storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1 to 7.
10. A computer device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1 to 7.