Exemplo n.º 1
0
        public static Nfa Match(int symbol)
        {
            State startState = new State();
            State endState   = new State();

            startState.AddTransition(new MatchRangeTransition(endState, Interval.FromBounds(symbol, symbol)));
            return(new Nfa(startState, endState));
        }
Exemplo n.º 2
0
        public static Nfa MatchAny(params int[] symbols)
        {
            State startState = new State();
            State endState   = new State();

            foreach (var symbol in symbols)
            {
                startState.AddTransition(new MatchRangeTransition(endState, Interval.FromBounds(symbol, symbol)));
            }
            return(new Nfa(startState, endState));
        }
Exemplo n.º 3
0
        /** Given the set of possible values (rather than, say UNICODE or MAXINT),
         *  return a new set containing all elements in vocabulary, but not in
         *  this.  The computation is (vocabulary - this).
         *
         *  'this' is assumed to be either a subset or equal to vocabulary.
         */
        public virtual IntervalSet Complement(Interval vocabulary)
        {
            if (vocabulary.EndInclusive < MinElement || vocabulary.Start > MaxElement)
            {
                // nothing in common with this set
                return(null);
            }

            int n = intervals.Count;

            if (n == 0)
            {
                return(IntervalSet.Of(vocabulary));
            }

            IntervalSet compl = new IntervalSet();

            Interval first = intervals[0];

            // add a range from 0 to first.Start constrained to vocab
            if (first.Start > vocabulary.Start)
            {
                compl.Intervals.Add(Interval.FromBounds(vocabulary.Start, first.Start - 1));
            }

            for (int i = 1; i < n; i++)
            {
                if (intervals[i - 1].EndInclusive >= vocabulary.EndInclusive)
                {
                    break;
                }

                if (intervals[i].Start <= vocabulary.Start)
                {
                    continue;
                }

                if (intervals[i - 1].EndInclusive == intervals[i].Start - 1)
                {
                    continue;
                }

                compl.Intervals.Add(Interval.FromBounds(Math.Max(vocabulary.Start, intervals[i - 1].EndInclusive + 1), Math.Min(vocabulary.EndInclusive, intervals[i].Start - 1)));

                //// from 2nd interval .. nth
                //Interval previous = intervals[i - 1];
                //Interval current = intervals[i];
                //IntervalSet s = IntervalSet.Of( previous.EndInclusive + 1, current.Start - 1 );
                //IntervalSet a = (IntervalSet)s.And( vocabularyIS );
                //compl.AddAll( a );
            }

            Interval last = intervals[n - 1];

            // add a range from last.EndInclusive to maxElement constrained to vocab
            if (last.EndInclusive < vocabulary.EndInclusive)
            {
                compl.Intervals.Add(Interval.FromBounds(last.EndInclusive + 1, vocabulary.EndInclusive));
                //IntervalSet s = IntervalSet.Of( last.EndInclusive + 1, maxElement );
                //IntervalSet a = (IntervalSet)s.And( vocabularyIS );
                //compl.AddAll( a );
            }

            return(compl);
        }