Exemplo n.º 1
0
        private void ConvertLexcionToNFA()
        {
            //Compact transition char set
            CompactCharSetManager = m_lexicon.CreateCompactCharSetManager();
            NFAConverter converter = new NFAConverter(CompactCharSetManager);

            NFAState entryState = new NFAState();
            NFAModel lexerNFA   = new NFAModel();

            lexerNFA.AddState(entryState);
            foreach (var token in m_lexicon.GetTokens())
            {
                NFAModel tokenNFA = token.CreateFiniteAutomatonModel(converter);

                entryState.AddEdge(tokenNFA.EntryEdge);
                lexerNFA.AddStates(tokenNFA.States);
            }
        }
Exemplo n.º 2
0
        public void RegExToDFATest()
        {
            //var RE_IF = RE.Literal("if");
            //var RE_ELSE = RE.Literal("else");
            var RE_ID = RE.Range('a', 'z').Concat(
                (RE.Range('a', 'z') | RE.Range('0', '9')).Many());
            //var RE_NUM = RE.Range('0', '9').Many1();
            //var RE_ERROR = RE.Range(Char.MinValue, (char)255);
            Lexicon lexicon = new Lexicon();
            var     ID      = lexicon.DefaultLexer.DefineToken(RE_ID);

            NFAConverter nfaConverter = new NFAConverter(lexicon.CreateCompactCharSetManager());

            DFAModel D_ID = DFAModel.Create(lexicon);

            //verify state 0
            var state0 = D_ID.States[0];

            Assert.AreEqual(3, state0.OutEdges.Count);
            foreach (var edge in state0.OutEdges)
            {
                Assert.AreEqual(0, edge.TargetState.Index);
            }

            //verify initialization state
            var state1 = D_ID.States[1];

            foreach (var edge in state1.OutEdges)
            {
                if (edge.Symbol == 1) //a..z
                {
                    Assert.IsTrue(edge.TargetState.Index > 0);
                }
                else
                {
                    Assert.AreEqual(0, edge.TargetState.Index);
                }
            }
        }