コード例 #1
0
        private void CreateTable()
        {
            List <LalrState> stateList = new List <LalrState>();

            mTable = new LalrTable(stateList);

            for (int i = 0; i < mStates.Count; i++)
            {
                var actions     = new Dictionary <Symbol, ReadOnlyCollection <LalrAction> >();
                var tempActions = new Dictionary <Symbol, List <LalrAction> >();
                var st          = new LalrState(actions);
                stateList.Add(st);

                var currentItem = mStates[i].Closure(mDatabase);
                foreach (var item in currentItem)
                {
                    if (!item.AtEnd && item.CurrentSymbol is TerminalSymbol)
                    {
                        if (!tempActions.ContainsKey(item.CurrentSymbol))
                        {
                            tempActions[item.CurrentSymbol] = new List <LalrAction>();
                        }

                        if (tempActions[item.CurrentSymbol].Any(a => a is LalrShift))
                        {
                            continue;
                        }

                        tempActions[item.CurrentSymbol].Add(new LalrShift(mRecordedGotos[Tuple.Create(i, item.CurrentSymbol)], mTable));
                    }
                    else if (item.AtEnd && item.Production.Head == NonterminalSymbol.StartingSymbol)
                    {
                        if (!tempActions.ContainsKey(TerminalSymbol.Eof))
                        {
                            tempActions[TerminalSymbol.Eof] = new List <LalrAction>();
                        }

                        tempActions[TerminalSymbol.Eof].Add(new LalrAccept());
                    }
                    else if (item.AtEnd)
                    {
                        if (!tempActions.ContainsKey(item.Lookahead))
                        {
                            tempActions[item.Lookahead] = new List <LalrAction>();
                        }

                        if (tempActions[item.Lookahead].Any(a => a is LalrReduce && ((LalrReduce)a).Production == item.Production))
                        {
                            continue;
                        }

                        tempActions[item.Lookahead].Add(new LalrReduce(item.Production));
                    }
                    else
                    {
                        if (tempActions.ContainsKey(item.CurrentSymbol))
                        {
                            continue;
                        }

                        tempActions[item.CurrentSymbol] = new List <LalrAction>();
                        tempActions[item.CurrentSymbol].Add(new LalrGoto(mRecordedGotos[Tuple.Create(i, item.CurrentSymbol)], mTable));
                    }
                }

                foreach (var act in tempActions)
                {
                    actions.Add(act.Key, act.Value.AsReadOnly());
                }
            }
        }
コード例 #2
0
 public LalrShift(int stateIndex, LalrTable table)
 {
     this.StateIndex = stateIndex;
     this.Table      = table;
 }