Example #1
0
        internal static int NumberOfPseudoDeadStates(this IDfa <char> dfa)
        {
            int number = 0;
            Queue <IDfaState <char> >          Q     = new Queue <IDfaState <char> >();
            Dictionary <IDfaState <char>, int> color = new Dictionary <IDfaState <char>, int>();

            Q.Enqueue(dfa.Start);

            while (Q.Count > 0)
            {
                IDfaState <char> current_state = Q.Dequeue();
                if (!color.ContainsKey(current_state))
                {
                    color[current_state] = 1;
                    if (current_state.IsPseudoDead())
                    {
                        number++;
                    }
                    foreach (var transition in current_state.Transitions)
                    {
                        if (!color.ContainsKey(transition.Value))
                        {
                            Q.Enqueue(transition.Value);
                        }
                    }
                }
            }
            return(number);
        }
Example #2
0
 public DfaLexeme(IDfaLexerRule dfaLexerRule, int position)
 {
     LexerRule      = dfaLexerRule;
     Position       = position;
     _stringBuilder = SharedPools.Default <StringBuilder>().AllocateAndClear();
     _currentState  = dfaLexerRule.Start;
 }
Example #3
0
        static NumberLexerRule()
        {
            var states = new DfaState[5];

            for (int i = 0; i < states.Length; i++)
            {
                states[i] = new DfaState(i == 4 || i == 2);
            }

            var zeroThroughNine = new RangeTerminal('0', '9');

            var plusOrMinusTo1     = new DfaTransition(new SetTerminal('+', '-'), states[1]);
            var dotTo3             = new DfaTransition(new CharacterTerminal('.'), states[3]);
            var zeroThroughNineTo2 = new DfaTransition(zeroThroughNine, states[2]);
            var zeroThroughNineTo4 = new DfaTransition(zeroThroughNine, states[4]);

            states[0].AddTransition(dotTo3);
            states[0].AddTransition(plusOrMinusTo1);
            states[0].AddTransition(zeroThroughNineTo2);

            states[1].AddTransition(dotTo3);
            states[1].AddTransition(zeroThroughNineTo2);

            states[2].AddTransition(zeroThroughNineTo2);
            states[2].AddTransition(dotTo3);

            states[3].AddTransition(zeroThroughNineTo4);

            states[4].AddTransition(zeroThroughNineTo4);

            _start = states[0];
        }
Example #4
0
        static NumberLexerRule()
        {
            var states = new DfaState[5];
            for (int i = 0; i < states.Length; i++)
                states[i] = new DfaState(i==4 || i== 2);

            var zeroThroughNine = new RangeTerminal('0', '9');

            var plusOrMinusTo1 = new DfaTransition(new SetTerminal('+', '-'), states[1]);
            var dotTo3 = new DfaTransition(new CharacterTerminal('.'), states[3]);
            var zeroThroughNineTo2 = new DfaTransition(zeroThroughNine, states[2]);
            var zeroThroughNineTo4 = new DfaTransition(zeroThroughNine, states[4]);

            states[0].AddTransition(dotTo3);
            states[0].AddTransition(plusOrMinusTo1);
            states[0].AddTransition(zeroThroughNineTo2);

            states[1].AddTransition(dotTo3);
            states[1].AddTransition(zeroThroughNineTo2);

            states[2].AddTransition(zeroThroughNineTo2);
            states[2].AddTransition(dotTo3);

            states[3].AddTransition(zeroThroughNineTo4);

            states[4].AddTransition(zeroThroughNineTo4);

            _start = states[0];
        }
Example #5
0
 public void Reset(IDfaLexerRule dfaLexerRule)
 {
     _capture = null;
     if(IsStringBuilderAllocated())
         _stringBuilder.Clear();
     _currentState = dfaLexerRule.Start;
     LexerRule = dfaLexerRule;
 }
Example #6
0
 public override void Reset()
 {
     _capture = null;
     if (IsStringBuilderAllocated())
     {
         _stringBuilder.Clear();
     }
     _currentState = ConcreteLexerRule.Start;
 }
        internal static bool IsPseudoDead(this IDfaState <char> state)
        {
            Dictionary <IDfaState <char>, int> color = new Dictionary <IDfaState <char>, int>();

            if (!FindAcceptingState(state, color))
            {
                return(true);
            }
            return(false);
        }
Example #8
0
 public void Reset(IDfaLexerRule dfaLexerRule)
 {
     _capture = null;
     if (IsStringBuilderAllocated())
     {
         _stringBuilder.Clear();
     }
     _currentState = dfaLexerRule.Start;
     LexerRule     = dfaLexerRule;
 }
Example #9
0
 static WhitespaceLexerRule()
 {
     _start = new DfaState();
     var end = new DfaState(isFinal: true);
     var transition = new DfaTransition(
         new WhitespaceTerminal(),
         end);
     _start.AddTransition(transition);
     end.AddTransition(transition);
 }
Example #10
0
        static WordLexerRule()
        {
            _start = new DfaState();
            var end        = new DfaState(isFinal: true);
            var transition = new DfaTransition(
                new WordTerminal(),
                end);

            _start.AddTransition(transition);
            end.AddTransition(transition);
        }
Example #11
0
 public bool Scan(char c)
 {
     foreach (var edge in _currentState.Edges)
     {
         if (edge.Terminal.IsMatch(c))
         {
             _currentState = edge.Target;
             _capture.Append(c);
             return true;
         }
     }
     return false;
 }
Example #12
0
 public bool Scan(char c)
 {
     for(var e = 0; e<_currentState.Transitions.Count; e++)
     {
         var edge = _currentState.Transitions[e];
         if (edge.Terminal.IsMatch(c))
         {
             if (!IsStringBuilderAllocated())
                 ReallocateStringBuilderFromCapture();
             _currentState = edge.Target;
             _stringBuilder.Append(c);
             return true;
         }
     }
     return false;
 }
Example #13
0
 public override bool Scan(char c)
 {
     for (var e = 0; e < _currentState.Transitions.Count; e++)
     {
         var edge = _currentState.Transitions[e];
         if (edge.Terminal.IsMatch(c))
         {
             if (!IsStringBuilderAllocated())
             {
                 ReallocateStringBuilderFromCapture();
             }
             _currentState = edge.Target;
             _stringBuilder.Append(c);
             return(true);
         }
     }
     return(false);
 }
Example #14
0
        public override bool Scan()
        {
            if (!Capture.Peek(out char c))
            {
                return(false);
            }

            for (var e = 0; e < _currentState.Transitions.Count; e++)
            {
                var edge = _currentState.Transitions[e];

                if (edge.Terminal.IsMatch(c))
                {
                    _currentState = edge.Target;
                    return(Capture.Grow());
                }
            }
            return(false);
        }
Example #15
0
 private void Check(IDfaState <TSymbol> state)
 {
     if (state == null)
     {
         Debug.Fail("Found null state in DFA");
     }
     if (_visitedStates.Contains(state))
     {
         return;
     }
     _visitedStates.Add(state);
     Debug.Assert(state.Transitions != null);
     Debug.Assert(state.Transitions.Count > 0);
     for (var i = 1; i < state.Transitions.Count; i++)
     {
         Debug.Assert(state.Transitions[i - 1].Key.CompareTo(state.Transitions[i].Key) < 0);
     }
     foreach (var transition in state.Transitions)
     {
         Check(transition.Value);
     }
 }
        static SingleQuoteStringLexerRule()
        {
            var states = new DfaState[3];

            for (var i = 0; i < states.Length; i++)
            {
                states[i] = new DfaState(i == 2);
            }

            var quote    = new CharacterTerminal('\'');
            var notQuote = new NegationTerminal(quote);

            var quoteToNotQuote    = new DfaTransition(quote, states[1]);
            var notQuoteToNotQuote = new DfaTransition(notQuote, states[1]);
            var notQuoteToQuote    = new DfaTransition(quote, states[2]);

            states[0].AddTransition(quoteToNotQuote);
            states[1].AddTransition(notQuoteToNotQuote);
            states[1].AddTransition(notQuoteToQuote);

            _start = states[0];
        }
        static bool FindAcceptingState(IDfaState <char> state, Dictionary <IDfaState <char>, int> color)
        {
            if (state.Accepting != 0)
            {
                return(true);
            }
            color[state] = 1;

            foreach (var transition in state.Transitions)
            {
                if (!color.ContainsKey(transition.Value))
                {
                    if (FindAcceptingState(transition.Value, color))
                    {
                        return(true);
                    }
                }
            }

            color[state] = 2;
            return(false);
        }
Example #18
0
 public DfaTransition(ITerminal terminal, IDfaState target)
 {
     Target = target;
     Terminal = terminal;
 }
Example #19
0
 public DfaLexeme(IDfaLexerRule dfaLexerRule)
 {
     LexerRule = dfaLexerRule;
     _stringBuilder = SharedPools.Default<StringBuilder>().AllocateAndClear();
     _currentState = dfaLexerRule.Start;
 }
Example #20
0
 public void AddTransition(ITerminal terminal, IDfaState node)
 {
     AddTransition(new DfaTransition(terminal, node));
 }
Example #21
0
 internal static bool IsAccepting <TSymbol>(this IDfaState <TSymbol> state)
     where TSymbol : IComparable <TSymbol>, IEquatable <TSymbol>
 {
     return(state.Accepting != 0);
 }
Example #22
0
 public DfaLexeme(IDfaState dfaState, TokenType tokenType)
 {
     _capture = new StringBuilder();
     _currentState = dfaState;
     TokenType = tokenType;
 }
Example #23
0
 public override void Reset()
 {
     _currentState = ConcreteLexerRule.Start;
 }
Example #24
0
 public DfaLexeme(IDfaLexerRule dfaLexerRule, ICapture <char> segment, int offset)
     : base(dfaLexerRule, segment, offset)
 {
     _currentState = dfaLexerRule.Start;
 }
Example #25
0
 public DfaLexerRule(IDfaState state, TokenType tokenType)
     : base(DfaLexerRuleType, tokenType)
 {
     Start = state;
 }
Example #26
0
 public DfaEdge(ITerminal terminal, IDfaState target)
 {
     Target = target;
     Terminal = terminal;
 }
Example #27
0
 public DfaLexerRule(IDfaState state, string tokenType)
     : this(state, new TokenType(tokenType))
 {
 }
Example #28
0
 public DfaTransition(ITerminal terminal, IDfaState target)
 {
     Target   = target;
     Terminal = terminal;
 }
Example #29
0
 public DfaLexerRule(IDfaState state, TokenType tokenType)
 {
     Start = state;
     TokenType = tokenType;
 }
Example #30
0
 public DfaLexerRule(IDfaState state, TokenType tokenType)
     : base(DfaLexerRuleType, tokenType)
 {
     Start     = state;
     _hashCode = ComputeHashCode(DfaLexerRuleType, tokenType);
 }
Example #31
0
 public DfaLexerRule(IDfaState state, string tokenType)
     : this(state, new TokenType(tokenType))
 {
 }