Code specification checking method and apparatus
By converting source code into a lexical unit stream and generating a binary tree for code style checking, the problem of long code style checking time and low efficiency in the existing technology is solved, and more efficient code style checking is achieved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- INSPUR SUZHOU INTELLIGENT TECH CO LTD
- Filing Date
- 2024-11-08
- Publication Date
- 2026-04-17
AI Technical Summary
Existing automated code style checking tools are based on compiler principles, resulting in long code style checking times, low efficiency, and wasted resources on syntax checking.
The source code is converted into a lexical unit stream, a binary tree is generated based on the syntax, and a binary tree of the target statement is generated through shift and reduction operations. The statement is then checked according to the rules of the specification to be checked, reducing data storage and checking time.
By generating a binary tree for standardization checks, data storage is reduced, check time is shortened, check efficiency is improved, and the maintenance cost of subsequent tools is simplified.
Smart Images

Figure CN119690447B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer technology, and in particular to a code style checking method and apparatus. Background Technology
[0002] During application development and maintenance, each developer has their own unique coding style. Within the same development company, different developers often collaborate on the same project. These differing styles inevitably reduce code readability and hinder subsequent maintenance. Therefore, code style checks are necessary to determine whether developers' code conforms to coding standards.
[0003] Currently, most commonly used automated code style checking tools are based on compiler principles. Some of the resources of code checking tools designed with this architecture are wasted on checking the syntax of the source code, resulting in long code style checking times and low efficiency. Summary of the Invention
[0004] This invention provides a code style checking method and apparatus to solve the problems of long checking time and low efficiency of existing automated code style checking tools.
[0005] This invention provides a code style checking method, which includes the following steps.
[0006] The source code to be inspected is converted into a lexical unit stream, which includes different types of lexical units that conform to the lexical rules corresponding to the source code.
[0007] Based on the syntax corresponding to the source code, a shift-reduce operation is performed on the lexical unit stream. During the shift-reduce operation, a binary tree is generated for the target statement output by the reduction, and the target statement is based on the syntax definition corresponding to the source code.
[0008] The target statement is examined based on the binary tree according to the rules defined in the specification to be examined.
[0009] According to a code style checking method provided by the present invention, a shift-reduce operation is performed on the lexical unit stream based on the syntax corresponding to the source code. During the shift-reduce operation, a binary tree is generated for the target statement output by the reduced operation, including the following steps.
[0010] An analysis table conforming to the syntax corresponding to the source code is generated. The analysis table is used to guide the shift and reduce operation. The analysis table is a two-dimensional table that records lexical units and shift and reduce states.
[0011] The lexical unit stream is moved sequentially into the lexical unit stack. After moving the current lexical unit, it jumps into the reduction state based on the parsing table. In the reduction state, statements that conform to the reduction syntax are output from the lexical unit stack.
[0012] Once the statement popped from the stack is identified as the target statement, a binary tree of the target statement is generated.
[0013] According to a code style checking method provided by the present invention, the source code to be checked is converted into a lexical unit stream, including the following steps.
[0014] The source code to be inspected is converted into a character stream.
[0015] Lexical analysis is performed on the character stream based on the lexical tag table to obtain the lexical unit stream. The lexical tag table records the types and matching rules of lexical units related to specification checks.
[0016] According to a code style checking method provided by the present invention, lexical analysis is performed on the character stream according to the lexical tag table to obtain the lexical unit stream, including the following steps.
[0017] The current character in the character stream and the strings already pushed into the character stack are combined to form the string to be matched.
[0018] The string to be matched is matched against the matching rules defined in the lexical tag table.
[0019] If a match is successful, the current character is input into the character stack; otherwise, the string in the stack is popped as a lexical unit, and the type of the popped lexical unit is marked. All popped lexical units form the lexical unit stream.
[0020] According to a code style checking method provided by the present invention, when the target statement is a basic statement, the root node of the binary tree is used to store the intermediate lexical unit of the basic statement, and the two leaf nodes respectively store the lexical units on both sides of the intermediate lexical unit in the basic statement. The basic statement is composed of three lexical units with the intermediate lexical unit being an operator.
[0021] When the target statement is a combined statement, the binary tree is an empty binary tree, or at least one leaf node of the binary tree is empty.
[0022] The root node of the empty binary tree stores the target pointer. The root node of the binary tree with at least one empty leaf node stores the intermediate lexical unit of the combined statement. The non-empty leaf nodes store the lexical unit on one side of the intermediate lexical unit in the combined statement. The empty leaf node stores the target pointer.
[0023] The target pointer points to a sub-binary tree, which is either a binary tree storing the basic statements that constitute the combined statement, or a binary tree storing the next level of combined statements that constitute the combined statement.
[0024] According to a code style checking method provided by the present invention, the target statement is checked based on the binary tree according to the rules defined in the style to be checked, including the following steps.
[0025] When the target statement corresponding to the binary tree is a basic statement, the statement category of the target statement is determined based on the type of the lexical unit stored in the root node of the binary tree, and the rule function corresponding to the statement category is called to check the target statement.
[0026] If the target statement corresponding to the binary tree is a composite statement and the root node of the binary tree is not empty, the statement category of the target statement is determined based on the type of the lexical unit stored in the root node, and the rule function corresponding to the statement category is called to check the target statement; or the sub-binary tree is located based on the target pointer stored in the empty leaf node of the binary tree, and the statement corresponding to the sub-binary tree is checked according to the rule function of the statement category of the target statement.
[0027] If the target statement corresponding to the binary tree is a combined statement and the binary tree is an empty binary tree, the sub-binary tree is located based on the target pointer stored in the root node of the empty binary tree, and the statement corresponding to the sub-binary tree is checked according to the rule function of the statement category of the target statement.
[0028] The rule function is the execution function corresponding to the rule defined in the specification to be inspected.
[0029] According to a code style checking method provided by the present invention, generating a binary tree for the target statement of the reduced output includes: generating a binary tree for the target statement of the reduced output when the statement category of the target statement of the reduced output is the statement category to be checked in the style to be checked.
[0030] The present invention also provides a code style checking device, comprising the following modules.
[0031] The lexical unit stream conversion module is used to convert the source code to be checked into a lexical unit stream, which includes different types of lexical units that conform to the lexical rules corresponding to the source code.
[0032] The binary tree generation module is used to perform shift and reduce operations on the lexical unit stream based on the syntax corresponding to the source code. During the shift and reduce operation, a binary tree is generated for the target statement output by the reduction, and the target statement is based on the syntax definition corresponding to the source code.
[0033] The specification inspection module is used to inspect the target statement based on the binary tree according to the rules defined in the specification to be inspected.
[0034] The present invention also provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor, when executing the program, implements the code style checking method as described above.
[0035] The present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the code style checking method as described above.
[0036] The code style checking method provided by this invention converts the source code to be checked into a lexical unit stream, which includes different types of lexical units that conform to the lexical rules corresponding to the source code. Based on the syntax corresponding to the source code, a shift-reduce operation is performed on the lexical unit stream. During the shift-reduce operation, a binary tree is generated for the target statements output by the reduction. The target statements are then checked based on the binary tree according to the rules defined in the style check. In this method, since a binary tree is generated only for the target statements reduced by syntax rules, the style check process based on the binary tree reduces the data storage volume, shortens the code style check time, and improves the style check efficiency compared to the traditional syntax tree. Attached Figure Description
[0037] To more clearly illustrate the technical solutions in this invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of this invention. For those skilled in the art, other drawings can be obtained from these drawings without creative effort.
[0038] Figure 1 This is one of the flowcharts of the code style checking method provided by the present invention.
[0039] Figure 2 This is a schematic diagram of the source code and its corresponding syntax in the code style checking method provided by this invention.
[0040] Figure 3 This is a flowchart illustrating the shift and reduction process for generating a binary tree in the code style checking method provided by this invention.
[0041] Figure 4 This is a schematic diagram of the binary tree structure constructed according to the shift-reduce process in the code style checking method provided by the present invention.
[0042] Figure 5This is a schematic diagram of the code style checking device provided by the present invention.
[0043] Figure 6 This is a schematic diagram of the structure of the electronic device provided by the present invention. Detailed Implementation
[0044] To make the objectives, technical solutions, and advantages of this invention clearer, the technical solutions of this invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some, not all, of the embodiments of this invention. All other embodiments obtained by those skilled in the art based on the embodiments of this invention without creative effort are within the scope of protection of this invention.
[0045] In existing related technologies, most commonly used automated code style checking tools are based on the architecture design of compiler principles. Code checking tools designed according to this architecture implement style checking based on abstract syntax trees. The main function of syntax trees is to be used for syntax checking. Their storage structure stores a lot of data related to syntax checking but not used for style checking. This means that in the process of style checking based on syntax trees, some resources are wasted on checking the syntax of the source code, which leads to increased data traversal costs, long code style checking time and low efficiency.
[0046] To address the aforementioned technical problems, this invention provides a code style checking method, such as... Figure 1 As shown, steps S110 to S130 are included.
[0047] Step S110: Convert the source code to be checked into a lexical unit stream. The lexical unit stream includes different types of lexical units that conform to the lexical rules corresponding to the source code. Conforming to the lexical rules corresponding to the source code means conforming to the lexical rules of the computer language corresponding to the source code. Different computer languages have different lexical rules, and the lexical unit stream also contains different types of lexical units. Lexical unit types typically include: variable, number, keyword, and operator lexical unit types.
[0048] Step S120: Based on the syntax corresponding to the source code, perform a shift-reduce operation on the lexical unit stream. During the shift-reduce operation, generate a binary tree for the target statement output by the reduction operation. The generated binary tree is used to store the corresponding target statement. The target statement is based on the syntax definition corresponding to the source code. The syntax definition of the source code defines the syntax of different statement categories in the source code. Statement categories include: judgment statements, conditional statements, execution statements, assignment statements, calculation statements, and comparison statements, etc. The syntax of each statement category guides the generation of the corresponding binary tree for the target statement.
[0049] like Figure 2As shown, taking the source code of if-else as an example, Figure 2 On the right is the syntax of each statement appearing in the if-else source code. Of course, the syntax of each statement also conforms to the syntax format of the computer language corresponding to the if-else source code. Figure 2 The syntax is defined for statement categories such as conditional statements, evaluation statements, execution statements, assignment statements, calculation statements, and comparison statements. For example, for comparison statements, the root node of the binary tree stores the comparison operator, and the two leaf nodes store the comparison operands. Both the comparison operator and the comparison operands are lexical units; the comparison operator is a lexical unit of operator type, and the comparison operands are lexical units of variable or numeric type.
[0050] Step S130: Check the target statement based on the binary tree according to the rules defined in the specification to be checked. The rules in the specification to be checked are defined based on actual conditions. For example, the data bit width of the two comparison operands in a comparison statement must be equal. Therefore, for the binary tree to be checked, obtain the data stored in the root node. If it is a comparison operator, then continue to obtain the comparison operands of the two leaf nodes and check whether the bit widths of the two comparison operands are equal. If they are not equal, it does not conform to the specification.
[0051] In this embodiment of the code style checking method, the source code to be checked is converted into a lexical unit stream, which includes different types of lexical units that conform to the lexical rules corresponding to the source code. Based on the syntax corresponding to the source code, a shift-reduce operation is performed on the lexical unit stream. During the shift-reduce operation, a binary tree is generated for the target statements output by the reduction. According to the rules defined in the style check, the target statements are checked based on the binary tree. Since a binary tree is generated only for the target statements reduced by syntax, the style check based on the binary tree reduces the data storage volume, shortens the code style check time, and improves the style check efficiency compared to the traditional syntax tree. Moreover, the information of the target statements stored in the binary tree is more conducive to developers' understanding and maintenance than the syntax tree (the structure of the syntax tree is mainly designed for computers), which can effectively reduce the maintenance cost of subsequent tools.
[0052] In some embodiments, after obtaining the lexical unit stream, a shift-reduce operation is performed on the lexical unit stream according to a predefined syntax. Each syntax corresponds to a reduction action, which is equivalent to defining the binary tree generation logic simultaneously when defining the syntax. Specifically, step S120 includes the following steps.
[0053] An analysis table conforming to the syntax corresponding to the source code is generated. This analysis table guides the shift-reduce operation. The analysis table is a two-dimensional table recording lexical units and shift-reduce states. Taking the assignment statement `output = dataa – datab` as an example, where I represents a variable, F represents a calculation statement, T represents an assignment statement, and # represents the end-of-line character (no data input), the assignment statement is: `I = II`. Figure 2 In the grammar rules, I=II corresponds to two grammars R1 and R2, where R1 is T:I=F and R2 is F:II. The generated parsing tables for grammars R1 and R2 are as follows: Figure 3 As shown, Figure 3 The table on the left is the analysis table.
[0054] The lexical unit stream is moved sequentially into the lexical unit stack. After moving the current lexical unit, it jumps into the reduction state based on the parsing table. In the reduction state, statements that conform to the reduction syntax are output from the lexical unit stack.
[0055] If the statement category of the popped statement is the statement category defined in the specification to be checked, the popped statement is determined to be the target statement, and a binary tree of the target statement is generated.
[0056] Specifically, such as Figure 3 As shown in the table on the right, for ease of understanding, a state stack is added during the shift-and-reduce process. The initial state is S0, meaning state 0 is pushed onto the state stack. At this point, no lexical units have been shifted, and the lexical unit stack is empty. When the input lexical unit is I, according to the definition of the parsing table, I is pushed onto the lexical unit stack and enters state S2, and state 2 is pushed onto the state stack. In state S2, when the input is =, = is pushed onto the lexical unit stack and enters state S3. Lexical units are shifted onto the lexical unit stack sequentially until state S7 is reached and the input is #, at which point the reduction action begins according to syntax R2. R2 is the syntax definition of the computation statement, used to guide the generation of the binary tree related to the computation statement. According to the definition of R2, the top data II of the lexical unit stack is popped, the input data is changed to F, and the popped data II is used as the root node with - and I as the left and right leaf nodes to generate the binary tree of the computation statement, which is then stored in the storage pool. The state stack simultaneously pops the three top states 4, 5, and 6. After the R2 reduction operation is completed, the system enters state S3, where the input data is F. According to the definition in the parsing table, F is pushed onto the stack, and the system enters state S5. At this point, according to the syntax R1 reduction, the top data I=F is popped from the lexical unit stack. The input data is changed to T, and the popped data I=F is used as the root node, I as the left leaf node, and F as the right leaf node to generate a binary tree for assignment statements. This binary tree is then stored in the memory pool. Here, F is a pointer to the binary tree of the computation statements. The final binary tree of the computation statements and the binary tree of the assignment statements are shown below. Figure 4 As shown. Afterwards, data T is pushed into the lexical unit stack, entering state S1. When the input continues to be #, the shift reduction ends and the lexical unit stack and state stack are cleared.
[0057] In some embodiments, step S110 specifically includes the following steps.
[0058] The source code to be inspected is converted into a character stream. In this step, the source code is read character by character, thereby converting the source code into a character stream.
[0059] Lexical analysis is performed on the character stream according to the lexical tag table to obtain the lexical unit stream. The lexical tag table records the types and matching rules of lexical units related to specification checking. In practical applications, a lexical analyzer of the computer language corresponding to the source code can also be directly used to perform lexical analysis on the character stream to obtain the lexical unit stream. However, this method involves a large amount of data for processing lexical units, especially keyword-type lexical units. In specification checking, it is not necessary to check the statements corresponding to certain keywords. Therefore, in this embodiment, the lexical tag table is redefined, which only records the types and matching rules of lexical units related to specification checking, reducing the amount of data for lexical analysis and improving the speed of lexical analysis. It can be understood that the lexical tag table in this embodiment is a subset of the complete lexical tag table in the lexical analyzer. For example: Figure 2 In the source code of if-else statements, the lexical unit types in the lexical tag table include: variables, numbers, keywords, and operators. Among them, the matching rule for variables is a random combination of letters, numbers, and non-reserved characters (characters other than reserved characters defined in computer languages). The matching rule for numbers is a random combination of 0-9. The matching rule for keywords is a set of keywords related to specification checks. For the source code of if-else statements, the set of keywords is {if, else}. The matching rule for operators is a set of operators related to specification checks. For the source code of if-else statements, the set of operators is {>, <, ==, =, +, -, ×, / , (, )}.
[0060] Specifically, the process of performing lexical analysis on the character stream based on the lexical tagging table to obtain the lexical unit stream includes the following steps.
[0061] The current character in the character stream and the string already pushed into the character stack are combined to form the string to be matched. If the current character is the first character of the source code, the string in the stack is empty.
[0062] The string to be matched is matched against the matching rules defined in the lexical tag table, that is, whether the string to be matched can match the lexical unit defined by the matching rules in the lexical tag table.
[0063] If a match is successful, the current character is input into the character stack; otherwise, the string in the stack is popped as a lexical unit, and the type of the popped lexical unit is marked. All popped lexical units form the lexical unit stream. A mismatch occurs when the current character, after being pushed onto the stack, cannot form a lexical unit according to the above matching rules. For example, if the current character is a terminator (such as a space or newline character).
[0064] For example: Figure 2 In the first case, the character "i" is pushed onto the stack and can be matched as a variable. Subsequent characters are pushed onto the stack. The character "f" is pushed onto the stack and can be matched as both a variable and the keyword "if". Subsequent characters are "(". Since the string "if(" cannot match the above matching rules, "if" is popped from the stack as a lexical unit. Because in computer languages, keywords have higher priority than variables in lexical unit matching rules, when both variable and keyword matching rules are satisfied, the type of the lexical unit "if" is prioritized as a keyword.
[0065] In some embodiments, when the target statement is a basic statement, the root node of the binary tree is used to store the intermediate lexical units of the basic statement, and the two leaf nodes respectively store the lexical units on both sides of the intermediate lexical unit in the basic statement. The basic statement consists of three lexical units whose intermediate lexical units are operators, for example: Figure 2 In the binary tree, comparison and calculation statements are used. For comparison statements, the root node stores the comparison operators (>, <, or ==), and the two leaf nodes store the two variables being compared.
[0066] When the target statement is a combined statement, the binary tree is an empty binary tree, and the root node of the empty binary tree stores the target pointer. Alternatively, at least one leaf node of the binary tree is empty, and the root node of the binary tree stores the intermediate lexical unit of the combined statement, the non-empty leaf nodes store the lexical unit on one side of the intermediate lexical unit in the combined statement, and the empty leaf node stores the target pointer.
[0067] The target pointer points to a sub-binary tree, which is either a binary tree storing the basic statements that constitute the combined statement, or a binary tree storing the next level of combined statements that constitute the combined statement.
[0068] Specifically, a compound statement consists of a base statement and / or a sub-level compound statement, for example: Figure 2Conditional statements (i.e., judgment conditions) consist of comparison statements or assignment statements. Comparison statements are basic statements, while assignment statements can be either basic statements or combined statements (below the conditional statement level). For conditional statements, the corresponding binary tree is an empty binary tree. The root node of this empty binary tree stores a target pointer, and the sub-binary tree pointed to by this target pointer is either the binary tree of the comparison statement or the binary tree of the assignment statement. For assignment statements, if the syntax of the assignment statement is: variable = calculation statement, then the root node of the binary tree corresponding to this assignment statement stores the intermediate lexical unit of the combined statement, i.e., "=". The left leaf nodes store the variable to the left of "=", and the right leaf nodes are empty leaf nodes. These empty leaf nodes store a target pointer, and the sub-binary tree pointed to by this target pointer is the binary tree of the calculation statement.
[0069] In this embodiment, regardless of the statement type, the binary tree is ultimately located at the binary tree of the basic statement. The storage of keywords such as if and else is omitted in the binary tree of the basic statement. Compared with the traditional syntax tree, the storage structure is simplified, which effectively shortens the traversal time during specification checks, thereby shortening the specification check time and improving the specification check efficiency.
[0070] In some embodiments, step S130 specifically includes the following steps.
[0071] When the target statement corresponding to the binary tree is a basic statement, the statement category of the target statement is determined based on the type of lexical units stored in the root node of the binary tree. The rule function corresponding to the statement category is then called to check the target statement. For example, for the currently checked binary tree, if the data stored in the root node is a comparison operator, the corresponding comparison statement is determined. A rule function corresponding to the comparison statement is then used to perform a specification check on the comparison statement. The rule function is the execution function corresponding to the rule defined in the specification to be checked. For example, for a comparison statement, the bit width of the two operands being compared must be equal. The rule function corresponding to the comparison statement will obtain the comparison operands from the two leaf nodes of the binary tree corresponding to the comparison statement, calculate the bit width of each operand, and if the bit widths are equal, it conforms to the specification; otherwise, it does not. Of course, each category of target statement can have multiple rule functions.
[0072] If the target statement corresponding to the binary tree is a composite statement and the root node of the binary tree is not empty, the statement category of the target statement is determined based on the type of the words stored in the root node, and the rule function corresponding to the statement category is called to check the target statement; or the sub-binary tree is located based on the target pointer stored in the empty leaf node of the binary tree, and the statement corresponding to the sub-binary tree is checked according to the rule function of the statement category of the target statement.
[0073] If the target statement corresponding to the binary tree is a combined statement and the binary tree is an empty binary tree, the sub-binary tree is located based on the target pointer stored in the root node of the empty binary tree, and the statement corresponding to the sub-binary tree is checked according to the rule function of the statement category of the target statement.
[0074] For example, if-else conditional statements are not allowed to contain assignment statements. During specification checks, when the corresponding rule function of the conditional statement is executed, it first locates the binary tree of the comparison statement or assignment statement from the target pointer in the root node of the empty binary tree of the conditional statement. If the binary tree of the comparison statement is located (i.e., the binary tree of the comparison statement is determined by the comparison operator stored in the root node of the sub-binary tree), then the conditional statement is considered to conform to the specification; if the binary tree of the assignment statement is located (i.e., the binary tree of the assignment statement is determined by the assignment operator stored in the root node of the sub-binary tree), then the conditional statement is considered to be non-compliant with the specification, and the non-compliant conditional statement is output.
[0075] In this embodiment, different rule functions can be dynamically constructed according to the requirements of the standard inspection, making it easier to add and modify the standard inspection function.
[0076] In some embodiments, step S120, generating a binary tree for the target statement output by reduction, includes: generating a binary tree for the target statement output by reduction if the statement category of the target statement output by reduction is the statement category to be checked in the specification to be checked. This avoids generating binary trees for all target statements defined in the syntax, while binary trees for some statements that do not need specification checking occupy storage space in the storage pool, thereby saving storage space.
[0077] In some embodiments, the root node of the binary tree is also used to store the statement category identifier of the target statement. After generating the binary tree, and before checking the target statements stored in the binary tree according to the rules defined in the specification to be checked, the method further includes: storing the binary tree according to the statement category of the target statement, with the binary trees corresponding to target statements with the same statement category identifier stored in the same storage block in the storage pool. That is, the corresponding binary trees are classified and stored according to statement category. During specification checking, specification checks can be performed on sets of target statements with the same category, further improving the efficiency of specification checking.
[0078] In some embodiments, when converting source code into a character stream, the line number of each character is simultaneously obtained. When performing lexical analysis on the character stream according to the lexical tag table, the line number of each lexical unit in the lexical unit stream is determined by the line number of the character that makes up the lexical unit, and the node storing the lexical unit in the generated binary tree stores the line number corresponding to the lexical unit. When performing a specification check on the target statement, if the check does not conform to the specification, the line number of the lexical unit stored in each node of the binary tree corresponding to the target statement is obtained. Then, while outputting the target statement, the line numbers of each lexical unit that makes up the target statement are also output to locate the position of the non-compliant statement in the source code.
[0079] The code style checking device provided by the present invention is described below. The code style checking device described below can be referred to in correspondence with the code style checking method described above.
[0080] The code style checking device of this invention embodiment, such as Figure 5 As shown, it includes the following modules.
[0081] Lexical unit stream conversion module 510 is used to convert the source code to be checked into a lexical unit stream, wherein the lexical unit stream includes different types of lexical units that conform to the lexical rules corresponding to the source code.
[0082] Binary tree generation module 520 is used to perform shift and reduce operations on the lexical unit stream based on the syntax corresponding to the source code. During the shift and reduce operation, a binary tree is generated for the target statement output by the reduction, and the target statement is based on the syntax definition corresponding to the source code.
[0083] The specification inspection module 530 is used to inspect the target statement based on the binary tree according to the rules defined in the specification to be inspected.
[0084] In the code style checking device of this embodiment, since a binary tree is generated only for the target statements reduced by syntax, the code style checking process based on the binary tree reduces the data storage volume, shortens the code style checking time, and improves the style checking efficiency compared to the traditional syntax tree. Moreover, the information of the target statements stored in the binary tree is more conducive to developers' understanding and maintenance than the syntax tree (the structure of the syntax tree is mainly designed for computers), which can effectively reduce the maintenance cost of subsequent tools.
[0085] In some embodiments, the binary tree generation module 520 is specifically used to generate an analysis table that conforms to the syntax corresponding to the source code, the analysis table being used to guide the shift-reduce operation, the analysis table being a two-dimensional table recording lexical units and shift-reduce states; shifting the lexical unit stream sequentially into the lexical unit stack, jumping into the reduce state based on the analysis table after shifting into the current lexical unit, and outputting statements that conform to the reduce syntax from the lexical unit stack in the reduce state; determining that the popped statement is the target statement, and generating a binary tree of the target statement.
[0086] In some embodiments, the lexical unit stream conversion module 510 includes the following modules.
[0087] The character stream conversion module is used to convert the source code to be inspected into a character stream.
[0088] The lexical analysis module is used to perform lexical analysis on the character stream according to the lexical tag table to obtain the lexical unit stream. The lexical tag table records the types and matching rules of lexical units related to specification checks.
[0089] In some embodiments, the lexical analysis module is specifically used to form a string to be matched by combining the current character in the character stream and the string already pushed into the character stack; to match the string to be matched with the matching rules defined in the lexical tag table; if the match is successful, to input the current character into the character stack; otherwise, to pop the string in the stack as a lexical unit and mark the type of the popped lexical unit; and all popped lexical units form the lexical unit stream.
[0090] In some embodiments, when the target statement is a basic statement, the root node of the binary tree is used to store the intermediate lexical unit of the basic statement, and the two leaf nodes respectively store the lexical units on both sides of the intermediate lexical unit in the basic statement. The basic statement consists of three lexical units with the intermediate lexical unit being an operator.
[0091] When the target statement is a combined statement, the binary tree is an empty binary tree, or at least one leaf node of the binary tree is empty.
[0092] The root node of the empty binary tree stores the target pointer. The root node of the binary tree with at least one empty leaf node stores the intermediate lexical unit of the combined statement. The non-empty leaf nodes store the lexical unit on one side of the intermediate lexical unit in the combined statement. The empty leaf node stores the target pointer.
[0093] The target pointer points to a sub-binary tree, which is either a binary tree storing the basic statements that constitute the combined statement, or a binary tree storing the next level of combined statements that constitute the combined statement.
[0094] In some embodiments, the specification checking module 530 is specifically used to: 1) determine the statement category of the target statement based on the type of lexical units stored in the root node of the binary tree when the target statement corresponding to the binary tree is a basic statement, and then call the rule function corresponding to the statement category to check the target statement; 2) determine the statement category of the target statement based on the type of lexical units stored in the root node when the target statement corresponding to the binary tree is a composite statement and the root node of the binary tree is not empty, and then call the rule function corresponding to the statement category to check the target statement; or 3) locate the sub-binary tree based on the target pointer stored in the empty leaf node of the binary tree, and check the statement corresponding to the sub-binary tree according to the rule function of the statement category of the target statement; 4) locate the sub-binary tree based on the target pointer stored in the root node of the empty binary tree when the target statement corresponding to the binary tree is a composite statement and the binary tree is an empty binary tree, and then check the statement corresponding to the sub-binary tree according to the rule function of the statement category of the target statement. Wherein, the rule function is the execution function corresponding to the rule defined in the specification to be checked.
[0095] In some embodiments, the binary tree generation module 520 is specifically used to generate a binary tree for the target statement of the reduced output when the statement category of the target statement is the statement category to be checked in the specification to be checked.
[0096] Figure 6 An example is a schematic diagram of the physical structure of an electronic device, such as... Figure 6 As shown, the electronic device may include a processor 610, a communications interface 620, a memory 630, and a communication bus 640, wherein the processor 610, the communications interface 620, and the memory 630 communicate with each other via the communication bus 640. The processor 610 can call logical instructions in the memory 630 to execute a code style checking method, which includes the following steps.
[0097] The source code to be inspected is converted into a lexical unit stream, which includes different types of lexical units that conform to the lexical rules corresponding to the source code.
[0098] Based on the syntax corresponding to the source code, a shift-reduce operation is performed on the lexical unit stream. During the shift-reduce operation, a binary tree is generated for the target statement output by the reduction, and the target statement is based on the syntax definition corresponding to the source code.
[0099] The target statement is examined based on the binary tree according to the rules defined in the specification to be examined.
[0100] Furthermore, the logical instructions in the aforementioned memory 630 can be implemented as software functional units and, when sold or used as independent products, can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, or a part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of the present invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0101] On the other hand, the present invention also provides a computer program product, the computer program product including a computer program, the computer program being stored on a non-transitory computer-readable storage medium, and when the computer program is executed by a processor, the computer is able to execute the code style checking method provided by the above methods, the method including the following steps.
[0102] The source code to be inspected is converted into a lexical unit stream, which includes different types of lexical units that conform to the lexical rules corresponding to the source code.
[0103] Based on the syntax corresponding to the source code, a shift-reduce operation is performed on the lexical unit stream. During the shift-reduce operation, a binary tree is generated for the target statement output by the reduction, and the target statement is based on the syntax definition corresponding to the source code.
[0104] The target statement is examined based on the binary tree according to the rules defined in the specification to be examined.
[0105] In another aspect, the present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, is implemented to perform the code style checking methods provided by the above methods, the method comprising the following steps.
[0106] The source code to be inspected is converted into a lexical unit stream, which includes different types of lexical units that conform to the lexical rules corresponding to the source code.
[0107] Based on the syntax corresponding to the source code, a shift-reduce operation is performed on the lexical unit stream. During the shift-reduce operation, a binary tree is generated for the target statement output by the reduction, and the target statement is based on the syntax definition corresponding to the source code.
[0108] The target statement is examined based on the binary tree according to the rules defined in the specification to be examined.
[0109] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs. Those skilled in the art can understand and implement this without any creative effort.
[0110] Through the above description of the embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by means of software plus necessary general-purpose hardware platforms, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solutions, in essence or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, magnetic disk, optical disk, etc., and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute the methods described in the various embodiments or some parts of the embodiments.
[0111] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, and not to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A code specification checking method characterized by, include: The source code to be inspected is converted into a lexical unit stream, which includes different types of lexical units that conform to the lexical code corresponding to the source code. Based on the syntax corresponding to the source code, a shift-reduce operation is performed on the lexical unit stream. During the shift-reduce operation, a binary tree is generated for the target statement output by the reduction. The target statement is based on the syntax definition corresponding to the source code. The target statement is examined based on the binary tree according to the rules defined in the specification to be examined; When the target statement is a basic statement, the root node of the binary tree is used to store the intermediate lexical unit of the basic statement, and the two leaf nodes respectively store the lexical units on both sides of the intermediate lexical unit in the basic statement. The basic statement consists of three lexical units with the intermediate lexical unit being an operator. When the target statement is a combined statement, the binary tree is an empty binary tree, or at least one leaf node of the binary tree is empty; The root node of the empty binary tree stores the target pointer. The root node of the binary tree with at least one empty leaf node stores the intermediate lexical unit of the combined statement. The non-empty leaf node stores the lexical unit on one side of the intermediate lexical unit in the combined statement. The empty leaf node stores the target pointer. The target pointer points to a sub-binary tree, which is either a binary tree that stores the basic statements that constitute the combined statement, or a binary tree that stores the next level of combined statements that constitute the combined statement. The process of examining the target statement based on the binary tree includes: When the target statement corresponding to the binary tree is a basic statement, the statement category of the target statement is determined based on the type of lexical unit stored in the root node of the binary tree, and the rule function corresponding to the statement category is called to check the target statement. If the target statement corresponding to the binary tree is a composite statement and the root node of the binary tree is not empty, the statement category of the target statement is determined based on the type of the lexical unit stored in the root node, and the rule function corresponding to the statement category is called to check the target statement; or the sub-binary tree is located based on the target pointer stored in the empty leaf node of the binary tree, and the statement corresponding to the sub-binary tree is checked according to the rule function of the statement category of the target statement. If the target statement corresponding to the binary tree is a combined statement and the binary tree is an empty binary tree, the sub-binary tree is located based on the target pointer stored in the root node of the empty binary tree, and the statement corresponding to the sub-binary tree is checked according to the rule function of the statement category of the target statement. The rule function is the execution function corresponding to the rule defined in the specification to be inspected.
2. The code specification checking method according to claim 1, wherein Based on the syntax corresponding to the source code, a shift-reduce operation is performed on the lexical unit stream. During the shift-reduce operation, a binary tree is generated from the target statement output by the reduced operation, including: An analysis table conforming to the syntax corresponding to the source code is generated. The analysis table is used to guide the shift and reduce operation. The analysis table is a two-dimensional table that records lexical units and shift and reduce states. The lexical unit stream is moved into the lexical unit stack in sequence. After moving into the current lexical unit, the jump is made into the reduction state based on the parsing table. In the reduction state, the statement that conforms to the reduction syntax is output from the lexical unit stack. Once the statement popped from the stack is identified as the target statement, a binary tree of the target statement is generated.
3. The code specification checking method according to claim 1, wherein, The source code to be inspected is converted into a lexical unit stream, including: Convert the source code to be inspected into a character stream; Lexical analysis is performed on the character stream based on the lexical tag table to obtain the lexical unit stream. The lexical tag table records the types and matching rules of lexical units related to specification checks.
4. The code specification checking method according to claim 3, characterized by, Lexical analysis is performed on the character stream based on the lexical tag table to obtain the lexical unit stream, including: The current character in the character stream and the strings already pushed into the character stack are combined to form the string to be matched; Match the string to be matched against the matching rules defined in the lexical tag table; If a match is successful, the current character is input into the character stack; otherwise, the string in the stack is popped as a lexical unit, and the type of the popped lexical unit is marked. All popped lexical units form the lexical unit stream.
5. The code specification checking method according to any one of claims 1 to 4, characterized by, The step of generating a binary tree from the target statement of the reduction output includes: generating a binary tree from the target statement of the reduction output when the statement category of the target statement of the reduction output is the statement category to be checked in the specification to be checked.
6. A code specification checking apparatus characterized by comprising: include: The lexical unit stream conversion module is used to convert the source code to be checked into a lexical unit stream, wherein the lexical unit stream includes different types of lexical units that conform to the lexical code corresponding to the source code. The binary tree generation module is used to perform shift and reduce operations on the lexical unit stream based on the syntax corresponding to the source code. During the shift and reduce operation, a binary tree is generated for the target statement output by the reduction, and the target statement is based on the syntax definition corresponding to the source code. The specification checking module is used to check the target statement based on the binary tree according to the rules defined in the specification to be checked. When the target statement is a basic statement, the root node of the binary tree is used to store the intermediate lexical unit of the basic statement, and the two leaf nodes respectively store the lexical units on both sides of the intermediate lexical unit in the basic statement. The basic statement consists of three lexical units with the intermediate lexical unit being an operator. When the target statement is a combined statement, the binary tree is an empty binary tree, or at least one leaf node of the binary tree is empty; The root node of the empty binary tree stores the target pointer. The root node of the binary tree with at least one empty leaf node stores the intermediate lexical unit of the combined statement. The non-empty leaf node stores the lexical unit on one side of the intermediate lexical unit in the combined statement. The empty leaf node stores the target pointer. The target pointer points to a sub-binary tree, which is either a binary tree that stores the basic statements that constitute the combined statement, or a binary tree that stores the next level of combined statements that constitute the combined statement. Specifically, the specification checking module is used as follows: when the target statement corresponding to the binary tree is a basic statement, it determines the statement category of the target statement based on the type of lexical units stored in the root node of the binary tree, and calls the rule function corresponding to the statement category to check the target statement; when the target statement corresponding to the binary tree is a composite statement and the root node of the binary tree is not empty, it determines the statement category of the target statement based on the type of lexical units stored in the root node, and calls the rule function corresponding to the statement category to check the target statement; or it locates the sub-binary tree based on the target pointer stored in the empty leaf node of the binary tree, and checks the statement corresponding to the sub-binary tree according to the rule function of the statement category of the target statement; when the target statement corresponding to the binary tree is a composite statement and the binary tree is an empty binary tree, it locates the sub-binary tree based on the target pointer stored in the root node of the empty binary tree, and checks the statement corresponding to the sub-binary tree according to the rule function of the statement category of the target statement; wherein, the rule function is the execution function corresponding to the rule defined in the specification to be checked.
7. An electronic device comprising a memory, a processor, and a computer program stored on the memory and executable on the processor, characterized in that, When the processor executes the computer program, it implements the code style checking method as described in any one of claims 1 to 5.
8. A non-transitory computer-readable storage medium having stored thereon a computer program, characterized in that, When the computer program is executed by a processor, it implements the code style checking method as described in any one of claims 1 to 5.
Citation Information
Patent Citations
Method and device for generating syntax tree of code file and electronic equipment
CN111381814A