当前位置: 首页>>代码示例>>C#>>正文


C# NFA类代码示例

本文整理汇总了C#中NFA的典型用法代码示例。如果您正苦于以下问题:C# NFA类的具体用法?C# NFA怎么用?C# NFA使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


NFA类属于命名空间,在下文中一共展示了NFA类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: NFATests

        public void NFATests()
        {
            var a = new NFA<char>();

            // "a" automaton
            a.AddTransition(a.Start, new State { Final = true }, 'a');
            a.Initial();

            a.Trigger('a');
            Assert.That(a.Current.Contains(a.LastAdded), Is.True);

            a.Initial();
            Assert.That(() => a.Trigger('b'), Throws.TypeOf<StateNotFoundException>());

            // "abcd" automaton
            a = new NFA<char>();
            a.AddTransition(a.Start, new State(), 'a');
            a.AddTransition(a.LastAdded, new State(), 'b');
            a.AddTransition(a.LastAdded, new State(), 'c');
            a.AddTransition(a.LastAdded, new State { Final = true }, 'd');
            a.Initial();

            foreach (char c in "abcd") {
                Assert.That(a.Current.Contains(a.LastAdded), Is.False);
                a.Trigger(c);
            }
            Assert.That(a.Current.Contains(a.LastAdded), Is.True);
            Assert.That(() => a.Trigger('b'), Throws.TypeOf<StateNotFoundException>());
        }
开发者ID:surenkov,项目名称:UCP,代码行数:29,代码来源:StateMachineTests.cs

示例2: AddExpression

        public void AddExpression(string name, string regex)
        {
            using (var stream = new StringReader(InfixToPostfix.Convert(regex)))
            {
                while (stream.Peek() != -1)
                {
                    var c = (char) stream.Read();
                    switch (c)
                    {
                        case '.': _stack.Concatenate(); break;
                        case '|': _stack.Unite(); break;
                        case '*': _stack.Iterate(); break;
                        case '+': _stack.AtLeast(); break;
                        case '?': _stack.Maybe(); break;
                        default:
                            var a = new NFA<char>();
                            a.AddTransition(a.Start, new State(), c == '\\' ? Escape((char) stream.Read()) : c);
                            _stack.Push(a);
                            break;
                    }
                }

                var top = _stack.Peek();
                top.LastAdded.Final = true;
                top.SetName(top.LastAdded, name);
            }
        }
开发者ID:surenkov,项目名称:UCP,代码行数:27,代码来源:RegexEngine.cs

示例3: AddStates

        public void AddStates(IDictionary<Mode, State> modeMap, CodePointEquivalenceClasses equivalenceClasses, NFA<LexerAction> nfa, int priority, Channel defaultChannel)
        {
            var states = Expression.AddTo(nfa, equivalenceClasses);

            foreach(var mode in Modes)
                nfa.AddEpsilonTransition(modeMap[mode], states.Start);

            nfa.SetFinal(states.End);

            // Set Action
            nfa.SetData(states.End, new LexerAction(priority, GetValueAction(), GetModeActions(), GetEmitAction(defaultChannel), GetCodeAction()));
        }
开发者ID:adamant-deprecated,项目名称:AdamantTemporaryCompiler,代码行数:12,代码来源:RuleSpec.cs

示例4: LTLMonitor

        public LTLMonitor(ITLFormula formula)
        {
            var tpositiveNFA = translator.GetBuchiAutomaton (formula).ToNFA();
            var tnegativeNFA = translator.GetBuchiAutomaton (formula.Negate ()).ToNFA();

            positiveNFA = tpositiveNFA.Determinize ();
            negativeNFA = tnegativeNFA.Determinize ();

            currentNegative = negativeNFA.InitialNode;
            currentPositive = positiveNFA.InitialNode;

            UpdateStatus ();
        }
开发者ID:ancailliau,项目名称:LtlSharp,代码行数:13,代码来源:LTLMonitor.cs

示例5: RepeatOnceOrMore

        private static NFA RepeatOnceOrMore(NFA nfa)
        {
            // Add an epsilon transition from the accept state back to the start state
            NFA.State oldAcceptState = nfa.States.First(f => f.AcceptState);
            nfa.Transitions.Add(new Transition<NFA.State>(oldAcceptState, nfa.StartState));

            // Add a new accept state, since we cannot have edges exiting the accept state
            var newAcceptState = new NFA.State { AcceptState = true };
            nfa.Transitions.Add(new Transition<NFA.State>(oldAcceptState, newAcceptState));
            nfa.States.Add(newAcceptState);

            // Clear the accept flag of the old accept state 
            oldAcceptState.AcceptState = false;

            return nfa;
        }
开发者ID:uptown1919,项目名称:Piglet,代码行数:16,代码来源:NfaBuilder.cs

示例6: Accept

        private static NFA Accept(CharSet acceptCharacters)
        {
            // Generate a NFA with a simple path with one state transitioning into an accept state.
            var nfa = new NFA();
            var state = new NFA.State();
            nfa.States.Add(state);

            var acceptState = new NFA.State { AcceptState = true };
            nfa.States.Add(acceptState);

            nfa.Transitions.Add(new Transition<NFA.State>(state, acceptState, acceptCharacters));

            nfa.StartState = state;

            return nfa;
        }
开发者ID:uptown1919,项目名称:Piglet,代码行数:16,代码来源:NfaBuilder.cs

示例7: AddExpression

 public void AddExpression(string name, string regex)
 {
     var stream = new StringReader(InfixToPostfix.Convert(regex));
     while (stream.Peek() != -1)
     {
         var c = (char) stream.Read();
         NFA<char> a;
         switch (c)
         {
             case '.':
                 _stack.Concatenate();
                 break;
             case '|':
                 _stack.Unite();
                 break;
             case '*':
                 _stack.Iterate();
                 break;
             case '+':
                 _stack.AtLeast();
                 break;
             case '?':
                 _stack.Maybe();
                 break;
             case '\\':
                 char n = (char) stream.Read();
                 a = new NFA<char>();
                 a.AddTransition(a.Start, new State(), Escape(n));
                 _stack.Push(a);
                 break;
             default:
                 a = new NFA<char>();
                 a.AddTransition(a.Start, new State(), c);
                 _stack.Push(a);
                 break;
         }
     }
     var automaton = _stack.Peek();
     automaton.LastAdded.Final = true;
     automaton.SetName(automaton.LastAdded, name);
 }
开发者ID:Davidyuk,项目名称:UCP,代码行数:41,代码来源:RegexBuilder.cs

示例8: ToNFA

 public override void ToNFA(NFA pad, int from, int to)
 {
     throw new InvalidOperationException();
 }
开发者ID:nbrown,项目名称:niecza,代码行数:4,代码来源:Cursor.cs

示例9: Reify

 public override LAD Reify(NFA pad)
 {
     return this;
 }
开发者ID:nbrown,项目名称:niecza,代码行数:4,代码来源:Cursor.cs

示例10: Next

    public LexerState Next(NFA nf, int ch)
    {
        LexerState l;
        if (dfc.TryGetValue(ch, out l))
            return l;
        l = new LexerState(nf);
        for (int i = 0; i < nstates.Length; i++) {
            int bm = nstates[i];
            for (int j = 0; j < 32; j++) {
                if ((bm & (1 << j)) == 0)
                    continue;
                foreach (NFA.Edge e in nf.nodes[32*i + j].edges) {
                    if (e.when != null && e.when.Accepts(ch))
                        l.Add(e.to);
                }
            }
        }

        nf.Close(l);
        LexerState cl;

        if (!nf.dfashare.TryGetValue(l, out cl)) {
            nf.dfashare[l] = cl = l;
        }
        dfc[ch] = cl;
        return cl;
    }
开发者ID:nbrown,项目名称:niecza,代码行数:27,代码来源:Cursor.cs

示例11: GetLexer

    public static Lexer GetLexer(Frame fromf, STable kl, LAD[] lads, string title)
    {
        LexerCache lc = kl.GetLexerCache();
        Lexer ret;
        if (lc.nfas.TryGetValue(lads, out ret))
            return ret;
        if (lc.parent != null && lc.parent.mo.name != "Cursor" && lc.parent.mo.name != "Any") {
            ret = GetLexer(fromf, lc.parent.mo, lads, title);
            foreach (string u in ret.pad.used_methods) {
                if (lc.repl_methods.Contains(u))
                    goto anew;
            }
            if (LtmTrace)
                Console.WriteLine("Reused {0} alternation lexer for {1} in {2}",
                        title, lc.parent.mo.name, kl.name);
            return lc.nfas[lads] = ret;
        }
        anew:
        if (LtmTrace) {
            Console.WriteLine("Need new alternation lexer for {0} in {1}",
                    title, kl.name);
        }
        NFA pad = new NFA();
        pad.cursor_class = kl;
        LAD[] lads_p = new LAD[lads.Length];
        pad.outer_stack.Add(fromf);
        pad.info_stack.Add(fromf.info);
        for (int i = 0; i < lads_p.Length; i++)
            lads_p[i] = lads[i].Reify(pad);

        ret = new Lexer(pad, title, lads_p);
        lc.nfas[lads] = ret;
        return ret;
    }
开发者ID:nbrown,项目名称:niecza,代码行数:34,代码来源:Cursor.cs

示例12: And

        public static NFA And(NFA first, NFA second)
        {
            // Create a new NFA and use the first NFAs start state as the starting point
            var nfa = new NFA { StartState = first.StartState };

            // Change all links in to first acceptstate to go to seconds 
            // start state
            foreach (var edge in first.Transitions.Where(f => f.To.AcceptState))
            {
                edge.To = second.StartState;
            }

            // Remove acceptstate from first
            first.States.Remove(first.States.First(f => f.AcceptState));

            // Add all states and edges in both NFAs
            // Second NFA already has an accept state, there is no need to create another one
            nfa.AddAll(first);
            nfa.AddAll(second);

            return nfa;
        }
开发者ID:uptown1919,项目名称:Piglet,代码行数:22,代码来源:NfaBuilder.cs

示例13: RepeatZeroOrOnce

 private static NFA RepeatZeroOrOnce(NFA nfa)
 {
     // Easy enough, add an epsilon transition from the start state
     // to the end state. Done
     nfa.Transitions.Add(new Transition<NFA.State>(nfa.StartState, nfa.States.First(f => f.AcceptState)));
     return nfa;
 }
开发者ID:uptown1919,项目名称:Piglet,代码行数:7,代码来源:NfaBuilder.cs

示例14: QueryLiteral

 public virtual void QueryLiteral(NFA pad, out int len, out bool cont)
 {
     len = 0; cont = false;
 }
开发者ID:nbrown,项目名称:niecza,代码行数:4,代码来源:Cursor.cs

示例15: list_common

    public Frame list_common(bool eval, Frame th, Variable var)
    {
        VarDeque iter = Builtins.start_iter(var);
        List<object> toks = new List<object>();
        List<LAD> lads = new List<LAD>();

        NFA pad = new NFA();
        pad.cursor_class = st.ns.klass;

        while (Kernel.IterHasFlat(iter, true)) {
            Variable svar = iter.Shift();
            P6any sobj = svar.Fetch();

        retry:
            if (sobj.Isa(Kernel.RegexMO)) {
                toks.Add(sobj);

                pad.outer_stack.Add(Kernel.GetOuter(sobj));
                pad.info_stack.Add(Kernel.GetInfo(sobj));
                lads.Add(pad.info_stack[0].ltm.Reify(pad));
                pad.outer_stack.Clear();
                pad.info_stack.Clear();
            } else if (eval) {
                sobj = Builtins.compile_bind_regex(th,
                        sobj.mo.mro_raw_Str.Get(svar));
                svar = Kernel.NewROScalar(sobj);
                goto retry;
            } else {
                string str = sobj.mo.mro_raw_Str.Get(svar);
                toks.Add(str);
                lads.Add(new LADStr(str));
            }
        }

        int[] cases = (new Lexer(pad, "array_var", lads.ToArray())).
            Run(global.orig_s, st.pos);

        Frame nth = th.MakeChild(null, ArrayHelperSI, Kernel.AnyP);
        nth.lex0 = MakeCursor();
        nth.lex1 = toks;
        nth.lex2 = cases;

        return nth;
    }
开发者ID:nbrown,项目名称:niecza,代码行数:44,代码来源:Cursor.cs


注:本文中的NFA类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。