コード例 #1
0
ファイル: Fsa.cs プロジェクト: aamshukov/miscellaneous
        private ail.net.parser.FsaPair GetPairFromStates(ail.net.parser.FsaState xi_p_state, ail.net.parser.FsaState xi_q_state, Hashtable xi_table)
        {
            ail.net.framework.Assert.NonNullReference(xi_p_state, "xi_p_state");
            ail.net.framework.Assert.NonNullReference(xi_q_state, "xi_q_state");
            ail.net.framework.Assert.NonNullReference(xi_table, "xi_table");

            ail.net.parser.FsaPair result = null;

            ail.net.parser.FsaPairKey key;

            key.PState = xi_p_state;
            key.QState = xi_q_state;

            result = (ail.net.parser.FsaPair)xi_table[key];

            if (result == (object)null)
            {
                key.PState = xi_q_state;
                key.QState = xi_p_state;

                result = (ail.net.parser.FsaPair)xi_table[key];
            }

            ail.net.framework.Assert.NonNullReference(result, "result");
            return(result);
        }
コード例 #2
0
ファイル: Fsa.cs プロジェクト: aamshukov/miscellaneous
        private void BuildTable(ref Hashtable xio_partition)
        {
            //!! not implemented due to huge tables being built, postponed :(
            // Mark the distinguishable pairs of states.
            // To achieve this task, we first mark all pairs (p,q), where p belongs to F and
            // q does not belong to F as distinguishable. Then, we proceed as follows:
            //      repeat
            //          for all non-marked pairs {p,q} do
            //              for each letter 'a' do
            //                  if the pair {(s)igma(p,a), (s)igma(q,a)} is marked
            //                      then mark {p,q}
            //      until no new pairs are marked
            ail.net.framework.Assert.NonNullReference(xio_partition, "xio_partition");

            // initialization, this algorithm is sensitive to states organization
            ResetMarkedStates();

            xio_partition.Clear();

            // table for 'A B C D E' states, cells in use marked with '*'
            //     0 A
            //     1 B *
            //     2 C * *
            //     3 D * * *
            //     4 E * * * *
            //         A B C D E
            //         0 1 2 3 4
            // access by (p, q) = table[ArithmeticProgressionSum(Math.Max(0, p_id-1)+q_id]

            // map[index][state]
            ArrayList map = new ArrayList(States.Values);

            Hashtable table = new Hashtable(CalculateTableSize());

            // create table of pairs, also mark all pairs of accept and non-accept states as non-equivalent
            // keep only one of pairs (p, q) or (q, p)
            ArrayList states = new ArrayList(States.Values);

            states.Sort(); // states must be numbered in sequence

            for (int i = 0; i < states.Count - 1; i++)
            {
                for (int j = i + 1; j < states.Count; j++)
                {
                    ail.net.parser.FsaState p_state = (ail.net.parser.FsaState)states[i];
                    ail.net.parser.FsaState q_state = (ail.net.parser.FsaState)states[j];

                    ail.net.parser.FsaPair pair = new ail.net.parser.FsaPair(p_state, q_state);

                    table.Add(new ail.net.parser.FsaPairKey(p_state, q_state), pair);

                    bool p_final = IsFinalState(p_state);
                    bool q_final = IsFinalState(q_state);

                    bool mark = ((p_final && !q_final) || (!p_final && q_final));

                    if (!mark && p_final && q_final)
                    {
                        mark = p_state.Token.Type != q_state.Token.Type;
                    }

                    if (mark)
                    {
                        pair.Marked = true;
                    }
                }
            }

            // populate table
            for (;;)
            {
                bool process = false;

                foreach (ail.net.parser.FsaPair pair in table.Values)
                {
                    if (!pair.Marked)
                    {
                        Hashtable predicates = CombinePredicates(pair.PState, pair.QState);

                        foreach (ail.net.parser.FsaTransitionPredicate predicate in predicates.Values)
                        {
                            ail.net.parser.FsaTransition p_state_t = pair.PState.GetTransitionByPredicate(predicate);
                            ail.net.parser.FsaTransition q_state_t = pair.QState.GetTransitionByPredicate(predicate);

                            if ((p_state_t == (object)null || q_state_t == (object)null))
                            {
                                continue;
                            }

                            ail.net.parser.FsaState p_state = (ail.net.parser.FsaState)States[p_state_t.End];
                            ail.net.parser.FsaState q_state = (ail.net.parser.FsaState)States[q_state_t.End];

                            if ((object)p_state == (object)q_state) // pairs with same states are assumped unmarked
                            {
                                continue;
                            }

                            ail.net.parser.FsaPair sigma_pair = GetPairFromStates(p_state, q_state, table);

                            if (sigma_pair.Marked)
                            {
                                pair.Marked = true;
                                process     = true;
                            }
                        }
                    }
                }

                if (!process)
                {
                    break;
                }
            }

            ResetMarkedStates();

            // build partition
            foreach (ail.net.parser.FsaState state in states)
            {
                if (!state.Marked)
                {
                    // add equivalent groups, optimistic assumption
                    ail.net.parser.FsaStateSet group = new ail.net.parser.FsaStateSet(0);

                    for (int i = 0; i < state.Id; i++)
                    {
                        ail.net.parser.FsaPair pair = GetPairFromStates((ail.net.parser.FsaState)States[i], state, table);

                        if (!pair.Marked)
                        {
                            if ((object)pair.QState != (object)state)
                            {
                                if (!pair.QState.Marked)
                                {
                                    group.States.Add(pair.QState.Id, pair.QState);
                                }
                            }
                            else
                            {
                                if (!pair.PState.Marked)
                                {
                                    group.States.Add(pair.PState.Id, pair.PState);
                                }
                            }
                        }
                    }

                    if (group.States.Count > 0)
                    {
                        group.States.Add(state.Id, state);
                        group.Id = GroupCounter.Next(); // correct id

                        xio_partition.Add(group.Id, group);

                        foreach (ail.net.parser.FsaState group_state in group.States.Values)
                        {
                            group_state.Marked = true;
                        }
                    }
                }
            }

            foreach (ail.net.parser.FsaState state in states)
            {
                if (!state.Marked)
                {
                    // add non-equivalent groups
                    ail.net.parser.FsaStateSet group = new ail.net.parser.FsaStateSet(GroupCounter.Next());
                    group.States.Add(state.Id, state);
                    xio_partition.Add(group.Id, group);
                }
            }

            ResetMarkedStates();
        }