Exemple #1
0
        public static void Display_Table()
        {
            var terminale = Get_Terminals();

            Console.Write("    ");
            for (int i = 0; i < terminale.Count; i++)
            {
                Console.Write("   " + terminale[i] + "   ");
            }
            Console.WriteLine();


            for (int i = 0; i < Graf_List.Count; i++)
            {
                var rand = new List <String>();
                for (int t = 0; t < terminale.Count; t++)
                {
                    rand.Add("*");
                }

                for (int t = 0; t < terminale.Count; t++)
                {
                    var simbol = terminale[t];
                    if (simbol.Equals("$") && Graf_List[i].Lista_noduri[0].Equals("S'>S.,$") == true)
                    {
                        rand[terminale.IndexOf(simbol)] = "acc";
                    }
                    else if (char.IsLower(simbol.ToCharArray()[0]))
                    {
                        var item = Graf_List[i].Lista_noduri.Where(elem => elem.Split('.')[1][0].ToString().Equals(simbol.ToString())).FirstOrDefault();
                        if (item != null)
                        {
                            var new_item = Move_Dot(item, 1);
                            var indice   = Graf_List.IndexOf(Graf_List.Where(elem => elem.Lista_noduri[0].Equals(new_item)).FirstOrDefault());
                            rand[terminale.IndexOf(simbol)] = "s" + indice;
                        }
                    }
                    else if (char.IsUpper(simbol.ToCharArray()[0]))
                    {
                        var item = Graf_List[i].Lista_noduri.Where(elem => elem.Split('.')[1][0].ToString().Equals(simbol.ToString())).FirstOrDefault();
                        if (item != null)
                        {
                            var new_item = Move_Dot(item, 1);
                            var indice   = Graf_List.IndexOf(Graf_List.Where(elem => elem.Lista_noduri[0].Equals(new_item)).FirstOrDefault());
                            rand[terminale.IndexOf(simbol)] = indice.ToString();
                        }
                    }

                    foreach (var item in Graf_List[i].Lista_noduri)
                    {
                        if (item.Split('.')[1][0].Equals(',') && item.Equals("S'>S.,$") == false)
                        {
                            var after  = item.Split(',')[1].Split('/');
                            var before = item.Split('.')[0];
                            var key    = Dictionary_Grammer.Where(elem => elem.Value.Equals(before.ToString())).FirstOrDefault().Key;
                            foreach (var aft in after)
                            {
                                if (aft.ToString().Equals(simbol.ToString()))
                                {
                                    if (rand[terminale.IndexOf(simbol)].Equals("*"))
                                    {
                                        rand[terminale.IndexOf(simbol)] = "r" + (key + 1);
                                    }
                                    else
                                    {
                                        rand[terminale.IndexOf(simbol)] += ",r" + (key + 1);
                                    }
                                }
                            }
                        }
                    }
                }
                MatrixTable.Add(rand);
            }
            for (int i = 0; i < MatrixTable.Count; i++)
            {
                Console.Write(i + "   ");
                for (int j = 0; j < MatrixTable[i].Count; j++)
                {
                    Console.Write("   " + MatrixTable[i][j] + "   ");
                }
                Console.WriteLine();
            }
        }
Exemple #2
0
        private static void Create_I0(string s, Graf g, int contor = 0, bool reset_parcurs = false)
        {
            if (reset_parcurs == false)
            {
                for (int i = 0; i < parcurs.Count; i++)
                {
                    parcurs[i] = false;
                }
            }

            if (contor == 0)
            {
                //s='S'>.S,$'
                //List<string> generated = new List<string>();
                List <string> firstAfterDotProductions = new List <string>();
                var           firstAfterDot            = s.Split('.')[1][0].ToString(); //S
                var           afterDot   = s.Split('.')[1].ToString();
                var           afterComma = s.Split(',')[1].ToString();
                var           items      = Dictionary_Grammer.Where(elem => elem.Value[0].ToString().Equals(firstAfterDot));
                foreach (var item in items)
                {
                    var left  = item.Value[0].ToString();
                    var right = item.Value.Split('>')[1];
                    //generated.Add(left + ">." + right + ",");
                    if (firstAfterDot.Equals(right[0].ToString()))
                    {
                        firstAfterDotProductions.Add(right.Substring(1));
                    }
                }
                //string lookahead = first(afterDot.Substring(1).Replace(",",""));
                string lookahead = String.Empty;
                if (!afterDot[1].ToString().Equals(","))
                {
                    lookahead = first(afterDot.Substring(1).Split(',')[0]);
                    if (lookahead.Equals(""))
                    {
                        lookahead = afterComma;
                    }
                }
                else
                {
                    lookahead = afterComma;
                }
                foreach (var i in firstAfterDotProductions)
                {
                    lookahead += '/' + first(i);
                }
                var    result2 = lookahead.Replace("/", "").Distinct();
                string result3 = string.Empty;
                foreach (var i in result2)
                {
                    result3 += "/" + i.ToString();
                }

                lookahead = result3.Substring(1);

                foreach (var item in items)
                {
                    var left  = item.Value[0].ToString();
                    var right = item.Value.Split('>')[1];
                    g.Lista_noduri.Add(left + ">." + right + ',' + lookahead);
                    if (left.Equals(right[0].ToString()))
                    {
                        var el  = left + ">" + right;
                        var poz = Grammer.IndexOf(Grammer.Where(elem => elem.Equals(el)).FirstOrDefault());
                        if (parcurs[poz] == false)
                        {
                            parcurs[poz] = true;
                            Create_I0(left + ">." + right + ',' + lookahead, g, 1, true);
                        }
                    }
                    else
                    {
                        var el  = left + ">" + right;
                        var poz = Grammer.IndexOf(Grammer.Where(elem => elem.Equals(el)).FirstOrDefault());
                        if (parcurs[poz] == false)
                        {
                            parcurs[poz] = true;
                            Create_I0(left + ">." + right + ',' + lookahead, g, 0, true);
                        }
                    }
                }
            }
        }