Esempio n. 1
0
        public static NFA CreateAutomaton(string a)
        {
            var result = new NFA();
            var start  = 0;
            var next   = 1;

            result.AddState(start, false);

            for (int i = 0; i < a.Length; ++i)
            {
                if (a[i] == '*')
                {
                    result.AddTransition(next - 1, next - 1, NFA.Any);
                }
                else
                {
                    result.AddState(next, false);
                    result.AddTransition(next - 1, next, (a[i] != '?' ? CharRange.SingleChar(a[i]) : NFA.Any));
                    ++next;
                }
            }

            result.AddState(next, true);
            result.AddTransition(next - 1, next, NFA.Epsilon);

            return(result);
        }
Esempio n. 2
0
        public override StateRange AddTo <T>(NFA <T> nfa, CodePointEquivalenceClasses equivalenceClasses)
        {
            var startState = nfa.AddState();
            var endState   = nfa.AddState();

            foreach (var states in Expressions.Select(exp => exp.AddTo(nfa, equivalenceClasses)))
            {
                nfa.AddEpsilonTransition(startState, states.Start);
                nfa.AddEpsilonTransition(states.End, endState);
            }
            return(new StateRange(startState, endState));
        }
Esempio n. 3
0
        public override StateRange AddTo <T>(NFA <T> nfa, CodePointEquivalenceClasses equivalenceClasses)
        {
            var startState = nfa.AddState();
            var endState   = nfa.AddState();

            foreach (var equivalenceClass in equivalenceClasses.GetClasses(CodePoints))
            {
                nfa.AddTransition(startState, equivalenceClass, endState);
            }

            return(new StateRange(startState, endState));
        }
Esempio n. 4
0
        public override StateRange AddTo <T>(NFA <T> nfa, CodePointEquivalenceClasses equivalenceClasses)
        {
            var startState = nfa.AddState();
            var endState   = startState;

            foreach (var codePoint in Value.GetCodePoints())
            {
                var lastState = endState;
                endState = nfa.AddState();
                nfa.AddTransition(lastState, equivalenceClasses.GetClass(codePoint), endState);
            }
            return(new StateRange(startState, endState));
        }
        public override StateRange AddTo <T>(NFA <T> nfa, CodePointEquivalenceClasses equivalenceClasses)
        {
            // Create a separate NFA
            var tempNFA = new NFA <State?>(equivalenceClasses.Count);
            var states  = Expression.AddTo(tempNFA, equivalenceClasses);

            tempNFA.SetStart(states.Start);
            tempNFA.SetFinal(states.End);

            // Convert to a DFA
            var dfa = tempNFA.ToDFA(stateData => null).Item2;

            dfa = dfa.Minimize().Item2;
            dfa.MakeComplete();             // Can only do complement on complete DFA

            // Now add the complement to this nfa
            var startState = nfa.AddState();
            var endState   = nfa.AddState();

            foreach (var dfaState in dfa.States)
            {
                var nfaState = GetMatchingState(nfa, dfa, dfaState);
                if (dfa.IsStart(dfaState))
                {
                    nfa.AddEpsilonTransition(startState, nfaState);
                }
                if (!dfa.IsFinal(dfaState))
                {
                    nfa.AddEpsilonTransition(nfaState, endState);
                }
                // Copy transitions
                foreach (var input in nfa.Inputs)
                {
                    var toDFAState = dfa.GetTransition(dfaState, input);
                    if (toDFAState != null)
                    {
                        var toNFAState = GetMatchingState(nfa, dfa, toDFAState.Value);
                        nfa.AddTransition(nfaState, input, toNFAState);
                    }
                }
            }

            return(new StateRange(startState, endState));
        }
Esempio n. 6
0
        public virtual NFAState NewState()
        {
            NFAState n     = new NFAState(nfa);
            int      state = nfa.GetNewNFAStateNumber();

            n.stateNumber = state;
            nfa.AddState(n);
            n.enclosingRule = currentRule;
            return(n);
        }
        private static State GetMatchingState <T>(NFA <T> nfa, DFA <State?> dfa, State dfaState)
        {
            var nfaState = dfa.GetData(dfaState);

            if (nfaState == null)
            {
                nfaState = nfa.AddState();
                dfa.SetData(dfaState, nfaState);
            }
            return(nfaState.Value);
        }
        public static LexerNFA ConvertToNFA(LexerSpec spec)
        {
            spec = spec.HasBeenSimplified ? spec : spec.Simplify();
            var equivalenceClasses = MakeEquivalenceClasses(spec);
            var nfa     = new NFA <LexerAction>(equivalenceClasses.Count);
            var modeMap = spec.Modes.ToDictionary(m => m, m => nfa.AddState(true));

            for (var i = 0; i < spec.Rules.Count; i++)
            {
                spec.Rules[i].AddStates(modeMap, equivalenceClasses, nfa, i, spec.DefaultChannel);
            }

            return(new LexerNFA(spec, modeMap, equivalenceClasses, nfa));
        }
        public static LexerNFA ConvertToNFA(LexerSpec spec)
        {
            spec = spec.HasBeenSimplified ? spec : spec.Simplify();
            var equivalenceClasses = MakeEquivalenceClasses(spec);
            var nfa = new NFA<LexerAction>(equivalenceClasses.Count);
            var modeMap = spec.Modes.ToDictionary(m => m, m => nfa.AddState(true));

            for(var i = 0; i < spec.Rules.Count; i++)
                spec.Rules[i].AddStates(modeMap, equivalenceClasses, nfa, i, spec.DefaultChannel);

            return new LexerNFA(spec, modeMap, equivalenceClasses, nfa);
        }