예제 #1
0
파일: Bunch.cs 프로젝트: petrroll/Parsers
        public void e_closure()
        {
            this.accept       = null;
            this.anchor       = 0;
            this.accept_index = 2147483647;
            Stack <Nfa> stack = new Stack <Nfa>();
            int         count = this.nfa_set.Count;

            for (int i = 0; i < count; i++)
            {
                Nfa nfa = this.nfa_set[i];
                stack.Push(nfa);
            }
            while (stack.Count > 0)
            {
                object obj = stack.Pop();
                if (obj == null)
                {
                    break;
                }
                Nfa nfa = (Nfa)obj;
                if (nfa.GetAccept() != null && nfa.Label < this.accept_index)
                {
                    this.accept_index = nfa.Label;
                    this.accept       = nfa.GetAccept();
                    this.anchor       = nfa.GetAnchor();
                }
                if ('' == nfa.Edge)
                {
                    if (nfa.Next != null && !this.nfa_set.Contains(nfa.Next))
                    {
                        this.nfa_bit.Set(nfa.Next.Label, true);
                        this.nfa_set.Add(nfa.Next);
                        stack.Push(nfa.Next);
                    }
                    if (nfa.Sibling != null && !this.nfa_set.Contains(nfa.Sibling))
                    {
                        this.nfa_bit.Set(nfa.Sibling.Label, true);
                        this.nfa_set.Add(nfa.Sibling);
                        stack.Push(nfa.Sibling);
                    }
                }
            }
            if (this.nfa_set != null)
            {
                this.sort_states();
            }
        }
예제 #2
0
/*
 * Function: e_closure
 * Description: Alters input set.
 */
        public void e_closure()
        {
            Nfa state = null;

            /*
             * Debug checks
             */
#if DEBUG
            Utility.assert(null != nfa_set);
            Utility.assert(null != nfa_bit);
#endif

            accept       = null;
            anchor       = Spec.NONE;
            accept_index = Utility.INT_MAX;

            /*
             * Create initial stack.
             */
            Stack nfa_stack = new Stack();
            int   size      = nfa_set.Count;

            for (int i = 0; i < size; i++)
            {
                state = (Nfa)nfa_set[i];
#if DEBUG
                Utility.assert(nfa_bit.Get(state.GetLabel()));
#endif
                nfa_stack.Push(state);
            }

            /*
             * Main loop.
             */
            while (nfa_stack.Count > 0)
            {
                Object o = nfa_stack.Pop();
                if (o == null)
                {
                    break;
                }
                state = (Nfa)o;

#if OLD_DUMP_DEBUG
                if (null != state.GetAccept())
                {
                    Console.WriteLine("Looking at accepting state "
                                      + Int32.ToString(state.GetLabel())
                                      + " with <"
                                      + state.GetAccept().action
                                      + ">");
                }
#endif
                if (null != state.GetAccept() && state.GetLabel() < accept_index)
                {
                    accept_index = state.GetLabel();
                    accept       = state.GetAccept();
                    anchor       = state.GetAnchor();

#if OLD_DUMP_DEBUG
                    Console.WriteLine("Found accepting state "
                                      + Int32.ToString(state.GetLabel())
                                      + " with <"
                                      + state.GetAccept().action
                                      + ">");
#endif
#if DEBUG
                    Utility.assert(null != accept);
                    Utility.assert(Spec.NONE == anchor ||
                                   0 != (anchor & Spec.END) ||
                                   0 != (anchor & Spec.START));
#endif
                }

                if (Nfa.EPSILON == state.GetEdge())
                {
                    if (state.GetNext() != null)
                    {
                        if (false == nfa_set.Contains(state.GetNext()))
                        {
#if DEBUG
                            Utility.assert(false == nfa_bit.Get(state.GetNext().GetLabel()));
#endif
                            nfa_bit.Set(state.GetNext().GetLabel(), true);
                            nfa_set.Add(state.GetNext());
                            nfa_stack.Push(state.GetNext());
                        }
                    }
                    if (null != state.GetSib())
                    {
                        if (false == nfa_set.Contains(state.GetSib()))
                        {
#if DEBUG
                            Utility.assert(false == nfa_bit.Get(state.GetSib().GetLabel()));
#endif
                            nfa_bit.Set(state.GetSib().GetLabel(), true);
                            nfa_set.Add(state.GetSib());
                            nfa_stack.Push(state.GetSib());
                        }
                    }
                }
            }
            if (null != nfa_set)
            {
                sort_states();
            }
        }