Example #1
0
        private cToken cm_readToken()
        {
            cToken _retToken = new cToken(eTokenType.Null, null);

            cf_reader.MoveToContent();
            if (cf_reader.IsStartElement(cc_product))
            {
                cf_reader.ReadStartElement(cc_product);
                cf_reader.MoveToContent();
                cf_buffer.Push(new cToken(eTokenType.стрелка, null));
                cLexem _lex = cLexem.cm_GetLexem(cf_reader.Value.Trim());

                _retToken = new cToken(eTokenType.лексема, _lex);
                cf_reader.Skip();
            }
            else if (cf_reader.IsStartElement(cc_epsilon))
            {
                cf_reader.Skip();
                _retToken = new cToken(eTokenType.пустая_лексема, cLexem.cc_EpsilonLexem);
            }
            else if (cf_reader.IsStartElement(cc_lexem))
            {
                XmlReader _subTree = cf_reader.ReadSubtree();
                _subTree.MoveToContent();
                _subTree.ReadStartElement(cc_lexem);
                _subTree.MoveToContent();
                _retToken = new cToken(eTokenType.лексема, cLexem.cm_GetLexem(_subTree.Value));
                _subTree.Close();
                cf_reader.Skip();
            }
            else if (cf_reader.IsStartElement(cc_action))
            {
                XmlReader _subTree = cf_reader.ReadSubtree();
                _subTree.MoveToContent();
                _subTree.ReadStartElement(cc_action);
                _subTree.MoveToContent();
                cLexem _lex = cLexem.cm_GetLexem(cf_reader.Value);
                _lex.cp_Type = eLexType.Action;
                _retToken    = new cToken(eTokenType.действие, _lex);
                _subTree.Close();
                cf_reader.Skip();
            }
            else if (cf_reader.NodeType == XmlNodeType.EndElement && cf_reader.Name == cc_product)
            {
                _retToken = new cToken(eTokenType.перевод_строки, null);
                cf_reader.ReadEndElement();
            }
            return(_retToken);
        }
        private cToken cm_readToken()
        {
            cToken _retToken = new cToken(eTokenType.Null, null);
            cf_reader.MoveToContent();
            if (cf_reader.IsStartElement(cc_product))
            {
                cf_reader.ReadStartElement(cc_product);
                cf_reader.MoveToContent();
                cf_buffer.Push(new cToken(eTokenType.стрелка, null));
                cLexem _lex = cLexem.cm_GetLexem(cf_reader.Value.Trim());

                _retToken = new cToken(eTokenType.лексема, _lex);
                cf_reader.Skip();
            }
            else if (cf_reader.IsStartElement(cc_epsilon))
            {
                cf_reader.Skip();
                _retToken = new cToken(eTokenType.пустая_лексема, cLexem.cc_EpsilonLexem);
            }
            else if (cf_reader.IsStartElement(cc_lexem))
            {
                XmlReader _subTree = cf_reader.ReadSubtree();
                _subTree.MoveToContent();
                _subTree.ReadStartElement(cc_lexem);
                _subTree.MoveToContent();
                _retToken = new cToken(eTokenType.лексема, cLexem.cm_GetLexem(_subTree.Value));
                _subTree.Close();
                cf_reader.Skip();
            }
            else if (cf_reader.IsStartElement(cc_action))
            {
                XmlReader _subTree = cf_reader.ReadSubtree();
                _subTree.MoveToContent();
                _subTree.ReadStartElement(cc_action);
                _subTree.MoveToContent();
                cLexem _lex = cLexem.cm_GetLexem(cf_reader.Value);
                _lex.cp_Type = eLexType.Action;
                _retToken = new cToken(eTokenType.действие, _lex);
                _subTree.Close();
                cf_reader.Skip();
            }
            else if (cf_reader.NodeType == XmlNodeType.EndElement && cf_reader.Name == cc_product)
            {
                _retToken = new cToken(eTokenType.перевод_строки, null);
                cf_reader.ReadEndElement();
            }
            return _retToken;
        }
        public override cToken cm_GetNextToken()
        {
            cToken _retToken = null;

            bool _valid = true;
            while (_valid && _retToken == null)
            {
                int _ch;
                if (cf_buf != -1)
                    _ch = cf_buf;
                else
                    _ch = cf_reader.Read();

                cf_buf = -1;

                while (_ch.Equals('\r'))
                    _ch = cf_reader.Read();
                switch (cf_state)
                {
                    case eState.Initial:
                        if (_ch.Equals('-'))
                        {
                            cf_state = eState.A;
                        }
                        else if (_ch.Equals('\n'))
                        {
                            cf_state = eState.N;
                        }
                        else if (_ch.Equals(cLexem.cc_Epsilon))
                        {
                            _retToken = new cToken(eTokenType.пустая_лексема, cLexem.cc_EpsilonLexem);
                        }
                        else if (_ch.Equals('{'))
                        {
                            cf_state = eState.B;
                        }
                        else if (cm_isLetter(_ch))
                        {
                            cf_state = eState.L;
                            cf_name = ((char)_ch).ToString();
                        }
                        else if (cm_isSeparator(_ch))
                        {
                            // разделители пропускаются
                        }
                        else if (_ch == -1)
                        {
                            _retToken = new cToken(eTokenType.Null, null);
                        }
                        else
                        {
                            _valid = false;
                        }
                        break;
                    case eState.A:
                        if (_ch.Equals('>'))
                        {
                            cf_state = eState.Initial;
                            _retToken = new cToken(eTokenType.стрелка, null);
                        }
                        else
                        {
                            _valid = false;
                        }
                        break;
                    case eState.B:
                        if (cm_isLetter(_ch))
                        {
                            cf_state = eState.E;
                            cf_name = ((char)_ch).ToString();
                        }
                        else
                        {
                            _valid = false;
                        }
                        break;
                    case eState.E:
                        if (cm_isLetter(_ch))
                        {
                            cf_name += ((char)_ch).ToString();
                        }
                        else if (_ch.Equals('}'))
                        {
                            cf_state = eState.Initial;
                            _retToken = new cToken(eTokenType.действие, cm_getAction());
                        }
                        else
                        {
                            _valid = false;
                        }
                        break;
                    case eState.L:
                        if (cm_isLetter(_ch))
                        {
                            cf_name += ((char)_ch).ToString();
                        }
                        else
                        {
                            cf_state = eState.Initial;
                            cf_buf = _ch;
                            _retToken = new cToken(eTokenType.лексема, cm_getLexem());
                        }
                        break;
                    case eState.N:
                        if (_ch.Equals('\n'))
                        {
                            
                        }
                        else
                        {
                            cf_state = eState.Initial;
                            cf_buf = _ch;
                            _retToken = new cToken(eTokenType.перевод_строки, null);
                        }
                        break;
                    default:
                        throw new Exception("Неизвестное состояние автомата.");
                }

                if (cf_buf == -1)
                    cf_buf = cf_reader.Read();
            }

            return _retToken;
        }
        public override void cm_Parse()
        {
            cLexem.cf_LexemDic.Clear();

            int         i  = 1;
            Stack <int> _s = new Stack <int>();

            _s.Push(0);
            bool   _la    = true;
            cToken _token = cf_scanner.cm_GetNextToken();

            cf_lisTokens.Add(_token);
            while (_token != null && i != 0)
            {
                DataRow _row = cf_dataTable.Rows[i - 1];
                if (_token.IsInTerminals(_row["terminals"] as string[]))
                {
                    _la = (bool)_row["accept"];

                    // действие
                    if (!string.IsNullOrEmpty(_row["action"] as string))
                    {
                        cm_doAction(_row["action"] as string);
                    }

                    if ((bool)_row["return"])
                    {
                        i = _s.Pop();
                    }
                    else
                    {
                        if ((bool)_row["stack"])
                        {
                            _s.Push(i + 1);
                        }
                        i = (int)_row["jump"];
                    }
                }
                else
                {
                    if ((bool)_row["error"])
                    {
                        throw new cParserException(cf_lisTokens, _row["terminals"] as string[]);
                    }
                    else
                    {
                        i   = i + 1;
                        _la = false;
                    }
                }
                if (_la)
                {
                    if (_token.cf_Type == eTokenType.Null)
                    {
                        throw new cParserException(cf_lisTokens, _row["terminals"] as string[]);
                    }
                    _token = cf_scanner.cm_GetNextToken();
                    cf_lisTokens.Add(_token);
                }
            }

            if (i != 0)
            {
                throw new cParserException(cf_lisTokens, new string[] { });
            }
            if (_token.cf_Type != eTokenType.Null)
            {
                throw new cParserException(cf_lisTokens, new string[] { });
            }
        }
        public override cToken cm_GetNextToken()
        {
            cToken _retToken = null;

            bool _valid = true;
            while (_valid && _retToken == null)
            {
                int _ch;
                if (cf_buf != -1)
                    _ch = cf_buf;
                else
                    _ch = cf_reader.Read();

                cf_buf = -1;

                while (_ch.Equals('\r'))
                    _ch = cf_reader.Read();
                switch (cf_state)
                {
                    case eState.Initial:
                        if (_ch.Equals('-'))
                        {
                            cf_state = eState.A;
                        }
                        else if (_ch.Equals('\n'))
                        {
                            cf_state = eState.N;
                        }
                        else if (_ch.Equals(cLexem.cc_Epsilon))
                        {
                            _retToken = new cToken(eTokenType.пустая_лексема, cLexem.cc_EpsilonLexem);
                        }
                        else if (_ch.Equals('{'))
                        {
                            cf_state = eState.B;
                        }
                        else if (cm_isLetter(_ch))
                        {
                            cf_state = eState.L;
                            cf_name = ((char)_ch).ToString();
                        }
                        else if (cm_isSeparator(_ch))
                        {
                            // разделители пропускаются
                        }
                        else if (_ch == -1)
                        {
                            _retToken = new cToken(eTokenType.Null, null);
                        }
                        else
                        {
                            _valid = false;
                        }
                        break;
                    case eState.A:
                        if (_ch.Equals('>'))
                        {
                            cf_state = eState.Initial;
                            _retToken = new cToken(eTokenType.стрелка, null);
                        }
                        else
                        {
                            _valid = false;
                        }
                        break;
                    case eState.B:
                        if (cm_isLetter(_ch))
                        {
                            cf_state = eState.E;
                            cf_name = ((char)_ch).ToString();
                        }
                        else
                        {
                            _valid = false;
                        }
                        break;
                    case eState.E:
                        if (cm_isLetter(_ch))
                        {
                            cf_name += ((char)_ch).ToString();
                        }
                        else if (_ch.Equals('}'))
                        {
                            cf_state = eState.Initial;
                            _retToken = new cToken(eTokenType.действие, cm_getAction());
                        }
                        else
                        {
                            _valid = false;
                        }
                        break;
                    case eState.L:
                        if (cm_isLetter(_ch))
                        {
                            cf_name += ((char)_ch).ToString();
                        }
                        else
                        {
                            cf_state = eState.Initial;
                            cf_buf = _ch;
                            _retToken = new cToken(eTokenType.лексема, cm_getLexem());
                        }
                        break;
                    case eState.N:
                        if (_ch.Equals('\n'))
                        {

                        }
                        else
                        {
                            cf_state = eState.Initial;
                            cf_buf = _ch;
                            _retToken = new cToken(eTokenType.перевод_строки, null);
                        }
                        break;
                    default:
                        throw new Exception("Неизвестное состояние автомата.");
                }

                if (cf_buf == -1)
                    cf_buf = cf_reader.Read();
            }

            return _retToken;
        }
Example #6
0
        public override void cm_Parse()
        {
            cLexem.cf_LexemDic.Clear();

            Stack <object> _s = new Stack <object>();

            _s.Push(0);

            cToken _token = cf_scanner.cm_GetNextToken();

            cf_lisTokens.Add(_token);
            string _lexem = _token.cf_Type.ToString();

            bool _finished = false, _error = false;

            //while (!_finished & !_error & (_token != null))
            while (!_finished & !_error)
            {
                int _num = 0;
                try
                {
                    _num = (int)_s.Peek();
                }
                catch
                {
                    throw new cParserException(cf_lisTokens, new string[] { });
                }
                DataRow _row   = cf_dataTable.Rows[_num];
                object  _value = _row[_lexem];
                if (!(_value is System.DBNull))
                {
                    string   _val      = _value as String;
                    string[] _splitVal = _val.Split(new char[] { cLRTableGenerator.cc_Separator }, StringSplitOptions.RemoveEmptyEntries);
                    if (_splitVal.Length > 0)
                    {
                        string _do = _splitVal[0];
                        char   _c  = _do[0];
                        switch (_c)
                        {
                        case 'S':
                            _s.Push(_lexem);
                            int _sNum = 0;
                            if (!Int32.TryParse(_do.TrimStart(new char[] { 'S' }), out _sNum))
                            {
                                throw new cParserException(cf_lisTokens, new string[] { });
                            }
                            _s.Push(_sNum);
                            _token = cf_scanner.cm_GetNextToken() ?? cToken.cc_NullToken;
                            cf_lisTokens.Add(_token);
                            _lexem = _token.cm_TypeToStr();
                            break;

                        case 'R':
                            uint _rNum = 0;
                            if (!UInt32.TryParse(_do.TrimStart(new char[] { 'R' }), out _rNum))
                            {
                                throw new cParserException(cf_lisTokens, new string[] { });
                            }
                            //_lexem=cm_doR(_rNum, _s);
                            cm_stackPop(_s, _rNum * 2);
                            _lexem = _splitVal[1];
                            cm_doAction(_splitVal);
                            break;

                        case 'A':
                            _finished = true;
                            break;

                        default:
                            _s.Push(_lexem);
                            int _numN = 0;
                            if (!Int32.TryParse(_do, out _numN))
                            {
                                throw new cParserException(cf_lisTokens, new string[] { });
                            }
                            _s.Push(_numN);
                            //_token = cf_scanner.cm_GetNextToken();
                            //cf_lisTokens.Add(_token);
                            _lexem = _token.cm_TypeToStr();
                            break;
                        }
                    }
                    else
                    {
                        throw new cParserException(cf_lisTokens, new string[] { });
                    }
                }
                else
                {
                    throw new cParserException(cf_lisTokens, new string[] { });
                }
            }
        }