示例#1
0
 public Analyser()
 {
     TokenTable.InitTable();
     ConTable.InitTable();
     IdnTable.InitTable();
     OutputTokenTable.InitTable();
 }
示例#2
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);
            }
        }