public void SetGrammaticKS(string str)
        {
            try
            {
                str = str.Replace("\r", "");
                if (str[0] == '\n')
                {
                    str = str.Substring(1);
                }
                List <Regular> list = new List <Regular>();
                while (str.Length > 1)
                {
                    Regular regular = new Regular();
                    regular.Right = new List <List <string> >();
                    var index = str.IndexOf("-");
                    var ch    = str.Substring(0, index);
                    if (ch.Length != 1)
                    {
                        throw new Exception("В левой части правила больше одного символа!");
                    }

                    if ("0987654321abcdefghijklmnopqrstuvwxyz".Contains(ch))
                    {
                        throw new Exception("В левой части правила находится терминальный символ!");
                    }
                    regular.Left = ch;

                    str   = str.Substring(index + 2);
                    index = str.IndexOf("\n");
                    if (index < 0)
                    {
                        var tmp1 = ConvertStringToStringList(str.Substring(0), "|");
                        foreach (var tmp in tmp1)
                        {
                            regular.Right.Add(ConvertStringToStringList(tmp));
                        }

                        //if (regular.right.Any(x => !x.Any(y => !"0987654321abcdefghijklmnopqrstuvwxyz".Contains(y) && !y.Equals(Lambda))))
                        //{
                        //    throw new Exception("в правой части правила находится только нетерминальный символ!");
                        //}

                        list.Add(regular);
                        break;
                    }

                    var tmp2 = ConvertStringToStringList(str.Substring(0, index), "|");
                    foreach (var tmp in tmp2)
                    {
                        regular.Right.Add(ConvertStringToStringList(tmp));
                    }

                    str = str.Substring(index + 1);
                    list.Add(regular);
                }

                Regulation = list;
            }
            catch (Exception ex)
            {
                if (string.IsNullOrEmpty(ex.Message))
                {
                    throw new Exception("Ошибка чтения КС-грамматики!");
                }
                else
                {
                    throw ex;
                }
            }
        }
        public Grammatic ConvertToBNFGrammatik()
        {
            var BNF = new Grammatic();

            BNF.VN         = VN.ToList();
            BNF.VT         = VT.ToList();
            BNF.Start      = Start;
            BNF.Lambda     = Lambda;
            BNF.Regulation = new List <Regular>();
            var regulation = Regulation.ToList();

            for (int i = 0; i < regulation.Count; i++)
            {
                if (regulation[i].Right.Count != 1)
                {
                    continue;
                }

                if (regulation[i].Right[0].Count != 1)
                {
                    continue;
                }

                if (!VN.Any(x => x.Equals(regulation[i].Right.FirstOrDefault())))
                {
                    BNF.Regulation.Add(regulation[i]);
                    regulation.RemoveAt(i);
                    i--;
                }
            }

            foreach (var regular in regulation)
            {
                BNF.Regulation.Add(new Regular()
                {
                    Left = regular.Left
                });
                BNF.Regulation.LastOrDefault().Right = new List <List <string> >();
                var nowRegulation = BNF.Regulation.LastOrDefault().Right;

                foreach (var p in regular.Right)
                {
                    var vnCount = VNCount(p, VN);
                    //1
                    if ((vnCount == 2 && p.Count == 2) || (vnCount == 0 && p.Count == 1))
                    {
                        nowRegulation.Add(p);
                        continue;
                    }
                    //2 и 3
                    if (vnCount == 1 && p.Count == 2)
                    {
                        var term = VN.Any(x => x.Equals(p[0])) ? p[1] : p[0];
                        var simv = GetRegularVNByVT(BNF.Regulation, term);
                        if (string.IsNullOrEmpty(simv))
                        {
                            var newVN = CreateVN(BNF.VN);
                            BNF.VN.Add(newVN);
                            var    newRegular = new List <string>();
                            string simvols    = "";
                            foreach (var ch in p)
                            {
                                if (BNF.VN.Any(x => x.Equals(ch)))
                                {
                                    newRegular.Add(ch);
                                }
                                else
                                {
                                    newRegular.Add(newVN);
                                    simvols = ch;
                                }
                            }

                            nowRegulation.Add(newRegular);
                            BNF.Regulation.Add(new Regular()
                            {
                                Left  = newVN,
                                Right = new List <List <string> >()
                                {
                                    new List <string>()
                                    {
                                        simvols
                                    }
                                }
                            });
                            continue;
                        }

                        var newRegular2 = new List <string>();
                        foreach (var ch in p)
                        {
                            if (BNF.VN.Any(x => x.Equals(ch)))
                            {
                                newRegular2.Add(ch);
                            }
                            else
                            {
                                newRegular2.Add(simv);
                            }
                        }

                        nowRegulation.Add(newRegular2);
                        continue;
                    }

                    //4
                    if (vnCount == 0 && p.Count == 2)
                    {
                        var sim1 = GetRegularVNByVT(BNF.Regulation, p[0]);
                        if (string.IsNullOrEmpty(sim1))
                        {
                            sim1 = CreateVN(BNF.VN);
                            BNF.VN.Add(sim1);
                            BNF.Regulation.Add(new Regular()
                            {
                                Left  = sim1,
                                Right = new List <List <string> >()
                                {
                                    new List <string>()
                                    {
                                        p[0]
                                    }
                                }
                            });
                        }
                        var sim2 = GetRegularVNByVT(BNF.Regulation, p[1]);
                        if (string.IsNullOrEmpty(sim2))
                        {
                            sim2 = CreateVN(BNF.VN);
                            BNF.VN.Add(sim2);
                            BNF.Regulation.Add(new Regular()
                            {
                                Left  = sim2,
                                Right = new List <List <string> >()
                                {
                                    new List <string>()
                                    {
                                        p[1]
                                    }
                                }
                            });
                        }
                        nowRegulation.Add(new List <string>()
                        {
                            sim1, sim2
                        });
                        continue;
                    }

                    //5
                    var newSim = CreateVN(BNF.VN);
                    //BNF.VN.Add(newSim);
                    if (VN.Any(x => x.Equals(p[0])))
                    {
                        nowRegulation.Add(new List <string>()
                        {
                            p[0], newSim
                        });
                    }
                    else
                    {
                        var sim2 = GetRegularVNByVT(BNF.Regulation, p[0]);
                        if (string.IsNullOrEmpty(sim2))
                        {
                            sim2 = CreateVN(BNF.VN);
                            BNF.VN.Add(sim2);
                            BNF.Regulation.Add(new Regular()
                            {
                                Left  = sim2,
                                Right = new List <List <string> >()
                                {
                                    new List <string>()
                                    {
                                        p[0]
                                    }
                                }
                            });
                        }
                        nowRegulation.Add(new List <string>()
                        {
                            sim2, newSim
                        });
                    }
                    for (var index = 1; index < p.Count - 1; index++)
                    {
                        Regular reg = new Regular();
                        reg.Left  = newSim;
                        reg.Right = new List <List <string> >();
                        BNF.VN.Add(newSim);
                        var ch = p[index];
                        if (index == p.Count - 2)
                        {
                            var lastSim = p[p.Count - 1];
                            if (VN.Any(x => x.Equals(lastSim)))
                            {
                                newSim = lastSim;
                            }
                            else
                            {
                                var sim2 = GetRegularVNByVT(BNF.Regulation, lastSim);
                                if (string.IsNullOrEmpty(sim2))
                                {
                                    sim2 = CreateVN(BNF.VN);
                                    BNF.VN.Add(sim2);
                                    BNF.Regulation.Add(new Regular()
                                    {
                                        Left  = sim2,
                                        Right = new List <List <string> >()
                                        {
                                            new List <string>()
                                            {
                                                lastSim
                                            }
                                        }
                                    });
                                }
                                newSim = sim2;
                            }
                        }
                        else
                        {
                            newSim = CreateVN(BNF.VN);
                        }
                        if (VN.Any(x => x.Equals(ch)))
                        {
                            reg.Right.Add(new List <string>()
                            {
                                ch, newSim
                            });
                        }
                        else
                        {
                            var sim2 = GetRegularVNByVT(BNF.Regulation, ch);
                            if (string.IsNullOrEmpty(sim2))
                            {
                                sim2 = CreateVN(BNF.VN);
                                BNF.VN.Add(sim2);
                                BNF.Regulation.Add(new Regular()
                                {
                                    Left  = sim2,
                                    Right = new List <List <string> >()
                                    {
                                        new List <string>()
                                        {
                                            ch
                                        }
                                    }
                                });
                            }
                            reg.Right.Add(new List <string>()
                            {
                                sim2, newSim
                            });
                        }
                        BNF.Regulation.Add(reg);
                    }
                }
            }

            return(BNF);
        }