示例#1
0
 public Analyser()
 {
     TokenTable.InitTable();
     ConTable.InitTable();
     IdnTable.InitTable();
     OutputTokenTable.InitTable();
 }
示例#2
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.");
     }
 }
示例#3
0
        private bool Assignment(ref int i)
        {
            if (IdnTable.Contains(outputTokens[i].Name))
            {
                if (!Increment(ref i))
                {
                    return(false);
                }

                if (outputTokens[i].Name == "=")
                {
                    if (!Increment(ref i))
                    {
                        return(false);
                    }

                    if (Expression(ref i))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
示例#4
0
        private void AddToConTable(ref string token, ref int i)
        {
            if (OutputTokenTable.Table.Count > 1)
            {
                string penultimateToken = OutputTokenTable.Table[OutputTokenTable.Table.Count - 2].Name;

                if (OutputTokenTable.Table.Last().Name.Equals("=") && IdnTable.Contains(penultimateToken))
                {
                    if (Checker.GetType(token) == IdnTable.GetType(penultimateToken))
                    {
                        ConTable.Add(token, IdnTable.GetType(penultimateToken));
                    }
                    else
                    {
                        throw new Exception($"Error on {i + 1} line!\tInvalid type of constant. Unable to cast " +
                                            $"'{Checker.GetType(token)}' to '{IdnTable.GetType(penultimateToken)}'");
                    }
                }
                else if (!ConTable.Contains(token))
                {
                    ConTable.Add(token);
                }
            }
            else if (!ConTable.Contains(token))
            {
                ConTable.Add(token);
            }
        }
示例#5
0
        private bool VariablesList(ref int i)// TODO: by daclarations
        {
            if (IdnTable.Contains(outputTokens[i].Name))
            {
                if (!Increment(ref i))
                {
                    return(false);
                }

                if (outputTokens[i].Name == ",")
                {
                    if (!Increment(ref i))
                    {
                        return(false);
                    }

                    return(VariablesList(ref i));
                }

                return(true);
            }
            else
            {
                errors.Add(" > Error on " + outputTokens[i].Row + " line!\tExepted name of variable!");
                return(false);
            }
        }
        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}");
            }
        }
示例#7
0
        public static bool IsAbleToCast(string token)
        {
            //output tokens is exist and last token in table is "="
            return(OutputTokenTable.Table.Count != 0 && OutputTokenTable.Table.Last().Name == "="

                   //penultimate token is identifier
                   && IdnTable.Contains(OutputTokenTable.Table[OutputTokenTable.Table.Count - 2].Name)

                   //current token is identifier
                   && IdnTable.Contains(token)

                   //types of tokens on both sides of "=" are equal
                   && (IdnTable.GetType(token) == IdnTable.GetType(OutputTokenTable.Table[OutputTokenTable.Table.Count - 2].Name)));
        }
示例#8
0
 private string GetLabelOfToken(string token)
 {
     if (IdnTable.Contains(token))
     {
         return("id");
     }
     else if (ConTable.Contains(token))
     {
         return("con");
     }
     else
     {
         return(token);
     }
 }
示例#9
0
        private bool Factor(ref int i)
        {
            if (outputTokens[i].Name == "(")
            {
                if (!Increment(ref i))
                {
                    return(false);
                }

                if (Expression(ref i))
                {
                    if (outputTokens[i].Name == ")")
                    {
                        if (!Increment(ref i))
                        {
                            return(false);
                        }

                        return(true);
                    }
                    else
                    {
                        errors.Add(" > Error on " + outputTokens[i].Row + " line!\tExpected ')'");
                    }
                }
            }
            if (IdnTable.Contains(outputTokens[i].Name))
            {
                if (!Increment(ref i))
                {
                    return(false);
                }

                return(true);
            }
            if (ConTable.Contains(outputTokens[i].Name))
            {
                if (!Increment(ref i))
                {
                    return(false);
                }

                return(true);
            }

            return(false);
        }
示例#10
0
        private bool Output(ref int i)
        {
            if (outputTokens[i].Name == "cout")
            {
                if (!Increment(ref i))
                {
                    return(false);
                }

                if (outputTokens[i].Name == ">>")
                {
                    if (!Increment(ref i))
                    {
                        return(false);
                    }

                    if (IdnTable.Contains(outputTokens[i].Name))
                    {
                        if (!Increment(ref i))
                        {
                            return(false);
                        }

                        while ((outputTokens[i].Name == ">>" && outputTokens[i - 1].Name != ">>") || IdnTable.Contains(outputTokens[i].Name))
                        {
                            if (!Increment(ref i))
                            {
                                return(false);
                            }
                        }

                        return(true);
                    }
                    else
                    {
                        errors.Add(" > Error on " + outputTokens[i].Row + " line!\tExpected variable!");
                    }
                }
                else
                {
                    errors.Add(" > Error on " + outputTokens[i].Row + " line!\tExpected '>>'!");
                }
            }
            return(false);
        }
示例#11
0
        private double GetOperand()
        {
            if (IdnTable.Contains(stack.Peek()))
            {
                return((double)IdnTable.GetValue(stack.Pop()));
            }
            else if (Double.TryParse(stack.Peek(), NumberStyles.Any, CultureInfo.InvariantCulture, out double operand))
            {
                stack.Pop();
                return(operand);
            }
            else if (new Regex("r\\d+").IsMatch(stack.Peek()))
            {
                return(cycleDesignations[stack.Pop()]);
            }

            throw new NotImplementedException();
        }
示例#12
0
        private void ExecuteIO(string operand, string @operator)
        {
            switch (@operator)
            {
            case ">>":
                string outputValue = string.Empty;
                if (IdnTable.Contains(operand))
                {
                    outputValue = IdnTable.GetValue(operand).ToString();
                }
                else
                {
                    outputValue = "undefined";
                }
                consoleWindow.consoleContent.ConsoleOutput.Add(outputValue);
                break;

            case "<<":
                consoleWindow.Focus();

                var inputWindow = new InputWindow(operand);

                inputWindow.ShowDialog();

                if (Double.TryParse(inputWindow.inputField.Text, NumberStyles.Any, CultureInfo.InvariantCulture, out double value))
                {
                    IdnTable.SetValue(operand, value);
                }
                else
                {
                    MessageBox.Show("Invalid value!");
                }

                break;

            default:
                throw new InvalidOperationException();
            }
        }
示例#13
0
        public void Execute()
        {
            double secondOperand, firstOperand;
            string @operator, lastLabel = string.Empty;
            bool   jumpFlag   = false;
            Regex  labelRegex = new Regex("l\\d+");

            for (int i = 0; i < Generator.Polish.Count; i++)
            {
                string token = Generator.Polish[i];

                if (jumpFlag)
                {
                    if (token == lastLabel)
                    {
                        jumpFlag = false;
                    }
                    continue;
                }

                if ((token != "true" && token != "false") && (Checker.IsUnaryOperator(token) || Checker.IsBinaryOperator(token)))
                {
                    if (Checker.IsBinaryOperator(token))
                    {
                        @operator = token;

                        if (Checker.IsOperator(@operator))
                        {
                            secondOperand = GetOperand();
                            firstOperand  = GetOperand();

                            ExecuteExpression(firstOperand, secondOperand, @operator);
                            AddToViewTable(token, $"{firstOperand} {@operator} {secondOperand}");
                        }
                        else if (Checker.IsRelation(@operator))
                        {
                            secondOperand = GetOperand();
                            firstOperand  = GetOperand();

                            ExecuteRelation(firstOperand, secondOperand, @operator);
                            AddToViewTable(token, $"{firstOperand} {@operator} {secondOperand}");
                        }
                        else if (Checker.IsIO(@operator))
                        {
                            ExecuteIO(stack.Pop(), @operator);
                        }
                        else if (@operator == "=")
                        {
                            secondOperand = GetOperand();
                            string identifierName = stack.Pop();

                            if (new Regex("r\\d+").IsMatch(identifierName))
                            {
                                if (cycleDesignations.FirstOrDefault(r => r.Key == identifierName).Key is null)
                                {
                                    cycleDesignations.Add(identifierName, secondOperand);
                                }
                                else
                                {
                                    cycleDesignations[identifierName] = secondOperand;
                                }
                            }
                            else
                            {
                                IdnTable.SetValue(identifierName, secondOperand);
                            }
                            AddToViewTable(token, $"{identifierName} {@operator} {secondOperand}");
                        }
                    }
                    else if (Checker.IsUnaryOperator(token))
                    {
                        if (token == "@")
                        {
                            string operand = stack.Pop();

                            if (IdnTable.Contains(operand) && IdnTable.GetValue(operand).HasValue)
                            {
                                IdnTable.SetValue(operand, -IdnTable.GetValue(operand).Value);
                            }

                            stack.Push($"-{operand}");
                            AddToViewTable(token, $"Change sign of {operand}");
                        }
                        else
                        {
                            string identifier;
                            while (stack.Count > 0)
                            {
                                identifier = stack.Pop();

                                if (IdnTable.GetType(identifier) != token)
                                {
                                    IdnTable.SetType(identifier, token);
                                }
                            }
                        }
                    }
                }
                else if (token.Contains("JNE"))
                {
                    lastLabel = $"{labelRegex.Match(token).Groups[0].Value}:";
                    bool condition = Boolean.Parse(stack.Pop());

                    jumpFlag = condition ? false : true;
                    AddToViewTable(token, $"Jump to label {lastLabel} if last condition is false.");
                }
                else if (token.Contains("JMP"))
                {
                    string label = labelRegex.Match(token).Groups[0].Value;

                    i = Generator.LabelsTable[label];
                    AddToViewTable(token, $"Jump to label {label}:");
                }
                else if (!token.Contains(":"))
                {
                    stack.Push(token);
                    AddToViewTable(token, "Add input token to stack");
                }
            }
        }
示例#14
0
        public bool Parse(string[] programCode)
        {
            try
            {
                string token = string.Empty;
                for (int i = 0; i < programCode.Length; i++)
                {
                    int j = 0;

                    foreach (char ch in programCode[i])
                    {
                        if (ch.Equals('$'))
                        {
                            OutputTokenTable.Add(numRow: i, ch.ToString());
                            j++;
                            continue;
                        }

                        if (Checker.IsConstant(ch) ||
                            Checker.IsLetter(ch) ||
                            (Checker.IsSign(ch) && !string.IsNullOrEmpty(token) && Checker.IsExponent(token.Last())))
                        {
                            token += ch;
                        }
                        else if (Checker.IsSingleCharacterSeparator(ch) || Checker.IsWhiteSeparator(ch) || Checker.IsPossibleDoubleSeparator(ch))
                        {
                            AddToInternalTable(ref token, ref i);

                            if (ConTable.Contains(token) ||
                                IdnTable.Contains(token) ||
                                (TokenTable.Contains(token) && !Checker.IsDoubleSeparator(token + ch)))
                            {
                                OutputTokenTable.Add(numRow: i + 1, token);
                                token = string.Empty;
                            }

                            if (Checker.IsPossibleDoubleSeparator(ch))
                            {
                                if (!Checker.IsDoubleSeparator(token) &&
                                    Checker.IsDoubleSeparator(ch.ToString()
                                                              + ((programCode[i].Length - 1 != j) ? programCode[i][j + 1].ToString() : string.Empty)))
                                {
                                    token += ch;
                                    j++;
                                    continue;
                                }
                                else
                                {
                                    OutputTokenTable.Add(numRow: i + 1, token + ch);
                                    token = string.Empty;
                                }
                            }
                            else if (Checker.IsSingleCharacterSeparator(ch))
                            {
                                OutputTokenTable.Add(numRow: i + 1, token + ch);
                            }
                        }
                        else
                        {
                            throw new Exception($"Error on {i + 1} line!\tUndefined exeption.");
                        }
                        j++;
                    }
                }
                return(true);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Alert", MessageBoxButton.OK);
                return(false);
            }
        }
示例#15
0
        private bool Cycle(ref int i)
        {
            if (outputTokens[i].Name == "for")
            {
                if (!Increment(ref i))
                {
                    return(false);
                }

                if (outputTokens[i].Name == "(")
                {
                    if (!Increment(ref i))
                    {
                        return(false);
                    }

                    if (IdnTable.Contains(outputTokens[i].Name))
                    {
                        if (!Increment(ref i))
                        {
                            return(false);
                        }

                        if (outputTokens[i].Name == "=")
                        {
                            if (!Increment(ref i))
                            {
                                return(false);
                            }

                            if (Expression(ref i))
                            {
                                if (outputTokens[i].Name == ";")
                                {
                                    if (!Increment(ref i))
                                    {
                                        return(false);
                                    }

                                    if (Relation(ref i))
                                    {
                                        if (outputTokens[i].Name == ";")
                                        {
                                            if (!Increment(ref i))
                                            {
                                                return(false);
                                            }

                                            if (Expression(ref i))
                                            {
                                                if (outputTokens[i].Name == ")")
                                                {
                                                    if (!Increment(ref i))
                                                    {
                                                        return(false);
                                                    }

                                                    if (OperatorBlock(ref i))
                                                    {
                                                        return(true);
                                                    }

                                                    return(false);
                                                }
                                                else
                                                {
                                                    errors.Add(" > Error on " + outputTokens[i].Row + " line!\tExpected ')'!");
                                                }
                                            }
                                        }
                                        else
                                        {
                                            errors.Add(" > Error on " + outputTokens[i].Row + " line!\tExpected ';'!");
                                        }
                                    }
                                }
                                else
                                {
                                    errors.Add(" > Error on " + outputTokens[i].Row + " line!\tExpected ';'!");
                                }
                            }
                        }
                        else
                        {
                            errors.Add(" > Error on " + outputTokens[i].Row + " line!\tExpected '='!");
                        }
                    }
                    else
                    {
                        errors.Add(" > Error on " + outputTokens[i].Row + " line!\tnNot initiated variable!");
                    }
                }
                else
                {
                    errors.Add(" > Error on " + outputTokens[i].Row + " line!\tExpected '('!");
                }
            }

            return(false);
        }