コード例 #1
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);
        }
コード例 #2
0
        public Grammar(IEnumerable <Production> productions, Nonterminal start) : base(start)
        {
            _productions = new List <Production>(productions);

            // if (simplify) {
            SimplifyWithoutInvalidate();
            //}

            _table = Cache.Create(() => Helpers.BuildLookup(
                                      () => _productions,
                                      (p) => p.Lhs,
                                      (p) => p,
                                      () => (ICollection <Production>) new List <Production>(),
                                      (x, y) => x.Add(y)
                                      ));
            this.Caches.Add(_table);

            BuildHelpers();
        }
コード例 #3
0
        private void BuildLookups()
        {
            _reverseTerminalProductions = Cache.Create(() => Helpers.BuildLookup(
                                                           () => _terminalProductions,
                                                           (p) => (Terminal)p.Rhs[0],
                                                           (p) => p,
                                                           () => (ICollection <Production>) new HashSet <Production>(),
                                                           (x, y) => x.Add(y)
                                                           ));
            this.Caches.Add(_reverseTerminalProductions);

            _ntProductionsByNonterminal = Cache.Create(() => Helpers.BuildLookup(
                                                           () => _nonterminalProductions,
                                                           (p) => p.Lhs,
                                                           (p) => p,
                                                           () => (ICollection <Production>) new HashSet <Production>(),
                                                           (x, y) => x.Add(y)
                                                           ));
            this.Caches.Add(_ntProductionsByNonterminal);

            _tProductionsByNonterminal = Cache.Create(() => Helpers.BuildLookup(
                                                          () => _terminalProductions,
                                                          (p) => p.Lhs,
                                                          (p) => p,
                                                          () => (ICollection <Production>) new HashSet <Production>(),
                                                          (x, y) => x.Add(y)
                                                          ));
            this.Caches.Add(_tProductionsByNonterminal);

            _productionsFrom = Cache.Create(() => Helpers.BuildLookup(
                                                () => _terminalProductions.Concat(_nonterminalProductions).Concat(_emptyProductions),
                                                (p) => p.Lhs,
                                                (p) => p,
                                                () => (ICollection <Production>) new HashSet <Production>(),
                                                (x, y) => x.Add(y)
                                                ));
            this.Caches.Add(_productionsFrom);
        }
コード例 #4
0
ファイル: Cache.cs プロジェクト: simple555a/CFGLib
 public static Cache <T> Create <T>(Func <T> build)
 {
     return(Cache <T> .Create(build));
 }