예제 #1
0
        // checks if the given text matches the regexp
        // if it does return true (its possible to reach state M in the NFA)

        // Logic:
        // 1) create a list of states that are reachable from 0 : ps
        // 2) create a list of states that are reachable after processing text.CharAt(i) : matches
        // 3) update ps with list of states that are reachable from matches
        // 4) if ever PS has a state that is the last state, then the given text matches the regexp
        public bool Matches(string text)
        {
            List <int>  ps  = new List <int>();
            DirectedDFS dfs = new DirectedDFS(G, 0);

            // states reachable from start by e-transitions
            for (int i = 0; i < G.Vertices(); i++)
            {
                if (dfs.marked(i))
                {
                    ps.Add(i);
                }
            }

            for (int i = 0; i < text.Length; i++)
            {
                List <int> match = new List <int>();

                // states reachable after scanning past txt.charAt(i)
                foreach (int v in ps)
                {
                    if (v == M)
                    {
                        return(true);
                    }
                    if (re[v] == text[i] || re[v] == '.')                                   // since '.' indicates any character, it is considered a match
                    {
                        match.Add(v + 1);
                    }
                }

                dfs = new DirectedDFS(G, match);
                ps  = new List <int>();
                for (int v = 0; v < G.Vertices(); v++)
                {
                    if (dfs.marked(v))
                    {
                        ps.Add(v);
                    }
                }
            }

            foreach (int v in ps)
            {
                if (v == M)
                {
                    return(true);
                }
            }

            return(false);
        }