private bool AddClosureItems(ParserState state) { bool result = false; for (int i = 0; i < state.Items.Count; i++) { LRItem item = state.Items[i]; NonTerminal nextNT = item.Core.NextElement as NonTerminal; if (nextNT == null) { continue; } foreach (Production prod in nextNT.Productions) { LR0Item core = prod.LR0Items[0]; LRItem newItem = TryFindItem(state, core); if (newItem == null) { newItem = new LRItem(state, core); state.Items.Add(newItem); result = true; } newItem.NewLookaheads.AddRange(item.Core.TailFirsts); if (item.Core.TailIsNullable && !item.PropagateTargets.Contains(newItem)) { item.PropagateTargets.Add(newItem); } } } return(result); }
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 bool AddClosureItems(ParserState state) { bool result = false; for (int i = 0; i < state.Items.Count; i++) { LRItem item = state.Items[i]; NonTerminal nextNT = item.Core.NextElement as NonTerminal; if (nextNT == null) continue; foreach (Production prod in nextNT.Productions) { LR0Item core = prod.LR0Items[0]; LRItem newItem = TryFindItem(state, core); if (newItem == null) { newItem = new LRItem(state, core); state.Items.Add(newItem); result = true; } newItem.NewLookaheads.AddRange(item.Core.TailFirsts); if (item.Core.TailIsNullable && !item.PropagateTargets.Contains(newItem)) item.PropagateTargets.Add(newItem); } } return result; }