Exemple #1
0
 public IEnumerable <Transition <Symbol, ProductionItem <TTokenKind> > > GetTransitions()
 {
     foreach (KeyValuePair <SourceTransitionPair <ProductionItem <TTokenKind>, Symbol>, HashSet <ProductionItem <TTokenKind> > > kvp in _delta)
     {
         ProductionItem <TTokenKind> sourceState = kvp.Key.SourceState;
         Symbol label = kvp.Key.Label;
         foreach (ProductionItem <TTokenKind> targetState in kvp.Value)
         {
             yield return(Transition.Move(sourceState, label, targetState));
         }
     }
 }
Exemple #2
0
        public bool Equals(ProductionItem <TTokenKind> other, ProductionItemComparison comparison)
        {
            switch (comparison)
            {
            case ProductionItemComparison.MarkedProductionOnly:
                return(MarkedProduction.Equals(other.MarkedProduction));    // CORE is the same

            case ProductionItemComparison.LookaheadsOnly:
                return(Lookaheads.SetEquals(other.Lookaheads));

            case ProductionItemComparison.MarkedProductionAndLookaheads:
            default:
                return(MarkedProduction.Equals(other.MarkedProduction) && Lookaheads.SetEquals(other.Lookaheads));
            }
        }
Exemple #3
0
        public LrItemNfa(
            IEnumerable <Transition <Symbol, ProductionItem <TTokenKind> > > transitions,
            ProductionItem <TTokenKind> startState,
            IEnumerable <ProductionItem <TTokenKind> > acceptStates)
        {
            StartState    = startState;
            _acceptStates = new HashSet <ProductionItem <TTokenKind> >(acceptStates);
            _states       = new HashSet <ProductionItem <TTokenKind> >(acceptStates)
            {
                startState
            };
            _delta    = new Dictionary <SourceTransitionPair <ProductionItem <TTokenKind>, Symbol>, HashSet <ProductionItem <TTokenKind> > >();
            _alphabet = new Set <Symbol>();

            foreach (var triple in transitions)
            {
                _states.Add(triple.SourceState);
                _states.Add(triple.TargetState);
                var pair = Transition.FromPair(triple.SourceState, triple.Label);
                if (_delta.ContainsKey(pair))
                {
                    _delta[pair].Add(triple.TargetState);
                }
                else
                {
                    _delta[pair] = new HashSet <ProductionItem <TTokenKind> > {
                        triple.TargetState
                    }
                };
                if (triple.Label.Equals(Symbol.Epsilon))
                {
                    IsEpsilonNfa = true;
                }
                else
                {
                    _alphabet.Add(triple.Label); // alphabet does not contain epsilon
                }
            }
        }
Exemple #4
0
        ProductionItemSet <TTokenKind> EpsilonClose(IEnumerable <ProductionItem <TTokenKind> > kernelItems)
        {
            var markedVisitedStates = new Queue <ProductionItem <TTokenKind> >(kernelItems);
            // base step
            var closure = new Set <ProductionItem <TTokenKind> >(kernelItems);

            // induction step: recurse until no more states in a round
            while (markedVisitedStates.Count != 0)
            {
                ProductionItem <TTokenKind> sourceState = markedVisitedStates.Dequeue();
                // if any epsilon-moves add them all to the closure (union)
                foreach (var targetState in Delta(Transition.FromPair(sourceState, Symbol.Epsilon)))
                {
                    if (!closure.Contains(targetState))
                    {
                        closure.Add(targetState);
                        markedVisitedStates.Enqueue(targetState);
                    }
                }
            }

            return(new ProductionItemSet <TTokenKind>(closure));
        }
Exemple #5
0
 public bool Equals(ProductionItem <TTokenKind> other)
 {
     return(Equals(other, ProductionItemComparison.MarkedProductionAndLookaheads));
 }
Exemple #6
0
 public bool IsAcceptState(ProductionItem <TTokenKind> state)
 {
     return(_acceptStates.Contains(state));
 }
Exemple #7
0
 public bool Contains(ProductionItem <TTokenKind> item)
 {
     return(_kernelItems.Contains(item) || _closureItems.Contains(item));
 }