public NFA(string regexp)
        {
            var ops = new Stack <int>();

            _regx   = regexp.ToCharArray();
            _states = _regx.Length;
            _graph  = new Digraph(_states + 1);

            for (var i = 0; i < _states; i++)
            {
                var lp = i;
                if (_regx[i] == '(' || _regx[i] == '|')
                {
                    ops.Push(i);
                }
                else if (_regx[i] == ')')
                {
                    var or = ops.Pop();
                    if (_regx[or] == '|')
                    {
                        lp = ops.Pop();
                        _graph.AddEdge(lp, or + 1);
                        _graph.AddEdge(or, i);
                    }
                    else
                    {
                        lp = or;
                    }
                }

                if (i < _states - 1 && _regx[i + 1] == '*')
                {
                    _graph.AddEdge(lp, i + 1);
                    _graph.AddEdge(i + 1, lp);
                }

                if (_regx[i] == '(' || _regx[i] == '*' || _regx[i] == ')')
                {
                    _graph.AddEdge(i, i + 1);
                }
            }
        }
Example #2
0
        public NFA(string regexp)
        {
            Stack <int> ops = new Stack <int>();

            _re = regexp.ToCharArray();
            M   = _re.Length;
            G   = new Digraph(M + 1);

            for (int i = 0; i < M; i++)
            {
                int lp = i;
                if (_re[i] == '(' || _re[i] == '|')
                {
                    ops.Push(i);
                }
                else if (_re[i] == ')')
                {
                    int or = ops.Pop();
                    if (_re[or] == '|')
                    {
                        lp = ops.Pop();
                        G.AddEdge(lp, or + 1);
                        G.AddEdge(or, i);
                    }
                    else
                    {
                        lp = or;
                    }
                }
                if (i < M - 1 && _re[i + 1] == '*')
                {
                    G.AddEdge(lp, i + 1);
                    G.AddEdge(i + 1, lp);
                }
                if (_re[i] == '(' || _re[i] == '*' || _re[i] == ')')
                {
                    G.AddEdge(i, i + 1);
                }
            }
        }
 public DirectedDFS(Digraph graph, int size)
 {
     _marked = new bool[graph.GetVertices()];
     DFS(graph, size);
 }
Example #4
0
 public DirectedDFS(Digraph g, int s)
 {
     _marked = new bool[g.V()];
     Dfs(g, s);
 }