C # lexical Analyzer (iv) constructs NFA

Source: Internet
Author: User
Tags definition character classes expression list of character classes regular expression

With the regular expression obtained in the previous section, you can use it to construct an NFA. The NFA can easily be converted from regular expressions and also helps to understand the pattern represented by regular expressions.

First, the NFA representation method

Here, an NFA has at least two states: the first state and the tail state, as shown in Figure 1, the NFA corresponding to the regular expression T is N (t), its first state is H, and the tail state is T. The diagram shows only two states, the other states and transitions between States are not represented, because in the recursive algorithm described below, only need to know the first and last state of the NFA, other information does not need to care.

Figure 1 An NFA representation

I use the following NFA class to represent an NFA that contains only the first state, the tail state, and a method that adds a new state.

Namespace Cyjb.Compilers.Lexers {    
    class Nfa:ilist<nfastate> {    
        //Gets or sets the first state of the NFA.    
        nfastate headstate {get; set;}    
        Gets or sets the tail state of the NFA.    
        nfastate tailstate {get; set;}    
        Creates a new state in the current NFA.    
        nfastate newstate () {}}}    

In the state of the NFA, there are only three required attributes: Symbol index, State transition, and state type. Only the symbolic index of the accepted state is meaningful, which indicates which regular expression corresponds to the current accepted state and is set to 1 for other states.

A state transition represents how to move from the current state to the next state, although in the definition of NFA, each node may contain multiple $\epsilon$ transitions and multiple character transfers (that is, the transfer of characters on the edge). But here, there is at most one character transfer, which is determined by the characteristics of the NFA constructor algorithm given later.

The state type is defined to support a forward-looking symbol, which may be one of the three enumerated values of Normal, Trailinghead, and trailing, which is described in detail in the section dealing with forward-looking symbols.

The following is the definition of the Nfastate class:

Namespace Cyjb.Compilers.Lexers {    
    class Nfastate {    
        //Get the NFA that contains the current state.    
        NFA NFA;    
        Gets the index of the current state.    
        int Index;    
        Gets or sets the symbolic index of the current state.    
        int symbolindex;    
        Gets or sets the type of the current state.    
        Nfastatetype Statetype;    
        Gets a list of character classes that correspond to the transfer of the character class.    
        iset<int> charclasstransition;    
        Gets the target state of the character class transfer.    
        nfastate Charclasstarget;    
        Gets  the collection of transfers.    
        ilist<nfastate> epsilontransitions;    
        Adds a transition to a specific state.    
        void Add (nfastate State, Char ch);    
        Adds a transition to a specific state.    
        void Add (nfastate state, String charclass);    
        Adds an ε transfer to a specific state.    
        void Add (nfastate state);    
    }    

The two attributes I have added to the Nfastate class Nfa and Index are purely for the convenience of state use. $\epsilon$ transfers are defined directly as a list, while character transfers are defined as two attributes: Charclasstarget and Charclasstransition,charclasstarget represent the target State, Charclasstransition represents a character class, which is explained in detail below.

The Nfastate class also defines three add methods, which are used to add a single character transfer, a character class transfer, and a $\epsilon$ transfer.

Related Article

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.