コード例 #1
0
ファイル: Program.cs プロジェクト: sh1nu11bi/Regex
        static void _RunStress2()
        {
            CharFA <string> fa  = null;
            var             min = 255;
            var             max = 511;

            Console.Write("Building NFA matching integer values {0}-{1} ", min, max);
            for (var i = min; i <= max; ++i)
            {
                // for perf reasons we reduce every 12 times
                if (null == fa)
                {
                    fa = CharFA <string> .Literal(i.ToString());
                }
                else
                {
                    fa = CharFA <string> .Or(new CharFA <string>[] { fa, CharFA <string> .Literal(i.ToString()) });
                }
                if (0 == (i % 12))
                {
                    Console.Write('.');
                }
                // replace the above "Console.Write('.');" line with below is MUCH faster
                //	fa=fa.Reduce(new _ConsoleProgress());
            }
            Console.WriteLine();
            Console.WriteLine("C# integer NFA has {0} states.", fa.FillClosure().Count);
            fa = fa.Reduce(new _ConsoleProgress());
            Console.WriteLine();
            Console.WriteLine("C# integer DFA has {0} states.", fa.FillClosure().Count);
            Console.WriteLine("Rendering stress2.jpg");
            fa.RenderToFile(@"..\..\..\stress2.jpg");
        }
コード例 #2
0
        public override CharFA ToFA(EbnfDocument parent, Cfg cfg)
        {
            string sym = "";

            if (null != parent)
            {
                sym = parent.GetContainingIdForExpression(this);
            }
            if (null == Right)
            {
                if (null == Left)
                {
                    return(null);
                }
                return(CharFA.Optional(Left.ToFA(parent, cfg), sym));
            }
            else if (null == Left)
            {
                return(CharFA.Optional(Right.ToFA(parent, cfg), sym));
            }
            return(CharFA.Or(new CharFA[] { Left.ToFA(parent, cfg), Right.ToFA(parent, cfg) }, sym));
        }
コード例 #3
0
        static void _RunStress2()
        {
            CharFA <string> fa  = null;
            var             min = 599;
            var             max = 639;

            Console.Write("Building NFA matching integer values {0}-{1} ", min, max);
            for (var i = min; i <= max; ++i)
            {
                if (null == fa)
                {
                    fa = CharFA <string> .Literal(i.ToString());
                }
                else
                {
                    fa = CharFA <string> .Or(new CharFA <string>[] { fa, CharFA <string> .Literal(i.ToString()) });
                }
                // for perf reasons we can reduce every 12 times
                if (0 == (i % 12))
                {
                    Console.Write('.');
                }
                // replace the above "Console.Write('.');" line with below is MUCH faster
                //	fa=fa.Reduce(new _ConsoleProgress());
            }
            Console.WriteLine();
            fa.TrimNeutrals();
            //fa.TrimDuplicates();
            Console.WriteLine("C# integer NFA has {0} states.", fa.FillClosure().Count);
            fa.RenderToFile(@"..\..\..\stress2_nfa.jpg");
            fa = fa.Reduce(new _ConsoleProgress());
            Console.WriteLine();
            Console.WriteLine("C# integer DFA has {0} states.", fa.FillClosure().Count);
            //var expr = RegexExpression.FromFA(fa);
            //Console.WriteLine("Final Expression: {0}", expr);
            Console.WriteLine("Rendering stress2.jpg");
            fa.RenderToFile(@"..\..\..\stress2.jpg");
        }
コード例 #4
0
        static void _BuildArticleImages()
        {
            // this generates the figures used in the code project article
            // at https://www.codeproject.com/Articles/5251476/How-to-Build-a-Regex-Engine-in-Csharp
            var litA = CharFA <string> .Literal("ABC", "Accept");

            litA.RenderToFile(@"..\..\..\literal.jpg");
            var litAa = CharFA <string> .CaseInsensitive(litA, "Accept");

            litAa.RenderToFile(@"..\..\..\literal_ci.jpg");
            var opt = CharFA <string> .Optional(litA, "Accept");

            opt.RenderToFile(@"..\..\..\optional.jpg");
            var litB = CharFA <string> .Literal("DEF");

            var or = CharFA <string> .Or(new CharFA <string>[] { litA, litB }, "Accept");

            or.RenderToFile(@"..\..\..\or.jpg");
            var set = CharFA <string> .Set("ABC", "Accept");

            set.RenderToFile(@"..\..\..\set.jpg");
            var loop = CharFA <string> .Repeat(litA, 1, -1, "Accept");

            loop.RenderToFile(@"..\..\..\repeat.jpg");
            var concat = CharFA <string> .Concat(new CharFA <string>[] { litA, litB }, "Accept");

            concat.RenderToFile(@"..\..\..\concat.jpg");
            var foobar = CharFA <string> .Or(new CharFA <string>[] { CharFA <string> .Literal("foo"), CharFA <string> .Literal("bar") }, "Accept");

            foobar.RenderToFile(@"..\..\..\foobar_nfa.jpg");
            var rfoobar = foobar.Reduce();

            rfoobar.RenderToFile(@"..\..\..\foobar.jpg");
            var lfoobar = CharFA <string> .Repeat(foobar, 1, -1, "Accept");

            lfoobar.RenderToFile(@"..\..\..\foobar_loop_nfa.jpg");
            var rlfoobar = lfoobar.Reduce();

            rlfoobar.RenderToFile(@"..\..\..\foobar_loop.jpg");

            var digits = CharFA <string> .Repeat(
                CharFA <string> .Set("0123456789"),
                1, -1
                , "Digits");

            var word = CharFA <string> .Repeat(
                CharFA <string> .Set(new CharRange[] { new CharRange('A', 'Z'), new CharRange('a', 'z') }),
                1, -1
                , "Word");

            var whitespace = CharFA <string> .Repeat(
                CharFA <string> .Set(" \t\r\n\v\f"),
                1, -1
                , "Whitespace");

            var lexer = CharFA <string> .ToLexer(digits, word, whitespace);

            lexer.RenderToFile(@"..\..\..\lexer.jpg");
            var dopt = new CharFA <string> .DotGraphOptions();

            dopt.DebugSourceNfa = lexer;
            var dlexer = lexer.ToDfa();

            dlexer.RenderToFile(@"..\..\..\dlexer.jpg", dopt
                                );
            dlexer.RenderToFile(@"..\..\..\dlexer2.jpg");
            var dom = RegexExpression.Parse("(ABC|DEF)+");
            var fa  = dom.ToFA("Accept");

            fa.RenderToFile(@"..\..\..\ABCorDEFloop.jpg");
        }