コード例 #1
0
        private static void CheckOutput(IDictionary <TypeVariable, IHerbrandObject> input, IDictionary <TypeVariable, IHerbrandObject> expectedOutput)
        {
            var output = GetOutput(input);

            Assert.IsTrue(MappingEquivalence.AreEquivalent(
                              (IReadOnlyDictionary <TypeVariable, IHerbrandObject>)output,
                              (IReadOnlyDictionary <TypeVariable, IHerbrandObject>)expectedOutput));
        }
コード例 #2
0
        public void TestSimple()
        {
            var dfa = CreateLinearDfa(new List <Label> {
                Label.B, Label.C
            });

            // A -> BC
            var rules = new Dictionary <Label, IDfa <Optional <Rule <Label> >, Label> > {
                { Label.A, dfa }
            };

            // regex matching the rule above
            var regex = Concat(Label.B.ToRegex(), Label.C.ToRegex());

            // state 2 is the only accepting state
            dfa.Labels[2] = Optional <Rule <Label> > .Some(new Rule <Label> {
                Lhs = Label.A, Rhs = regex
            });

            var grammar = new CompiledGrammar <Label> {
                Rules = rules, StartSymbol = Label.A
            };

            var follow = new Dictionary <Label, IReadOnlyCollection <DfaAndState <Label> > >
            {
                [Label.EOF] = new List <DfaAndState <Label> >
                {
                    new DfaAndState <Label> {
                        Dfa = dfa, State = dfa.GetState(2)
                    }
                }
            };

            var firstPlus = new Dictionary <Label, IReadOnlyCollection <DfaAndState <Label> > >
            {
                [Label.B] = new List <DfaAndState <Label> >
                {
                    new DfaAndState <Label> {
                        Dfa = dfa, State = dfa.GetState(0)
                    }
                },
                [Label.C] = new List <DfaAndState <Label> >
                {
                    new DfaAndState <Label> {
                        Dfa = dfa, State = dfa.GetState(1)
                    }
                }
            };

            var parseTable = ParseTableGenerator <Label> .Parse(grammar, follow, firstPlus);

            // compressed representation of the expected parse table
            var actions = new List <Tuple <int, Label, bool> >
            {
                Tuple.Create(0, Label.B, false),
                Tuple.Create(1, Label.C, false),
                Tuple.Create(2, Label.EOF, true)
            };

            var parseTableExpected =
                new Dictionary <Tuple <IDfa <Optional <Rule <Label> >, Label>, IState, Label>, ParseAction <Label> >();

            foreach (var(index, label, isReduce) in actions)
            {
                var state      = dfa.GetState(index);
                var key        = Tuple.Create <IDfa <Optional <Rule <Label> >, Label>, IState, Label>(dfa, state, label);
                var actionKind = isReduce
                    ? ParseAction <Label> .ActionKind.Reduce
                    : ParseAction <Label> .ActionKind.Shift;
                var actionLabel = new List <Label> {
                    Label.B, Label.C, Label.A
                }.ElementAt(index);

                parseTableExpected[key] = new ParseAction <Label> {
                    Kind = actionKind, Label = actionLabel
                };
            }

            Assert.IsTrue(MappingEquivalence.AreEquivalent(parseTable, parseTableExpected));
        }