A control structure alignment method based on bidirectional reconstruction

CN116775474BActive Publication Date: 2026-09-01NANJING UNIV
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202310703977.9
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-06-14
Publication Date
2026-09-01
Estimated Expiration
2043-06-14

AI Technical Summary

Technical Problem

他们的方法失效于当不存在这样的一个与错误程序控制结构匹配的正确程序

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116775474B_ABST
    Figure CN116775474B_ABST
Patent Text Reader

Abstract

This invention discloses a control structure alignment method based on bidirectional reconstruction, comprising the following steps: inputting two programs and an objective function as input to the method; designing an abstract control flow tree model representing the control flow structure of the objective function; constructing abstract control flow tree models of the objective functions in the two source programs respectively; designing legal matching rules for nodes in the abstract control flow tree models; constructing the set of the "longest" legal matches between nodes in the abstract control flow tree models of the objective functions in the two source programs and selecting the optimal match; designing general program reconstruction rules; and reconstructing the objective functions of the two source programs based on the optimal match to achieve control structure alignment, resulting in the reconstructed program. This method can provide bidirectional reconstruction guidance for two programs with inconsistent control structures without changing the program semantics, achieving the alignment of the control structure of the objective function, fundamentally solving the deficiency of existing automatic feedback generation technology in being unable to provide repair guidance for erroneous programs with unmatched structures.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of static analysis technology, and in particular to a control structure alignment method based on bidirectional reconstruction. Background Technology

[0002] Static analysis refers to methods of program analysis without running the program. A program's control structure is the structure that controls the execution order of the program; the three most basic control structures are sequence, selection, and loop. Theory and practice have proven that even the most complex algorithms can be constructed using these three basic structures. We use parser tools to perform static analysis of the source code structure from the perspective of the program's syntax tree, thereby constructing an abstract control flow tree model of the program. A common tool for Java language lexical analysis is JavaParser, which supports generating ASTs (Abstract Syntax Trees) for Java and allows modification of the AST to generate new Java file content. A common tool for Python language lexical analysis is Python Ast, which has similar functionality to JavaParser.

[0003] Automated feedback generation techniques aim to help students better understand programs, identify program defects, and learn how to fix them. These techniques act as teaching assistants in practice, typically taking an incorrect program P and a set of correct implementations as input, and outputting a fixed program P'. Recent automated feedback generation techniques like Clara and Sarfgen employ a "block-to-block" repair strategy, demonstrating significant advantages in fixing introductory programming assignments. They match the incorrect program with the closest correct program whose control structure aligns with it as a reference, generating repairs for each basic block of the incorrect program by replacing the original expressions with expressions from the corresponding blocks of the reference program. Therefore, they impose stricter requirements on the existing dataset, requiring it to contain at least one or more correct implementations with the exact same control structure as the incorrect program. Their method fails when no such correct program matching the control structure of the incorrect program exists. To fundamentally address this deficiency, we implemented a control structure alignment method based on bidirectional reconstruction, which reduces the requirements for the dataset. After matching the closest correct program to the faulty program, we do not require the correct program to have an identical control structure. After aligning the structures of the two programs using our tool, we then run a "block-to-block" repair strategy. Summary of the Invention

[0004] To address the problems existing in the prior art, this invention provides a control structure alignment method based on bidirectional reconstruction. This method uses static analysis to establish optimal matching of control flow nodes for two programs with similar functions, and performs minimal reconstruction operations based on this to achieve control structure alignment, thereby fundamentally solving the defects of existing automatic feedback generation technology. This invention is achieved through the following technical solutions.

[0005] A control structure alignment method based on bidirectional reconstruction, characterized by comprising the following steps:

[0006] Step 1) Input two programs and an objective function, naming them source program P, source program Q, and objective function, respectively, as inputs to the method;

[0007] Step 2) Design an abstract control flow tree model to represent the control flow structure of the program's objective function;

[0008] Step 3) Construct abstract control flow tree models of the objective functions in source programs P and Q respectively;

[0009] Step 4) Design the legal matching rules for nodes in the abstract control flow tree model;

[0010] Step 5) Construct the set of the "longest" legal matches between nodes in the abstract control flow tree model of the objective function in source program P and source program Q, and select the optimal match from them;

[0011] Step 6) Design general program refactoring rules so that the program semantics remain unchanged before and after refactoring;

[0012] Step 7) Based on the optimal matching obtained in Step 5), reconstruct the objective functions of source program P and source program Q to achieve control structure alignment, and obtain the reconstructed program P' and program Q'.

[0013] The control structure alignment method based on bidirectional reconstruction described above is characterized in that the two programs and the target function input in step 1) need to meet the requirement that both programs to be aligned contain the implementation of the target function.

[0014] The control structure alignment method based on bidirectional reconstruction described above is characterized in that step 2) of designing an abstract control flow tree model representing the control flow structure of the program's objective function specifically includes the following steps:

[0015] Step 21) Define each node n in the abstract control flow tree model as having the following properties: node type, denoted as type(n); node label, denoted as label(n); containing 0 or more child nodes, denoted as children(n); parent node, denoted as parent(n); ancestor node, denoted as ancestors(n);

[0016] Step 22) specifies that the abstract control flow tree model has three types of nodes, including: function entry node (Method Declaration), which corresponds to the entry point of a function and is the root node of the abstract control flow tree model; control flow node, which corresponds to the control flow statement in the function; and branch node, which corresponds to different branches of the If statement, the Then branch and the Else branch.

[0017] Step 23) Specify the label of the function entry node as MethodEntry, the parent node of the entry node is empty, the number of ancestor nodes is 0, and the type of child node is control flow node;

[0018] Step 24) specifies that the control flow node has 4 different labels, including: For statement (ForStatement); ForEach statement (ForEachStatement); While statement (WhileStatement); If statement (IfStatement); the child node of the If statement can only be a branch node, and the child nodes of other control flow nodes are of the control flow node type;

[0019] Step 25) specifies that the branch node has two different labels, namely Then Branch and Else Branch. The parent node of the branch node is a control flow node with the label If statement, and the child node of the branch node is a control flow node.

[0020] The control structure alignment method based on bidirectional reconstruction described above is characterized in that step 3) of constructing the abstract control flow tree model of the objective function in source program P and source program Q respectively includes the following steps:

[0021] Step 31) Use a parser tool (such as JavaParser for Java, PythonAst for Python) to extract the abstract syntax tree of the program's target function;

[0022] Step 32) Perform a depth-first traversal of the abstract syntax tree of the program's target function. When the target function entry point is reached, create the root node of the abstract control flow tree model and establish a link between the root node and the abstract syntax tree node.

[0023] Step 33) Continue the depth-first traversal of the abstract syntax tree of the program's target function. When the control flow statement is encountered, create a control flow node in the abstract control flow tree model and establish a link between the control flow node and the abstract syntax tree node. At the same time, establish the parent-child relationship between nodes. The parent-child relationship of the nodes in the abstract control flow tree model corresponds one-to-one with the parent-child relationship in the abstract syntax tree.

[0024] Step 34) When traversing to the If statement, create a control flow node labeled as If statement, and create a Then branch child node for the control flow node. If the If statement contains an Else branch, continue to create an Else branch child node for the If control flow node; traverse the different branches of the If statement in the abstract syntax tree to complete the construction of the abstract control flow tree model.

[0025] Step 35) Perform steps 31)-34) to construct an abstract control flow tree model of the objective function in the source program P;

[0026] Step 36) Perform steps 31)-34) to construct an abstract control flow tree model of the objective function in the source program Q.

[0027] The control structure alignment method based on bidirectional reconstruction described above is characterized in that the design process of designing the legal matching rules for nodes in the abstract control flow tree model in step 4) includes: assuming that the legal matching of nodes between the two current abstract control flow tree models T1 and T2 is M, after a new node pair (T1.x, T2.y) is added to the legal matching M, the new matching M' is still legal. The legal matching rules include:

[0028] (1) Define the first legal matching rule: if T1.x and T2.y can form a node pair, then one of the following three conditions must be met: both are function entry nodes; both are control flow nodes with the same label; both are branch nodes, and the labels can be different.

[0029] (2) Define the second rule for legal matching: if there exists an ancestor node T1.x of T1.x / T2.y... c / T2.y c In a valid match M, the node T1.x / T2.y matches node T2.y in match M. d / T1.x d It should also be an ancestor node of T2.y / T1.x;

[0030] (3) Rule 3 for legal matching: if both T1.x and T2.y are branch nodes, then pair(parent(T1.x), parent(T2.y)) should also be in the matching M;

[0031] (4) Rule 4 for legal matching: If T1.x / T2.y is a child of a cyclic node, then all cyclic ancestor nodes of T1.x / T2.y should be in the matching M;

[0032] (5) The fifth rule of legal matching is defined: if T1.x / T2.y is a child of a Flse branch that is not in the matching M, then the Then branch t of the parent node of the Flse branch and all its children are not in the matching M.

[0033] The control structure alignment method based on bidirectional reconstruction described above is characterized in that step 5) constructing the set of the "longest" legal matches between nodes in the abstract control flow tree model of the objective functions in source programs P and Q, and selecting the optimal match from it, specifically includes the following steps:

[0034] Step 51) Define the "longest" set of valid matches S, M = {pair1, pair2, ..., pair...} w} represents any valid match in S, and pair w For a valid match M to be the last matching pair, if M is not another pair containing "pair"... w If a subset of all valid matches is a subset of all valid matches, then the set of valid matches S is called the "longest" set of valid matches.

[0035] Step 52) Perform a depth-first traversal of the abstract control flow tree models T1 and T2 of the objective functions in source programs P and Q to obtain the abstract control flow tree sequences X and Y;

[0036] Step 53) specifies the head series L n This represents the subsequence consisting of the first n nodes of the abstract control flow tree sequence L;

[0037] Step 54) Assume an abstract control flow tree sequence X = (x1, x2, ..., x...). m ) and Y = (y1, y2, ..., y n The head sequence of X is X0, X1, ..., X m The head sequence of Y is Y0, Y1, ..., Y n The LLM(X) specification defines... i Y j ) represents X i With Y j The set of the "longest" valid matches. LLM(X) i Y jThe state transition equation is as follows (let M) a =LLM(X i-1 Y j-1 M b =LLM(X i-1 Y j M c =LLM(X i Y j-1 )): (1) If i = 0 or j = 0, (2) If i > 0 and j > 0, if x i With y j The valid matching rule zero is not satisfied, meaning x i With y j Unable to form a matching pair, LLM(X) i Y j )=(M b -M a )∪(M c -M a )∪(M b ∩M c (3) If i > 0 and j > 0, if x i With y j Let M satisfy the valid matching rule of zero. l Stored M a It is legal to add a matching pair (x) to the middle. i y j All matches of M. l If empty, continue searching for LLM(X) i-2 Y j-2 ), LLM(X i-3 Y j-3 ...until M l Not empty or the search boundary has been reached. LLM(X) i Y j )=(M b -M a )∪(M c -M a )∪((M b ∩M c )-M l )∪(M l ^(i, j));

[0038] Step 55) Calculate and obtain LLM(X) m Y n ), which is the set of the "longest" legal matches between nodes in the abstract control flow tree model of the objective function in source program P and source program Q;

[0039] Step 56) specifies that the optimal match must contain the maximum number of cyclic nodes;

[0040] Step 57) stipulates that the optimal match satisfies step 56) and must contain the maximum number of nodes;

[0041] Step 58) stipulates that the optimal match satisfies steps 56) and 57), while the sum of the height differences between matching pairs is minimized;

[0042] Step 59) Based on steps 56)-58), select the optimal match between nodes in the abstract control flow tree model of the objective function in source program P and source program Q from the matching set obtained in step 55).

[0043] The control structure alignment method based on bidirectional reconstruction described above is characterized in that step 6) of designing general program reconstruction rules specifically includes the following steps:

[0044] Step 61) Define refactoring rule one: swap the branches of the If statement and negate the condition of the If statement;

[0045] Step 62) specifies the second reconstruction rule: introduce new control flow nodes. The control flow nodes are mainly divided into four types, including (1) introducing a new If node; (2) introducing a new While node; (3) introducing a new For node; and (4) introducing a new ForEach node.

[0046] Step 63) specifies refactoring rule three: introduce new If guards or If-else guards; add an If guard or If-else guard to the program block.

[0047] The control structure alignment method based on bidirectional reconstruction described above is characterized in that step 7) reconstructs the objective functions of source program P and source program Q based on the optimal matching obtained in step 5) to achieve control structure alignment, and obtains the reconstructed programs P' and Q', specifically including the following steps:

[0048] Step 71) Scan the matching pairs in the optimal match. If there is a matching pair of type between branch nodes, but the labels of the two nodes are inconsistent, use the reconstruction rule specified in step 61) to exchange the branches of the parent node of the corresponding node in the source program P to obtain the reconstructed program P.

[0049] Step 72) If program P changes, re-execute step 3) to construct the abstract control flow tree model of program P, and execute step 5) to construct the "longest" legal matching set between nodes in the abstract control flow tree model of program P and the objective function in the source program Q, and select the optimal match from it;

[0050] Step 73) Scan the abstract control flow tree sequence X of the objective function of program P. If there are nodes that are not in the optimal match, then use the reconstruction rule two specified in step 62) to insert a new control structure node in the abstract control flow tree of the objective function of program Q, or use the reconstruction rule three specified in step 63) to insert a new If or If-else guard.

[0051] Step 74) Scan the abstract control flow tree sequence Y of the objective function of program Q. If there are nodes that are not in the optimal match, then use the reconstruction rule two specified in step 62) to insert a new control structure node in the abstract control flow tree of the objective function of program P, or use the reconstruction rule three specified in step 63) to insert a new If or If-else guard.

[0052] Step 75) Obtain the reconstructed program P' and program Q', wherein the objective function control structure of program P' and program Q' remains consistent.

[0053] The present invention adopts the above technical solution and has the following beneficial effects:

[0054] This method uses static analysis to construct an abstract control flow tree model of the program. By constructing the "longest" legal matching set of nodes between control flow tree models, the optimal matching of nodes between control flow tree models is obtained. Based on this, the code is reconstructed. This method can provide bidirectional reconstruction guidance for two programs with inconsistent control structures without changing the program semantics, thereby aligning the control structures of the objective function and fundamentally solving the defects of existing automatic feedback generation technology. Attached Figure Description

[0055] Figure 1 This is a flowchart illustrating the control structure alignment method based on bidirectional reconstruction according to an embodiment of the present invention.

[0056] Figure 2 This is a schematic diagram of the source program input for an embodiment of the present invention.

[0057] Figure 3 This is a schematic diagram of the abstract control flow tree model in an embodiment of the present invention.

[0058] Figure 4 This is a schematic diagram of an illegal match in the abstract control flow tree of an embodiment of the present invention.

[0059] Figure 5 This is a schematic diagram illustrating the optimal matching in an embodiment of the present invention.

[0060] Figure 6 This is a schematic diagram of the general program refactoring rules in an embodiment of the present invention.

[0061] Figure 7 This is a schematic diagram of the bidirectional reconstructed program according to an embodiment of the present invention. Detailed Implementation

[0062] The present invention will now be described in further detail with reference to the accompanying drawings and specific embodiments.

[0063] The purpose of this method is to provide a control structure alignment method based on bidirectional reconstruction. It uses static analysis techniques to establish optimal matching of control flow nodes for two programs with similar functions, and then performs minimal reconstruction operations to achieve control structure alignment, thereby fundamentally solving the shortcomings of existing automatic feedback generation techniques. For example... Figure 1 As shown, Figure 1 This is a flowchart illustrating the control structure alignment method based on bidirectional reconstruction according to an embodiment of the present invention. The method of the present invention specifically includes the following steps:

[0064] Step 1) Input two programs and the target function, and name them source program P, source program Q and target function, as input to the method;

[0065] Step 2) Design an abstract control flow tree model to represent the control flow structure of the program's objective function;

[0066] Step 3) Construct abstract control flow tree models of the objective functions in source programs P and Q respectively;

[0067] Step 4) Design the legal matching rules for nodes in the abstract control flow tree model;

[0068] Step 5) Construct the set of the "longest" legal matches between nodes in the abstract control flow tree model of the objective function in source program P and source program Q, and select the optimal match from them;

[0069] Step 6) Design general program refactoring rules so that the program semantics remain unchanged before and after refactoring;

[0070] Step 7) Based on the optimal matching obtained in step 14, reconstruct the objective functions of source program P and source program Q to achieve control structure alignment, and obtain the reconstructed program P' and program Q'.

[0071] The two programs and the objective function input in step 1) must meet the following requirements:

[0072] Both programs requiring structural alignment must contain the implementation of the objective function. For example... Figure 2 As shown, Figure 2 This is a schematic diagram of the source program input for an embodiment of the present invention.

[0073] Figure 3 This is a schematic diagram of the abstract control flow tree model in an embodiment of the present invention. Step 2) of designing the abstract control flow tree model specifically includes the following steps:

[0074] Step 21) Each node n in the abstract control flow tree model is defined to have the following properties: (1) Node type, denoted as type(n); (2) Node label, denoted as label(n); (3) Contains 0 or more child nodes, denoted as children(n); (4) Parent node, denoted as parent(n); (5) Ancestor node, denoted as ancestors(n).

[0075] Step 22) specifies that the abstract control flow tree model has three types of nodes: (1) Function entry node (MethodDeclaration), which corresponds to the entry point of the function. This type of node is the root node of the abstract control flow tree model. (2) Control-flow node, which corresponds to the control flow statement in the function. (3) Branch node, which corresponds to the different branches of the If statement, the Then branch and the Else branch.

[0076] Step 23) Specify the label of the function entry node as MethodEntry, the parent node of the entry node is empty, the number of ancestor nodes is 0, and the type of child node is control flow node.

[0077] Step 24) specifies that the control flow node has four different labels: (1) For statement; (2) ForEach statement; (3) While statement; and (4) If statement. The child node of the If statement can only be a branch node, while the child nodes of the other control flow nodes are of the control flow node type.

[0078] Step 25) specifies that the branch node has two different labels: Then Branch and Else Branch. The parent node of the branch node is a control flow node labeled with the If statement. The child nodes of the branch node are control flow nodes.

[0079] Step 3) The process of constructing the abstract control flow tree model specifically includes the following steps:

[0080] Step 31) Use a parser tool to extract the abstract syntax tree of the program's target function. Parser tools include JavaParser for Java and Python Ast for Python.

[0081] Step 32) Perform a depth-first traversal of the abstract syntax tree of the program's target function. When the traversal reaches the entry point of the target function, create the root node of the abstract control flow tree model and establish a link between the root node and the abstract syntax tree node.

[0082] Step 33) Continue the depth-first traversal of the abstract syntax tree of the program's target function. When a control flow statement is encountered, create a control flow node in the abstract control flow tree model and establish a link between the control flow node and the abstract syntax tree node. Simultaneously, establish parent-child relationships between nodes; the parent-child relationships in the abstract control flow tree model correspond one-to-one with the parent-child relationships in the abstract syntax tree.

[0083] Step 34) When the If statement is encountered, a control flow node labeled "If statement" is created, and a Then branch child node is created for the control flow node. If the If statement contains an Else branch, Else branch child nodes are created for the If control flow node. Different branches of the If statement in the abstract syntax tree are traversed to complete the construction of the abstract control flow tree model.

[0084] Step 35) Perform steps 31)-34) to construct an abstract control flow tree model of the objective function in the source program P.

[0085] Step 36) Perform steps 31)-34) to construct an abstract control flow tree model of the objective function in the source program Q.

[0086] Step 4) involves designing the legal matching rules for nodes in the abstract control flow tree model. Figure 4 This is a schematic diagram of an illegal match in an abstract control flow tree. Assume the legal match between nodes in the current two abstract control flow tree models T1 and T2 is M. After adding a new node pair (T1.x, T2.y) to match M, the new match M' is still legal. The legal match rule must satisfy the following conditions:

[0087] S1 specifies the first legal matching rule: if T1.x and T2.y can form a node pair, then one of the following three conditions must be met: (1) Both are function entry nodes; (2) Both are control flow nodes with the same label; (3) Both are branch nodes, and the labels can be different.

[0088] S2 specifies the second legal matching rule: if there exists an ancestor node T1.x of T1.x / T2.y... c / T2.y c In a valid match M, the node T1.x / T2.y matches node T2.y in match M. d / T1.x d It should also be an ancestor node of T2.y / T1.x, such as Figure 4As shown, illegal match 1 in (a) violates the second legal match rule because there is an ancestor node T2.b of T2.c in the match, but the node T1.b matched by T2.b is not an ancestor node of T1.c, so the match is illegal.

[0089] S3 specifies the third rule for legal matching: if both T1.x and T2.y are branch nodes, then pair(parent(T1.x), parent(T2.y)) should also be in the matching M.

[0090] S4 specifies the fourth rule for valid matching: if T1.x / T2.y is a child of a cyclic node, then all cyclic ancestor nodes of T1.x / T2.y should be in match M. For example... Figure 4 As shown, illegal match 2 in (b) violates the fourth rule of legal matching because T1.b has a cyclic ancestor node T1.while that is not in the match, so the match is illegal.

[0091] S5 specifies the fifth rule for valid matching: if T1.x / T2.y is a descendant of an Else branch that is not in match M, then the Then branch t of the parent node of that Else branch and all its descendants are not in match M. For example... Figure 4 As shown, illegal match 3 in (c) violates the legal match rule 5 because T2.c is a child of T2.else, and T2.else is not in the match, but T2.b, a child of T2.then, is in the match. Therefore, the match is illegal (nodes T1.b and T1.c of the same branch match T2.b and T2.c of different branches, which is illegal).

[0092] Figure 5 This is a schematic diagram illustrating the optimal matching in an embodiment of the present invention, as shown below. Figure 5 As shown, Figure 5 This demonstrates the optimal matching between nodes in the abstract control flow tree model of the objective function in program P and source program Q in step 5). The implementation process specifically includes the following steps:

[0093] Step 51) Define the "longest" set of valid matches S as M = {pair1, pair2, ..., pair...} w} represents any valid match in S, and pair w For a valid match M to be the last matching pair, if M is not another pair containing "pair"... w If a subset of all valid matches is a subset of all valid matches, then the set of valid matches S is called the "longest" set of valid matches.

[0094] Step 52) Perform a depth-first traversal of the abstract control flow tree models T1 and T2 of the objective function in source program P and source program Q to obtain the abstract control flow tree sequences X and Y.

[0095] Step 53) specifies the head series L n This represents the subsequence consisting of the first n nodes of the abstract control flow tree sequence L. For example, the head sequence of L = (Method, While, If, Then, While) is L0 = (), L1 = (Method), L2 = (Method, While), ..., L5 = (Method, While, If, Then, While).

[0096] Step 54) Assume an abstract control flow tree sequence X = (x1, x2, ..., x...). m ) and Y = (y1, y2, ..., y n The head sequence of X is X0, X1, ..., X m The head sequence of Y is Y0, Y1, ..., Y n The LLM(X) specification defines... i Y j ) represents X i With Y j The set of the "longest" valid matches. LLM(X) i Y j The state transition equation is as follows (let M) a =LLM(X i-1 Y j-1 M b =LLM(X i-1 Y j M c =LLM(X i Y j-1 )): (1) If i = 0 or j = 0, (2) If i > 0 and j > 0, if x i With y j It does not satisfy the first valid matching rule, that is, x i With y j Unable to form a matching pair, LLM(X) i Y j )=(M b -M a )∪(M c -M a )∪(M b ∩M c (3) If i > 0 and j > 0, if x i With y j If M satisfies the first valid matching rule, then M l Stored M a It is legal to add a matching pair (x) to the middle. i y j All matches of M.l If empty, continue searching for LLM(X) i-2 Y j-2 ), LLM(X i-3 Y j-3 ...until M l Not empty or the search boundary has been reached. LLM(X) i Y j )=(M b -M a )∪(M c -M a )∪((M b ∩M c )-M l )∪(M l ^(i, j)).

[0097] Step 55) Calculate and obtain LLM(X) m Y n ), which is the set of the "longest" legal matches between nodes in the abstract control flow tree model of the objective function in source program P and source program Q.

[0098] Step 56) stipulates that the optimal match must contain the maximum number of cyclic nodes.

[0099] Step 57) stipulates that the optimal match satisfies step 56) and must contain the maximum number of nodes.

[0100] Step 58) stipulates that the optimal match satisfies steps 56)-57), and the sum of the height differences between the matching pairs is minimized.

[0101] Step 59) Based on steps 56)-58), select the optimal match between nodes in the abstract control flow tree model of the objective function in source program P and source program Q from the matching set obtained in step 55).

[0102] Figure 6 This is a schematic diagram of the general program refactoring rules in an embodiment of the present invention. Step 6) designing the general program refactoring rules specifically includes the following steps:

[0103] Step 61) specifies refactoring rule one: swap the branches of the If statement and negate the condition of the If statement. For example... Figure 6 As shown, (b) is the refactored code snippet of (a), without changing its program semantics.

[0104] Step 62) specifies Refactoring Rule Two: Introduce new control flow nodes. This mainly falls into four types: (1) Introduce a new If node; (2) Introduce a new While node; (3) Introduce a new For node; (4) Introduce a new ForEach node. For example... Figure 6As shown in (c)(d)(e)(f).

[0105] Step 63) specifies refactoring rule three: introduce a new If guard or If-else guard. Add an If guard or an If-else guard to the block, such as... Figure 6 As shown, (h) adds an If guard to block B, and (i) adds an If-else guard to block B.

[0106] Figure 7 This is a schematic diagram of the bidirectionally reconstructed program according to an embodiment of the present invention, showing the bidirectionally reconstructed program P' and program Q' in step 7), specifically including the following steps:

[0107] Step 71) Scan the matching pairs in the optimal match. If there is a matching pair of type between branch nodes, but the labels of the two nodes are inconsistent, use reconstruction rule one to exchange the branches of the parent node of the corresponding node in the source program P to obtain the reconstructed program P.

[0108] Step 72) If program P changes, re-execute step 3) to construct the abstract control flow tree model of program P, and execute step 5) to construct the "longest" legal matching set between nodes in the abstract control flow tree model of program P and the objective function in the source program Q, and select the optimal match from it.

[0109] Step 73) Scan the abstract control flow tree sequence X of the objective function of program P. If there are nodes that are not in the optimal match, use reconstruction rule two to insert new control structure nodes in the abstract control flow tree of the objective function of program Q, or use reconstruction rule three to insert new If or If-else protections.

[0110] Step 74) Scan the abstract control flow tree sequence Y of the objective function of program Q. If there are nodes that are not in the optimal match, use reconstruction rule two to insert new control structure nodes in the abstract control flow tree of the objective function of program P, or use reconstruction rule three to insert new If or If-else protections.

[0111] Step 75) yields the reconstructed program P' and program Q', wherein the objective function control structures of program P' and program Q' remain consistent. For example... Figure 7 As shown, we added an If statement guard to the second While loop node of program P to obtain program P', and we added an If statement before the first If statement node of program Q to obtain program Q'. This ultimately achieved the alignment of the control structures of program P' and program Q'.

[0112] The above description is merely a preferred embodiment of the present invention, but the specific embodiments described herein are only for explaining the present invention and are not intended to limit the present invention. Any simple modifications, equivalent changes, and alterations made by those skilled in the art to the above embodiments based on the technical essence of the present invention without departing from the principles and spirit of the present invention should be included within the protection scope of the present invention.

Claims

1. A control structure alignment method based on bidirectional reconstruction, characterized in that, Includes the following steps: Step 1) Input two programs and an objective function, naming them source program P, source program Q, and objective function, respectively, as inputs to the method; Step 2) Design an abstract control flow tree model to represent the control flow structure of the program's objective function; Step 3) Construct abstract control flow tree models of the objective functions in source programs P and Q respectively; Step 4) Design the legal matching rules for nodes in the abstract control flow tree model; Step 5) Construct the set of longest legal matches between nodes in the abstract control flow tree model of the objective functions in source programs P and Q, and select the optimal match from it; Step 5) specifically includes the following steps: Step 51) Define the longest set of valid matches. , For any valid match in S, For a valid match M, the last matching pair is considered, if M is not another containing pair. If a subset of all valid matches is a subset of all valid matches, then the set of valid matches S is called the longest valid match set. Step 52) Depth-first traversal of the abstract control flow tree model of the objective function in source program P and source program Q and The abstract control flow tree sequence is obtained. and ; Step 53) Specify the head series Represents an abstract control flow tree sequence The subsequence consisting of the first n nodes; Step 54) Assume an abstract control flow tree sequence and ; The head sequence is , The head sequence is ;Regulation express and The set of longest valid matches; The state transition equation is as follows: Let , (1) If i=0 or j=0, (2) If i>0 and j>0, if and The valid matching rule zero is not satisfied, that is... and Unable to form a matching pair (3) If i>0 and j>0, if and Satisfying the valid matching rule zero, let Stored Legally join the matching pair , All matches; if If empty, continue searching. , …until Not empty or the search boundary has been reached; ; Step 55) Calculate and obtain That is, the longest legal matching set between nodes in the abstract control flow tree model of the objective function in source program P and source program Q; Step 56) specifies that the optimal match must contain the maximum number of cyclic nodes; Step 57) specifies that the optimal match satisfies Step 56) and must contain the maximum number of nodes; Step 58) stipulates that the optimal match satisfies steps 56) and 57), while the sum of the height differences between matching pairs is minimized; Step 59) Based on steps 56)-58), select the optimal match between nodes in the abstract control flow tree model of the objective function in source program P and source program Q from the matching set obtained in step 55). Step 6) Design general program refactoring rules so that the program semantics remain unchanged before and after refactoring; Step 7) Based on the optimal matching obtained in Step 5), reconstruct the objective functions of source program P and source program Q to achieve control structure alignment, and obtain the reconstructed program P' and program Q'.

2. The control structure alignment method based on bidirectional reconstruction according to claim 1, characterized in that, In step 1), the two input programs and the target function need to meet the requirement that both programs to be aligned contain the implementation of the target function.

3. The control structure alignment method based on bidirectional reconstruction according to claim 1, characterized in that, Step 2) Designing an abstract control flow tree model to represent the control flow structure of the program's objective function specifically includes the following steps: Step 21) Define each node n in the abstract control flow tree model as having the following properties: node type, denoted as type(n); node label, denoted as label(n); containing 0 or more child nodes, denoted as children(n); parent node, denoted as parent(n); ancestor node, denoted as ancestors(n); Step 22) Defines that the abstract control flow tree model has three types of nodes, including: function entry nodes, which correspond to the entry point of a function and are the root nodes of the abstract control flow tree model; control flow nodes, which correspond to the control flow statements in a function; and branch nodes, which correspond to different branches of the If statement, the Then branch and the Else branch. Step 23) Specify the label of the function entry node as MethodEntry, the parent node of the entry node is empty, the number of ancestor nodes is 0, and the type of child node is control flow node; Step 24) Specify that the control flow node has 4 different labels, including: For statement; ForEach statement; While statement; If statement; the child node of the If statement can only be a branch node, and the child nodes of other control flow nodes are of the control flow node type; Step 25) Specify that the branch node has two different labels, namely Then branch and Else branch. The parent node of the branch node is a control flow node with the label If statement, and the child node of the branch node is a control flow node.

4. The control structure alignment method based on bidirectional reconstruction according to claim 1, characterized in that, Step 3) involves constructing the abstract control flow tree models of the objective functions in source programs P and Q, respectively, and includes the following steps: Step 31) Use the parser tool to extract the abstract syntax tree of the program's target function; Step 32) Perform a depth-first traversal of the abstract syntax tree of the program's target function. When the target function entry point is reached, create the root node of the abstract control flow tree model and establish a link between the root node and the abstract syntax tree node. Step 33) Continue the depth-first traversal of the abstract syntax tree of the program's target function. When the control flow statement is encountered, create a control flow node in the abstract control flow tree model and establish a link between the control flow node and the abstract syntax tree node. At the same time, establish the parent-child relationship between nodes. The parent-child relationship of the nodes in the abstract control flow tree model corresponds one-to-one with the parent-child relationship in the abstract syntax tree. Step 34) When traversing to the If statement, create a control flow node labeled as If statement, and create a Then branch child node for the control flow node. If the If statement contains an Else branch, continue to create an Else branch child node for the If control flow node; traverse the different branches of the If statement in the abstract syntax tree to complete the construction of the abstract control flow tree model. Step 35) Perform steps 31)-34) to construct an abstract control flow tree model of the objective function in the source program P; Step 36) Perform steps 31)-34) to construct an abstract control flow tree model of the objective function in the source program Q.

5. The control structure alignment method based on bidirectional reconstruction according to claim 1, characterized in that, Step 4) involves designing the legal matching rules for nodes in the abstract control flow tree model. This process includes: assuming the current two abstract control flow tree models... and The valid match between nodes is M, and the new node pair ( , After being added to the legal match M, the new match M' is still legal. The legal matching rules include: (1) Define the first rule for legal matching, if To form a node pair, one of the following three conditions must be met: both are function entry nodes; both are control flow nodes with the same label; or both are branch nodes with different labels. (2) Define the second rule for legal matching, if there exists / Ancestor node / In a valid match M, the node / The nodes matched in M / Too / Ancestor node; (3) Define the third rule for legal matching, if and If both are branch nodes, then pair( , It is also in the match M; (4) Define the fourth rule for legal matching, if / If it is a child of a cyclic node, then / All cyclic ancestor nodes are in the match M; (5) Define the fifth rule for legal matching, if / If a node is a descendant of an Else branch that is not in match M, then the Then branch t of the parent node of the Else branch and all its descendants are not in match M.

6. The control structure alignment method based on bidirectional reconstruction according to claim 1, characterized in that, Step 6) Designing general program refactoring rules specifically includes the following steps: Step 61) Define refactoring rule one: swap the branches of the If statement and negate the condition of the If statement; Step 62) stipulates the second reconstruction rule: introduce new control flow nodes. The control flow nodes are mainly divided into four types, including (1) introducing new If nodes; (2) introducing new While nodes; (3) introducing new For nodes; (4) introducing new ForEach nodes; Step 63) stipulates refactoring rule three: introduce new If guards or If-else guards; add an If guard or If-else guard to the program block.

7. The control structure alignment method based on bidirectional reconstruction according to claim 6, characterized in that, Step 7) involves reconstructing the objective functions of source programs P and Q based on the optimal match obtained in step 5) to achieve control structure alignment, resulting in the reconstructed programs P' and Q'. This step specifically includes the following steps: Step 71) Scan the matching pairs in the optimal match. If there is a matching pair of type between branch nodes, but the labels of the two nodes are inconsistent, use the reconstruction rule specified in step 61) to exchange the branches of the parent node of the corresponding node in the source program P to obtain the reconstructed program P. Step 72) If program P changes, re-execute step 3) to construct the abstract control flow tree model of program P, and execute step 5) to construct the longest legal matching set between nodes in the abstract control flow tree model of program P and the objective function in the source program Q, and select the optimal match from it; Step 73) Scan the abstract control flow tree sequence X of the objective function of program P. If there are nodes that are not in the optimal match, then use the reconstruction rule two specified in step 62) to insert a new control structure node in the abstract control flow tree of the objective function of program Q, or use the reconstruction rule three specified in step 63) to insert a new If or If-else guard. Step 74) Scan the abstract control flow tree sequence Y of the objective function of program Q. If there are nodes that are not in the optimal match, then use the reconstruction rule two specified in step 62) to insert a new control structure node in the abstract control flow tree of the objective function of program P, or use the reconstruction rule three specified in step 63) to insert a new If or If-else guard. Step 75) Obtain the reconstructed program P' and program Q', wherein the objective function control structure of program P' and program Q' remains consistent.

Citation Information

Patent Citations

  • Parallel error mode matching method based on overall analysis

    CN107273289A

  • Automatic code generation method based on synchronous reaction type component

    CN113590129A