} //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
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
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; }
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; }