Beispiel #1
0
 /// <summary>
 /// Adiciona um estado.
 /// </summary>
 /// <param name="index"></param>
 /// <param name="fastate"></param>
 private void AddDfaState(int index, FAState fastate)
 {
     if (!_initialized)
     {
         throw new ParserException("Table sizes not initialized");
     }
     _DfaStates[index] = fastate;
 }
Beispiel #2
0
        /// <summary>
        /// Carrega as tabelas da gramática.
        /// </summary>
        /// <param name="reader"></param>
        private void LoadTables(GrammarReader reader)
        {
            object obj;
            int    index;

            while (reader.MoveNext())
            {
                byte id = (byte)reader.RetrieveNext();
                switch ((RecordId)id)
                {
                case RecordId.Parameters:
                    _parameters["Name"]    = (string)reader.RetrieveNext();
                    _parameters["Version"] = (string)reader.RetrieveNext();
                    _parameters["Author"]  = (string)reader.RetrieveNext();
                    _parameters["About"]   = (string)reader.RetrieveNext();
                    _caseSensitive         = (bool)reader.RetrieveNext();
                    _startSymbol           = (int)reader.RetrieveNext();
                    break;

                case RecordId.TableCounts:
                    _symbols     = new Symbol[(int)reader.RetrieveNext()];
                    _charsets    = new String[(int)reader.RetrieveNext()];
                    _rules       = new Rule[(int)reader.RetrieveNext()];
                    _DfaStates   = new FAState[(int)reader.RetrieveNext()];
                    _LalrTables  = new LRActionTable[(int)reader.RetrieveNext()];
                    _initialized = true;
                    break;

                case RecordId.Initial:
                    _initDfaState  = (int)reader.RetrieveNext();
                    _initLalrState = (int)reader.RetrieveNext();
                    break;

                case RecordId.Symbols:
                    index = (int)reader.RetrieveNext();
                    var name   = (string)reader.RetrieveNext();
                    var kind   = (SymbolType)(int)reader.RetrieveNext();
                    var symbol = new Symbol(index, name, kind);
                    AddSymbol(symbol);
                    break;

                case RecordId.CharSets:
                    index = (int)reader.RetrieveNext();
                    var charset = (string)reader.RetrieveNext();
                    AddCharset(index, charset);
                    break;

                case RecordId.Rules:
                    index = (int)reader.RetrieveNext();
                    var head = _symbols[(int)reader.RetrieveNext()];
                    var rule = new Rule(index, head);
                    reader.RetrieveNext();
                    while ((obj = reader.RetrieveNext()) != null)
                    {
                        rule.AddItem(_symbols[(int)obj]);
                    }
                    AddRule(rule);
                    break;

                case RecordId.DFAStates:
                    FAState fastate = new FAState();
                    index = (int)reader.RetrieveNext();
                    if ((bool)reader.RetrieveNext())
                    {
                        fastate.AcceptSymbol = (int)reader.RetrieveNext();
                    }
                    else
                    {
                        reader.RetrieveNext();
                    }
                    reader.RetrieveNext();
                    while (!reader.RetrieveDone())
                    {
                        var ci = (int)reader.RetrieveNext();
                        var ti = (int)reader.RetrieveNext();
                        reader.RetrieveNext();
                        fastate.AddEdge(_charsets[ci], ti);
                    }
                    AddDfaState(index, fastate);
                    break;

                case RecordId.LRTables:
                    LRActionTable table = new LRActionTable();
                    index = (int)reader.RetrieveNext();
                    reader.RetrieveNext();
                    while (!reader.RetrieveDone())
                    {
                        var sid    = (int)reader.RetrieveNext();
                        var action = (int)reader.RetrieveNext();
                        var tid    = (int)reader.RetrieveNext();
                        reader.RetrieveNext();
                        table.AddItem(_symbols[sid], (Action)action, tid);
                    }
                    AddLalrTable(index, table);
                    break;

                case RecordId.Comment:
                    Console.WriteLine("Comment record encountered");
                    break;

                default:
                    throw new ParserException("Wrong id for record");
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Recupera um token.
        /// </summary>
        /// <returns></returns>
        private Token RetrieveToken()
        {
            Token   result;
            int     peekChar        = -1;
            int     currentPos      = 0;
            int     lastAcceptState = -1;
            int     lastAcceptPos   = -1;
            FAState currentState    = _DfaStates[_initDfaState];

            while (true)
            {
                int target = -1;
                peekChar = _source.LookAhead(currentPos);
                if (peekChar == -1)
                {
                    break;
                }
                char ch = FixCase((char)peekChar);
                foreach (FAEdge edge in currentState.Edges)
                {
                    String chars = edge.Characters;
                    if (chars.IndexOf(ch) != -1)
                    {
                        target = edge.TargetIndex;
                        break;
                    }
                }
                if (target != -1)
                {
                    if (_DfaStates[target].AcceptSymbol != -1)
                    {
                        lastAcceptState = target;
                        lastAcceptPos   = currentPos;
                    }
                    currentState = _DfaStates[target];
                    currentPos++;
                }
                else
                {
                    break;
                }
            }
            if (lastAcceptState != -1)
            {
                Symbol symbol = _symbols[_DfaStates[lastAcceptState].AcceptSymbol];
                result      = new Token(symbol);
                result.Data = _source.Read(lastAcceptPos + 1);
            }
            else
            {
                if (peekChar == -1)
                {
                    result      = new Token(_endSymbol);
                    result.Data = "";
                }
                else
                {
                    result      = new Token(_errorSymbol);
                    result.Data = _source.Read(1);
                }
            }
            UpdateLineNumber((String)result.Data);
            return(result);
        }