GetNullable() static private method

Returns a dictionary containing the probability that any particular nonterminal yields ε
static private GetNullable ( ISet originalProductions ) : double>.Dictionary
originalProductions ISet
return double>.Dictionary
Ejemplo n.º 1
0
        /// <summary>
        /// Eliminate ε-rules
        /// </summary>
        /// <param name="productions"></param>
        // TODO: Does not preserve weights
        private void StepDel(ISet <Production> productions)
        {
            var nullableProbabilities = GrammarHelpers.GetNullable(productions);
            var newRules = new List <Production>();

            foreach (var production in productions)
            {
                var toAdd = Nullate(production, nullableProbabilities);
                RemoveExtraneousNulls(toAdd);
                newRules.AddRange(toAdd);
            }
            productions.Clear();
            productions.UnionWith(newRules);
        }
Ejemplo n.º 2
0
        protected void BuildHelpers()
        {
            _weightTotalsByNonterminal = Cache.Create(() => Helpers.BuildLookup(
                                                          () => this.Productions,
                                                          (p) => p.Lhs,
                                                          (p) => p.Weight,
                                                          () => new Boxed <double>(0.0),
                                                          (x, y) => x.Value += y
                                                          ));
            this.Caches.Add(_weightTotalsByNonterminal);

            _nonterminals = Cache.Create(() => {
                var hs = new HashSet <Nonterminal>();
                hs.Add(Start);
                foreach (var production in this.Productions)
                {
                    hs.Add(production.Lhs);
                    foreach (var word in production.Rhs)
                    {
                        var nonterminal = word as Nonterminal;
                        if (nonterminal != null)
                        {
                            hs.Add(nonterminal);
                        }
                    }
                }
                return((ISet <Nonterminal>)hs);
            });
            this.Caches.Add(_nonterminals);

            _terminals = Cache.Create(() => {
                var hs = new HashSet <Terminal>();
                foreach (var production in this.Productions)
                {
                    foreach (var word in production.Rhs)
                    {
                        var terminal = word as Terminal;
                        if (terminal != null)
                        {
                            hs.Add(terminal);
                        }
                    }
                }
                return((ISet <Terminal>)hs);
            });
            this.Caches.Add(_terminals);

            _nullableDict = Cache.Create(() => GrammarHelpers.GetNullable(new HashSet <Production>(Productions)));
            this.Caches.Add(_nullableDict);
        }