Exemplo n.º 1
0
        public void complement_with_set_of_different_size_returns_correct_result(IntSet set)
        {
            var otherSetType = new BitSetType(10);
            var y            = otherSetType.Of(2, 5, 9);

            var got = set.Complement(y);

            foreach (var item in set)
            {
                Assert.IsTrue(!got.Contains(item));
            }

            foreach (var item in y)
            {
                Assert.IsTrue(got.Contains(item) || set.Contains(item));
            }

            int countToCheck = 1000;

            foreach (var item in got)
            {
                Assert.IsFalse(set.Contains(item));
                if (--countToCheck == 0)
                {
                    break;
                }
            }

            set_is_optimized(got);
        }
Exemplo n.º 2
0
        private DotState[] BuildLr0ItemSets()
        {
            var result = new List <DotState>();

            var initialItemSet = ClosureLr0(new MutableDotItemSet
            {
                new DotItem(grammar.AugmentedProduction, 0)
                {
                    LA = TokenSet.Mutable()
                }
            });

            result.Add(new DotState(0, initialItemSet));

            bool addedStatesInRound;

            do
            {
                addedStatesInRound = false;

                for (int i = 0; i != result.Count; ++i)
                {
                    var itemSet = result[i].Items;

                    foreach (var token in GetOutTokens(itemSet))
                    {
                        var nextStateItems = GoTo(itemSet, token);

                        CollectClosureLookaheads(nextStateItems, grammar);
                        if (nextStateItems.Count == 0)
                        {
                            throw new InvalidOperationException("Internal error: next state cannot be empty");
                        }

                        var nextState = result.Find(state => state.Items.Equals(nextStateItems));
                        if (nextState == null)
                        {
                            addedStatesInRound = true;
                            nextState          = new DotState(result.Count, nextStateItems);
                            result.Add(nextState);
                        }

                        if (result[i].AddTransition(token, nextState, TokenSet))
                        {
                            addedStatesInRound = true;
                        }
                    }
                }
            }while (addedStatesInRound);

            StateSet = new BitSetType(result.Count);

            return(result.ToArray());
        }
Exemplo n.º 3
0
        public NullableFirstTables(Grammar grammar)
        {
            this.grammar = grammar;
            int count = grammar.Symbols.Count;

            this.tokenSet   = new BitSetType(count);
            this.firsts     = new MutableIntSet[count];
            this.isNullable = new bool[count];

            MaxRuleSize = grammar.Productions.Select(r => r.PatternTokens.Length).Max();
            Build();
        }
Exemplo n.º 4
0
        public Lalr1Dfa(GrammarAnalysis grammar, LrTableOptimizations optimizations)
        {
            this.grammar       = grammar;
            this.Optimizations = optimizations;
            this.TokenSet      = grammar.TokenSet;

            BuildLalr1States();

            if ((Optimizations & LrTableOptimizations.EliminateLr0ReduceStates) != 0)
            {
                EliminateLr0ReduceStates();
            }
        }
Exemplo n.º 5
0
        public void intersect_with_set_of_different_size(IntSet x)
        {
            var otherSetType = new BitSetType(10);
            var y            = otherSetType.Of(2, 5, 9);

            var intersection   = x.Intersect(y);
            var hsIntersection = new HashSet <int>(x);

            hsIntersection.IntersectWith(new HashSet <int>(y));
            CollectionAssert.AreEquivalent(hsIntersection, hsIntersection);

            set_is_optimized(intersection);
        }
Exemplo n.º 6
0
        public void addall_with_set_of_different_size(IntSet set)
        {
            var otherSetType = new BitSetType(10);
            var other        = otherSetType.Of(2, 5, 9);

            MutableIntSet editedSet = set.EditCopy();

            editedSet.AddAll(other);
            Assert.IsTrue(editedSet.IsSupersetOf(other));
            Assert.IsTrue(set.Union(other).SetEquals(editedSet));
            IntSet result = editedSet.CompleteAndDestroy();

            Assert.IsTrue(result.IsSupersetOf(other));
            Assert.IsTrue(set.Union(other).SetEquals(result));
        }
        private bool CompileScannerTdfas(Grammar grammar)
        {
            var tokenSet = new BitSetType(grammar.Symbols.Count);

            IScanAmbiguityResolver scanAmbiguityResolver
                = new ScanAmbiguityResolver(
                      tokenSet,
                      grammar.Matchers.Count);

            foreach (var condition in grammar.Conditions)
            {
                ITdfaData tdfa;
                if (!CompileTdfa(logging, condition, out tdfa))
                {
                    logging.Write(
                        new LogEntry
                    {
                        Severity = Severity.Error,
                        Origin   = source.Origin,
                        Message  = string.Format(
                            "Unable to create scanner for '{0}' language.",
                            source.LanguageName)
                    });

                    return(false);
                }

                // For each action store information about produced tokens
                foreach (var scanProduction in condition.Matchers)
                {
                    scanAmbiguityResolver.RegisterAction(scanProduction);
                }

                // For each 'ambiguous scanner state' deduce all tokens
                // which can be produced in this state.
                foreach (var state in tdfa.EnumerateStates())
                {
                    scanAmbiguityResolver.RegisterState(state);
                }
            }

            scanAmbiguityResolver.DefineAmbiguities(grammar);

            return(true);
        }
Exemplo n.º 8
0
        public void equility_and_hash_to_set_with_different_size_works_according_to_the_content(IntSet x, IntSet yOfSameType)
        {
            BitSetType otherSetType = new BitSetType(10);
            var        y            = yOfSameType.Intersect(otherSetType.All);
            bool       equals       = x.Equals(y);
            bool       setEquals    = x.SetEquals(y);
            bool       hsEquals     = new HashSet <int>(x).SetEquals(new HashSet <int>(y));

            Assert.AreEqual(hsEquals, equals);
            Assert.AreEqual(hsEquals, setEquals);
            if (equals)
            {
                Assert.AreEqual(x.GetHashCode(), y.GetHashCode());
            }
            else
            {
                // Uncomment for hashing failure statistics
                // Assert.AreNotEqual(x.GetHashCode(), y.GetHashCode());
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// 비트 값을 설정 합니다.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="bit"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static int SetBit(int value, int bit, BitSetType type)
        {
            int b = 1 << bit;

            int iOn = (value & b);

            if (iOn > 0)
            {
                if (type == BitSetType.Off || type == BitSetType.Toggle)
                {
                    value = value - iOn;
                }
            }
            else
            {
                if (type == BitSetType.On || type == BitSetType.Toggle)
                {
                    value = value + (1 << bit);
                }
            }

            return(value);
        }