示例#1
0
        public void SetLookahead()
        {
            var set1 = ParseItemSet.New(new ParseItem[] { _item10, _item11, _item20 });

            var la1 = SegmentSet.New(new Segment[] { _lookaheadA, _lookaheadB });
            var la2 = SegmentSet.New(new Segment[] { _lookaheadB, _lookaheadC });

            Assert.That(set1.GetLookahead(_item10), Is.EqualTo(SegmentSet.EmptySet));
            Assert.That(set1.GetLookahead(_item11), Is.EqualTo(SegmentSet.EmptySet));
            Assert.That(set1.GetLookahead(_item20), Is.EqualTo(SegmentSet.EmptySet));

            set1.SetLookahead(_item10, la1);
            set1.SetLookahead(_item11, la1);

            Assert.That(set1.GetLookahead(_item10), Is.EqualTo(la1));
            Assert.That(set1.GetLookahead(_item11), Is.EqualTo(la1));
            Assert.That(set1.GetLookahead(_item20), Is.EqualTo(SegmentSet.EmptySet));

            set1.SetLookahead(_item10, la2);

            Assert.That(set1.GetLookahead(_item10), Is.EqualTo(la2));
            Assert.That(set1.GetLookahead(_item11), Is.EqualTo(la1));
            Assert.That(set1.GetLookahead(_item20), Is.EqualTo(SegmentSet.EmptySet));
        }
示例#2
0
        Config ConstructSample2()
        {
            /*
             * S    := "id"
             *      | V ":=" E
             *
             * V    := "id"
             *
             * E    := V
             *      | "n"
             */

            var s = new Segment("S", false);
            var v = new Segment("V", false);
            var e = new Segment("E", false);

            var id     = new Segment("id", true);
            var assign = new Segment(":=", true);
            var n      = new Segment("n", true);

            return(new Config()
            {
                TopLevelSegments = SegmentSet.New(new Segment[] { s, e, }),
                Productions =
                {
                    new ConfigProduction()
                    {
                        Target = new ConfigToken(ConfigTokenType.NonTerminal, null, null, "S"),
                        Segment = s,
                        Rules =
                        {
                            new ConfigRule()
                            {
                                Segments =
                                {
                                    new ConfigSegment()
                                    {
                                        Token = new ConfigToken(ConfigTokenType.Label, null, null, "id"),
                                        Segment = id,
                                    },
                                },
                                Production = new Production(s, ImmutableArray.Create(id)),
                            },
                            new ConfigRule()
                            {
                                Segments =
                                {
                                    new ConfigSegment()
                                    {
                                        Token = new ConfigToken(ConfigTokenType.NonTerminal, null, null, "V"),
                                        Segment = v,
                                    },
                                    new ConfigSegment()
                                    {
                                        Token = new ConfigToken(ConfigTokenType.Label, null, null, ":="),
                                        Segment = assign,
                                    },
                                    new ConfigSegment()
                                    {
                                        Token = new ConfigToken(ConfigTokenType.NonTerminal, null, null, "E"),
                                        Segment = e,
                                    },
                                },
                                Production = new Production(s, ImmutableArray.Create(v, assign, e)),
                            },
                        },
                    },
                    new ConfigProduction()
                    {
                        Target = new ConfigToken(ConfigTokenType.NonTerminal, null, null, "V"),
                        Segment = v,
                        Rules =
                        {
                            new ConfigRule()
                            {
                                Segments =
                                {
                                    new ConfigSegment()
                                    {
                                        Token = new ConfigToken(ConfigTokenType.Label, null, null, "id"),
                                        Segment = id,
                                    },
                                },
                                Production = new Production(v, ImmutableArray.Create(id)),
                            },
                        },
                    },
                    new ConfigProduction()
                    {
                        Target = new ConfigToken(ConfigTokenType.NonTerminal, null, null, "E"),
                        Segment = e,
                        Rules =
                        {
                            new ConfigRule()
                            {
                                Segments =
                                {
                                    new ConfigSegment()
                                    {
                                        Token = new ConfigToken(ConfigTokenType.NonTerminal, null, null, "V"),
                                        Segment = v,
                                    },
                                },
                                Production = new Production(e, ImmutableArray.Create(v)),
                            },
                            new ConfigRule()
                            {
                                Segments =
                                {
                                    new ConfigSegment()
                                    {
                                        Token = new ConfigToken(ConfigTokenType.Label, null, null, "n"),
                                        Segment = n,
                                    },
                                },
                                Production = new Production(e, ImmutableArray.Create(n)),
                            },
                        },
                    },
                },
            });
        }
示例#3
0
        Config ConstructSample1()
        {
            /*
             * A    := "(" A ")"
             *      | "a"
             */

            var nonTerminal = new Segment("A", false);
            var terminal    = new Segment("a", true);
            var openParen   = new Segment("(", true);
            var closeParen  = new Segment(")", true);

            return(new Config()
            {
                TopLevelSegments = SegmentSet.New(new Segment[] { nonTerminal }),
                Productions =
                {
                    new ConfigProduction()
                    {
                        Target = new ConfigToken(ConfigTokenType.NonTerminal, null, null, "A"),
                        Segment = nonTerminal,
                        Rules =
                        {
                            new ConfigRule()
                            {
                                Segments =
                                {
                                    new ConfigSegment()
                                    {
                                        Token = new ConfigToken(ConfigTokenType.Label, null, null, "("),
                                        Segment = openParen,
                                    },
                                    new ConfigSegment()
                                    {
                                        Token = new ConfigToken(ConfigTokenType.NonTerminal, null, null, "A"),
                                        Segment = nonTerminal,
                                    },
                                    new ConfigSegment()
                                    {
                                        Token = new ConfigToken(ConfigTokenType.Label, null, null, ")"),
                                        Segment = closeParen,
                                    },
                                },
                                Production = new Production(nonTerminal, ImmutableArray.Create(openParen, nonTerminal, closeParen)),
                            },
                            new ConfigRule()
                            {
                                Segments =
                                {
                                    new ConfigSegment()
                                    {
                                        Token = new ConfigToken(ConfigTokenType.Label, null, null, "a"),
                                        Segment = terminal,
                                    },
                                },
                                Production = new Production(nonTerminal, ImmutableArray.Create(terminal)),
                            },
                        },
                    },
                },
            });
        }