public FirstSetBuilder(cfg cfg)
 {
     FirstSets = new FirstSet();
     this.cfg  = cfg;
     Empty     = System.Configuration.ConfigurationManager.AppSettings["Empty"][0];
     foreach (char c in cfg.Vn)
     {
         FirstSets.Add(c, new HashSet <char>());
     }
     Build();
 }
Exemplo n.º 2
0
        public static void Closure(this ItemSet itemset, cfg cfg, FirstSet first)
        {
            HashSet <Production> productions = cfg.Productions;
            bool changed;

            do
            {
                changed = false;
                char end = System.Configuration.ConfigurationManager.AppSettings["End"][0];
                for (int i = 0; i < itemset.Count; ++i)
                {
                    Item   item  = itemset.ElementAt(i);
                    int    index = item.Index;
                    string right = item.Production.Right;
                    if (index >= right.Length || !cfg.isVn(right[index]))
                    {
                        continue;
                    }
                    foreach (Production production in productions)
                    {
                        if (production.Left != right[index])
                        {
                            continue;
                        }
                        int length = right.Length;
                        if (index + 1 == right.Length)
                        {
                            Item newItem = new Item(production, 0, item.Symbol);
                            changed |= itemset.AddItem(newItem);
                            continue;
                        }

                        if (index + 1 < right.Length)
                        {
                            char           empty   = System.Configuration.ConfigurationManager.AppSettings["Empty"][0];
                            HashSet <char> symbols = first.GetFirst(right[index + 1]);
                            foreach (char c in symbols)
                            {
                                if (c == empty)
                                {
                                    Item newItem = new Item(production, 0, item.Symbol);
                                    changed |= itemset.Add(newItem);
                                }
                                else
                                {
                                    Item newItem = new Item(production, 0, c);
                                    changed |= itemset.Add(newItem);
                                }
                            }
                        }
                    }
                }
            } while (changed);
        }
Exemplo n.º 3
0
 public TableBuilder(cfg cfg, FirstSet first)
 {
     this.CFG   = cfg;
     this.First = first;
     Build();
 }