Example #1
0
        /// <summary>
        /// 根据当前的正则表达式构造 NFA。
        /// </summary>
        /// <param name="nfa">要构造的 NFA。</param>
        internal override void BuildNfa(Nfa nfa)
        {
            NfaState head     = nfa.NewState();
            NfaState tail     = nfa.NewState();
            NfaState lastHead = head;
            // 如果没有上限,则需要特殊处理。
            int times = maxTimes == int.MaxValue ? minTimes : maxTimes;

            if (times == 0)
            {
                // 至少要构造一次。
                times = 1;
            }
            for (int i = 0; i < times; i++)
            {
                innerExp.BuildNfa(nfa);
                lastHead.Add(nfa.HeadState);
                if (i >= minTimes)
                {
                    // 添加到最终的尾状态的转移。
                    lastHead.Add(tail);
                }
                lastHead = nfa.TailState;
            }
            // 为最后一个节点添加转移。
            lastHead.Add(tail);
            // 无上限的情况。
            if (maxTimes == int.MaxValue)
            {
                // 在尾部添加一个无限循环。
                nfa.TailState.Add(nfa.HeadState);
            }
            nfa.HeadState = head;
            nfa.TailState = tail;
        }
Example #2
0
        /// <summary>
        /// 根据当前的正则表达式构造 NFA。
        /// </summary>
        /// <param name="nfa">要构造的 NFA。</param>
        internal override void BuildNfa(Nfa nfa)
        {
            NfaState head = nfa.NewState();
            NfaState tail = nfa.NewState();

            left.BuildNfa(nfa);
            head.Add(nfa.HeadState);
            nfa.TailState.Add(tail);
            right.BuildNfa(nfa);
            head.Add(nfa.HeadState);
            nfa.TailState.Add(tail);
            nfa.HeadState = head;
            nfa.TailState = tail;
        }
Example #3
0
        private static DfaMatcher MakeMatcher()
        {
            // a?[ab]
            var first = new NfaState();
            var last  = new NfaState();
            var end   = new NfaState();

            var a1 = Atom.From('a');
            var a2 = Atom.From('a', 'b');

            first.Add(a1, last);
            first.Add(last);
            last.Add(a2, end);

            var nfa = new Nfa(first, end);

            return(new DfaMatcher(nfa.ToDfa().Minimize()));
        }
Example #4
0
            public static Nfa Single(Atom terminal)
            {
                var start = new NfaState();
                var end   = new NfaState();

                start.Add(terminal, end);

                return(new Nfa(start, end));
            }
        /// <summary>
        /// 根据当前的正则表达式构造 NFA。
        /// </summary>
        /// <param name="nfa">要构造的 NFA。</param>
        internal override void BuildNfa(Nfa nfa)
        {
            left.BuildNfa(nfa);
            NfaState head = nfa.HeadState;
            NfaState tail = nfa.TailState;

            right.BuildNfa(nfa);
            tail.Add(nfa.HeadState);
            nfa.HeadState = head;
        }
Example #6
0
        private static DfaMatcher MakeMatcher()
        {
            // [ab]?[abc][ab]?
            var first  = new NfaState();
            var center = new NfaState();
            var last   = new NfaState();
            var end    = new NfaState();

            var ab = Atom.From('a', 'b');
            var ac = Atom.From('a', 'c');

            first.Add(ab, center);
            first.Add(center);
            center.Add(ac, last);
            last.Add(ab, end);
            last.Add(end);

            var nfa = new Nfa(first, end);

            return(new DfaMatcher(nfa.ToDfa().Minimize()));
        }
Example #7
0
 /// <summary>
 /// 根据当前的正则表达式构造 NFA。
 /// </summary>
 /// <param name="nfa">要构造的 NFA。</param>
 internal override void BuildNfa(Nfa nfa)
 {
     innerExp.BuildNfa(nfa);
     if (TrailingExpression != null)
     {
         NfaState head = nfa.HeadState;
         TrailingHeadState = nfa.TailState;
         TrailingExpression.BuildNfa(nfa);
         TrailingHeadState.Add(nfa.HeadState);
         nfa.HeadState = head;
         TrailingHeadState.StateType = NfaStateType.TrailingHead;
         nfa.TailState.StateType     = NfaStateType.Trailing;
     }
 }
Example #8
0
        public void TransformShouldCreateDfa()
        {
            var state1 = new NfaState();
            var state2 = new NfaState();
            var state3 = new NfaState();

            state1.Add(Atom.From('1'), state2);
            state2.Add(Atom.From('2'), state3);
            state3.Add(Atom.From('3'), state1);

            var nfa = new Nfa(state1, state3);

            var dfa = nfa.ToDfa().Minimize();

            Assert.IsNotNull(dfa);
            Assert.AreEqual(3, new DfaPlumber(dfa).StateCount);
        }