Beispiel #1
0
 private void ReadSignAsID()
 {
     SymbolQueue.Enqueue(character);
     while (true)
     {
         if ('a' <= (char)Reader.Peek() && 'z' >= (char)Reader.Peek() || 'A' <= (char)Reader.Peek() && 'Z' >= (char)Reader.Peek())
         {
             SymbolQueue.Enqueue((char)Reader.Peek());
         }
         else if ('0' <= (char)Reader.Peek() && '9' >= (char)Reader.Peek())
         {
             SymbolQueue.Enqueue((char)Reader.Peek());
         }
         else
         {
             LexicalLabel label = GetLexicalLabel(new string(SymbolQueue.ToArray()));
             if (LexicalLabel.PL0_ID == label)
             {
                 VocabularyUnitNext = new IdentifierVocabularyUnit(label, new string(SymbolQueue.ToArray()), Line);
             }
             else
             {
                 VocabularyUnitNext = new GeneralVocabularyUnit(label, Line);
             }
             SymbolQueue.Clear();
             return;
         }
         character = (char)Reader.Read();
     }
 }
        public void LexAnalys()
        {
            StreamReader reader = new StreamReader(ReaderPath);

            try
            {
                LexAnalysisUnit LexAnalysis = new LexAnalysisUnit(reader);
                Console.WriteLine(string.Format("{0,-10}\t{1,-10}\t{2,-8}\t{3,-8}",
                                                "符号类型", "符号名", "符号值", "行号"));
                while (null != LexAnalysis.Peek)
                {
                    GeneralVocabularyUnit unit = LexAnalysis.Read;
                    Console.WriteLine(string.Format("{0,-10}\t{1,-15}\t{2,-8}\t{3,-8}",
                                                    unit.LexicalLabel.ToString(), unit.GetString(), unit.GetInt(), unit.Line));
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                reader.Close();
            }
        }
Beispiel #3
0
 public LexAnalysisUnit(StreamReader reader)
 {
     SymbolQueue        = new Queue <char>();
     Reader             = reader;
     VocabularyUnit     = null;
     VocabularyUnitNext = null;
     character          = ' ';
     Line = 1;
     Init();
 }
Beispiel #4
0
 private void ReadSignAsUInt()
 {
     SymbolQueue.Enqueue(character);
     while (true)
     {
         if ('0' <= (char)Reader.Peek() && '9' >= (char)Reader.Peek())
         {
             SymbolQueue.Enqueue((char)Reader.Peek());
         }
         else
         {
             VocabularyUnitNext = new UnsignedNumberVocabularyUnit(LexicalLabel.PL0_Number, FormatNumber(new string(SymbolQueue.ToArray())), Line);
             SymbolQueue.Clear();
             return;
         }
         character = (char)Reader.Read();
     }
 }
Beispiel #5
0
 private void ReadStr()
 {
     if ('a' <= character && 'z' >= character || 'A' <= character && 'Z' >= character)
     {
         ReadSignAsID();
     }
     else if ('0' <= character && '9' >= character)
     {
         ReadSignAsUInt();
     }
     else if ('\uffff' == character)
     {
         VocabularyUnitNext = null;
         return;
     }
     else
     {
         throw new LexAnalysisException(Line, "‘" + character.ToString() + "’");
     }
 }
Beispiel #6
0
        private void LexicalAnalysis()
        {
            if (!Reader.EndOfStream)
            {
                RemoveSplitSign();
                switch (character)
                {
                case ',':
                    VocabularyUnitNext = new GeneralVocabularyUnit(LexicalLabel.PL0_Comma, Line);
                    break;

                case '(':
                    VocabularyUnitNext = new GeneralVocabularyUnit(LexicalLabel.PL0_LBrace, Line);
                    break;

                case ')':
                    VocabularyUnitNext = new GeneralVocabularyUnit(LexicalLabel.PL0_RBrace, Line);
                    break;

                case '.':
                    VocabularyUnitNext = new GeneralVocabularyUnit(LexicalLabel.PL0_Dot, Line);
                    break;

                case ';':
                    VocabularyUnitNext = new GeneralVocabularyUnit(LexicalLabel.PL0_Semicolon, Line);
                    break;

                case '+':
                    VocabularyUnitNext = new GeneralVocabularyUnit(LexicalLabel.PL0_Plus, Line);
                    break;

                case '-':
                    VocabularyUnitNext = new GeneralVocabularyUnit(LexicalLabel.PL0_Sub, Line);
                    break;

                case '*':
                    VocabularyUnitNext = new GeneralVocabularyUnit(LexicalLabel.PL0_Mul, Line);
                    break;

                case '/':
                    VocabularyUnitNext = new GeneralVocabularyUnit(LexicalLabel.PL0_Div, Line);
                    break;

                case '#':
                    VocabularyUnitNext = new GeneralVocabularyUnit(LexicalLabel.PL0_Nequal, Line);
                    break;

                case '=':
                    VocabularyUnitNext = new GeneralVocabularyUnit(LexicalLabel.PL0_Equal, Line);
                    break;

                case '<':
                    if ('=' == (char)Reader.Peek())
                    {
                        Reader.Read();
                        VocabularyUnitNext = new GeneralVocabularyUnit(LexicalLabel.PL0_LessEqual, Line);
                    }
                    else
                    {
                        VocabularyUnitNext = new GeneralVocabularyUnit(LexicalLabel.PL0_Less, Line);
                    }
                    break;

                case '>':
                    if ('=' == (char)Reader.Peek())
                    {
                        Reader.Read();
                        VocabularyUnitNext = new GeneralVocabularyUnit(LexicalLabel.PL0_GreaterQeual, Line);
                    }
                    else
                    {
                        VocabularyUnitNext = new GeneralVocabularyUnit(LexicalLabel.PL0_Greater, Line);
                    }
                    break;

                case ':':
                    if ('=' == (char)Reader.Peek())
                    {
                        Reader.Read();
                        VocabularyUnitNext = new GeneralVocabularyUnit(LexicalLabel.PL0_Become, Line);
                    }
                    else
                    {
                        throw new LexAnalysisException(Line, "‘:’、‘" + (char)Reader.Peek() + "’");
                    }
                    break;

                default:
                    ReadStr();
                    break;
                }
            }
            else
            {
                VocabularyUnitNext = null;
            }
        }
Beispiel #7
0
 private void Init()
 {
     LexicalAnalysis();
     VocabularyUnit = VocabularyUnitNext;
     LexicalAnalysis();
 }