Пример #1
0
        public static IEnumerable<State> AStar()
        {
            var aAlternation = new Alternation();
            State a = new State("A", aAlternation);

            aAlternation.Alternations.Add(new Concat(new PushState(a), new RegexLiteral("a")));
            aAlternation.Alternations.Add(new RegexLiteral(""));

            return new[] { a };
        }
Пример #2
0
        public static IEnumerable<State> AnBn()
        {
            State b = new State("B", new RegexLiteral("b"));

            var aAlternation = new Alternation();
            State a = new State("A", aAlternation);

            aAlternation.Alternations.Add(new Concat(new PushState(b), new RegexLiteral("a"), new PushState(a)));
            aAlternation.Alternations.Add(new RegexLiteral(""));

            return new[] { a, b };
        }
Пример #3
0
        public static void AnBmCnm()
        {
            State c = new State("C", new RegexLiteral("c"));

            var bAlternation=new Alternation();
            State b = new State("B", bAlternation);

            bAlternation.Alternations.Add(new Concat(new PushState(c), new RegexLiteral("b"), new PushState(b)));
            bAlternation.Alternations.Add(new RegexLiteral(""));

            var aAlternation=new Alternation();
            State a = new State("A", aAlternation);
        }
Пример #4
0
 private void Write(Alternation alternation)
 {
     if (alternation.Alternations.Count == 1)
     {
         Write(alternation.Alternations.Single());
         return;
     }
     using (NonCapturingGroup())
     {
         WriteWithSeparator("|", alternation.Alternations);
     }
 }
Пример #5
0
        public static IEnumerable<State> NumberedStatesPcreGrammarStates()
        {
            var q0Altermations = new Alternation(new RegexLiteral(@"\w"));
            State q = new State("Q", q0Altermations);

            //// A -> A (',' | Q) | ε
            var a0Altermations = new Alternation();
            State a = new State("A", a0Altermations);
            a0Altermations.Alternations.Add(new Concat(new PushState(a), new Alternation(new RegexLiteral(","), new PushState(q))));
            a0Altermations.Alternations.Add(new RegexLiteral(""));

            // pop Q2Close -> '>'
            State q2Close = new State("Q2Close", new RegexLiteral(">"));
            State q2 = new State("Q2", new Concat(new RegexLiteral("<"),
                new PushState(q2Close),
                new PushState(a)));
            q0Altermations.Alternations.Add(q2.StateBody);

            // pop QStar -> push QStar push Q | ε
            State qStar = GetStateStar(q);

            State q1Close = new State("Q1Close", new RegexLiteral(@"\]"));
            // pop Q2Semicolon -> ';'
            State q1Semicolon = new State("Q1Semicolon", new RegexLiteral(";"));
            //pop Q2OptionalComma -> ',' | ε
            State q1OptionalComma = new State("Q1Comma", new RegexLiteral(",?"));

            //pop Q1 -> '[' push Q1Close push QStar push Q1OptionalComma push QStar push Q1Semicolon push A

            State q1 = new State("Q1", new Concat(
                new RegexLiteral(@"\["),
                new PushState(q1Close),
                new PushState(qStar),
                new PushState(q1OptionalComma),
                new PushState(qStar),
                new PushState(q1Semicolon),
                new PushState(a)
                ));
            q0Altermations.Alternations.Add(q1.StateBody);

            return new[] { a, q, q2Close, qStar, q1OptionalComma, q1Semicolon, q1Close };
        }
Пример #6
0
        /*
        A -> (Q | ',')*
        Q -> \w | '[' A ';' Q* ','? Q* ']' | '<' A '>'

          \__________Q1___________/  \__Q2___/

        ========================================

        pop A -> push A (',' | Q) | ε

        pop Q -> \w | push Q1 | push Q2
        pop QStar -> push QStar push Q | ε

        pop Q2 -> '<' push Q2Close push A
        pop Q2Close -> '>'

        pop Q1 -> '[' push Q1Close push QStar push Q1OptionalComma push QStar push Q1Semicolon push A
        pop Q1Close -> ']'
        pop Q2Semicolon -> ';'
        pop Q2OptionalComma -> ',' | ε

        */
        /// <summary>
        /// For a State "X", generate a new State classed "XStar" representing "X*".
        /// </summary>
        private static State GetStateStar(State state)
        {
            var starAlternations = new Alternation();
            State stateStar = new State(state.Name + "Star", starAlternations);
            starAlternations.Alternations.Add(new Concat(new PushState(stateStar), new PushState(state)));
            starAlternations.Alternations.Add(new RegexLiteral(""));
            return stateStar;
        }
Пример #7
0
        public static IEnumerable<State> NumberedStatesPcreGrammarWithCapturesStates()
        {
            var q0Altermations = new Alternation(new RegexLiteral(@"\w(?<Q-Start>)"));
            State q = new State("Q", new Concat(new RegexLiteral(@"(?<Start>)"), q0Altermations));

            var a0 = new State("A0", new Alternation(new RegexLiteral(","), new PushState(q)));
            var a0Star = GetStateStar(a0);
            var aEnd = new State("AEnd", new RegexLiteral("(?<A-Start>)"));
            var a = new State("A", new Concat(new RegexLiteral(@"(?<Start>)") ,new PushState(aEnd),new PushState(a0Star)));

            // pop Q2Close -> '>'
            State q2Close = new State("Q2Close", new RegexLiteral(">(?<Q-Start>)"));
            State q2 = new State("Q2", new Concat(new RegexLiteral("<"),
                new PushState(q2Close),
                new PushState(a)));
            q0Altermations.Alternations.Add(q2.StateBody);

            // pop QStar -> push QStar push Q | ε
            State qStar = GetStateStar(q);

            State q1Close = new State("Q1Close", new RegexLiteral(@"\](?<Q-Start>)"));
            // pop Q2Semicolon -> ';'
            State q1Semicolon = new State("Q1Semicolon", new RegexLiteral(";"));
            //pop Q2OptionalComma -> ',' | ε
            State q1OptionalComma = new State("Q1Comma", new RegexLiteral(",?"));

            //pop Q1 -> '[' push Q1Close push QStar push Q1OptionalComma push QStar push Q1Semicolon push A

            State q1 = new State("Q1", new Concat(
                new RegexLiteral(@"\["),
                new PushState(q1Close),
                new PushState(qStar),
                new PushState(q1OptionalComma),
                new PushState(qStar),
                new PushState(q1Semicolon),
                new PushState(a)
                ));
            q0Altermations.Alternations.Add(q1.StateBody);

            return new[] { a, a0, a0Star,aEnd, q, q2Close, qStar, q1OptionalComma, q1Semicolon, q1Close };
        }