public LRItem FindByCore(LR0Item core)
 {
     foreach (LRItem item in this)
     {
         if (item.Core == core)
         {
             return(item);
         }
     }
     return(null);
 }
 private static int CompareLR0Items(LR0Item x, LR0Item y)
 {
     if (x.ID < y.ID)
     {
         return(-1);
     }
     if (x.ID == y.ID)
     {
         return(0);
     }
     return(1);
 }
Exemplo n.º 3
0
        private Production CreateProduction(NonTerminal lvalue, BnfTermList operands)
        {
            Production      prod  = new Production(lvalue);
            GrammarHintList hints = null;

            //create RValues list skipping Empty terminal and collecting grammar hints
            foreach (BnfTerm operand in operands)
            {
                if (operand == Grammar.CurrentGrammar.Empty)
                {
                    continue;
                }
                //Collect hints as we go - they will be added to the next non-hint element
                GrammarHint hint = operand as GrammarHint;
                if (hint != null)
                {
                    if (hints == null)
                    {
                        hints = new GrammarHintList();
                    }
                    hints.Add(hint);
                    continue;
                }
                //Check if it is a Terminal or Error element
                Terminal t = operand as Terminal;
                if (t != null)
                {
                    prod.Flags |= ProductionFlags.HasTerminals;
                    if (t.Category == TokenCategory.Error)
                    {
                        prod.Flags |= ProductionFlags.IsError;
                    }
                }
                //Add the operand info and LR0 Item
                LR0Item item = new LR0Item(_lastItemId++, prod, prod.RValues.Count, hints);
                prod.LR0Items.Add(item);
                prod.RValues.Add(operand);
                hints = null;
            }//foreach operand
            //set the flags
            if (prod.RValues.Count == 0)
            {
                prod.Flags |= ProductionFlags.IsEmpty;
            }
            //Add final LRItem
            prod.LR0Items.Add(new LR0Item(_lastItemId++, prod, prod.RValues.Count, hints));
            return(prod);
        }
        }//method

        public LRItem AddItem(LR0Item core)
        {
            var item = AllItems.FindByCore(core);

            if (item != null)
            {
                return(item);
            }
            item = new LRItem(State, core);
            AddItem(item);
            //If current term is non-terminal, expand it
            var currNt = core.Current as NonTerminal;

            if (currNt == null)
            {
                return(item);
            }
            foreach (var prod in currNt.Productions)
            {
                var expItem = AddItem(prod.LR0Items[0]);
                item.Expansions.Add(expItem);
            }
            return(item);
        }//method
        public readonly BnfTermSet Lookaheads             = new BnfTermSet(); //actual active lookaheads

        public LRItem(ParserState state, LR0Item core)
        {
            State = state;
            Core  = core;
            Key   = state.Name + "/" + core.ID;
        }