コード例 #1
0
 private static INfa Character(RegexCharacter character)
 {
     var start = new NfaState();
     var end = new NfaState();
     var terminal = new CharacterTerminal(character.Value);
     var transition = new TerminalNfaTransition(
         terminal: terminal,
         target: end);
     start.AddTransistion(transition);
     return new Nfa(start, end);
 }
コード例 #2
0
 private static INfa Character(RegexCharacterClassCharacter character, bool negate)
 {
     var start = new NfaState();
     var end = new NfaState();
     ITerminal terminal = new CharacterTerminal(character.Value);
     if (negate)
         terminal = new NegationTerminal(terminal);
     var transition = new TerminalNfaTransition(
         terminal: terminal,
         target: end);
     start.AddTransistion(transition);
     return new Nfa(start, end);
 }
コード例 #3
0
ファイル: EbnfGrammar.cs プロジェクト: bilsaboob/Pliant
        private static BaseLexerRule CreateNotDoubleQuoteLexerRule()
        {
            // ([^"]|(\\.))*
            var start = new DfaState();
            var escape = new DfaState();
            var final = new DfaState(true);

            var notDoubleQuoteTerminal = new NegationTerminal(
                new CharacterTerminal('"'));
            var escapeTerminal = new CharacterTerminal('\\');
            var anyTerminal = new AnyTerminal();

            var notDoubleQuoteEdge = new DfaTransition(notDoubleQuoteTerminal, final);
            start.AddTransition(notDoubleQuoteEdge);
            final.AddTransition(notDoubleQuoteEdge);

            var escapeEdge = new DfaTransition(escapeTerminal, escape);
            start.AddTransition(escapeEdge);
            final.AddTransition(escapeEdge);

            var anyEdge = new DfaTransition(anyTerminal, final);
            escape.AddTransition(anyEdge);

            return new DfaLexerRule(start, new TokenType(@"([^""]|(\\.))+"));
        }
コード例 #4
0
ファイル: EbnfGrammar.cs プロジェクト: bilsaboob/Pliant
        private static BaseLexerRule CreateMultiLineCommentLexerRule()
        {
            var pattern = @"\/[*]([*][^\/]|[^*])*[*][\/]";

            var states = new DfaState[5];
            for (int i = 0; i < states.Length; i++)
                states[i] = new DfaState();

            var slash = new CharacterTerminal('/');
            var star = new CharacterTerminal('*');
            var notStar = new NegationTerminal(star);
            var notSlash = new NegationTerminal(slash);

            var firstSlash = new DfaTransition(slash, states[1]);
            var firstStar = new DfaTransition(star, states[2]);
            var repeatNotStar = new DfaTransition(notStar, states[2]);
            var lastStar = new DfaTransition(star, states[3]);
            var goBackNotSlash = new DfaTransition(notSlash, states[2]);
            var lastSlash = new DfaTransition(slash, states[4]);

            states[0].AddTransition(firstSlash);
            states[1].AddTransition(firstStar);
            states[2].AddTransition(repeatNotStar);
            states[2].AddTransition(lastStar);
            states[3].AddTransition(goBackNotSlash);
            states[3].AddTransition(lastSlash);

            return new DfaLexerRule(states[0], pattern);
        }
コード例 #5
0
        public void ShouldConvertComplexNfaToDfa()
        {
            var a = new CharacterTerminal('a');
            var b = new CharacterTerminal('b');
            var c = new CharacterTerminal('c');
            var states = CreateStates(4);

            states[0].AddTransistion(new TerminalNfaTransition(a, states[1]));
            states[0].AddTransistion(new TerminalNfaTransition(c, states[3]));
            states[1].AddTransistion(new NullNfaTransition(states[0]));
            states[1].AddTransistion(new TerminalNfaTransition(b, states[2]));
            states[2].AddTransistion(new TerminalNfaTransition(a, states[1]));
            states[3].AddTransistion(new TerminalNfaTransition(c, states[2]));
            states[3].AddTransistion(new NullNfaTransition(states[2]));

            var dfa_0 = ConvertNfaToDfa(new Nfa(states[0], states[2]));

            Assert.IsNotNull(dfa_0);

            IDfaTransition transition_0_01 = null;
            IDfaTransition transition_0_23 = null;

            var count = 0;
            foreach (var transition in dfa_0.Transitions)
            {
                var terminal = transition.Terminal;
                if (terminal.IsMatch('a'))
                    transition_0_01 = transition;
                else if (terminal.IsMatch('c'))
                    transition_0_23 = transition;
                count++;
            }

            Assert.AreEqual(2, count);

            var dfa_01 = transition_0_01.Target;
            IDfaTransition transition_01_01 = null;
            IDfaTransition transition_01_23 = null;
            IDfaTransition transition_01_2 = null;

            count = 0;
            foreach (var transition in dfa_01.Transitions)
            {
                var terminal = transition.Terminal;
                if (terminal.IsMatch('a'))
                    transition_01_01 = transition;
                else if (terminal.IsMatch('b'))
                    transition_01_2 = transition;
                else if (terminal.IsMatch('c'))
                    transition_01_23 = transition;
                count++;
            }

            Assert.AreEqual(3, count);

            var dfa_23 = transition_0_23.Target;
            IDfaTransition transition_23_01 = null;
            IDfaTransition transition_23_2 = null;

            count = 0;
            foreach (var transition in dfa_23.Transitions)
            {
                var terminal = transition.Terminal;
                if (terminal.IsMatch('a'))
                    transition_23_01 = transition;
                else if (terminal.IsMatch('c'))
                    transition_23_2 = transition;
                count++;
            }

            Assert.AreEqual(2, count);

            var dfa_2 = transition_23_2.Target;
            IDfaTransition transition_2_01 = null;
            count = 0;
            foreach (var transition in dfa_2.Transitions)
            {
                var terminal = transition.Terminal;
                if (terminal.IsMatch('a'))
                    transition_2_01 = transition;
                count++;
            }

            Assert.AreEqual(1, count);
        }
コード例 #6
0
        public void ShouldConvertSingleCharacterNfaToEquivalentDfa()
        {
            var a = new CharacterTerminal('a');
            var states = CreateStates(2);

            states[0].AddTransistion(new TerminalNfaTransition(a, states[1]));

            var nfa = new Nfa(states[0], states[1]);
            var dfa = ConvertNfaToDfa(nfa);

            Assert.IsNotNull(dfa);
        }