Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        static void ProcessTest(string[] args)
        {
            switch (args[0].ToInt32())
            {
            case 1:
                var c2c = new CALtoCS();
                c2c.Compile(File.ReadAllLines("script/danbooru-pages.srcal"));
                break;

            case 2:
                var cal = new ESRCAL();
                cal.Compile(new[] { "5+4-(-4*(2-4)*2)/3+-(-(2*2+3)-2)*(3+1)" });
                //cal.Compile(new[] { "(2*2+3)" });
                break;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        static void ProcessTest(string[] args)
        {
            switch (args[0].ToInt32())
            {
            case 1:
                var c2c = new CALtoCS();
                c2c.Compile(File.ReadAllLines("script/danbooru-pages.srcal"));
                break;

            case 2:
                var cal = new ESRCAL();
                cal.Compile(new[] { "5+4-(-4*(2-4)*2)/3+-(-(2*2+3)-2)*(3+1)" });
                //cal.Compile(new[] { "(2*2+3)" });
                break;

            case 3:
                var x = new HTMLParser();

                break;

            case 4:
                var p = JSParserGenerator.Parser;

                break;

            case 5:
                var s = JSScannerGenerator.Scanner;
                break;

            case 6:

                var sg = new ScannerGenerator();

                sg.PushRule("", @"[\t\r\n ]");
                sg.PushRule("comma", "\\,");
                sg.PushRule("v_pair", "\\:");
                sg.PushRule("object_starts", "\\{");
                sg.PushRule("object_ends", "\\}");
                sg.PushRule("array_starts", "\\[");
                sg.PushRule("array_ends", "\\]");
                sg.PushRule("v_true", "true");
                sg.PushRule("v_false", "false");
                sg.PushRule("v_null", "null");

                var vv = new List <StringBuilder>(5);
                vv.Add(new StringBuilder());     // 6
                vv.Add(new StringBuilder());     // 5
                vv.Add(new StringBuilder());     // 4
                vv.Add(new StringBuilder());     // 3
                vv.Add(new StringBuilder());     // 2

                for (int cc = 0xc0; cc < 0xff; cc++)
                {
                    if ((cc & 0xfc) == 0xfc)
                    {
                        vv[0].Append("\\x" + cc.ToString("X2"));
                    }
                    else if ((cc & 0xf8) == 0xf8)
                    {
                        vv[1].Append("\\x" + cc.ToString("X2"));
                    }
                    else if ((cc & 0xf0) == 0xf0)
                    {
                        vv[2].Append("\\x" + cc.ToString("X2"));
                    }
                    else if ((cc & 0xe0) == 0xe0)
                    {
                        vv[3].Append("\\x" + cc.ToString("X2"));
                    }
                    else if ((cc & 0xc0) == 0xc0)
                    {
                        vv[4].Append("\\x" + cc.ToString("X2"));
                    }
                }

                var bb = new StringBuilder();
                for (int i = 0, j = 5; i < 5; i++, j--)
                {
                    bb.Append("[" + vv[i].ToString() + "]" + new string('.', j));

                    if (i != 4)
                    {
                        bb.Append("|");
                    }
                }

                //SimpleRegex sr = new SimpleRegex();
                //sr.MakeNFA($"\"({bb.ToString()}|[^\\\"]|\\\")*\"");
                //sr.OptimizeNFA();
                //sr.NFAtoDFA();
                //sr.MinimizeDFA();
                //Console.Instance.WriteLine(sr.PrintDiagram());

                //sg.PushRule("v_string", $"\"({bb.ToString()}|[^\\\"]|\\\")*\"");
                sg.PushRule("v_string", $"\"([^\\\"]|\\\")*\"");
                sg.PushRule("v_number", @"-?[0-9]+(\.[0-9]+)?([Ee][\+\-]?[0-9]+)?");

                sg.Generate();
                Console.Instance.WriteLine(sg.PrintDiagram());

                var s3 = sg.CreateScannerInstance();
                Console.Instance.WriteLine(s3.ToCSCode("json_lexer"));

                var gen = new ParserGenerator();

                var JSON     = gen.CreateNewProduction("JSON", false);
                var ARRAY    = gen.CreateNewProduction("ARRAY", false);
                var OBJECT   = gen.CreateNewProduction("OBJECT", false);
                var MEMBERS  = gen.CreateNewProduction("MEMBERS", false);
                var PAIR     = gen.CreateNewProduction("PAIR", false);
                var ELEMENTS = gen.CreateNewProduction("ELEMENTS", false);
                var VALUE    = gen.CreateNewProduction("VALUE", false);

                var object_starts = gen.CreateNewProduction("object_starts");
                var object_ends   = gen.CreateNewProduction("object_ends");
                var comma         = gen.CreateNewProduction("comma");
                var v_pair        = gen.CreateNewProduction("v_pair");
                var array_starts  = gen.CreateNewProduction("array_starts");
                var array_ends    = gen.CreateNewProduction("array_ends");
                var v_true        = gen.CreateNewProduction("v_true");
                var v_false       = gen.CreateNewProduction("v_false");
                var v_null        = gen.CreateNewProduction("v_null");
                var v_string      = gen.CreateNewProduction("v_string");
                var v_number      = gen.CreateNewProduction("v_number");

                JSON     |= OBJECT;
                JSON     |= ARRAY;
                OBJECT   |= object_starts + object_ends;
                OBJECT   |= object_starts + MEMBERS + object_ends;
                MEMBERS  |= PAIR;
                MEMBERS  |= PAIR + comma + MEMBERS;
                PAIR     |= v_string + v_pair + VALUE;
                ARRAY    |= array_starts + array_ends;
                ARRAY    |= array_starts + ELEMENTS + array_ends;
                ELEMENTS |= VALUE;
                ELEMENTS |= VALUE + comma + ELEMENTS;
                VALUE    |= v_string;
                VALUE    |= v_number;
                VALUE    |= OBJECT;
                VALUE    |= ARRAY;
                VALUE    |= v_true;
                VALUE    |= v_false;
                VALUE    |= v_null;

                gen.PushStarts(JSON);
                gen.PrintProductionRules();
                gen.GenerateLALR2();
                gen.PrintStates();
                gen.PrintTable();

                Console.Instance.WriteLine(gen.GlobalPrinter.ToString());
                Console.Instance.WriteLine(gen.CreateExtendedShiftReduceParserInstance().ToCSCode("json_parser"));

                break;
            }
        }