Construct ε-NFA from a regular expression

Source: Internet
Author: User
Tags repetition

From vczh

?

1: Character Set

??

The character set is the most basic element of a regular expression. Therefore, the character set is reflected in the status chart and is also the basic element of the status chart. For character set C, if a rule only accepts C, the status chart corresponding to this rule will be constructed in the following form:

??

??

The initial status of the status chart is start and end. The START Status reads character set C and jumps to the end status. Other character sets are not accepted.

??

2: Series

??

If we use a ⊙ B to indicate the concatenation of Rule A and rule B, we can easily know the concatenation of this operation, that is, (a ⊙ B) ⊙ c = A ⊙ (B ⊙ C ). Therefore, for the series of N rules, we only need to first concatenate the first n-1 rules, then regard the obtained rules as a whole, and connect them with the last rule, then we get the concatenation of all rules. If we know how to concatenate two rules, we know how to concatenate n rules.

??

To convert two series rules into a state chart, we only need to first convert the two rules into a state chart, and then let the end state of the first State jump to the start state of the second state chart. This jump must be a jump that does not read characters, which is equivalent to the two states. Therefore, when the first status chart jumps to the end state, it can be regarded as the start state of the second status chart and continue the second rule check. Therefore, we use the ε edge to connect two state diagrams:

??

3: Parallel

??

The method of parallel connection is similar to that of serial connection. To read a character in the starting state, we need to first construct the status chart of all the branches so that we can know which branches are connected in parallel and jump, connect the Starting Status to the starting status of all branches. In addition, after a branch successfully accepts a string, in order to make the end status of the status chart reflect the end status of the entire status chart, we also connect the end status of all branches to the end status of the rule. As follows:

??

4: repeated

??

For a single repeat, we can set up two states. The first state is the starting state, and the second State is the ending state. When the status ends, if you encounter a string that can be accepted by the rule, return to the end status again. In this way, you can use a status chart to represent repetition. For repetition, we can construct a state chart as follows:

??

5: Optional

??

Creating a status chart for optional operations is relatively simple. To complete the optional operations, we need to take the status chart of the current rule if the string prefix is accepted by the current rule when accepting a single character, if the subsequent rules of the Optional Rules accept strings, the status chart of the subsequent rules is taken. If both rules are accepted, both charts are taken. To achieve this goal, we connect the starting and ending states of the rule's status chart, and obtain the following state chart:

??

??

If you repeatedly use more than 0 duplicates, that is, adding optional results to the original duplicates, you can simply remove the start status in Figure 4.4, enable the end state to have both the start state and end state roles. The [start] and [end] roles remain unchanged.

??

So far, we have mapped the five methods for constructing the state chart to the five methods for constructing rules. For any regular expression, we only need to restore this expression to the nesting of the five structures, and then construct each step corresponding to the construction of a state chart, you can convert a regular expression into an ε-NFA.

??

??

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.