public AnnotatedGrammarElement GetAnnotatedGrammarElement(GrammarDefinition element, IntermediateParserState transitionSource) { int elementHash = element.GetHashCode(); IDictionary<int, AnnotatedGrammarElement> annotatedElements; if (!_elementMapping.TryGetValue(elementHash, out annotatedElements)) return null; return annotatedElements.FirstOrDefault(x => x.Value.Transition?.Source == transitionSource).Value; }
public Lr1Item(IntermediateParserState state, Lr0Item kernel) { if (state == null) throw new ArgumentNullException(nameof(state)); if (kernel == null) throw new ArgumentNullException(nameof(kernel)); State = state; Kernel = kernel; Lookahead = new HashSet<TokenGrammarElement>(); }
public ParserTransition GetOrCreateTransition(IntermediateParserState source, GrammarElement element, IntermediateParserState destination) { var transition = source.OutgoingTransitions.Values.FirstOrDefault(x => x.Destination == destination); if (transition == null) { transition = new ParserTransition(source, element, destination); source.OutgoingTransitions.Add(element, transition); destination.IncomingTransitions.Add(transition); } return transition; }
public IntermediateParserState GetOrCreateState(IList<Lr0Item> kernels) { // Sort the LR(0) items to avoid the creation of states // that have the same LR(0) items but in a different order. var identifiers = (from item in kernels orderby item.Id select item.Id).ToArray(); // Compute hash for quick look up. int hash = ComputeHashCode(identifiers); IntermediateParserState state; if (!_itemIdStateMapping.TryGetValue(hash, out state)) { state = new IntermediateParserState(this, new ParserState()); state.State.Intermediate = state; state.State.Id = States.Count; foreach (var item in kernels) state.RegisterItemAndChildren(this, item); States.Add(state); _itemIdStateMapping.Add(hash, state); } return state; }
private static void ReplaceRootReductionWithAcceptance(GrammarDefinition augmentedRoot, IntermediateParserState initialState) { // The augmented root only contains one sequence. var augmentedRootSequence = augmentedRoot.Rule.Switch[0]; var rootDefinition = (GrammarDefinition)augmentedRootSequence[0]; // Replace the existing reduce action with the accept action. var shiftAction = (ShiftParserAction)initialState.State.Actions[rootDefinition]; var shiftState = shiftAction.NextState; shiftState.Actions[Grammar.Eof] = new AcceptParserAction(); shiftState.DefaultAction = null; }
private static void ExpandState(GrammarCompilationContext context, IntermediateParserState state) { // Check for possible shifts. foreach (var shifter in state.Items.Shifters) { var currentElement = shifter.Kernel.Element; // Get all LR(1) items sharing the same grammar element, get or // create the state that corresponds to the shifted items, and create // a transition to that state. var shiftedKernels = from item in state.Items let kernel = item.Kernel where kernel.Element == currentElement select kernel.NextItem; var kernelsArray = shiftedKernels.ToArray(); var nextState = context.GetOrCreateState(kernelsArray); shifter.NextItem = nextState.Items.First(x => x.Kernel == shifter.Kernel.NextItem); context.GetOrCreateTransition(state, currentElement, nextState); if (!state.State.Actions.ContainsKey(currentElement)) { var action = new ShiftParserAction(nextState.State); var customActionElement = currentElement as CustomActionGrammarElement; if (customActionElement != null) action.CustomAction = customActionElement.Action; state.State.Actions[currentElement] = action; } } }
public ParserTransition(IntermediateParserState source, GrammarElement element, IntermediateParserState destination) { Source = source; Element = element; Destination = destination; }