示例#1
0
 private static BaseLexerRule CreateRegexDfa(string pattern)
 {
     var regexParser = new RegexParser();
     var regex = regexParser.Parse(pattern);
     var regexCompiler = new RegexCompiler();
     var dfa = regexCompiler.Compile(regex);
     return new DfaLexerRule(dfa, pattern);
 }
示例#2
0
        public void RegexToDfaShouldConvertCharacterRegexToDfa()
        {
            var pattern = "a";
            var regex = new RegexParser().Parse(pattern);
            var nfa = new ThompsonConstructionAlgorithm().Transform(regex);
            var dfa = new SubsetConstructionAlgorithm().Transform(nfa);
            Assert.IsNotNull(dfa);

            var lexerRule = new DfaLexerRule(dfa, "a");
            var lexeme = new DfaLexemeFactory().Create(lexerRule);
            Assert.IsTrue(lexeme.Scan('a'));
        }
示例#3
0
 public void RegexToDfaShouldConvertOptionalCharacterClassToDfa()
 {
     var pattern = @"[-+]?[0-9]";
     var regex = new RegexParser().Parse(pattern);
     var nfa = new ThompsonConstructionAlgorithm().Transform(regex);
     var dfa = new SubsetConstructionAlgorithm().Transform(nfa);
     Assert.IsNotNull(dfa);
     Assert.AreEqual(3, dfa.Transitions.Count);
     var lexerRule = new DfaLexerRule(dfa, pattern);
     AssertLexerRuleMatches(lexerRule, "+0");
     AssertLexerRuleMatches(lexerRule, "-1");
     AssertLexerRuleMatches(lexerRule, "9");
 }
 private static INfa CreateNfa(string input)
 {
     var regex = new RegexParser().Parse(input);
     return new ThompsonConstructionAlgorithm().Transform(regex);
 }