Пример #1
0
        public static NfaPair NewNLPair(Spec spec)
        {
            NfaPair nfaPair = Alloc.NewNfaPair();

            nfaPair.end   = Alloc.NewNfa(spec);
            nfaPair.start = Alloc.NewNfa(spec);
            Nfa start = nfaPair.start;

            start.Next = Alloc.NewNfa(spec);
            Nfa next = start.Next;

            next.Edge = '￾';
            next.SetCharSet(new CharSet());
            next.GetCharSet().add(10);
            next.Next     = nfaPair.end;
            start.Sibling = Alloc.NewNfa(spec);
            Nfa sibling = start.Sibling;

            sibling.Edge = '\r';
            sibling.Next = Alloc.NewNfa(spec);
            Nfa next2 = sibling.Next;

            next2.Next         = null;
            next2.Sibling      = Alloc.NewNfa(spec);
            next2.Sibling.Edge = '\n';
            next2.Sibling.Next = nfaPair.end;
            return(nfaPair);
        }
Пример #2
0
/*
 * Function: cat_expr
 * Description: Recursive descent regular expression parser.
 */
        private static void cat_expr(NfaPair pair)
        {
            NfaPair e2_pair;

#if DESCENT_DEBUG
            Utility.enter("cat_expr", spec.lexeme, spec.current_token);
#endif

#if DEBUG
            Utility.assert(null != pair);
#endif

            e2_pair = Alloc.NewNfaPair();

            if (first_in_cat(spec.current_token))
            {
                factor(pair);
            }

            while (first_in_cat(spec.current_token))
            {
                factor(e2_pair);

                /* Destroy */
                pair.end.mimic(e2_pair.start);
                discardNfa(e2_pair.start);

                pair.end = e2_pair.end;
            }

#if DESCENT_DEBUG
            Utility.leave("cat_expr", spec.lexeme, spec.current_token);
#endif
        }
Пример #3
0
        private static int add_to_dstates(Spec s, Bunch bunch)
        {
            Dfa dfa = Alloc.NewDfa(s);

            dfa.SetNFASet(new List <Nfa>(bunch.GetNFASet()));
            dfa.SetNFABit(new BitSet(bunch.GetNFABit()));
            dfa.SetAccept(bunch.GetAccept());
            dfa.SetAnchor(bunch.GetAnchor());
            dfa.ClearMarked();
            s.dfa_sets[dfa.GetNFABit()] = dfa;
            return(dfa.Label);
        }
Пример #4
0
        private static void cat_expr(NfaPair pair)
        {
            NfaPair nfaPair = Alloc.NewNfaPair();

            if (MakeNfa.first_in_cat(MakeNfa.spec.current_token))
            {
                MakeNfa.factor(pair);
            }
            while (MakeNfa.first_in_cat(MakeNfa.spec.current_token))
            {
                MakeNfa.factor(nfaPair);
                pair.end.mimic(nfaPair.start);
                MakeNfa.discardNfa(nfaPair.start);
                pair.end = nfaPair.end;
            }
        }
Пример #5
0
        private static Nfa machine()
        {
            Nfa    nfa    = Alloc.NewNfa(MakeNfa.spec);
            Nfa    nfa2   = nfa;
            BitSet states = MakeNfa.gen.GetStates();

            MakeNfa.spec.current_token = Tokens.EOS;
            MakeNfa.gen.Advance();
            if (Tokens.END_OF_INPUT != MakeNfa.spec.current_token)
            {
                nfa2.Next = MakeNfa.rule();
                MakeNfa.ProcessStates(states, nfa2.Next);
            }
            while (Tokens.END_OF_INPUT != MakeNfa.spec.current_token)
            {
                states = MakeNfa.gen.GetStates();
                MakeNfa.gen.Advance();
                if (Tokens.END_OF_INPUT == MakeNfa.spec.current_token)
                {
                    break;
                }
                nfa2.Sibling = Alloc.NewNfa(MakeNfa.spec);
                nfa2         = nfa2.Sibling;
                nfa2.Next    = MakeNfa.rule();
                MakeNfa.ProcessStates(states, nfa2.Next);
            }
            nfa2.Sibling = Alloc.NewNfa(MakeNfa.spec);
            nfa2         = nfa2.Sibling;
            nfa2.Next    = Alloc.NewNfa(MakeNfa.spec);
            Nfa next = nfa2.Next;

            next.Edge = '￾';
            next.Next = Alloc.NewNfa(MakeNfa.spec);
            next.SetCharSet(new CharSet());
            next.GetCharSet().add((int)MakeNfa.spec.BOL);
            next.GetCharSet().add((int)MakeNfa.spec.EOF);
            next.Next.SetAccept(new Accept(null));
            for (int i = 0; i < MakeNfa.spec.States.Count; i++)
            {
                List <Nfa> list = MakeNfa.spec.state_rules[i];
                list.Add(next);
            }
            return(nfa);
        }
Пример #6
0
        private static Nfa rule()
        {
            int     num     = 0;
            NfaPair nfaPair = Alloc.NewNfaPair();
            Nfa     nfa;
            Nfa     end;

            if (MakeNfa.spec.current_token == Tokens.AT_BOL)
            {
                num |= 1;
                MakeNfa.gen.Advance();
                MakeNfa.expr(nfaPair);
                nfa      = Alloc.NewNfa(MakeNfa.spec);
                nfa.Edge = MakeNfa.spec.BOL;
                nfa.Next = nfaPair.start;
                end      = nfaPair.end;
            }
            else
            {
                MakeNfa.expr(nfaPair);
                nfa = nfaPair.start;
                end = nfaPair.end;
            }
            if (Tokens.AT_EOL == MakeNfa.spec.current_token)
            {
                MakeNfa.gen.Advance();
                NfaPair nfaPair2 = Alloc.NewNLPair(MakeNfa.spec);
                end.Next = Alloc.NewNfa(MakeNfa.spec);
                Nfa next = end.Next;
                next.Next         = nfaPair2.start;
                next.Sibling      = Alloc.NewNfa(MakeNfa.spec);
                next.Sibling.Edge = MakeNfa.spec.EOF;
                next.Sibling.Next = nfaPair2.end;
                end  = nfaPair2.end;
                num |= 2;
            }
            if (end == null)
            {
                Error.ParseError(Errors.ZERO, MakeNfa.gen.InputFilePath, MakeNfa.input.line_number);
            }
            end.SetAccept(MakeNfa.gen.packAccept());
            end.SetAnchor(num);
            return(nfa);
        }
Пример #7
0
        private static void expr(NfaPair pair)
        {
            NfaPair nfaPair = Alloc.NewNfaPair();

            MakeNfa.cat_expr(pair);
            while (Tokens.OR == MakeNfa.spec.current_token)
            {
                MakeNfa.gen.Advance();
                MakeNfa.cat_expr(nfaPair);
                Nfa nfa = Alloc.NewNfa(MakeNfa.spec);
                nfa.Sibling      = nfaPair.start;
                nfa.Next         = pair.start;
                pair.start       = nfa;
                nfa              = Alloc.NewNfa(MakeNfa.spec);
                pair.end.Next    = nfa;
                nfaPair.end.Next = nfa;
                pair.end         = nfa;
            }
        }
Пример #8
0
/*
 * Function: factor
 * Description: Recursive descent regular expression parser.
 */
        private static void factor(NfaPair pair)
        {
            Nfa start = null;
            Nfa end   = null;

#if DESCENT_DEBUG
            Utility.enter("factor", spec.lexeme, spec.current_token);
#endif

            term(pair);

            if (Gen.CLOSURE == spec.current_token ||
                Gen.PLUS_CLOSE == spec.current_token ||
                Gen.OPTIONAL == spec.current_token)
            {
                start = Alloc.NewNfa(spec);
                end   = Alloc.NewNfa(spec);

                start.SetNext(pair.start);
                pair.end.SetNext(end);

                if (Gen.CLOSURE == spec.current_token ||
                    Gen.OPTIONAL == spec.current_token)
                {
                    start.SetSib(end);
                }

                if (Gen.CLOSURE == spec.current_token ||
                    Gen.PLUS_CLOSE == spec.current_token)
                {
                    pair.end.SetSib(pair.start);
                }

                pair.start = start;
                pair.end   = end;
                gen.Advance();
            }

#if DESCENT_DEBUG
            Utility.leave("factor", spec.lexeme, spec.current_token);
#endif
        }
Пример #9
0
 private static void factor(NfaPair pair)
 {
     MakeNfa.term(pair);
     if (Tokens.CLOSURE == MakeNfa.spec.current_token || Tokens.PLUS_CLOSE == MakeNfa.spec.current_token || Tokens.OPTIONAL == MakeNfa.spec.current_token)
     {
         Nfa nfa  = Alloc.NewNfa(MakeNfa.spec);
         Nfa nfa2 = Alloc.NewNfa(MakeNfa.spec);
         nfa.Next      = pair.start;
         pair.end.Next = nfa2;
         if (MakeNfa.spec.current_token == Tokens.CLOSURE || MakeNfa.spec.current_token == Tokens.OPTIONAL)
         {
             nfa.Sibling = nfa2;
         }
         if (MakeNfa.spec.current_token == Tokens.CLOSURE || MakeNfa.spec.current_token == Tokens.PLUS_CLOSE)
         {
             pair.end.Sibling = pair.start;
         }
         pair.start = nfa;
         pair.end   = nfa2;
         MakeNfa.gen.Advance();
     }
 }
Пример #10
0
/*
 * Function: expr
 * Description: Recursive descent regular expression parser.
 */
        private static void expr(NfaPair pair)
        {
            NfaPair e2_pair;
            Nfa     p;

#if DESCENT_DEBUG
            Utility.enter("expr", spec.lexeme, spec.current_token);
#endif

#if DEBUG
            Utility.assert(null != pair);
#endif

            e2_pair = Alloc.NewNfaPair();

            cat_expr(pair);

            while (Gen.OR == spec.current_token)
            {
                gen.Advance();
                cat_expr(e2_pair);

                p = Alloc.NewNfa(spec);
                p.SetSib(e2_pair.start);
                p.SetNext(pair.start);
                pair.start = p;

                p = Alloc.NewNfa(spec);
                pair.end.SetNext(p);
                e2_pair.end.SetNext(p);
                pair.end = p;
            }

#if DESCENT_DEBUG
            Utility.leave("expr", spec.lexeme, spec.current_token);
#endif
        }
Пример #11
0
/*
 * function: add_to_dstates
 * Description: Takes as input a CBunch with details of
 * a dfa state that needs to be created.
 * 1) Allocates a new dfa state and saves it in the appropriate Spec list
 * 2) Initializes the fields of the dfa state with the information in the CBunch.
 * 3) Returns index of new dfa.
 */
        private static int add_to_dstates(Spec s, Bunch bunch)
        {
            Dfa dfa;

#if DEBUG
            Utility.assert(null != bunch.GetNFASet());
            Utility.assert(null != bunch.GetNFABit());
            Utility.assert(null != bunch.GetAccept() || Spec.NONE == bunch.GetAnchor());
#endif

            /* Allocate, passing Spec so dfa label can be set. */
            dfa = Alloc.NewDfa(s);

            /* Initialize fields, including the mark field. */
            dfa.SetNFASet(new ArrayList(bunch.GetNFASet()));
            dfa.SetNFABit(new BitSet(bunch.GetNFABit()));
            dfa.SetAccept(bunch.GetAccept());
            dfa.SetAnchor(bunch.GetAnchor());
            dfa.ClearMarked();

#if OLD_DUMP_DEBUG
            Console.WriteLine("[Created new dfa_state #" + Int32.ToString(dfa.GetLabel()) + "]");
            dfa.dump();
#endif

            /* Register dfa state using BitSet in spec Hashtable. */
            s.dfa_sets[dfa.GetNFABit()] = dfa;

#if OLD_DUMP_DEBUG
            Console.Write("Registering set : ");
            Print_Set(dfa.GetNFASet());
            Console.WriteLine("");
#endif

            return(dfa.GetLabel());
        }
Пример #12
0
/*
 * Function: term
 * Description: Recursive descent regular expression parser.
 */
        private static void term(NfaPair pair)
        {
            Nfa  start;
            bool isAlphaL;

#if DESCENT_DEBUG
            Utility.enter("term", spec.lexeme, spec.current_token);
#endif

            if (Gen.OPEN_PAREN == spec.current_token)
            {
                gen.Advance();
                expr(pair);

                if (Gen.CLOSE_PAREN == spec.current_token)
                {
                    gen.Advance();
                }
                else
                {
                    Error.parse_error(Error.E_SYNTAX, input.line_number);
                }
            }
            else
            {
                start      = Alloc.NewNfa(spec);
                pair.start = start;

                start.SetNext(Alloc.NewNfa(spec));
                pair.end = start.GetNext();

                if (Gen.L == spec.current_token && Char.IsLetter(spec.lexeme))
                {
                    isAlphaL = true;
                }
                else
                {
                    isAlphaL = false;
                }
                if (false == (Gen.ANY == spec.current_token ||
                              Gen.CCL_START == spec.current_token ||
                              (spec.ignorecase && isAlphaL)))
                {
                    start.SetEdge(spec.lexeme);
                    gen.Advance();
                }
                else
                {
                    start.SetEdge(Nfa.CCL);
                    start.SetCharSet(new CharSet());
                    CharSet cset = start.GetCharSet();

                    /* Match case-insensitive letters using character class. */
                    if (spec.ignorecase && isAlphaL)
                    {
                        cset.addncase(spec.lexeme);
                    }
                    /* Match dot (.) using character class. */
                    else if (Gen.ANY == spec.current_token)
                    {
                        cset.add('\n');
                        cset.add('\r');
                        /* exclude BOL and EOF from character classes */
                        cset.add(spec.BOL);
                        cset.add(spec.EOF);
                        cset.complement();
                    }
                    else
                    {
                        gen.Advance();
                        if (Gen.AT_BOL == spec.current_token)
                        {
                            gen.Advance();
                            /* exclude BOL and EOF from character classes */
                            cset.add(spec.BOL);
                            cset.add(spec.EOF);
                            cset.complement();
                        }
                        if (!(Gen.CCL_END == spec.current_token))
                        {
                            dodash(cset);
                        }
                    }
                    gen.Advance();
                }
            }

#if DESCENT_DEBUG
            Utility.leave("term", spec.lexeme, spec.current_token);
#endif
        }
Пример #13
0
/*
 * Function: rule
 * Description: Recursive descent regular expression parser.
 */
        private static Nfa rule()
        {
            NfaPair pair;
            Nfa     start  = null;
            Nfa     end    = null;
            int     anchor = Spec.NONE;

#if DESCENT_DEBUG
            Utility.enter("rule", spec.lexeme, spec.current_token);
#endif

            pair = Alloc.NewNfaPair();

            if (Gen.AT_BOL == spec.current_token)
            {
                anchor = anchor | Spec.START;
                gen.Advance();
                expr(pair);

                start = Alloc.NewNfa(spec);
                start.SetEdge(spec.BOL);
                start.SetNext(pair.start);
                end = pair.end;
            }
            else
            {
                expr(pair);
                start = pair.start;
                end   = pair.end;
            }

            if (Gen.AT_EOL == spec.current_token)
            {
                gen.Advance();

                NfaPair nlpair = Alloc.NewNLPair(spec);
                end.SetNext(Alloc.NewNfa(spec));
                Nfa enext = end.GetNext();
                enext.SetNext(nlpair.start);
                enext.SetSib(Alloc.NewNfa(spec));
                enext.GetSib().SetEdge(spec.EOF);
                enext.GetSib().SetNext(nlpair.end);
                end = nlpair.end;

                anchor = anchor | Spec.END;
            }

            /* check for null rules */
            if (end == null)
            {
                Error.parse_error(Error.E_ZERO, input.line_number);
            }

            /* Handle end of regular expression */
            end.SetAccept(gen.packAccept());
            end.SetAnchor(anchor);

#if DESCENT_DEBUG
            Utility.leave("rule", spec.lexeme, spec.current_token);
#endif
            return(start);
        }
Пример #14
0
/*
 * Function: machine
 * Description: Recursive descent regular expression parser.
 */
        private static Nfa machine()
        {
            Nfa    start;
            Nfa    p;
            BitSet states;

#if DESCENT_DEBUG
            Utility.enter("machine", spec.lexeme, spec.current_token);
#endif

            start = Alloc.NewNfa(spec);
            p     = start;

            states = gen.GetStates();

            /* Begin: Added for states. */
            spec.current_token = Gen.EOS;
            gen.Advance();
            /* End: Added for states. */

            if (Gen.END_OF_INPUT != spec.current_token)
            {
                p.SetNext(rule());
                ProcessStates(states, p.GetNext());
            }

            while (Gen.END_OF_INPUT != spec.current_token)
            {
                /* Make state changes HERE. */
                states = gen.GetStates();

                /* Begin: Added for states. */
                gen.Advance();
                if (Gen.END_OF_INPUT == spec.current_token)
                {
                    break;
                }
                /* End: Added for states. */

                p.SetSib(Alloc.NewNfa(spec));
                p = p.GetSib();
                p.SetNext(rule());

                ProcessStates(states, p.GetNext());
            }

            /*
             * add pseudo-rules for BOL and EOF
             */
            p.SetSib(Alloc.NewNfa(spec));
            p = p.GetSib();
            p.SetNext(Alloc.NewNfa(spec));
            Nfa pnext = p.GetNext();
            pnext.SetEdge(Nfa.CCL);
            pnext.SetNext(Alloc.NewNfa(spec));
            pnext.SetCharSet(new CharSet());
            pnext.GetCharSet().add(spec.BOL);
            pnext.GetCharSet().add(spec.EOF);

            // do-nothing accept rule
            pnext.GetNext().SetAccept(new Accept(null, input.line_number + 1));

            /* add the pseudo rules */
            for (int i = 0; i < spec.states.Count; i++)
            {
                ArrayList srule = spec.state_rules[i];
                srule.Add(pnext);
            }

#if DESCENT_DEBUG
            Utility.leave("machine", spec.lexeme, spec.current_token);
#endif

            return(start);
        }
Пример #15
0
 private static void term(NfaPair pair)
 {
     if (Tokens.OPEN_PAREN == MakeNfa.spec.current_token)
     {
         MakeNfa.gen.Advance();
         MakeNfa.expr(pair);
         if (Tokens.CLOSE_PAREN == MakeNfa.spec.current_token)
         {
             MakeNfa.gen.Advance();
             return;
         }
         Error.ParseError(Errors.SYNTAX, MakeNfa.gen.InputFilePath, MakeNfa.input.line_number);
         return;
     }
     else
     {
         Nfa nfa = Alloc.NewNfa(MakeNfa.spec);
         pair.start = nfa;
         nfa.Next   = Alloc.NewNfa(MakeNfa.spec);
         pair.end   = nfa.Next;
         bool flag = MakeNfa.spec.current_token == Tokens.LETTER && char.IsLetter(MakeNfa.spec.current_token_value);
         if (MakeNfa.spec.current_token != Tokens.ANY && MakeNfa.spec.current_token != Tokens.CCL_START && (!MakeNfa.spec.IgnoreCase || !flag))
         {
             nfa.Edge = MakeNfa.spec.current_token_value;
             MakeNfa.gen.Advance();
             return;
         }
         nfa.Edge = '￾';
         nfa.SetCharSet(new CharSet());
         CharSet charSet = nfa.GetCharSet();
         if (MakeNfa.spec.IgnoreCase && flag)
         {
             charSet.addncase(MakeNfa.spec.current_token_value);
         }
         else
         {
             if (MakeNfa.spec.current_token == Tokens.ANY)
             {
                 charSet.add(10);
                 charSet.add(13);
                 charSet.add((int)MakeNfa.spec.BOL);
                 charSet.add((int)MakeNfa.spec.EOF);
                 charSet.complement();
             }
             else
             {
                 MakeNfa.gen.Advance();
                 if (MakeNfa.spec.current_token == Tokens.CHAR_CLASS)
                 {
                     MakeNfa.gen.Advance();
                     if (!charSet.AddClass(MakeNfa.spec.class_name.ToLower()))
                     {
                         Error.ParseError(Errors.InvalidCharClass, MakeNfa.gen.InputFilePath, MakeNfa.input.line_number);
                     }
                 }
                 else
                 {
                     if (MakeNfa.spec.current_token == Tokens.AT_BOL)
                     {
                         MakeNfa.gen.Advance();
                         charSet.add((int)MakeNfa.spec.BOL);
                         charSet.add((int)MakeNfa.spec.EOF);
                         charSet.complement();
                     }
                 }
                 if (MakeNfa.spec.current_token != Tokens.CCL_END)
                 {
                     MakeNfa.dodash(charSet);
                 }
             }
         }
         MakeNfa.gen.Advance();
         return;
     }
 }