Exemplo n.º 1
0
        public void ReadFromFile(string filePath)
        {
            int number = 1;

            string[] lines = System.IO.File.ReadAllLines(@filePath);
            foreach (string line in lines)
            {
                string[] parts = line.Split("-");
                Nonterminals.Add(parts[0]);
                string[] fileProductions = parts[1].Split("|");
                var      havePoductions  = Productions.TryGetValue(parts[0], out var productions);
                if (!havePoductions)
                {
                    productions = new List <string>();
                }
                foreach (string production in fileProductions)
                {
                    productions.Add(production);
                    ProductionNumber.Add(Tuple.Create(parts[0], production), number);
                    number++;
                }
                Productions.Add(parts[0], productions);
            }
        }
Exemplo n.º 2
0
        public bool Verify(string state, int k)
        {
            var results  = new HashSet <string>();
            var LLkTable = new List <Tuple <string, string, int> >();
            var length   = 0;

            if (Productions.TryGetValue(state, out var productions))
            {
                productions.ForEach(p =>
                {
                    if (p == "?")
                    {
                        p = "";
                    }
                    var words = new HashSet <string>();
                    if (p.Length < k && !HasNonterminal(p))
                    {
                        words.Add(p);
                    }
                    else
                    {
                        words = GenerateWords(p, k);
                    }
                    var hasFollow = Follow.TryGetValue(state, out var follows);
                    var values    = new HashSet <string>();

                    foreach (var follow in follows)
                    {
                        foreach (var word in words)
                        {
                            if ((word + follow).Length >= k)
                            {
                                values.Add((word + follow).Substring(0, k));
                            }
                            else
                            {
                                values.Add(word + follow);
                            }
                        }
                    }
                    foreach (var value in values)
                    {
                        results.Add(value);
                        //ParserTable.Add(Tuple.Create(state, value), Tuple.Create(p, number));
                        ProductionNumber.TryGetValue(Tuple.Create(state, (p == "" ? "?" : p)), out var number);
                        LLkTable.Add(Tuple.Create(value, p, number));
                    }

                    length += values.Count;
                });
            }
            if (length == results.Count)
            {
                LLkTable.ForEach(l =>
                {
                    ParserTable.Add(Tuple.Create(state, l.Item1), Tuple.Create((l.Item2 == "" ? "?" : l.Item2), l.Item3));
                });
                return(true);
            }
            return(false);
        }