private static INfa Any()
        {
            var start = new NfaState();
            var end   = new NfaState();

            start.AddTransistion(new TerminalNfaTransition(new AnyTerminal(), end));
            return(new Nfa(start, end));
        }
        private static INfa Empty()
        {
            var start = new NfaState();
            var end   = new NfaState();

            start.AddTransistion(new NullNfaTransition(end));
            return(new Nfa(start, end));
        }
Ejemplo n.º 3
0
        private static Nfa Any()
        {
            var start = new NfaState();
            var end   = new NfaState();

            start.AddTransition(AnyTerminal.Instance, end);
            return(new Nfa(start, end));
        }
Ejemplo n.º 4
0
        private static Nfa KleenePlus(Nfa nfa)
        {
            var end = new NfaState();

            nfa.End.AddEpsilon(end);
            nfa.End.AddEpsilon(nfa.Start);
            return(new Nfa(nfa.Start, end));
        }
Ejemplo n.º 5
0
        private static Nfa Empty()
        {
            var start = new NfaState();
            var end   = new NfaState();

            start.AddEpsilon(end);
            return(new Nfa(start, end));
        }
        private static INfa KleenePlus(INfa nfa)
        {
            var end = new NfaState();

            nfa.End.AddTransistion(new NullNfaTransition(end));
            nfa.End.AddTransistion(new NullNfaTransition(nfa.Start));
            return(new Nfa(nfa.Start, end));
        }
Ejemplo n.º 7
0
            public static Nfa Single(Atom terminal)
            {
                var start = new NfaState();
                var end   = new NfaState();

                start.Add(terminal, end);

                return(new Nfa(start, end));
            }
Ejemplo n.º 8
0
        public override Nfa GenerateNfa(bool ignoreCase)
        {
            if (!transformed)
            {
                if (Options.getIgnoreCase() || ignoreCase)
                {
                    ToCaseNeutral();
                    SortDescriptors();
                }

                if (Negated)
                {
                    RemoveNegation();                     // This also sorts the list
                }
                else
                {
                    SortDescriptors();
                }
            }

            if (descriptors.Count == 0 && !Negated)
            {
                CSharpCCErrors.SemanticError(this, "Empty character set is not allowed as it will not match any character.");
                return(new Nfa());
            }

            transformed = true;
            Nfa      retVal     = new Nfa();
            NfaState startState = retVal.Start;
            NfaState finalState = retVal.End;
            int      i;

            for (i = 0; i < descriptors.Count; i++)
            {
                if (descriptors[i] is SingleCharacter)
                {
                    startState.AddChar(((SingleCharacter)descriptors[i]).Character);
                }
                else                 // if (descriptors.get(i) instanceof CharacterRange)
                {
                    CharacterRange cr = (CharacterRange)descriptors[i];

                    if (cr.Left == cr.Right)
                    {
                        startState.AddChar(cr.Left);
                    }
                    else
                    {
                        startState.AddRange(cr.Left, cr.Right);
                    }
                }
            }

            startState.next = finalState;

            return(retVal);
        }
Ejemplo n.º 9
0
        private static Nfa Character(RegexCharacterClassCharacter character, bool negate)
        {
            var start    = new NfaState();
            var end      = new NfaState();
            var terminal = CreateTerminalForCharacter(character.Value, character.IsEscaped, negate);

            start.AddTransition(terminal, end);

            return(new Nfa(start, end));
        }
        private static INfa Optional(INfa nfa)
        {
            var start = new NfaState();
            var end   = new NfaState();

            start.AddTransistion(new NullNfaTransition(nfa.Start));
            start.AddTransistion(new NullNfaTransition(end));
            nfa.End.AddTransistion(new NullNfaTransition(end));
            return(new Nfa(start, end));
        }
Ejemplo n.º 11
0
        private static Nfa Optional(Nfa nfa)
        {
            var start = new NfaState();
            var end   = new NfaState();

            start.AddEpsilon(nfa.Start);
            start.AddEpsilon(end);
            nfa.End.AddEpsilon(end);
            return(new Nfa(start, end));
        }
Ejemplo n.º 12
0
        /// <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;
        }
Ejemplo n.º 13
0
        public static Nfa EmulateTrivial(char character)
        {
            NfaBuilder builder = new NfaBuilder();
            NfaState   head    = builder.CreateState();
            NfaState   tail    = builder.CreateState();

            Transition.Create(head, tail, character);

            return(builder.Create(head, tail));
        }
        private static NfaState[] CreateStates(int count)
        {
            var states = new NfaState[count];

            for (int i = 0; i < states.Length; i++)
            {
                states[i] = new EntityNfaState(i);
            }
            return(states);
        }
Ejemplo n.º 15
0
 private int DFAStateNumber(NfaState state)
 {
     for (var i = 0; i < m_dfa.Count; ++i)
     {
         if (m_dfa[i].NFAStates.SetEquals(state.NFAStates))
         {
             return(i);
         }
     }
     return(-1);
 }
Ejemplo n.º 16
0
        public void NfaClosuresFromSameSetShouldBeEqual()
        {
            var state1 = new NfaState();
            var state2 = new NfaState();

            var sut1 = new NfaClosure(new[] { state1, state2 }, state1);
            var sut2 = new NfaClosure(new[] { state2, state1 }, state1);

            Assert.IsTrue(sut1.Equals(sut2));
            Assert.AreEqual(sut1.GetHashCode(), sut1.GetHashCode());
        }
        private static INfa Character(RegexCharacter character)
        {
            var start      = new NfaState();
            var end        = new NfaState();
            var terminal   = new CharacterTerminal(character.Value);
            var transition = new TerminalNfaTransition(
                terminal: terminal,
                target: end);

            start.AddTransistion(transition);
            return(new Nfa(start, end));
        }
Ejemplo n.º 18
0
        public void NfaClosureShouldNewWithEmptyStates()
        {
            var state = new NfaState();

            var sut = new NfaClosure(Enumerable.Empty <NfaState>(), state);

            Assert.IsNotNull(sut);
            Assert.IsNotNull(sut.Set);
            Assert.AreEqual(0, sut.Set.Count);
            Assert.IsNotNull(sut.State);
            Assert.AreEqual(false, sut.State.IsFinal);
        }
Ejemplo n.º 19
0
        public void NfaClosureShouldNewWithOneState()
        {
            var state = new NfaState();

            var sut = new NfaClosure(Enumerable.Repeat(state, 1), state);

            Assert.IsNotNull(sut);
            Assert.IsNotNull(sut.Set);
            Assert.AreEqual(1, sut.Set.Count);
            Assert.IsNotNull(sut.State);
            Assert.AreEqual(true, sut.State.IsFinal);
        }
Ejemplo n.º 20
0
        public NfaFragment Visit(CharSetNode node)
        {
            var s = new NfaState(node.Characters);

            return(new NfaFragment
            {
                Start = s,
                Outs = new List <List <NfaState> > {
                    s.Out
                }
            });
        }
Ejemplo n.º 21
0
        public void NfaClosureShouldNewWithTwoStates()
        {
            var state1 = new NfaState();
            var state2 = new NfaState();

            var sut = new NfaClosure(new [] { state1, state2 }, state1);

            Assert.IsNotNull(sut);
            Assert.IsNotNull(sut.Set);
            Assert.AreEqual(2, sut.Set.Count);
            Assert.IsNotNull(sut.State);
            Assert.AreEqual(true, sut.State.IsFinal);
        }
Ejemplo n.º 22
0
        public INfa Union(INfa nfa)
        {
            var newStart = new NfaState();
            var newEnd = new NfaState();

            newStart.AddTransistion(new NullNfaTransition(Start));
            newStart.AddTransistion(new NullNfaTransition(nfa.Start));

            End.AddTransistion(new NullNfaTransition(newEnd));
            nfa.End.AddTransistion(new NullNfaTransition(newEnd));

            return new Nfa(newStart, newEnd);
        }
Ejemplo n.º 23
0
        public Nfa Union(Nfa other)
        {
            var newStart = new NfaState();
            var newEnd   = new NfaState();

            newStart.AddEpsilon(Start);
            newStart.AddEpsilon(other.Start);

            End.AddEpsilon(newEnd);
            other.End.AddEpsilon(newEnd);

            return(new Nfa(newStart, newEnd));
        }
Ejemplo n.º 24
0
        public Nfa Kleene()
        {
            var newStart = new NfaState();
            var newEnd   = new NfaState();

            newStart.AddEpsilon(Start);
            newStart.AddEpsilon(newEnd);

            newEnd.AddEpsilon(Start);
            End.AddEpsilon(newEnd);

            return(new Nfa(newStart, newEnd));
        }
Ejemplo n.º 25
0
        private static Nfa Union(Nfa first, Nfa second)
        {
            var start = new NfaState();
            var end   = new NfaState();

            start.AddEpsilon(first.Start);
            start.AddEpsilon(second.Start);

            first.End.AddEpsilon(end);
            second.End.AddEpsilon(end);

            return(new Nfa(start, end));
        }
Ejemplo n.º 26
0
        public INfa Union(INfa nfa)
        {
            var newStart = new NfaState();
            var newEnd   = new NfaState();

            newStart.AddTransistion(new NullNfaTransition(Start));
            newStart.AddTransistion(new NullNfaTransition(nfa.Start));

            End.AddTransistion(new NullNfaTransition(newEnd));
            nfa.End.AddTransistion(new NullNfaTransition(newEnd));

            return(new Nfa(newStart, newEnd));
        }
Ejemplo n.º 27
0
        public INfa Kleene()
        {
            var newStart = new NfaState();
            var newEnd = new NfaState();

            newStart.AddTransistion(new NullNfaTransition(Start));
            newStart.AddTransistion(new NullNfaTransition(newEnd));

            newEnd.AddTransistion(new NullNfaTransition(Start));
            End.AddTransistion(new NullNfaTransition(newEnd));

            return new Nfa(newStart, newEnd);
        }
Ejemplo n.º 28
0
        public INfa Kleene()
        {
            var newStart = new NfaState();
            var newEnd   = new NfaState();

            newStart.AddTransistion(new NullNfaTransition(Start));
            newStart.AddTransistion(new NullNfaTransition(newEnd));

            newEnd.AddTransistion(new NullNfaTransition(Start));
            End.AddTransistion(new NullNfaTransition(newEnd));

            return(new Nfa(newStart, newEnd));
        }
Ejemplo n.º 29
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;
        }
        public void Apply(NfaState state)
        {
            if (!Add(state))
            {
                return;
            }
            var epsilonStates = state.EpsilonStates;
            var cnt           = epsilonStates.Length;

            for (var i = 0; i < cnt; i++)
            {
                Add(epsilonStates[i]);
            }
        }
Ejemplo n.º 31
0
        public override Nfa GenerateNfa(bool ignoreCase)
        {
            Nfa      retVal     = new Nfa();
            NfaState startState = retVal.Start;
            NfaState finalState = retVal.End;

            Nfa temp = RegularExpression.GenerateNfa(ignoreCase);

            startState.AddMove(temp.Start);
            temp.End.AddMove(temp.Start);
            temp.End.AddMove(finalState);

            return(retVal);
        }
Ejemplo n.º 32
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;
     }
 }