Пример #1
0
        //*
        // * Creates a new look-ahead set filter. The filter will contain
        // * all sequences from this set, possibly left trimmed by each one
        // * of the sequences in the specified set.
        // *
        // * @param set the look-ahead set to trim with
        // *
        // * @return a new look-ahead set filter
        //

        public LookAheadSet CreateFilter(LookAheadSet @set)
        {
            LookAheadSet result = new LookAheadSet(maxLength);
            Sequence     first  = default(Sequence);
            Sequence     second = default(Sequence);

            // Handle special cases
            if (this.Size() <= 0 || @set.Size() <= 0)
            {
                return(this);
            }
            for (int i = 0; i <= elements.Count - 1; i++)
            {
                // Create combinations
                first = (Sequence)elements[i];
                for (int j = 0; j <= @set.elements.Count - 1; j++)
                {
                    second = (Sequence)@set.elements[j];
                    if (first.StartsWith(second))
                    {
                        result.Add(first.Subsequence(second.Length()));
                    }
                }
            }
            return(result);
        }
        //*
        // * Calculates the look-aheads needed for the specified pattern
        // * alternative. This method attempts to resolve any conflicts in
        // * optional elements by recalculating look-aheads for referenced
        // * productions.
        // *
        // * @param alt the production pattern alternative
        // * @param pos the pattern element position
        // *
        // * @throws ParserCreationException if the look-ahead set couldn't
        // * be determined due to inherent ambiguities
        //

        private void CalculateLookAhead(ProductionPatternAlternative alt, int pos)
        {
            ProductionPattern        pattern   = default(ProductionPattern);
            ProductionPatternElement elem      = default(ProductionPatternElement);
            LookAheadSet             first     = default(LookAheadSet);
            LookAheadSet             follow    = default(LookAheadSet);
            LookAheadSet             conflicts = default(LookAheadSet);
            LookAheadSet             previous  = new LookAheadSet(0);
            string location = null;
            int    length   = 1;

            // Check trivial cases
            if (pos >= alt.Count)
            {
                return;
            }

            // Check for non-optional element
            pattern = alt.Pattern;
            elem    = alt[pos];
            if (elem.MinCount == elem.MaxCount)
            {
                CalculateLookAhead(alt, pos + 1);
                return;
            }

            // Calculate simple look-aheads
            first  = FindLookAhead(elem, 1, new CallStack(), null);
            follow = FindLookAhead(alt, 1, pos + 1, new CallStack(), null);

            // Resolve conflicts
            location  = "at position " + (pos + 1);
            conflicts = FindConflicts(pattern.Name, location, first, follow);
            while (conflicts.Size() > 0)
            {
                length += 1;
                conflicts.AddAll(previous);
                first          = FindLookAhead(elem, length, new CallStack(), conflicts);
                follow         = FindLookAhead(alt, length, pos + 1, new CallStack(), conflicts);
                first          = first.CreateCombination(follow);
                elem.LookAhead = first;
                if (first.Intersects(conflicts))
                {
                    first = first.CreateIntersection(conflicts);
                    ThrowAmbiguityException(pattern.Name, location, first);
                }
                previous  = conflicts;
                conflicts = FindConflicts(pattern.Name, location, first, follow);
            }

            // Check remaining elements
            CalculateLookAhead(alt, pos + 1);
        }
        //*
        // * Finds the look-ahead set for a production pattern element. The
        // * maximum look-ahead length must be specified. This method takes
        // * the element repeats into consideration when creating the
        // * look-ahead set, but does NOT include an empty sequence even if
        // * the minimum count is zero (0). It is also possible to specify a
        // * look-ahead set filter, which will make sure that unnecessary
        // * token sequences will be avoided.
        // *
        // * @param elem the production pattern element
        // * @param length the maximum look-ahead length
        // * @param stack the call stack used for loop detection
        // * @param filter the look-ahead set filter
        // *
        // * @return the look-ahead set for the pattern element
        // *
        // * @throws ParserCreationException if an infinite loop was found
        // * in the grammar
        //

        private LookAheadSet FindLookAhead(ProductionPatternElement elem, int length, CallStack stack, LookAheadSet filter)
        {
            LookAheadSet result = default(LookAheadSet);
            LookAheadSet first  = default(LookAheadSet);
            LookAheadSet follow = default(LookAheadSet);
            int          max    = 0;

            // Find initial element look-ahead
            first  = FindLookAhead(elem, length, 0, stack, filter);
            result = new LookAheadSet(length);
            result.AddAll(first);
            if (filter == null || !filter.IsOverlap(result))
            {
                return(result);
            }

            // Handle element repetitions
            if (elem.MaxCount == Int32.MaxValue)
            {
                first = first.CreateRepetitive();
            }
            max = elem.MaxCount;
            if (length < max)
            {
                max = length;
            }
            for (int i = 1; i <= max - 1; i++)
            {
                first = first.CreateOverlaps(filter);
                if (first.Size() <= 0 || first.GetMinLength() >= length)
                {
                    break;                     // TODO: might not be correct. Was : Exit For
                }
                follow = FindLookAhead(elem, length, 0, stack, filter.CreateFilter(first));
                first  = first.CreateCombination(follow);
                result.AddAll(first);
            }

            return(result);
        }
Пример #4
0
        //*
        // * Creates a new look-ahead set that is the combination of
        // * this set with another set. The combination is created by
        // * creating new token sequences that consist of appending all
        // * elements from the specified set onto all elements in this
        // * set. This is sometimes referred to as the cartesian
        // * product.
        // *
        // * @param set the set to combine with
        // *
        // * @return a new look-ahead set containing the combination
        //

        public LookAheadSet CreateCombination(LookAheadSet @set)
        {
            LookAheadSet result = new LookAheadSet(maxLength);
            Sequence     first  = default(Sequence);
            Sequence     second = default(Sequence);

            // Handle special cases
            if (this.Size() <= 0)
            {
                return(@set);
            }
            else if (@set.Size() <= 0)
            {
                return(this);
            }
            for (int i = 0; i <= elements.Count - 1; i++)
            {
                // Create combinations
                first = (Sequence)elements[i];
                if (first.Length() >= maxLength)
                {
                    result.Add(first);
                }
                else if (first.Length() <= 0)
                {
                    result.AddAll(@set);
                }
                else
                {
                    for (int j = 0; j <= @set.elements.Count - 1; j++)
                    {
                        second = (Sequence)@set.elements[j];
                        result.Add(first.Concat(maxLength, second));
                    }
                }
            }
            return(result);
        }
        //*
        // * Calculates the look-ahead needed for the specified production
        // * pattern. This method attempts to resolve any conflicts and
        // * stores the results in the pattern look-ahead object.
        // *
        // * @param pattern the production pattern
        // *
        // * @throws ParserCreationException if the look-ahead set couldn't
        // * be determined due to inherent ambiguities
        //

        private void CalculateLookAhead(ProductionPattern pattern)
        {
            ProductionPatternAlternative alt = default(ProductionPatternAlternative);
            LookAheadSet result = default(LookAheadSet);

            LookAheadSet[] alternatives = null;
            LookAheadSet   conflicts    = default(LookAheadSet);
            LookAheadSet   previous     = new LookAheadSet(0);
            int            length       = 1;
            int            i            = 0;
            CallStack      stack        = new CallStack();

            // Calculate simple look-ahead
            stack.Push(pattern.Name, 1);
            result       = new LookAheadSet(1);
            alternatives = new LookAheadSet[pattern.Count];
            for (i = 0; i <= pattern.Count - 1; i++)
            {
                alt             = pattern[i];
                alternatives[i] = FindLookAhead(alt, 1, 0, stack, null);
                alt.LookAhead   = alternatives[i];
                result.AddAll(alternatives[i]);
            }
            if (pattern.LookAhead == null)
            {
                pattern.LookAhead = result;
            }
            conflicts = FindConflicts(pattern, 1);

            // Resolve conflicts
            while (conflicts.Size() > 0)
            {
                length += 1;
                stack.Clear();
                stack.Push(pattern.Name, length);
                conflicts.AddAll(previous);
                for (i = 0; i <= pattern.Count - 1; i++)
                {
                    alt = pattern[i];
                    if (alternatives[i].Intersects(conflicts))
                    {
                        alternatives[i] = FindLookAhead(alt, length, 0, stack, conflicts);
                        alt.LookAhead   = alternatives[i];
                    }
                    if (alternatives[i].Intersects(conflicts))
                    {
                        if (pattern.DefaultAlternative == null)
                        {
                            pattern.DefaultAlternative = alt;
                        }
                        else if (!object.ReferenceEquals(pattern.DefaultAlternative, alt))
                        {
                            result = alternatives[i].CreateIntersection(conflicts);
                            ThrowAmbiguityException(pattern.Name, null, result);
                        }
                    }
                }
                previous  = conflicts;
                conflicts = FindConflicts(pattern, length);
            }
            for (i = 0; i <= pattern.Count - 1; i++)
            {
                // Resolve conflicts inside rules
                CalculateLookAhead(pattern[i], 0);
            }
        }