示例#1
0
        public bool Equals(Lr0State i)
        {
            if ((System.Object)i == null)
            {
                return false;
            }

            if (itemSet.Count != i.itemSet.Count)
                return false;

            bool result = true;

            foreach (var item in i.itemSet)
            {
                result = result && itemSet.Contains(item);
            }

            return result;
        }
示例#2
0
文件: Program.cs 项目: paulochang/LR0
 public static Lr0State Closure(Lr0State theState, List<GrammarRule> theGrammar)
 {
     Lr0State ResultState = new Lr0State(theState.itemSet);
     bool hasChanged = false;
     do
     {
         hasChanged = false;
         List<Item> forwardItems = theState.GetForwardItems();
         foreach (Item theItem in forwardItems)
         {
             Symbol currentSymbol = theItem.getCurrentSymbol();
             List<Item> theProductions = Lr0GenerationHelper.getItemsById(currentSymbol.id, theGrammar);
             foreach (var item in theProductions)
             {
                 hasChanged = theState.Add(item) || hasChanged;
             }
         }
     } while (hasChanged);
     return ResultState;
 }
示例#3
0
文件: Program.cs 项目: paulochang/LR0
        public static DFA BuildLr0(List<GrammarRule> AugmentedGrammar, out HashSet<Tuple<dfaState, Symbol, Int32>> ReduceStates, out HashSet<input> Tokens)
        {
            Tokens = new HashSet<input>();
            ReduceStates = new HashSet<Tuple<dfaState, Symbol, Int32>>();
            DFA E = new DFA();
            E.start = 0;
            List<Lr0State> T = new List<Lr0State>();

            GrammarRule FirstProduction = AugmentedGrammar[0];
            Lr0State InitialState = new Lr0State();
            InitialState.Add(new Item(FirstProduction));

            InitialState = Closure(InitialState, AugmentedGrammar);
            T.Add(InitialState);
            bool hasChanged = false;

            do
            {
                hasChanged = false;
                List<Lr0State> tempState = new List<Lr0State>();
                foreach (Lr0State s in T)
                {
                    tempState.Add(s);
                }
                foreach (Lr0State I in tempState)
                {
                    foreach (Item item in I.itemSet)
                    {
                        if (!item.isFinal())
                        {
                            Symbol X = item.getCurrentSymbol();
                            if (X.id != "úEndSymbol")
                            {
                                Lr0State J = Goto(I, X, AugmentedGrammar);
                                int JIndex = -1;
                                if (T.Contains(J))
                                {
                                    JIndex = T.IndexOf(J);
                                }
                                else
                                {
                                    hasChanged = true;
                                    T.Add(J);
                                    JIndex = T.IndexOf(J);
                                }
                                if (item.getNextSymbol() != null)
                                    if (item.getNextSymbol().id == "úEndSymbol")
                                        E.final.Add(JIndex);

                                int Iindex = T.IndexOf(I);
                                string TransitionSymbol = X.id;
                                Tokens.Add(TransitionSymbol);

                                if (!E.transitionTable.ContainsKey(new KeyValuePair<int, string>(Iindex, TransitionSymbol)))
                                    E.transitionTable.Add(new KeyValuePair<int, string>(Iindex, TransitionSymbol), JIndex);
                            }
                        }
                        else
                        {
                            HashSet<Symbol> followSet = Follow(item.Rule.RuleSymbol, AugmentedGrammar);
                            foreach (Symbol x in followSet)
                            {
                                ReduceStates.Add(new Tuple<dfaState, Symbol, Int32>(T.IndexOf(I), x, AugmentedGrammar.IndexOf(item.Rule)));
                            }

                        }
                    }
                }
            } while (hasChanged);

            return E;
        }
示例#4
0
文件: Program.cs 项目: paulochang/LR0
        public static Lr0State Goto(Lr0State theState, Symbol inputSymbol, List<GrammarRule> theGrammar)
        {
            List<Item> generatedItems = theState.GetGotoItems(inputSymbol);
            Lr0State resultState = new Lr0State(generatedItems);

            return Closure(resultState, theGrammar);
        }