Beispiel #1
0
 public static Dfa <TSymbol> ProductDfa(DfaUtils.MinimizedDfa <TSymbol>[] dfas)
 {
     if (dfas.Length == 0)
     {
         throw new ArgumentException();
     }
     else if (dfas.Length == 1)
     {
         return(new Dfa <TSymbol>(dfas[0]));
     }
     else
     {
         DfaUtils.MinimizedDfa <TSymbol> cur = dfas[0];
         for (int i = 1; i < dfas.Length; i++)
         {
             cur = DfaUtils.MakeMinimizedProductDfa <
                 DfaUtils.MinimizedDfa <TSymbol>,
                 DfaUtils.MinimizedDfaState <TSymbol>,
                 DfaUtils.MinimizedDfa <TSymbol>,
                 DfaUtils.MinimizedDfaState <TSymbol>,
                 TSymbol
                 >(cur, dfas[i], new DfaUtils.AmbiguityHandler(MaxAmbiguityHandler));
         }
         return(new Dfa <TSymbol>(cur));
     }
 }
Beispiel #2
0
        public Dfa(DfaUtils.MinimizedDfa <TSymbol> lexerDfa)
        {
            var states      = new Dictionary <DfaUtils.MinimizedDfaState <TSymbol>, DfaState <TSymbol> >();
            var accepting   = new Dictionary <DfaState <TSymbol>, uint>();
            var transitions = new Dictionary <DfaState <TSymbol>, List <KeyValuePair <TSymbol, DfaState <TSymbol> > > >();
            var queue       = new Queue <DfaUtils.MinimizedDfaState <TSymbol> >();

            states[lexerDfa.Start]              = new DfaState <TSymbol>();
            accepting[states[lexerDfa.Start]]   = lexerDfa.Start.Accepting;
            transitions[states[lexerDfa.Start]] = new List <KeyValuePair <TSymbol, DfaState <TSymbol> > >();
            queue.Enqueue(lexerDfa.Start);

            while (queue.Count > 0)
            {
                DfaUtils.MinimizedDfaState <TSymbol> s1 = queue.Dequeue();
                DfaState <TSymbol> s2 = states[s1];
                foreach (var i in s1.Transitions)
                {
                    if (!states.ContainsKey(i.Value))
                    {
                        states[i.Value]              = new DfaState <TSymbol>();
                        accepting[states[i.Value]]   = i.Value.Accepting;
                        transitions[states[i.Value]] = new List <KeyValuePair <TSymbol, DfaState <TSymbol> > >();
                        queue.Enqueue(i.Value);
                    }
                    transitions[s2].Add(new KeyValuePair <TSymbol, DfaState <TSymbol> >(i.Key, states[i.Value]));
                }
            }

            foreach (var i in states)
            {
                i.Value.Initialize(accepting[i.Value], transitions[i.Value]);
            }
            Start = states[lexerDfa.Start];
        }
        //1:0 --a--> 2
        //2:1 --a--> 3
        //3:2 --a--> 2
        public void SimpleTest2()
        {
            var firstState = new DfaUtils.MinimizedDfaState <char>();

            firstState._accepting = 0;
            var secondState = new DfaUtils.MinimizedDfaState <char>();

            secondState._accepting = 1;
            var thirdState = new DfaUtils.MinimizedDfaState <char>();

            thirdState._accepting   = 2;
            firstState._transitions = new KeyValuePair <char, DfaUtils.MinimizedDfaState <char> >[1] {
                new KeyValuePair <char, DfaUtils.MinimizedDfaState <char> >('a', secondState)
            };
            secondState._transitions = new KeyValuePair <char, DfaUtils.MinimizedDfaState <char> >[1] {
                new KeyValuePair <char, DfaUtils.MinimizedDfaState <char> >('a', thirdState)
            };
            thirdState._transitions = new KeyValuePair <char, DfaUtils.MinimizedDfaState <char> >[1] {
                new KeyValuePair <char, DfaUtils.MinimizedDfaState <char> >('a', secondState)
            };
            var dfa = new DfaUtils.MinimizedDfa <char>();

            dfa._start = firstState;


            dfa = DfaUtils.Minimized <DfaUtils.MinimizedDfa <char>, DfaUtils.MinimizedDfaState <char>, char>(dfa);


            var stateList = DfaUtils.PrepareStateList <DfaUtils.MinimizedDfa <char>, DfaUtils.MinimizedDfaState <char>, char>(dfa);

            Assert.AreEqual(stateList.Count, 3);
        }