Non-deterministic finite automaton generation method, device and equipment and storage medium
Patent Information
- Application Number
- CN202410164488.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-02-05
- Publication Date
- 2026-10-09
- Estimated Expiration
- 2044-02-05
AI Technical Summary
[0004]本发明提供一种非确定型有穷自动机生成方法、装置、设备及存储介质,用以解决现有技术中NFA运行效率较低的技术问题
[0032] The present invention provides a method, apparatus, device, and storage medium for generating a nondeterministic finite automaton (NFA). After creating a corresponding state for a character to be processed, an identification identifier is added to the state. The character to be processed is a special character in a regular expression to be processed. After generating the NFA for the regular expression to be processed, a start state and an end state are added. Based on the start state and/or the end state, the state with the identification identifier is modified. The start state is used to mark the starting position when matching using the NFA, and the end state is used to mark the ending position when matching using the NFA. By introducing a special set of states—a start state marking the beginning of a match and an end state marking the end of a match—the states with identification marks are corrected after the NFA is built. The introduction of start and end state lists ensures good consistency in the NFA, effectively reducing unnecessary overhead caused by inconsistencies in the NFA graph and improving its efficiency. Furthermore, by marking the start of the matching content with the start state, the matching engine, upon learning that all data has begun, processes the start state to activate it. This triggering mechanism based on the start state node attributes avoids unnecessary matching overhead and further enhances the NFA's performance.
Smart Images

Figure CN118796314B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of automata technology, specifically to a method, apparatus, device, and storage medium for generating nondeterministic finite automata. Background Technology
[0002] Regular expressions and finite state automata are important research areas in computer science, with wide applications in numerous fields. For example, in computational biology, regular expressions and finite state automata are used for protein sequencing; in information retrieval and communication, they are used for protocol identification and analysis, and security attack detection; in real-time systems, they are applied to simulation and modeling; and in artificial intelligence, they are used for text recognition and handwriting recognition.
[0003] In existing technical implementations, regular expression matching libraries such as RE2 and Hyperscan support a subset of most of the standard PCRE syntax and support multi-rule matching. However, in terms of constructing non-deterministic finite automata (NFA) with special symbols, there is a problem of low NFA running efficiency due to inconsistencies in the NFA graph. Summary of the Invention
[0004] This invention provides a method, apparatus, device, and storage medium for generating nondeterministic finite automata (NFAs) to solve the technical problem of low operating efficiency in existing NFAs.
[0005] This invention provides a method for generating nondeterministic finite automata, comprising:
[0006] After creating a corresponding state for the character to be processed, an identification identifier is added to the state. The character to be processed is a special character in the regular expression to be processed.
[0007] After generating the nondeterministic finite automaton (NFA) of the regular expression to be processed, a start state and an end state are added. Based on the start state and / or the end state, the state with the identification mark is modified. The start state is used to mark the start position when matching with the NFA, and the end state is used to mark the end position when matching with the NFA.
[0008] According to a method for generating a nondeterministic finite automaton (NFA) based on the present invention, when the character to be processed is a first character, the first character is a character used to indicate the end of a line. After generating the NFA of the regular expression to be processed, a start state and an end state are added. Based on the start state and / or the end state, the state with the identification identifier is modified, including:
[0009] After generating the nondeterministic finite automaton (NFA) of the regular expression to be processed, a new end state and an end state are added, and the predecessor node corresponding to the first character is updated to the end state.
[0010] If the first character does not have a corresponding successor node, then after updating the state of the first character to the accept state, the newline character is taken as the successor node corresponding to the first character.
[0011] According to a method for generating a nondeterministic finite automaton provided by the present invention, when the character to be processed is a second character, the second character is a character used to indicate the beginning of a line. In multi-line mode, after generating the nondeterministic finite automaton (NFA) of the regular expression to be processed, a start state and an end state are added. Based on the start state and / or the end state, the state with the identification identifier is modified, including:
[0012] When there is no newline character before the second character in the regular expression to be processed, replace the second character with a newline character;
[0013] If a newline character precedes the second character in the regular expression to be processed, the second character is removed from the regular expression to be processed.
[0014] After generating a nondeterministic finite automaton (NFA) from the updated regular expression to be processed, a start state is added.
[0015] A new first node is added, and the successor node corresponding to the newline character is taken as the successor node of the first node. The state of the first node is set to the start state.
[0016] According to a method for generating a nondeterministic finite automaton provided by the present invention, when the character to be processed is a second character, in non-multiline mode, after generating the nondeterministic finite automaton (NFA) of the regular expression to be processed, a start state and an end state are added, and the state with the identification identifier is modified based on the start state and / or the end state, including:
[0017] Remove the second character from the regular expression to be processed;
[0018] After generating a nondeterministic finite automaton (NFA) from the updated regular expression to be processed, a start state is added.
[0019] Add a second node, take the successor node corresponding to the second character as the successor node of the second node, and set the state of the second node to the start state.
[0020] According to a method for generating a nondeterministic finite automaton (NFA) provided by the present invention, when the character to be processed is a third character, the third character is a character used to indicate word boundaries. If the third character is at the end of the regular expression to be processed, after generating the NFA of the regular expression to be processed, a start state and an end state are added. Based on the start state and / or the end state, the state with the identification identifier is modified, including:
[0021] After generating the nondeterministic finite automaton (NFA) of the regular expression to be processed, a termination state and a third node are added.
[0022] When the character of the predecessor node corresponding to the third character is a first type character, the third node is matched with a second type character;
[0023] When the character of the predecessor node corresponding to the third character is a second type of character, the third node matches the first type of character;
[0024] Update the state of the predecessor node to the termination state.
[0025] According to a method for generating a nondeterministic finite automaton provided by the present invention, when the character to be processed is a third character, if the third character is not at the end of the regular expression to be processed, after generating the nondeterministic finite automaton (NFA) of the regular expression to be processed, a connection is established between the complementary node pairs between the predecessor node corresponding to the third character and the successor node corresponding to the third character, and the node corresponding to the third character is removed. The complementary node pair is a pair of nodes whose characters are of the first type of character and the second type of character, respectively.
[0026] The present invention also provides a nondeterministic finite automaton generation apparatus, comprising:
[0027] The identification module is used to: after creating a corresponding state for the character to be processed, add an identification identifier to the state, wherein the character to be processed is a special character in the regular expression to be processed;
[0028] The correction module is used to: after generating the nondeterministic finite automaton (NFA) of the regular expression to be processed, add a start state and an end state, and correct the state with the identification mark based on the start state and / or the end state, wherein the start state is used to mark the start position when matching with the NFA, and the end state is used to mark the end position when matching with the NFA.
[0029] 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 nondeterministic finite automaton generation method as described above.
[0030] 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 nondeterministic finite automaton generation method as described above.
[0031] The present invention also provides a computer program product, including a computer program that, when executed by a processor, implements the nondeterministic finite automaton generation method as described above.
[0032] The present invention provides a method, apparatus, device, and storage medium for generating a nondeterministic finite automaton (NFA). After creating a corresponding state for a character to be processed, an identification identifier is added to the state. The character to be processed is a special character in a regular expression to be processed. After generating the NFA for the regular expression to be processed, a start state and an end state are added. Based on the start state and / or the end state, the state with the identification identifier is modified. The start state is used to mark the starting position when matching using the NFA, and the end state is used to mark the ending position when matching using the NFA. By introducing a special set of states—a start state marking the beginning of a match and an end state marking the end of a match—the states with identification marks are corrected after the NFA is built. The introduction of start and end state lists ensures good consistency in the NFA, effectively reducing unnecessary overhead caused by inconsistencies in the NFA graph and improving its efficiency. Furthermore, by marking the start of the matching content with the start state, the matching engine, upon learning that all data has begun, processes the start state to activate it. This triggering mechanism based on the start state node attributes avoids unnecessary matching overhead and further enhances the NFA's performance. Attached Figure Description
[0033] To more clearly illustrate the technical solutions in this application 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 application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0034] Figure 1 This is a flowchart illustrating the method for generating nondeterministic finite automata provided by the present invention.
[0035] Figure 2 This is one of the structural schematic diagrams of the NFA provided by the present invention;
[0036] Figure 3 This is the second structural schematic diagram of the NFA provided by the present invention;
[0037] Figure 4 This is the third structural schematic diagram of the NFA provided by the present invention;
[0038] Figure 5 This is the fourth structural schematic diagram of the NFA provided by the present invention;
[0039] Figure 6 This is the fifth structural schematic diagram of the NFA provided by the present invention;
[0040] Figure 7 This is the sixth structural schematic diagram of the NFA provided by the present invention;
[0041] Figure 8 This is the seventh structural schematic diagram of the NFA provided by the present invention;
[0042] Figure 9 This is the eighth schematic diagram of the NFA structure provided by the present invention;
[0043] Figure 10 This is the ninth structural schematic diagram of the NFA provided by the present invention;
[0044] Figure 11 This is a schematic diagram of the structure of the nondeterministic finite automaton generation device provided by the present invention;
[0045] Figure 12 This is a schematic diagram of the structure of the electronic device provided by the present invention. Detailed Implementation
[0046] To make the objectives, technical solutions, and advantages of this application clearer, the technical solutions of this application will be clearly and completely described below with reference to the accompanying drawings of the embodiments. Obviously, the described embodiments are only some embodiments of this application, not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0047] Figure 1 This is a flowchart illustrating the method for generating nondeterministic finite automata provided by the present invention. (Refer to...) Figure 1 This application provides a method for generating a nondeterministic finite automaton, which may include:
[0048] S110, after creating a corresponding state for the character to be processed, add an identification identifier to the state, wherein the character to be processed is a special character in the regular expression to be processed.
[0049] S120, after generating the nondeterministic finite automaton (NFA) of the regular expression to be processed, a start state and an end state are added. Based on the start state and / or the end state, the state with the identification mark is modified. The start state is used to mark the start position when matching with the NFA, and the end state is used to mark the end position when matching with the NFA.
[0050] It should be noted that the execution subject of the nondeterministic finite automaton generation method provided in this application embodiment can be a server, computer device, such as a tablet computer, laptop computer, handheld computer, ultra-mobile personal computer (UMPC), netbook, or personal digital assistant (PDA), etc.
[0051] The NFA generation method provided by this invention is a method for generating Glushkov automata based on regular expressions.
[0052] Here, special characters are characters with special meanings in regular expressions, such as ^, $, \b, etc.
[0053] Alternatively, the Glushkov NFA can be constructed as follows:
[0054] (1) Set the regular expressions (sets) to be analyzed, given in string form, and add them to the patterns of NFABuilder using the addRule method of NFABuilder. NFABuilder is the main class to be called and the external interface class, which is responsible for receiving the rule set, merging the rule set, and constructing the complete Glushkov NFA. The addRule method is the default method for adding patterns.
[0055] For example:
[0056] NFABuilder nb;
[0057] StringPiece pattern="(ad|0)f{3,300}";
[0058] StringPiece pattern2="ea|bc[AZ]+?efg";
[0059] StringPiece pattern3="a|bd[\\d]?";
[0060] nb.addRule(pattern,1);
[0061] Each regular expression corresponds to a rule ID. The withDOTBegin flag can be set according to the needs of the NFA engine to determine whether to add the (.*) flag before the regular expression.
[0062] (2) Call nb.buildGlushkovNFA() to construct the Glushkov NFA corresponding to the given rule set:
[0063] ① Call the RE2 regular expression parser based on the number of rules passed in:
[0064] If it is a single rule, directly call Regexp::Parse to analyze it, get a Regexp (regular expression syntax tree structure) instance, and save it to nb.all;
[0065] If there are multiple rules, each rule calls Regexp::Parse for analysis, and all results are stored in the subs array. Then, Regexp::AlternateNoFactor is used to integrate them into a single Regexp instance, which is then saved to nb.all. This is equivalent to integrating multiple rules into one rule through an "OR" operation. However, the advantage of this approach is that the ID of each rule can be recorded.
[0066] ② Based on the obtained Regexp object all, call DumpRegexpGlushkov to convert the regular expression syntax tree into a binary tree form and record the NFA state list:
[0067] NFABuildState*root=DumpRegexpGlushkov(all);
[0068] The main task of DumpRegexpGlushkov is to construct the NFABuildState (a binary regular expression syntax tree, constructed from Regexp, to determine the numbering of subsequent Glushkov NFA states) node types based on the type of the root node of the current subexpression syntax tree. If the current subexpression is a regular expression operator (including *, +, ?, |, bounded repetition, etc.), DumpRexpGlushkov is recursively called to calculate the left and right subtree nodes of the current root node, and the operator nodes are constructed using the makeOpNode function. If the current subexpression is a simple character, the makeLeafNode function is called to construct the leaf nodes. Finally, the root node of the regular expression binary syntax tree is returned.
[0069] Thus, the syntax structure of the regular expression was obtained, and the list of all states was recorded in nfa (done by the makeLeafNode function).
[0070] ③ The binary tree structure obtained in the previous step retains the positions of the symbols in the original regular expression, and each position here is used to generate the state nodes required by the Glushkov NFA. Next, this binary tree structure is used in conjunction with the Glushkov algorithm to construct the edge set and accepting state set of the NFA:
[0071] set <int>P = computeP(root); / / Calculate all starting nodes;
[0072] set <int>D = computeD(root); / / Calculate the terminal node;
[0073] bool isnullable = compute_A(root); / / Determine if an empty string is acceptable;
[0074] set <pair<int,int> >F = computeF(root); / / Construct the edge set.
[0075] The core of the Glushkov algorithm is to calculate the above four sets: isnullable indicates whether the regular expression accepts an empty string, set P records the first set of characters that the regular expression can match (corresponding to the NFA state number), set D records the last set of characters that the regular expression can match (corresponding to the accepting state set), and set F is the order relationship between characters (corresponding to the order between states, which is an order pair).
[0076] Based on the Glushkov construction method, these four sets are used as follows:
[0077] a. If isnullable is true, set the default state 0 to the accepting state;
[0078] b. Set all states appearing in set D to accept states;
[0079] c. Establish a transition relationship (edge) between the initial state 0 and all states in set P;
[0080] d. Establish an edge between all state pairs appearing in set F;
[0081] e. If withDOTBegin is true, an edge from the initial state to itself needs to be added;
[0082] f. All accepted states are stored in nfa.final_states, and all edges are stored in nfa.edges.
[0083] Based on the above NFA construction method, for the regular expression (AB|CD)*AFF*, the following is generated: Figure 2 The NFA shown;
[0084] Given the rule sets StringPiece pattern1 = "ea|bc[AZ]+?efg" and StringPiece pattern2 = "a|bd[\\d]?", generate the following: Figure 3 The NFA shown is in which acceptance states 2 and 8 correspond to pattern 1, and acceptance states 9 and 12 correspond to pattern 2.
[0085] In common regular expression syntax such as PRCE, there are many special-purpose markers, such as ^, $, \A, \z, \b, \B, etc., as well as some syntaxes that change local settings (?x), including (?i), (?m), etc. These special symbols mainly serve as positional matching functions and do not match specific symbol content in the actual regular expression matching process. This conflicts with the construction principle of Glushkov NFA and requires additional processing during the construction process.
[0086] During the construction of the regular expression syntax tree, RE2 treats them all as zero-ary operators, i.e., operators that do not require operands. The syntax tree node names corresponding to the above tags are shown in Table 1.
[0087] Table 1. Definitions of Special Characters
[0088]
[0089]
[0090] For example, the initial semantics of $ refers to the position of the end of the line, and it does not match the \n or \r\n at the end of the line. To solve this problem, a special set of states is introduced, using the start state SoD (Start of Data) and the end state EoD (End of Data) to mark the start and end positions of the matched content, respectively, to correct the NFA.
[0091] Specifically, after completing the Glushkov NFA construction, based on the identification identifier, the corresponding special symbol is obtained to create the corresponding state; if the created state has no successor, the created state is modified to an accepting state (EoD state); if the created state is connected to all subsequent states, the created state is used as the initial state (SoD state), and the matching engine, knowing that all data is currently being processed, additionally processes the SoD state to activate it; or, after completing the Glushkov NFA construction, the node corresponding to the special symbol is retrieved to obtain the predecessor and successor of that node; the state is corrected based on whether there are \w and \W relationships between the predecessor and successor.
[0092] The nondeterministic finite automaton generation method provided in this application involves creating a corresponding state for a character to be processed, adding an identification identifier to the state, wherein the character to be processed is a special character in the regular expression to be processed; after generating the nondeterministic finite automaton (NFA) for the regular expression to be processed, adding a start state and an end state, and modifying the state with the identification identifier based on the start state and / or the end state, wherein the start state is used to mark the start position when matching using the NFA, and the end state is used to mark the end position when matching using the NFA. By introducing a special set of states—a start state marking the beginning of a match and an end state marking the end of a match—the states with identification marks are corrected after the NFA is built. The introduction of start and end state lists ensures good consistency in the NFA, effectively reducing unnecessary overhead caused by inconsistencies in the NFA graph and improving its efficiency. Furthermore, by marking the start of the matching content with the start state, the matching engine, upon learning that all data has begun, processes the start state to activate it. This triggering mechanism based on the start state node attributes avoids unnecessary matching overhead and further enhances the NFA's performance.
[0093] In an optional embodiment, when the character to be processed is a first character, the first character is a character used to indicate the end of a line. After generating the nondeterministic finite automaton (NFA) of the regular expression to be processed, a start state and an end state are added. Based on the start state and / or the end state, the state with the identification identifier is modified, including:
[0094] After generating the nondeterministic finite automaton (NFA) of the regular expression to be processed, a new end state and an end state are added, and the predecessor node corresponding to the first character is updated to the end state.
[0095] If the first character does not have a corresponding successor node, then after updating the state of the first character to the accept state, the newline character is taken as the successor node corresponding to the first character.
[0096] Specifically, during the parsing phase, the first character ($ and \z) are both treated as normal matchable character nodes, and corresponding NFA states are created. However, an identification mark EOT is made. After the entire NFA is constructed, the states with the EOT mark are found from the state set, and the state edges are corrected and the EoD state list is added according to the following principles:
[0097] Set all predecessor states of the node containing $ or \z to EoD state. If the state corresponding to $ or \z has no successor, change the state to accept state and set the character to \n.
[0098] For example< / [Ht][Tt][Mm][Ll]> \s*$ matches a tag that is not followed by any other non-whitespace character, and the constructed NFA is as follows: Figure 4 As shown, states 7 and 8 are both added to the EoD list.
[0099] Understandably, by using EoD states, support for complex regular expression syntax is achieved while maintaining good consistency of the NFA graph.
[0100] In an optional embodiment, when the character to be processed is a second character, the second character is a character used to indicate the beginning of a line. In multi-line mode, after generating the nondeterministic finite automaton (NFA) of the regular expression to be processed, a start state and an end state are added. Based on the start state and / or the end state, the state with the identification identifier is modified, including:
[0101] When there is no newline character before the second character in the regular expression to be processed, replace the second character with a newline character;
[0102] If a newline character precedes the second character in the regular expression to be processed, the second character is removed from the regular expression to be processed.
[0103] After generating a nondeterministic finite automaton (NFA) from the updated regular expression to be processed, a start state is added.
[0104] A new first node is added, and the successor node corresponding to the newline character is taken as the successor node of the first node. The state of the first node is set to the start state.
[0105] The second character (^ and \A) has the same meaning in single-line mode. ^ indicates the beginning of a line, and \A indicates the beginning of a string. Similar to $, ^ and \A only indicate position and do not match specific characters. Therefore, the Start of Data (SoD) state is introduced to mark the beginning of the matched content. The specific processing method for multi-line mode is as follows:
[0106] Expression parsing is achieved by adding a newline character (\n) before the ^ symbol (if the original expression already has a newline character before ^, it is not necessary to add it). The ^ symbol itself is removed from the expression. The original state node after ^ (excluding the added newline node) is not directly connected to the initial state (such as state 0). The added newline node is connected to the original state node after ^ and to the initial state 0 before it. At the same time, an extra start state is added and placed in the start state set. This extra state is not connected to the initial state 0 before it, but is connected to all states immediately following ^ (excluding the added newline node). The matching engine needs to process the SoD state and activate it after knowing that all data is currently being processed.
[0107] For example, the NFA of the regular expression (?m)^(ab|12)efg$ is as follows: Figure 5 As shown.
[0108] In an optional embodiment, when the character to be processed is a second character, in non-multiline mode, after generating the nondeterministic finite automaton (NFA) of the regular expression to be processed, a start state and an end state are added. Based on the start state and / or the end state, the state with the identification identifier is modified, including:
[0109] Remove the second character from the regular expression to be processed;
[0110] After generating a nondeterministic finite automaton (NFA) from the updated regular expression to be processed, a start state is added.
[0111] Add a second node, take the successor node corresponding to the second character as the successor node of the second node, and set the state of the second node to the start state.
[0112] Specifically, in non-multiline mode, only an extra start state needs to be added and placed in the SoD state set; no additional characters like \n are needed. This extra state is not connected to the initial state 0 before it, and is connected to all states immediately following ^ (excluding the extra \n node). ^ is removed from the expression, but the original state node after ^ is not directly connected to the initial state (such as state 0). The rest of the matching engine handles it the same way. \A is handled the same way as ^ in non-multiline mode.
[0113] For example, the NFAs of the regular expressions ^(ab|12)efg$ and \A(ab|12)efg$ in non-multiline mode are the same, such as Figure 6 As shown.
[0114] Understandably, by introducing SoD states, support for complex regular expression syntax is achieved while maintaining good consistency of the NFA graph. At the same time, the triggering mechanism based on SoD node attributes avoids unnecessary matching overhead and can effectively improve the runtime performance of NFA.
[0115] In an optional embodiment, when the character to be processed is a third character, the third character is a character used to indicate word boundaries. If the third character is at the end of the regular expression to be processed, after generating the nondeterministic finite automaton (NFA) of the regular expression to be processed, a start state and an end state are added. Based on the start state and / or the end state, the state with the identification identifier is modified, including:
[0116] After generating the nondeterministic finite automaton (NFA) of the regular expression to be processed, a termination state and a third node are added.
[0117] When the character of the predecessor node corresponding to the third character is a first type character, the third node is matched with a second type character;
[0118] When the character of the predecessor node corresponding to the third character is a second type of character, the third node matches the first type of character;
[0119] Update the state of the predecessor node to the termination state.
[0120] When the character to be processed is the third character, if the third character is not at the end of the regular expression to be processed, then after generating the nondeterministic finite automaton (NFA) of the regular expression to be processed, a connection is established between the complementary node pairs between the predecessor node corresponding to the third character and the successor node corresponding to the third character, and the node corresponding to the third character is removed. The complementary node pair is a pair of nodes whose characters are of the first type and the second type, respectively.
[0121] The third character (\b) indicates a word boundary and does not match any character (zero-width). The PCRE syntax specification for \b stipulates that the characters before and after it cannot both match \w (Class 1 characters, matching letters, numbers, and underscores. Equivalent to '[A-Za-z0-9_]') or \W (Class 2 characters, matching non-letters, numbers, and underscores. Equivalent to '[^A-Za-z0-9_]'), or it requires the first character to match \w at the beginning of the string and the last character to match \w at the end of the string.
[0122] Treat \b as a normal character node for parsing. Based on the parsing result, set a state position. After the NFA is built, retrieve the node corresponding to \b to find its predecessor and successor. Adjustments are made based on the position of \b and the character types of its preceding and following states.
[0123] If \b is at the end of the regular expression, we need to consider adding a virtual state whose character set matches the character set of the predecessor with \w and \W respectively, and then treat the predecessor node as an EoD state.
[0124] For example, the NFA for the regular expression (ab|12,)\b is as follows: Figure 7 As shown.
[0125] If \b is not at the end, then based on the predecessor and successor of \b, establish edges for states where there are \w and \W relationships between the predecessor and successor, and then remove the \b node.
[0126] For example, the NFA for the regular expression (ab|12,)\b(\s|efg) is as follows: Figure 8 As shown.
[0127] As can be seen from the generated NFA, the semantic requirements of \b have been reflected in the state transition relations. For example, states 2 and 7 are associated, but states 2 and 8 are not associated because the characters in state 2 are \w content, state 7 is \W content, and state 8 is also \w content. Similarly, state 5 is associated with state 8, but not with state 7.
[0128] For example, the NFA for the regular expression (ab|12,)\b\d* is as follows: Figure 9 As shown.
[0129] In this example, \b might be at the end, so two virtual nodes \W and \w need to be added, along with an EoD state.
[0130] In addition, if \b appears at the beginning of the regular expression, a node \W needs to be appended before it, and SoD needs to be set to the first character.
[0131] For example, the NFA for the regular expression \b(ab|12,)\b\d* is as follows: Figure 10 As shown.
[0132] In summary, the nondeterministic finite automaton generation method provided by this invention supports the Gluskov NFA construction method for complex regular expression syntax $, \z, ^, \A, \b, and \B, which can maintain good consistency of the NFA graph. This can effectively save unnecessary additional checking overhead caused by the inconsistency of the NFA graph during NFA runtime, improve the running efficiency of NFA, and achieve lightweight, simple, and efficient operation without adding too many additional data structures.
[0133] The nondeterministic finite automaton generation apparatus provided in the embodiments of this application is described below. The nondeterministic finite automaton generation apparatus described below and the nondeterministic finite automaton generation method described above can be referred to in correspondence.
[0134] Figure 11 This is a schematic diagram of the nondeterministic finite automaton generation device provided by the present invention, as shown below. Figure 11 As shown, the nondeterministic finite automaton generation apparatus includes:
[0135] The identification module 1110 is used to: after creating a corresponding state for the character to be processed, add an identification identifier to the state, wherein the character to be processed is a special character in the regular expression to be processed;
[0136] The correction module 1120 is used to: after generating the nondeterministic finite automaton (NFA) of the regular expression to be processed, add a start state and an end state, and correct the state with the identification mark based on the start state and / or the end state, wherein the start state is used to mark the start position when matching with the NFA, and the end state is used to mark the end position when matching with the NFA.
[0137] Figure 12 An example is a schematic diagram of the physical structure of an electronic device, such as... Figure 12 As shown, the electronic device may include: a processor 1210, a communication interface 1220, a memory 1230, and a communication bus 1240, wherein the processor 1210, the communication interface 1220, and the memory 1230 communicate with each other via the communication bus 1240. The processor 1210 can call a computer program in the memory 1230 to execute the steps of a nondeterministic finite automaton generation method, such as including:
[0138] After creating a corresponding state for the character to be processed, an identification identifier is added to the state. The character to be processed is a special character in the regular expression to be processed.
[0139] After generating the nondeterministic finite automaton (NFA) of the regular expression to be processed, a start state and an end state are added. Based on the start state and / or the end state, the state with the identification mark is modified. The start state is used to mark the start position when matching with the NFA, and the end state is used to mark the end position when matching with the NFA.
[0140] Furthermore, the logical instructions in the aforementioned memory 1230 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 this application, 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 this application. 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.
[0141] On the other hand, the present invention also provides a computer program product, which includes a computer program that can be stored on a non-transitory computer-readable storage medium. When the computer program is executed by a processor, the computer can execute the nondeterministic finite automaton generation method provided by the above methods. The method includes: after creating a corresponding state for a character to be processed, adding an identification identifier to the state, wherein the character to be processed is a special character in a regular expression to be processed; after generating a nondeterministic finite automaton (NFA) for the regular expression to be processed, adding a start state and an end state; and modifying the state with the identification identifier based on the start state and / or the end state, wherein the start state is used to mark the start position when matching using the NFA, and the end state is used to mark the end position when matching using the NFA.
[0142] In another aspect, the present invention also provides a non-transitory computer-readable storage medium storing a computer program thereon, which, when executed by a processor, implements a method for generating a nondeterministic finite automaton provided by the methods described above. This method includes: after creating a corresponding state for a character to be processed, adding an identification identifier to the state, wherein the character to be processed is a special character in a regular expression to be processed; after generating a nondeterministic finite automaton (NFA) for the regular expression to be processed, adding a start state and an end state; and modifying the state with the identification identifier based on the start state and / or the end state, wherein the start state is used to mark the start position when matching using the NFA, and the end state is used to mark the end position when matching using the NFA.
[0143] The non-transitory computer-readable storage medium can be any available medium or data storage device that the processor can access, including but not limited to magnetic memory (e.g., floppy disk, hard disk, magnetic tape, magneto-optical disk (MO)), optical memory (e.g., CD, DVD, BD, HVD), and semiconductor memory (e.g., ROM, EPROM, EEPROM, non-volatile memory (NAND FLASH), solid-state drive (SSD)).
[0144] 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.
[0145] 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.
[0146] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of this application, and are not intended to limit them. Although this application 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. Such 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 this application.< / int> < / int>
Claims
1. A method for generating nondeterministic finite automata, characterized in that, include: After creating a state for the character to be processed, an identification identifier is added to the state, where the character to be processed is a special character in the regular expression to be processed; The characters to be processed include a first character, a second character, and a third character. The first character is used to indicate the end of a line, the second character is used to indicate the beginning of a line, and the third character is used to indicate word boundaries. When the character to be processed is the first character, after generating the nondeterministic finite automaton (NFA) of the regular expression to be processed, an end state is added, and the predecessor node corresponding to the first character is set as the end state. The end state is used to mark the end position when matching using the NFA. If the first character does not have a corresponding successor node, the state of the first character is updated to the accept state, and the newline character is set as the successor node corresponding to the first character. When the character to be processed is the second character, the second character is removed from the regular expression to be processed; after generating a nondeterministic finite automaton (NFA) for the updated regular expression to be processed, a start state is added, which is used to mark the starting position when matching using the NFA; a second node is added, the successor node corresponding to the second character is taken as the successor node of the second node, and the state of the second node is set to the start state; When the character to be processed is a third character, if the third character is at the end of the regular expression to be processed, after generating the nondeterministic finite automaton (NFA) of the regular expression to be processed, an end state is added, and a third node is added; when the character of the predecessor node corresponding to the third character is a first type of character, the third node matches a second type of character; when the character of the predecessor node corresponding to the third character is a second type of character, the third node matches a first type of character; the state of the predecessor node is updated to the end state; wherein, the first type of character is a character that matches letters, numbers, and underscores, and the second type of character is a character that matches characters other than letters, numbers, and underscores.
2. The method for generating nondeterministic finite automata according to claim 1, characterized in that, When the character to be processed is the second character, in multiline mode, if there is no newline character before the second character in the regular expression to be processed, a newline character is added before the second character before removing the second character from the regular expression to be processed.
3. The method for generating nondeterministic finite automata according to claim 1, characterized in that, When the character to be processed is the third character, if the third character is not at the end of the regular expression to be processed, then after generating the nondeterministic finite automaton (NFA) of the regular expression to be processed, a connection is established between the complementary node pairs between the predecessor node corresponding to the third character and the successor node corresponding to the third character, and the node corresponding to the third character is removed. The complementary node pair is a pair of nodes whose characters are of the first type and the second type, respectively.
4. A nondeterministic finite automaton generation device, characterized in that, include: The identification module is used to: after creating a corresponding state for the character to be processed, add an identification identifier to the state, wherein the character to be processed is a special character in the regular expression to be processed; The correction module is used to: when the character to be processed is the first character, after generating the nondeterministic finite automaton (NFA) of the regular expression to be processed, add an end state, set the predecessor node corresponding to the first character as the end state, and the end state is used to mark the end position when matching using the NFA; if the first character does not have a corresponding successor node, then after updating the state of the first character to the accept state, set the newline character as the successor node corresponding to the first character; When the character to be processed is the second character, the second character is removed from the regular expression to be processed; after generating a nondeterministic finite automaton (NFA) for the updated regular expression to be processed, a start state is added, which is used to mark the starting position when matching using the NFA; a second node is added, the successor node corresponding to the second character is taken as the successor node of the second node, and the state of the second node is set to the start state; When the character to be processed is a third character, if the third character is at the end of the regular expression to be processed, after generating the nondeterministic finite automaton (NFA) of the regular expression to be processed, an end state is added, and a third node is added; when the character of the predecessor node corresponding to the third character is a first type of character, the third node matches a second type of character; when the character of the predecessor node corresponding to the third character is a second type of character, the third node matches a first type of character; the state of the predecessor node is updated to the end state; wherein, the first type of character is a character that matches letters, numbers, and underscores, and the second type of character is a character that matches characters other than letters, numbers, and underscores.
5. An electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the program, it implements the nondeterministic finite automaton generation method as described in any one of claims 1 to 3.
6. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the nondeterministic finite automaton generation method as described in any one of claims 1 to 3.
7. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, it implements the nondeterministic finite automaton generation method as described in any one of claims 1 to 3.
Citation Information
Patent Citations
Finite automaton generating method of regular expression with wildcards
CN104407849A
Regular expression engine construction method and device, storage medium and equipment
CN114896469A