Пример #1
0
        public static LalrTable ComputeTable(GrammarProductionDatabase db)
        {
            var ctx = new LalrContext(db);

            ctx.ComputeStates();
            return(ctx.mTable);
        }
Пример #2
0
        private Dictionary <Symbol, LalrItemSet> InternalGoto(GrammarProductionDatabase db)
        {
            HashSet <Symbol> symbols = new HashSet <Symbol>();

            //var closure = this.Closure(db);
            var closure = this;

            foreach (var item in closure)
            {
                if (item.AtEnd)
                {
                    continue;
                }

                symbols.Add(item.CurrentSymbol);
            }

            Dictionary <Symbol, LalrItemSet> itemSets = new Dictionary <Symbol, LalrItemSet>();

            foreach (var symb in symbols)
            {
                itemSets[symb] = closure.Goto(symb).Closure(db);
            }
            return(itemSets);
        }
Пример #3
0
        public HashSet <TerminalSymbol> GetNextLookaheads(GrammarProductionDatabase db)
        {
            if (ParsingPoint == (Production.Body.Count - 1))
            {
                return new HashSet <TerminalSymbol> {
                           Lookahead
                }
            }
            ;

            return(SymbolString.Concat(Production.Body.Range(ParsingPoint + 1), Lookahead).FirstSet(db));
        }
    }
Пример #4
0
        private HashSet <TerminalSymbol> InternalFirstSet(GrammarProductionDatabase db)
        {
            if (Count == 0)
            {
                return(new HashSet <TerminalSymbol>());
            }

            HashSet <TerminalSymbol>    ts       = new HashSet <TerminalSymbol>();
            HashSet <NonterminalSymbol> analyzed = new HashSet <NonterminalSymbol>();

            InternalFirstSet(ts, analyzed, db);

            return(ts);
        }
Пример #5
0
        private LalrItemSet InternalClosure(GrammarProductionDatabase db)
        {
            HashSet <LalrItem> added = new HashSet <LalrItem>();
            HashSet <LalrItem> toAdd = new HashSet <LalrItem>();
            HashSet <LalrItem> next  = new HashSet <LalrItem>();

            toAdd.UnionWith(this);

            while (toAdd.Count != 0)
            {
                next.Clear();
                foreach (var item in toAdd)
                {
                    if (item.AtEnd || !(item.CurrentSymbol is NonterminalSymbol))
                    {
                        continue;
                    }

                    var productions    = db[item.CurrentSymbol as NonterminalSymbol];
                    var nextLookaheads = item.GetNextLookaheads(db);
                    foreach (var p in productions)
                    {
                        foreach (var la in nextLookaheads)
                        {
                            var newItem = new LalrItem(p, 0, la);
                            if (!added.Contains(newItem) && !toAdd.Contains(newItem))
                            {
                                next.Add(newItem);
                            }
                        }
                    }
                }

                added.UnionWith(toAdd);
                toAdd.Clear();
                toAdd.UnionWith(next);
            }

            added.UnionWith(toAdd);
            added.UnionWith(next);
            return(new LalrItemSet(added));
        }
Пример #6
0
 private LalrContext(GrammarProductionDatabase db)
 {
     this.mDatabase = db;
 }
Пример #7
0
 public LalrItemSet Closure(GrammarProductionDatabase db)
 {
     return(Memoize.Function(ClosureName, Tuple.Create(this, db), (arg) => arg.Item1.InternalClosure(arg.Item2)));
 }
Пример #8
0
 public Dictionary <Symbol, LalrItemSet> Goto(GrammarProductionDatabase db)
 {
     return(Memoize.Function(GotoName2, Tuple.Create(this, db), (arg) => arg.Item1.InternalGoto(arg.Item2)));
 }
Пример #9
0
        private void InternalFirstSet(HashSet <TerminalSymbol> ts, HashSet <NonterminalSymbol> analyzed, GrammarProductionDatabase db)
        {
            foreach (var item in this)
            {
                if (item is TerminalSymbol)
                {
                    ts.Add(item as TerminalSymbol);
                    break;
                }
                else
                {
                    if (analyzed.Contains(item as NonterminalSymbol))
                    {
                        if (!db.IsNullable(item as NonterminalSymbol))
                        {
                            break;
                        }
                        else
                        {
                            continue;
                        }
                    }

                    analyzed.Add(item as NonterminalSymbol);
                    var rules = db[item as NonterminalSymbol];
                    foreach (var prod in rules)
                    {
                        prod.Body.InternalFirstSet(ts, analyzed, db);
                    }
                    if (!db.IsNullable(item as NonterminalSymbol))
                    {
                        break;
                    }
                }
            }
        }
Пример #10
0
 public HashSet <TerminalSymbol> FirstSet(GrammarProductionDatabase db)
 {
     return(Memoize.Function(FirstSetName, Tuple.Create(this, db), (input) => input.Item1.InternalFirstSet(input.Item2)));
 }