Ejemplo n.º 1
0
        //построить Delta-правила ДКА
        public void BuildDeltaDKAutomate(myAutomate ndka)
        {
            this.Sigma     = ndka.Sigma;
            this.DeltaList = ndka.DeltaList;
            ArrayList currState = EpsClosure(new ArrayList()
            {
                ndka.Q0
            });

            //Debug("step 1", currState);

            config.Add(SetName(currState));
            //Debug("name",SetName(currState));
            Dtran(currState);
            this.Q         = config;
            this.Q0        = this.Q[0].ToString();
            this.DeltaList = DeltaD;
            this.F         = getF(config, ndka.F);

            /*  this.Q = makeNames(config);
             * this.Q0 = this.Q[0].ToString();
             * this.DeltaList = NameRules(DeltaD);
             * this.F = makeNames(getF(config, ndka.F));*/
        }
Ejemplo n.º 2
0
        static void Main()
        {
            while (true)
            {
                Dialog();
                switch (Console.ReadLine())
                {
                case "1":
                    myAutomate ka = new myAutomate(new ArrayList()
                    {
                        "S0", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "qf"
                    },
                                                   new ArrayList()
                    {
                        "0", "1", "-", "+", ""
                    },
                                                   new ArrayList()
                    {
                        "qf"
                    }, "S0");
                    ka.AddRule("S0", "1", "A");
                    ka.AddRule("A", "0", "B");
                    ka.AddRule("B", "1", "C");
                    ka.AddRule("C", "0", "D");
                    ka.AddRule("D", "-", "E");
                    ka.AddRule("E", "1", "F");
                    ka.AddRule("F", "+", "G");
                    ka.AddRule("G", "0", "qf");
                    ka.AddRule("G", "1", "qf");

                    Console.WriteLine("Enter line to execute :");
                    ka.Execute(Console.ReadLine());
                    break;

                case "2.1":
                    myAutomate example = new myAutomate(new ArrayList()
                    {
                        "S0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "qf"
                    },
                                                        new ArrayList()
                    {
                        "a", "b"
                    },
                                                        new ArrayList()
                    {
                        "qf"
                    }, "S0");
                    example.AddRule("S0", "", "1");
                    example.AddRule("S0", "", "7");
                    example.AddRule("1", "", "2");
                    example.AddRule("1", "", "4");
                    example.AddRule("2", "a", "3");
                    example.AddRule("4", "b", "5");
                    example.AddRule("3", "", "6");
                    example.AddRule("5", "", "6");
                    example.AddRule("6", "", "1");
                    example.AddRule("6", "", "7");
                    example.AddRule("7", "a", "8");
                    example.AddRule("8", "b", "9");
                    example.AddRule("9", "b", "qf");

                    myAutomate dkaEX = new myAutomate();
                    dkaEX.BuildDeltaDKAutomate(example);
                    dkaEX.DebugAuto();
                    Console.WriteLine("Enter line to execute :");
                    dkaEX.Execute(Console.ReadLine());

                    break;

                case "2":
                    myAutomate ndka = new myAutomate(new ArrayList()
                    {
                        "S0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                        "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "qf"
                    },
                                                     new ArrayList()
                    {
                        "1", "0", "+", "2"
                    },
                                                     new ArrayList()
                    {
                        "qf"
                    }, "S0");
                    ndka.AddRule("S0", "1", "1");              //W1
                    ndka.AddRule("1", "0", "2");
                    ndka.AddRule("2", "+", "3");

                    ndka.AddRule("3", "", "4");                //W2
                    ndka.AddRule("4", "", "5");
                    ndka.AddRule("4", "", "7");
                    ndka.AddRule("4", "", "9");
                    ndka.AddRule("5", "1", "6");
                    ndka.AddRule("7", "2", "8");
                    ndka.AddRule("6", "", "9");
                    ndka.AddRule("8", "", "9");
                    ndka.AddRule("9", "", "4");
                    ndka.AddRule("9", "", "10");

                    ndka.AddRule("10", "1", "11");              //W3
                    ndka.AddRule("11", "0", "12");
                    ndka.AddRule("12", "", "13");
                    ndka.AddRule("13", "", "9");
                    ndka.AddRule("13", "", "14");

                    ndka.AddRule("14", "", "15");               //W4
                    ndka.AddRule("14", "", "17");
                    ndka.AddRule("15", "0", "16");
                    ndka.AddRule("17", "1", "18");
                    ndka.AddRule("16", "", "19");
                    ndka.AddRule("18", "", "19");
                    ndka.AddRule("19", "", "14");
                    ndka.AddRule("19", "", "20");
                    ndka.AddRule("20", "", "15");
                    ndka.AddRule("14", "", "qf");
                    ndka.AddRule("20", "", "qf");

                    myAutomate dka = new myAutomate();
                    dka.BuildDeltaDKAutomate(ndka);
                    dka.DebugAuto();
                    Console.WriteLine("Enter line to execute :");
                    dka.Execute(Console.ReadLine());
                    break;

                case "3":
                    myGrammar G = new myGrammar(new ArrayList()
                    {
                        "a", "b", "c", "d"
                    }, new ArrayList()
                    {
                        "S", "A", "B", "C", "F"
                    }, "S");
                    G.AddRule("S", new ArrayList()
                    {
                        "b"
                    });
                    G.AddRule("S", new ArrayList()
                    {
                        "c", "A", "B"
                    });
                    G.AddRule("A", new ArrayList()
                    {
                        "A", "b"
                    });
                    G.AddRule("A", new ArrayList()
                    {
                        "c"
                    });
                    G.AddRule("B", new ArrayList()
                    {
                        "c", "B"
                    });
                    G.AddRule("C", new ArrayList()
                    {
                        "C", "a"
                    });
                    G.AddRule("F", new ArrayList()
                    {
                        "d"
                    });

                    G.DebugPrules();

                    myGrammar G1 = G.unUsefulDelete();
                    G1.DebugPrules();

                    myGrammar G2 = G1.EpsDelete();
                    G2.DebugPrules();

                    myGrammar G3 = G2.ChainRuleDelete();
                    G3.DebugPrules();

                    myGrammar G4 = G3.LeftRecursDelete();
                    G4.DebugPrules();
                    // G4 - приведенная грамматика

                    break;

                case "4":     //МП - автоматы
                    myGrammar kcGrammar = new myGrammar(new ArrayList()
                    {
                        "i", ":", "*", "(", ")"
                    },
                                                        new ArrayList()
                    {
                        "S", "F", "L"
                    }, "S");
                    kcGrammar.AddRule("S", new ArrayList()
                    {
                        "(", "F", ":", "L", ")"
                    });
                    kcGrammar.AddRule("F", new ArrayList()
                    {
                        "L", "*"
                    });
                    kcGrammar.AddRule("F", new ArrayList()
                    {
                        "i"
                    });
                    kcGrammar.AddRule("L", new ArrayList()
                    {
                        "F"
                    });

                    Console.Write("Debug KC-Grammar ");
                    kcGrammar.DebugPrules();

                    myMp MP = new myMp(kcGrammar);
                    Console.Write("Debug Mp ");
                    MP.debugDelta();

                    Console.WriteLine("\nEnter the line :");
                    Console.WriteLine(MP.Execute(Console.ReadLine(), 0, 0, MP.Z).ToString());
                    break;

                case "5":     // LL Разбор
                    myGrammar exemple = new myGrammar(new ArrayList()
                    {
                        "i", "(", ")", "+", "*", ""
                    },
                                                      new ArrayList()
                    {
                        "S", "E", "T", "T'", "P"
                    }, "S");
                    exemple.AddRule("S", new ArrayList()
                    {
                        "T", "E"
                    });
                    exemple.AddRule("E", new ArrayList()
                    {
                        "+", "T", "E"
                    });
                    exemple.AddRule("E", new ArrayList()
                    {
                        ""
                    });
                    exemple.AddRule("T", new ArrayList()
                    {
                        "P", "T'"
                    });
                    exemple.AddRule("T'", new ArrayList()
                    {
                        "*", "P", "T'"
                    });
                    exemple.AddRule("T'", new ArrayList()
                    {
                        ""
                    });
                    exemple.AddRule("P", new ArrayList()
                    {
                        "(", "S", ")"
                    });
                    exemple.AddRule("P", new ArrayList()
                    {
                        "i"
                    });

                    LLParser parser = new LLParser(exemple);
                    Console.WriteLine("Введите строку: ");
                    if (parser.Parse(Console.ReadLine()))
                    {
                        Console.WriteLine("Успех. Строка соответствует грамматике.");
                        Console.WriteLine(parser.OutputConfigure);
                    }
                    else
                    {
                        Console.WriteLine("Не успех. Строка не соответствует грамматике.");
                    }
                    break;

                case "6":
                    ReadGrammar();
                    Execute();
                    break;

                case "6.1":
                    Terminals    = "+*i()";
                    NonTerminals = "STP";
                    Grammar.Add("S S+T");
                    Grammar.Add("S T");
                    Grammar.Add("T T*P");
                    Grammar.Add("T P");
                    Grammar.Add("P i");
                    Grammar.Add("P (S)");
                    Execute();
                    break;

                case "7":     //МП - автоматы
                    myMp Mp = new myMp(new ArrayList()
                    {
                        "q0", "q1", "q2", "qf"
                    }, new ArrayList()
                    {
                        "a", "b"
                    }, new ArrayList()
                    {
                        "z0", "a"
                    }, "q0", new ArrayList()
                    {
                        "qf"
                    });

                    Mp.addDeltaRule("q0", "a", "z0", new ArrayList()
                    {
                        "q1"
                    }, new ArrayList()
                    {
                        "a", "z0"
                    });
                    Mp.addDeltaRule("q1", "a", "a", new ArrayList()
                    {
                        "q1"
                    }, new ArrayList()
                    {
                        "a", "a"
                    });
                    Mp.addDeltaRule("q1", "b", "a", new ArrayList()
                    {
                        "q2"
                    }, new ArrayList()
                    {
                        "e"
                    });
                    Mp.addDeltaRule("q2", "b", "a", new ArrayList()
                    {
                        "q2"
                    }, new ArrayList()
                    {
                        "e"
                    });
                    Mp.addDeltaRule("q2", "e", "z0", new ArrayList()
                    {
                        "qf"
                    }, new ArrayList()
                    {
                        "e"
                    });
                    Console.Write("Debug Mp ");
                    Mp.debugDelta();

                    Console.WriteLine("\nEnter the line :");
                    Console.WriteLine(Mp.Execute_(Console.ReadLine()).ToString());
                    break;
                }
            }
        }