Exemple #1
0
        internal bool ComputeShiftAfterLookaheads(Func <int> timeStamp)
        {
            if (!AfterLookaheads.Add(shiftParents.Select(it => it.AfterLookaheads.Chunks).Flatten()))
            {
                return(false);
            }

            lookaheadsStamp = timeStamp();
            return(true);
        }
Exemple #2
0
        internal bool ComputeClosureAfterLookaheads(Func <int> timeStamp,
                                                    Dictionary <Production <SYMBOL_ENUM, TREE_NODE>, Dictionary <int, SymbolChunkSet <SYMBOL_ENUM> > > precomputedRhsFirsts,
                                                    int lookaheadWidth)
        {
            int capacity = 0;

            foreach (SingleState <SYMBOL_ENUM, TREE_NODE> source in closureParents)
            {
                // equals -- deals with recursive closure so comparing stamp of the same state
                // and since we use "=" in comparison now we can initialize states stamps with the same value
                if (source.lookaheadsStamp >= this.lookaheadsStamp)
                {
                    // not the exact science, rather educated guess, plus little something extra
                    // this calculation is rather OK with lookaheadWidth=1, but with >1 is more and more off
                    capacity += Math.Max(source.AfterLookaheads.Count,
                                         precomputedRhsFirsts[source.Production][source.RhsSeenCount + 1].Count) + 1;
                }
            }

            if (capacity == 0)
            {
                return(false);
            }

            var buffer = new List <SymbolChunk <SYMBOL_ENUM> >(capacity);

            foreach (SingleState <SYMBOL_ENUM, TREE_NODE> source in closureParents)
            {
                if (source.lookaheadsStamp >= this.lookaheadsStamp)
                {
                    bufferCombinedLookaheads(precomputedRhsFirsts,
                                             source.Production,
                                             source.RhsSeenCount + 1,
                                             source.AfterLookaheads,
                                             lookaheadWidth,
                                             buffer);
                }
            }

            if (AfterLookaheads.Add(buffer))
            {
                lookaheadsStamp = timeStamp();
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemple #3
0
        public bool Merge(SingleState <SYMBOL_ENUM, TREE_NODE> incoming, Func <int> timeStamp)
        {
            // here do not merge closure, because closures are inter-node links
            // and the incoming state will be deleted -- thus all inter-node links
            // would be invalid (in sense of grammar)
            shiftParents.AddRange(incoming.shiftParents);

            if (!AfterLookaheads.Add(incoming.AfterLookaheads))
            {
                return(false);
            }

            lookaheadsStamp = timeStamp();
            return(true);
        }
Exemple #4
0
        public string ToString(StringRep <SYMBOL_ENUM> symbolsRep)
        {
            // lowering the case so we can search a string more effectively in DFA text file
            string next_lookaheads  = NextLookaheads.ToString(symbolsRep, verboseMode: false).ToLower();
            string after_lookaheads = AfterLookaheads.ToString(symbolsRep, verboseMode: false).ToLower();
            var    source           = new List <string>();

            if (closureParents.Any())
            {
                source.AddRange(closureParents.Select(it => "c:" + it.IndexStr));
            }
            if (shiftParents.Any())
            {
                source.AddRange(shiftParents.Select(it => "s:" + it.IndexStr));
            }

            return(IndexStr + ")  " + symbolsRep.Get(LhsSymbol) + " := "
                   + (String.Join(" ", Production.RhsSymbols.Take(RhsSeenCount).Select(it => symbolsRep.Get(it)))
                      + " . "
                      + String.Join(" ", Production.RhsSymbols.Skip(RhsSeenCount).Select(it => symbolsRep.Get(it)))).Trim()
                   + (next_lookaheads.Length > 0 ? "\t (n: " + next_lookaheads + " )" : "")
                   + (after_lookaheads.Length > 0 ? "\t (a: " + after_lookaheads + " )" : "")
                   + (source.Any() ? "\t <-- " + source.Join(" ") : ""));
        }