Ejemplo n.º 1
0
        private void ParseString(string code)
        {
            int[] states = this.InitState();

            int lastFinalState = 0; // последнее состояние автомата
            int lastPositionWithFinalState = 0; // позиция последнего успешнего автомата
            int startingPosition = 0; // начало новой лексемы

            for (int i = 0; i < code.Length; i++)
            {
                this.Identify(code.ElementAt(i), states);

                if (this.AllBroken(states))
                {
                    // если lastFinalState == 0 - нет такой лексемы
                    if (lastFinalState == 0)
                    {
                        TokenReader token = new TokenReader(startingPosition, startingPosition, code.Substring(startingPosition, 1), 0);
                        token.AddToList(token);

                        states = this.InitState();
                        startingPosition = i + 1;
                    }
                    else
                    {
                        TokenReader token = new TokenReader(startingPosition, lastPositionWithFinalState, code.Substring(startingPosition, lastPositionWithFinalState + 1 - startingPosition), lastFinalState);
                        token.AddToList(token);

                        lastFinalState = 0;
                        i = lastPositionWithFinalState;
                        startingPosition = i + 1;
                        states = this.InitState();
                    }
                }
                else
                {
                    if (this.GetFinalState(states) != 0)
                    {
                        if (states[1] == -110)
                        {
                            lastFinalState = this.GetFinalState(states);
                            lastPositionWithFinalState = i;

                            TokenReader token = new TokenReader(i, i, code[i].ToString(), lastFinalState);
                            token.AddToList(token);

                            lastFinalState = 0;
                            i = lastPositionWithFinalState;
                            startingPosition = i + 1;
                            states = this.InitState();
                        }
                        else
                        {
                            lastFinalState = this.GetFinalState(states);
                            lastPositionWithFinalState = i;
                        }
                        ////states = initState();
                    }
                }
            }

            if (lastFinalState != 0)
            {
                TokenReader token = new TokenReader(startingPosition, lastPositionWithFinalState + 1, code.Substring(startingPosition, lastPositionWithFinalState + 1 - startingPosition), lastFinalState);
                token.AddToList(token);
            }
        }
Ejemplo n.º 2
0
 public void AddToList(TokenReader token)
 {
     tokenReaderList.Add(token);
 }