Пример #1
0
        void ReadDFAState(Stream stream, int entityNum)
        {
            var index         = (int)ReadEntity(stream);
            var isAcceptState = (bool)ReadEntity(stream);
            var acceptIndex   = (int)ReadEntity(stream);
            var reserve       = ReadEntity(stream);

            entityNum -= 4;

            var dfaState = new DFAState()
            {
                ID           = index,
                AcceptSymbol = isAcceptState ? SymbolGroup[acceptIndex] : null,
                EdgGroup     = EnumerableHelper.For(entityNum / 3)
                               .Select(item =>
                {
                    var edge = new DFAEdge();

                    edge.CharSet      = CharSetGroup[(int)ReadEntity(stream)];
                    var dfaStateIndex = (int)ReadEntity(stream);
                    var reserve2      = ReadEntity(stream);

                    Binding.Bind(() => edge.TargetState, () => DFAStateGroup[dfaStateIndex]);

                    return(edge);
                })
                               .ToList()
            };

            DFAStateGroup.Add(dfaState);
        }
Пример #2
0
        public TokenInfo ReadToken()
        {
            if (_Index == _Str.Length)
            {
                return(new TokenInfo(TokenInfoState.End, _Egt.SymbolGroup.First(item => item.Type == SymbolType.EndofFile), null,
                                     _Index, _Line, _Col));
            }

            var index      = _Index;
            var startIndex = index;

            var    state        = _Egt.DFAStateGroup[0];
            Symbol acceptSymbol = null;
            int    accpetIndex  = -1;

            while (true)
            {
                DFAEdge edge = null;

                if (index <= _Str.Length - 1)
                {
                    var cha = _Str[index];
                    edge = state.GetEdge(cha);
                }

                if (edge != null)
                {
                    state = edge.TargetState;

                    if (state.AcceptSymbol != null)
                    {
                        acceptSymbol = state.AcceptSymbol;
                        accpetIndex  = index;
                    }

                    index++;
                }
                else
                {
                    if (acceptSymbol != null)
                    {
                        var token = new TokenInfo(TokenInfoState.Accept, acceptSymbol,
                                                  _Str.Substring(startIndex, accpetIndex - startIndex + 1),
                                                  _Index, _Line, _Col);
                        Consumn(token.Value);
                        return(token);
                    }
                    else
                    {
                        var token = new TokenInfo(TokenInfoState.Error, null, _Str[startIndex].ToString(),
                                                  _Index, _Line, _Col);
                        Consumn(token.Value);

                        return(token);
                    }
                }
            }
        }