Ejemplo n.º 1
0
        public void ReadFromFile(string filename)
        {
            string[] lines = System.IO.File.ReadAllLines("../../Input/" + filename);
            Nonterminals = new HashSet <string>(lines[0].Split(' '));
            Terminals    = new HashSet <string>(lines[1].Split(' '));
            StartSymbol  = lines[2];
            foreach (var line in lines.Skip(3))
            {
                string[] elements  = line.Split('?');
                string[] right     = elements[1].Split(' ');
                var      produRule = new ProductionRule()
                {
                    Left = elements[0], Right = right.ToList()
                };
                ProductionRules.Add(produRule, count++);
            }

            Console.WriteLine("Terminals:");
            foreach (var t in Terminals)
            {
                Console.Write(t + " ");
            }
            Console.WriteLine("\nNonterminals");
            foreach (var t in Nonterminals)
            {
                Console.Write(t + " ");
            }
            Console.WriteLine("\nProduction rules");
            foreach (var rule in ProductionRules)
            {
                Console.Write(rule.Key.Left + " -> " + String.Join(" ", rule.Key.Right) + " (" + rule.Value + ")");
                Console.WriteLine();
            }
        }
Ejemplo n.º 2
0
        //Determine the first set for sym, where sym is the first element in the RHS of the rule
        public static HashSet <string> FirstOf(string sym, Dictionary <string, HashSet <string> > firsts, ProductionRule rule)
        {
            var first = firsts[sym];

            if (first.Count() == 0 || first.Any(x => x == EPSILON))
            {
                foreach (var rightElem in rule.Right.Where(x => x != sym))
                {
                    first = new HashSet <string>(first.Union(firsts[rightElem]));
                    if (first.Count() == 0 || firsts[rightElem].Any(x => x == EPSILON))
                    {
                        continue;
                    }
                    else
                    {
                        first.Remove(EPSILON);
                        break;
                    }
                }
            }
            return(first);
        }