Пример #1
0
        private void state9(CharChain chain)
        {
            char next = chain.GetNext();

            if (next >= '0' && next <= '9')
            {
                state = 9;

                int digit = ((int)next) - ((int)'0');
                result = result * 10 + digit;
            }
            else if (next == '\n' || next == '\0')
            {
                state = 13;

                if (isNegative)
                {
                    result *= -1;
                }
                isNegative = false;
            }
            else
            {
                throw new Exception("Обнаружен символ: '" + charView(next) + "', ожидался перенос строки или конец файла.");
            }
        }
Пример #2
0
        private void state4(CharChain chain)
        {
            char next = chain.GetNext();

            if (next == '0')
            {
                state = 7;
            }
            else if (next >= '1' && next <= '9')
            {
                state = 5;

                int digit = ((int)next) - ((int)'0');
                secondNumber = secondNumber * 10 + digit;
            }
            else if (next == '-')
            {
                state = 6;

                isNegative = true;
            }
            else
            {
                throw new Exception("Обнаружен символ: '" + charView(next) + "', ожидалась цифра.");
            }
        }
Пример #3
0
        private void state8(CharChain chain)
        {
            char next = chain.GetNext();

            if (next == '0')
            {
                state = 11;
            }
            else if (next >= '1' && next <= '9')
            {
                state = 9;

                int digit = ((int)next) - ((int)'0');
                result = result * 10 + digit;
            }
            else if (next == '-')
            {
                state = 10;

                isNegative = true;
            }
            else if (next == '_')
            {
                state = 12;

                skip = true;
            }
            else
            {
                throw new Exception("Обнаружен символ: '" + charView(next) + "', ожидалась цифра.");
            }
        }
Пример #4
0
        private void state5(CharChain chain)
        {
            char next = chain.GetNext();

            if (next >= '0' && next <= '9')
            {
                state = 5;

                int digit = ((int)next) - ((int)'0');
                secondNumber = secondNumber * 10 + digit;
            }
            else if (next == '=')
            {
                state = 8;

                if (isNegative)
                {
                    secondNumber *= -1;
                }
                isNegative = false;
            }
            else
            {
                throw new Exception("Обнаружен символ: '" + charView(next) + "', ожидалось '='.");
            }
        }
Пример #5
0
        private void state1(CharChain chain)
        {
            char next = chain.GetNext();

            if (next >= '0' && next <= '9')
            {
                state = 1;

                int digit = ((int)next) - ((int)'0');
                firstNumber = firstNumber * 10 + digit;
            }
            else if (isOperation(next))
            {
                state = 4;

                operation = next;
                if (isNegative)
                {
                    firstNumber *= -1;
                }
                isNegative = false;
            }
            else
            {
                throw new Exception("Обнаружен символ: '" + charView(next) + "', ожидалась операция.");
            }
        }
Пример #6
0
        private void state12(CharChain chain)
        {
            char next = chain.GetNext();

            if (next == '\n' || next == '\0')
            {
                state = 13;
            }
            else
            {
                throw new Exception("Обнаружен символ : '" + charView(next) + "', ожидался перенос строки или конец файла.");
            }
        }
Пример #7
0
        private void state7(CharChain chain)
        {
            char next = chain.GetNext();

            if (next == '=')
            {
                state = 8;
            }
            else
            {
                throw new Exception("Обнаружен символ: '" + charView(next) + "', ожидался '='.");
            }
        }
Пример #8
0
        private void state3(CharChain chain)
        {
            char next = chain.GetNext();

            if (isOperation(next))
            {
                state = 4;

                operation = next;
            }
            else
            {
                throw new Exception("Обнаружен символ: '" + charView(next) + "', ожидался знак операции.");
            }
        }
Пример #9
0
        private void state10(CharChain chain)
        {
            char next = chain.GetNext();

            if (next >= '1' && next <= '9')
            {
                state = 9;

                int digit = ((int)next) - ((int)'0');
                result = result * 10 + digit;
            }
            else
            {
                throw new Exception("Обнаружен символ: '" + charView(next) + "', ожидалась цифра.");
            }
        }
Пример #10
0
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            parsedText = CodeField.Text;

            CharChain chain  = new CharChain(CodeField.Text);
            Parser    parser = new Parser();

            ResultField.Text = "";

            int count = 0;

            while (chain.Next().Char != '\0')
            {
                parser.Parse(chain);

                var errors = parser.GetErrors();

                foreach (var error in errors)
                {
                    count++;

                    ResultField.Text += error.Message;
                    if (error.IncorrStr != null)
                    {
                        ResultField.Text += " (Отброшенный фрагмент: '" + error.IncorrStr + "' на позиции: " + error.Idx + ")";
                    }
                    ResultField.Text += "\r\n";

                    CodeField.Select(error.Idx, 1);
                    CodeField.SelectionBackColor = Color.Red;
                    CodeField.DeselectAll();
                }
            }

            if (count == 0)
            {
                ResultField.Text += "Ошибок нет.\r\n";
            }
            else
            {
                ResultField.Text += "Обнаружено " + count + " ошибок.\r\n";
            }
        }
Пример #11
0
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            CharChain chain   = new CharChain(CodeField.Text);
            Automat   automat = new Automat();
            bool      result  = false;

            int correct = 0;
            int wrong   = 0;

            for (int i = 1; i <= 5; i++)
            {
                try
                {
                    result = automat.Check(chain);
                }
                catch (Exception ex)
                {
                    ResultField.Text += "\r\nПример №" + i.ToString() + " " + ex.Message;
                    return;
                }

                if (result)
                {
                    correct++;
                }
                else
                {
                    wrong++;
                }
            }

            char next = chain.GetNext();

            if (next != '\0')
            {
                ResultField.Text += "\r\nВведено больше 5 примеров.";
                return;
            }

            ResultField.Text += "\r\nВерно: " + correct.ToString() + ", неверно: " + wrong.ToString();
        }
Пример #12
0
        public bool Check(CharChain chain)
        {
            state        = 0;
            firstNumber  = 0;
            secondNumber = 0;
            result       = 0;
            isNegative   = false;
            skip         = false;
            operation    = '\0';

            while (state != 13)
            {
                switch (state)
                {
                case 0:
                    state0(chain);
                    break;

                case 1:
                    state1(chain);
                    break;

                case 2:
                    state2(chain);
                    break;

                case 3:
                    state3(chain);
                    break;

                case 4:
                    state4(chain);
                    break;

                case 5:
                    state5(chain);
                    break;

                case 6:
                    state6(chain);
                    break;

                case 7:
                    state7(chain);
                    break;

                case 8:
                    state8(chain);
                    break;

                case 9:
                    state9(chain);
                    break;

                case 10:
                    state10(chain);
                    break;

                case 11:
                    state11(chain);
                    break;

                case 12:
                    state12(chain);
                    break;
                }
            }
            return(isCorrect());
        }
Пример #13
0
        public bool Parse(CharChain c)
        {
            chain = c;
            state = 1;
            id    = "";

            errors = new List <ParseError>();

            chain.SkipSpaces();

            while (state != 13)
            {
                switch (state)
                {
                case 1:
                    state1();
                    break;

                case 2:
                    state2();
                    break;

                case 3:
                    state3();
                    break;

                case 4:
                    state4();
                    break;

                case 5:
                    state5();
                    break;

                case 6:
                    state6();
                    break;

                case 7:
                    state7();
                    break;

                case 8:
                    state8();
                    break;

                case 9:
                    state9();
                    break;

                case 10:
                    state10();
                    break;

                case 11:
                    state11();
                    break;

                case 12:
                    state12();
                    break;
                }
            }

            return(true);
        }