示例#1
0
 private void AddToIdnTable(ref string token, ref int i)
 {
     if (OutputTokenTable.Table.Count != 0 && Checker.IsType(OutputTokenTable.Table.Last().Name))
     {
         if (IdnTable.Contains(token))
         {
             throw new Exception($"Error on {i + 1} line!\tThe variable '{token}' is already declared.");
         }
         else
         {
             IdnTable.Add(token);
         }
     }
     else if (OutputTokenTable.Table.Count != 0 && OutputTokenTable.Table.Last().Name == ",")
     {
         if (IdnTable.Contains(token))
         {
             throw new Exception($"Error on {i + 1} line!\tThe variable '{token}' is already declared.");
         }
         else
         {
             IdnTable.Add(token);
         }
     }
     else if (!IdnTable.Contains(token) && !TokenTable.Contains(token))
     {
         throw new Exception($"Error on {i + 1} line!\tThe variable '{token}' wasn't declared.");
     }
 }
        private void CalculateButton_Click(object sender, RoutedEventArgs e)
        {
            resultTextBox.IsReadOnly = false;
            errorsTextBox.IsReadOnly = false;
            try
            {
                if (!string.IsNullOrEmpty(expressionTextBox.Text))
                {
                    LexicalAnalyse.Analyser lexicalAnalyser = new LexicalAnalyse.Analyser();
                    if (variablesListView.Items.Count > 1)
                    {
                        for (int i = 0; i < variablesListView.Items.Count - 1; i++)
                        {
                            var ci      = new DataGridCellInfo(variablesListView.Items[i], variablesListView.Columns[0]);
                            var varName = ((Variable)ci.Item).Name;

                            ci = new DataGridCellInfo(variablesListView.Items[i], variablesListView.Columns[1]);
                            var varValue = ((Variable)ci.Item).Value.ToString();

                            IdnTable.Add(varName, Double.Parse(varValue));
                        }
                    }

                    if (lexicalAnalyser.Parse(new string[] { "$ " + expressionTextBox.Text.Trim() + " $" }))
                    {
                        RelationshipsTable.InitTable();
                        OperatorPrecedenceMethod.Analyser syntaxAnalyser = new OperatorPrecedenceMethod.Analyser();
                        syntaxAnalyser.Parse();

                        resultTextBox.Text = syntaxAnalyser.GetPolisResult().ToString();
                        errorsTextBox.Text = syntaxAnalyser.Error;
                    }
                    else
                    {
                        errorsTextBox.Text = "Lexical error!";
                    }
                    resultTextBox.IsReadOnly = true;
                    errorsTextBox.IsReadOnly = true;
                }
                else
                {
                    errorsTextBox.Text       = "Error! Expression can't be null or empty. Please fill expression form.";
                    errorsTextBox.IsReadOnly = true;
                    MessageBox.Show("Error!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"ERROR! {ex.Message}");
            }
        }