}         //method

        #endregion

        #region Creating parser states
        private void CreateInitialAndFinalStates()
        {
            //there is always just one initial production "Root' -> .Root", and we're interested in LR item at 0 index
            LR0ItemList itemList = new LR0ItemList();

            itemList.Add(Data.AugmentedRoot.Productions[0].LR0Items[0]);
            Data.InitialState = FindOrCreateState(itemList); //it is actually create
            Data.InitialState.Items[0].NewLookaheads.Add(Grammar.Eof.Key);
            #region comment about FinalState
            //Create final state - because of the way states construction works, it doesn't create the final state automatically.
            // We need to create it explicitly and assign it to _data.FinalState property
            // The final executed reduction is "Root' -> Root.". This jump is executed as follows:
            //   1. parser creates Root' node
            //   2. Parser pops the state from stack - that would be initial state
            //   3. Finally, parser tries to find the transition in state.Actions table by the key of [Root'] element.
            // We must create the final state, and create the entry in transition table
            // The final state is based on the same initial production, but different LRItem - the one with dot AFTER the root nonterminal.
            // it is item at index 1.
            #endregion
            itemList.Clear();
            itemList.Add(Data.AugmentedRoot.Productions[0].LR0Items[1]);
            Data.FinalState = FindOrCreateState(itemList); //it is actually create
            //Create shift transition from initial to final state
            Data.InitialState.Actions[Data.AugmentedRoot.Key] =
                new ActionRecord(Data.AugmentedRoot.Key, ParserActionType.Shift, Data.FinalState, null);
        }
Ejemplo n.º 2
0
        private string CalcItemListKey(LR0ItemList items)
        {
            items.Sort((x, y) => x.ID - y.ID); //Sort by ID
            if (items.Count == 0)
            {
                return("");
            }

            if (items.Count == 1 && items[0].IsKernel)
            {
                return(items[0].ID.ToString());
            }

            StringBuilder sb = new StringBuilder(1024);

            foreach (LR0Item item in items)
            {
                if (item.IsKernel)
                {
                    sb.Append(item.ID);
                    sb.Append(",");
                }
            }
            return(sb.ToString());
        }
Ejemplo n.º 3
0
        //Parser states are distinguished by the subset of kernel LR0 items.
        // So when we derive new LR0-item list by shift operation,
        // we need to find out if we have already a state with the same LR0Item list.
        // We do it by looking up in a state hash by a key - [LR0 item list key].
        // Each list's key is a concatenation of items' IDs separated by ','.
        // Before producing the key for a list, the list must be sorted;
        //   thus we garantee one-to-one correspondence between LR0Item sets and keys.
        // And of course, we count only kernel items (with dot NOT in the first position).
        #endregion
        public static string ComputeLR0ItemSetKey(LR0ItemSet items)
        {
            if (items.Count == 0)
            {
                return("");
            }
            //Copy non-initial items to separate list, and then sort it
            LR0ItemList itemList = new LR0ItemList();

            foreach (var item in items)
            {
                itemList.Add(item);
            }
            //quick shortcut
            if (itemList.Count == 1)
            {
                return(itemList[0].ID.ToString());
            }
            itemList.Sort(CompareLR0Items); //Sort by ID
            //now build the key
            StringBuilder sb = new StringBuilder(255);

            foreach (LR0Item item in itemList)
            {
                sb.Append(item.ID);
                sb.Append(",");
            }//foreach
            return(sb.ToString());
        }
Ejemplo n.º 4
0
 public ParserState(string name, LR0ItemList coreItems)
 {
     Name = name;
     foreach (LR0Item coreItem in coreItems)
     {
         Items.Add(new LRItem(this, coreItem));
     }
 }
        }     //method

        private ParserState FindOrCreateState(LR0ItemList lr0Items)
        {
            string      key = CalcItemListKey(lr0Items);
            ParserState result;

            if (_stateHash.TryGetValue(key, out result))
            {
                return(result);
            }
            result = new ParserState("S" + Data.States.Count, lr0Items);
            Data.States.Add(result);
            _stateHash[key] = result;
            return(result);
        }
Ejemplo n.º 6
0
        private void CreateInitialAndFinalStates()
        {
            LR0ItemList itemList = new LR0ItemList();

            itemList.Add(_data.AugmentedRoot.Productions[0].LR0Items[0]);
            _data.InitialState = FindOrCreateState(itemList);
            _data.InitialState.Items[0].NewLookaheads.Add(Grammar.Eof.Key);

            itemList.Clear();
            itemList.Add(_data.AugmentedRoot.Productions[0].LR0Items[1]);

            _data.FinalState = FindOrCreateState(itemList);
            _data.InitialState.Actions[_data.AugmentedRoot.Key] =
                new ActionRecord(_data.AugmentedRoot.Key, ParserActionType.Shift, _data.FinalState, null);
        }
Ejemplo n.º 7
0
        }         //method

        #endregion

        #region Creating parser states
        private void CreateParserStates()
        {
            Data.States.Clear();
            _stateHash = new ParserStateTable();
            //Create initial state
            //there is always just one initial production Root' -> Root + LF, and we're interested in LR item at 0 index
            LR0ItemList itemList = new LR0ItemList();

            itemList.Add(Data.AugmentedRoot.Productions[0].LR0Items[0]);
            Data.InitialState = FindOrCreateState(itemList); //it is actually create
            Data.InitialState.Items[0].NewLookaheads.Add(Grammar.Eof.Key);
            //create final state - we need to create it explicitly to assign to _data.FinalState property
            // final state is based on the same initial production, but different LRItem - the one with dot AFTER the root nonterminal.
            // it is item at index 1.
            itemList = new LR0ItemList();
            itemList.Add(Data.AugmentedRoot.Productions[0].LR0Items[1]);
            Data.FinalState = FindOrCreateState(itemList);

            // Iterate through states (while new ones are created) and create shift transitions and new states
            for (int index = 0; index < Data.States.Count; index++)
            {
                ParserState state = Data.States[index];
                AddClosureItems(state);
                //Get keys of all possible shifts
                ShiftTable shiftTable = GetStateShifts(state);
                //Each key in shifts dict is an input element
                // Value is LR0ItemList of shifted LR0Items for this input element.
                foreach (string input in shiftTable.Keys)
                {
                    LR0ItemList shiftedCoreItems = shiftTable[input];
                    ParserState newState         = FindOrCreateState(shiftedCoreItems);
                    state.Actions[input] = new ActionRecord(input, ParserActionType.Shift, newState, null);
                    //link original LRItems in original state to derived LRItems in newState
                    foreach (LR0Item coreItem in shiftedCoreItems)
                    {
                        LRItem fromItem = FindItem(state, coreItem.Production, coreItem.Position - 1);
                        LRItem toItem   = FindItem(newState, coreItem.Production, coreItem.Position);
                        if (!fromItem.PropagateTargets.Contains(toItem))
                        {
                            fromItem.PropagateTargets.Add(toItem);
                        }
                    } //foreach coreItem
                }     //foreach input
            }         //for index
        }             //method
Ejemplo n.º 8
0
 //Parser states are distinguished by the subset of kernel LR0 items.
 // So when we derive new LR0-item list by shift operation,
 // we need to find out if we have already a state with the same LR0Item list.
 // We do it by looking up in a state hash by a key - [LR0 item list key].
 // Each list's key is a concatenation of items' IDs separated by ','.
 // Before producing the key for a list, the list must be sorted;
 //   thus we garantee one-to-one correspondence between LR0Item sets and keys.
 // And of course, we count only kernel items (with dot NOT in the first position).
 public static string ComputeLR0ItemSetKey(LR0ItemSet items)
 {
     if (items.Count == 0) return string.Empty;
       //Copy non-initial items to separate list, and then sort it
       LR0ItemList itemList = new LR0ItemList();
       itemList.AddRange(items);
       //quick shortcut
       if (itemList.Count == 1)
     return itemList[0].ID.ToString();
       itemList.Sort(CompareLR0Items); //Sort by ID
       //now build the key
       StringBuilder sb = new StringBuilder(100);
       foreach (LR0Item item in itemList) {
     sb.Append(item.ID);
     sb.Append(",");
       }//foreach
       return sb.ToString();
 }
Ejemplo n.º 9
0
        private Dictionary <string, LR0ItemList> GetStateShifts(ParserState state)
        {
            Dictionary <string, LR0ItemList> shifts = new Dictionary <string, LR0ItemList>();
            LR0ItemList list;

            foreach (LRItem item in state.Items)
            {
                GrammarTerm term = item.Core.NextElement;
                if (term == null)
                {
                    continue;
                }
                LR0Item shiftedItem = item.Core.Production.LR0Items[item.Core.Position + 1];
                if (!shifts.TryGetValue(term.Key, out list))
                {
                    shifts[term.Key] = list = new LR0ItemList();
                }
                list.Add(shiftedItem);
            }
            return(shifts);
        }
        }//method

        private ShiftTable GetStateShifts(ParserState state)
        {
            ShiftTable  shifts = new ShiftTable();
            LR0ItemList list;

            foreach (LRItem item in state.Items)
            {
                BnfTerm term = item.Core.NextElement;
                if (term == null)
                {
                    continue;
                }
                LR0Item shiftedItem = item.Core.Production.LR0Items[item.Core.Position + 1];
                if (!shifts.TryGetValue(term.Key, out list))
                {
                    shifts[term.Key] = list = new LR0ItemList();
                }
                list.Add(shiftedItem);
            } //foreach
            return(shifts);
        }     //method
        private void CreateParserStates()
        {
            Data.States.Clear();
            _stateHash = new ParserStateTable();
            CreateInitialAndFinalStates();

            string augmRootKey = Data.AugmentedRoot.Key;

            // Iterate through states (while new ones are created) and create shift transitions and new states
            for (int index = 0; index < Data.States.Count; index++)
            {
                ParserState state = Data.States[index];
                AddClosureItems(state);
                //Get keys of all possible shifts
                ShiftTable shiftTable = GetStateShifts(state);
                //Each key in shifts dict is an input element
                // Value is LR0ItemList of shifted LR0Items for this input element.
                foreach (string input in shiftTable.Keys)
                {
                    LR0ItemList  shiftedCoreItems = shiftTable[input];
                    ParserState  newState         = FindOrCreateState(shiftedCoreItems);
                    ActionRecord newAction        = new ActionRecord(input, ParserActionType.Shift, newState, null);
                    state.Actions[input] = newAction;
                    //link original LRItems in original state to derived LRItems in newState
                    foreach (LR0Item coreItem in shiftedCoreItems)
                    {
                        LRItem fromItem = FindItem(state, coreItem.Production, coreItem.Position - 1);
                        LRItem toItem   = FindItem(newState, coreItem.Production, coreItem.Position);
                        if (!fromItem.PropagateTargets.Contains(toItem))
                        {
                            fromItem.PropagateTargets.Add(toItem);
                        }
                        //copy hints from core items into the newAction
                        newAction.ShiftItems.Add(fromItem);
                    } //foreach coreItem
                }     //foreach input
            }         //for index
            Data.FinalState = Data.InitialState.Actions[augmRootKey].NewState;
        }             //method
        //Parser states are distinguished by the subset of kernel LR0 items.
        // So when we derive new LR0-item list by shift operation,
        // we need to find out if we have already a state with the same LR0Item list.
        // We do it by looking up in a state hash by a key - [LR0 item list key].
        // Each list's key is a concatenation of items' IDs separated by ','.
        // Before producing the key for a list, the list must be sorted;
        //   thus we garantee one-to-one correspondence between LR0Item sets and keys.
        // And of course, we count only kernel items (with dot NOT in the first position).
        #endregion
        private string CalcItemListKey(LR0ItemList items)
        {
            items.Sort(ById); //Sort by ID
            if (items.Count == 0)
            {
                return("");
            }
            //quick shortcut
            if (items.Count == 1 && items[0].IsKernel)
            {
                return(items[0].ID.ToString());
            }
            StringBuilder sb = new StringBuilder(1024);

            foreach (LR0Item item in items)
            {
                if (item.IsKernel)
                {
                    sb.Append(item.ID);
                    sb.Append(",");
                }
            }//foreach
            return(sb.ToString());
        }
Ejemplo n.º 13
0
        private void CreateParserStates()
        {
            _data.States.Clear();
            _stateHash = new ParserStateTable();
            CreateInitialAndFinalStates();

            string augmRootKey = _data.AugmentedRoot.Key;

            for (int index = 0; index < _data.States.Count; index++)
            {
                ParserState state = _data.States[index];
                AddClosureItems(state);

                Dictionary <string, LR0ItemList> shiftTable = GetStateShifts(state);

                foreach (string input in shiftTable.Keys)
                {
                    LR0ItemList shiftedCoreItems = shiftTable[input];
                    ParserState newState         = FindOrCreateState(shiftedCoreItems);

                    state.Actions[input] = new ActionRecord(input, ParserActionType.Shift, newState, null);

                    foreach (LR0Item coreItem in shiftedCoreItems)
                    {
                        LRItem fromItem = FindItem(state, coreItem.Production, coreItem.Position - 1);
                        LRItem toItem   = FindItem(newState, coreItem.Production, coreItem.Position);
                        if (!fromItem.PropagateTargets.Contains(toItem))
                        {
                            fromItem.PropagateTargets.Add(toItem);
                        }
                    }
                }
            }

            _data.FinalState = _data.InitialState.Actions[_data.AugmentedRoot.Key].NewState;
        }
Ejemplo n.º 14
0
 public ParserState(string name, LR0ItemList coreItems)
 {
   Name = name;
   foreach (LR0Item coreItem in coreItems)
     Items.Add(new LRItem(this, coreItem));
 }
Ejemplo n.º 15
0
    private void CreateInitialAndFinalStates()
    {
      LR0ItemList itemList = new LR0ItemList();
      itemList.Add(_data.AugmentedRoot.Productions[0].LR0Items[0]);
      _data.InitialState = FindOrCreateState(itemList);
      _data.InitialState.Items[0].NewLookaheads.Add(Grammar.Eof.Key);

      itemList.Clear();
      itemList.Add(_data.AugmentedRoot.Productions[0].LR0Items[1]);

      _data.FinalState = FindOrCreateState(itemList);
      _data.InitialState.Actions[_data.AugmentedRoot.Key] =
        new ActionRecord(_data.AugmentedRoot.Key, ParserActionType.Shift, _data.FinalState, null);
    }
Ejemplo n.º 16
0
    private string CalcItemListKey(LR0ItemList items)
    {
      items.Sort((x, y) => x.ID - y.ID); //Sort by ID
      if (items.Count == 0) return "";

      if (items.Count == 1 && items[0].IsKernel)
        return items[0].ID.ToString();

      StringBuilder sb = new StringBuilder(1024);
      foreach (LR0Item item in items)
      {
        if (item.IsKernel)
        {
          sb.Append(item.ID);
          sb.Append(",");
        }
      }
      return sb.ToString();
    }
Ejemplo n.º 17
0
 private ParserState FindOrCreateState(LR0ItemList lr0Items)
 {
   string key = CalcItemListKey(lr0Items);
   ParserState result;
   if (_stateHash.TryGetValue(key, out result))
     return result;
   result = new ParserState("S" + _data.States.Count, lr0Items);
   _data.States.Add(result);
   _stateHash[key] = result;
   return result;
 }
Ejemplo n.º 18
0
 private Dictionary<string, LR0ItemList> GetStateShifts(ParserState state)
 {
   Dictionary<string, LR0ItemList> shifts = new Dictionary<string, LR0ItemList>();
   LR0ItemList list;
   foreach (LRItem item in state.Items)
   {
     GrammarTerm term = item.Core.NextElement;
     if (term == null) continue;
     LR0Item shiftedItem = item.Core.Production.LR0Items[item.Core.Position + 1];
     if (!shifts.TryGetValue(term.Key, out list))
       shifts[term.Key] = list = new LR0ItemList();
     list.Add(shiftedItem);
   }
   return shifts;
 }