Esempio n. 1
0
        public void DebugPrule(Prule p)
        {
            string right = "";

            for (int i = 0; i < p.rightChain.Count; i++)
            {
                right += p.rightChain[i].ToString();
            }
            Console.WriteLine(p.leftNoTerm + " -> " + right + " ");
        }
Esempio n. 2
0
 //проверка пустоты правой цепочки
 private bool ContainEps(Prule p)
 {
     if (p.rightChain.Contains(""))
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Esempio n. 3
0
        //все символы в правиле
        private ArrayList SymbInRules(Prule p)
        {
            ArrayList SymbInRules = new ArrayList()
            {
                p.LeftNoTerm
            };

            for (int i = 0; i < p.rightChain.Count; i++)
            {
                SymbInRules.Add(p.rightChain[i].ToString());
            }
            return(SymbInRules);
        }
        public List <Prule> getRules(string noTermSymbol)
        {
            List <Prule> result = new List <Prule>();

            for (int i = 0; i < G.Prules.Count; ++i)
            {
                Prule currRule = (Prule)G.Prules[i];
                if (currRule.leftNoTerm == noTermSymbol)
                {
                    result.Add(currRule);
                }
            }
            return(result);
        }
Esempio n. 5
0
        //удаление эпсилон правил
        public myGrammar EpsDelete()
        {
            Console.WriteLine("\t\tDeleting epsylon rules");
            Console.WriteLine("Executing: ");
            ArrayList Ve = ShortNoTerm(); Debug("Ve", Ve);
            ArrayList P1 = new ArrayList();
            ArrayList V1 = this.V;

            foreach (Prule p in Prules)
            {
                if (!ContainEps(p))
                {
                    // DebugPrule(p);
                    P1.Add(p);
                    Prule p1 = new Prule(p.leftNoTerm, TermReturn(p.rightChain));
                    DebugPrule(p1);
                    if (p.rightChain.Count != 1)
                    {
                        Console.WriteLine("No contain");
                        P1.Add(p1);
                    }
                    else
                    {
                        Console.WriteLine("Contain");
                    }
                }
            }
            if (Ve.Contains(this.S0))
            {
                V1.Add("S1");
                P1.Add(new Prule("S1", new ArrayList()
                {
                    this.S0
                }));
                P1.Add(new Prule("S1", new ArrayList()
                {
                    ""
                }));
                return(new myGrammar(this.T, V1, P1, "S1"));
            }
            else
            {
                return(new myGrammar(this.T, V1, P1, this.S0));
            }
        }
        public void ComputeFirstSets(myGrammar grammar)
        {
            for (int i = 0; i < grammar.T.Count; i++)
            {
                FirstSet[(string)grammar.T[i]] = new HashSet <string>()
                {
                    grammar.T[i].ToString()
                }
            }
            ;                                                                                         // FIRST[c] = {c}*/
            for (int i = 0; i < grammar.V.Count; i++)
            {
                FirstSet[(string)grammar.V[i]] = new HashSet <string>();                     //First[x] = empty list
            }
            bool changes = true;

            while (changes)
            {
                changes = false;
                for (int i = 0; i < grammar.Prules.Count; ++i)
                {
                    // Для каждого правила X-> Y0Y1…Yn
                    Prule         currRule = (Prule)(grammar.Prules[i]);
                    string        X        = currRule.LeftNoTerm;
                    List <string> Y        = currRule.rightChain.Cast <string>().ToList();
                    for (int k = 0; k < Y.Count; k++)
                    {
                        foreach (string currFirstSymb in (HashSet <string>)FirstSet[Y[k]])
                        {
                            if (((HashSet <string>)FirstSet[X]).Add(currFirstSymb)) //Добавить а в FirstSets[X]
                            {
                                changes = true;
                            }
                        }
                        if (!((HashSet <string>)FirstSet[Y[k]]).Contains(""))
                        {
                            break;
                        }
                    }
                }
            } //  пока вносятся изменения
        }
        public void ComputeFollowSets(myGrammar grammar)
        {
            for (int i = 0; i < grammar.V.Count; i++)
            {
                FollowSet[(string)grammar.V[i]] = new HashSet <string>();
            }
            FollowSet[G.S0] = new HashSet <string>()
            {
                ""
            };
            bool changes = true;

            while (changes)
            {
                changes = false;
                for (int i = 0; i < grammar.Prules.Count; ++i)
                {
                    // Для каждого правила X-> Y0Y1…Yn
                    Prule         currRule   = (Prule)(grammar.Prules[i]);
                    List <string> rightChain = currRule.RightChain.Cast <string>().ToList();
                    for (int indexOfSymbol = 0; indexOfSymbol < rightChain.Count; ++indexOfSymbol)
                    {
                        string currSymbol = rightChain[indexOfSymbol];
                        if (G.T.Contains(currSymbol))
                        {
                            continue;
                        }
                        if (indexOfSymbol == rightChain.Count - 1)
                        {
                            foreach (string currFollowSymbol in (HashSet <string>)FollowSet[currRule.LeftNoTerm])
                            {
                                if (((HashSet <string>)FollowSet[rightChain[indexOfSymbol]]).Add(currFollowSymbol))
                                {
                                    changes = true;
                                }
                            }
                        }
                        else
                        {
                            List <string> currFirst = First(rightChain[indexOfSymbol + 1]);
                            bool          epsFound  = false;
                            foreach (var currFirstSymbol in currFirst)
                            {
                                if (currFirstSymbol != "")
                                {
                                    if (((HashSet <string>)FollowSet[rightChain[indexOfSymbol]]).Add(currFirstSymbol))
                                    {
                                        changes = true;
                                    }
                                }
                                else
                                {
                                    epsFound = true;
                                }
                            }
                            if (epsFound)
                            {
                                foreach (string currFollowSymbol in (HashSet <string>)FollowSet[currRule.LeftNoTerm])
                                {
                                    if (((HashSet <string>)FollowSet[rightChain[indexOfSymbol]]).Add(currFollowSymbol))
                                    {
                                        changes = true;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }