Esempio n. 1
0
        private void RecogId()
        {
            string str = "";
            uint   Typecode;

            do
            {
                str = str + Line_Str[i];
                i++;
            } while (IsAlpha(Line_Str[i]) || IsDight(Line_Str[i]));//判断是不是字母或数字,是的话继续执行  


                        Typecode = keyword.FindKeyWord(str); //是否为关键字
            Token token = new Token();                       //存入token文件  

            token.content = str;
            token.Line    = line;
            if (Typecode == 0)//不在关键字中
            {
                //标识符
                token.type = 2;
                token.symb = symbles.Count;

                Symble sym = new Symble(); //存入符号表
                sym.content = str;
                sym.type    = 2;           //TAG
                sym.token   = token.symb;
                symbles.Add(sym);
            }
            else
            {
                token.type = Typecode;//关键字
                token.symb = -1;
            }

            tokens.Add(token);
        }
Esempio n. 2
0
        private void RecogCons()
        {
            string str   = Line_Str[i].ToString();
            bool   flag  = true;
            bool   point = true;

            while (flag)
            {
                i++;
                if (IsDight(Line_Str[i]))
                {
                    str += Line_Str[i];
                }
                else if (Line_Str[i] == '.')
                {
                    if (point)
                    {
                        str  += Line_Str[i];
                        point = false;//读取小数点,此后不再读取
                    }
                    else
                    {
                        Error e = new Error(line, "出现第二个'.'号");
                        errors.Add(e);
                        flag = false;
                    }
                }
                else if (IsAlpha(Line_Str[i]))
                {
                    i++;
                    if (IsDight(Line_Str[i]))
                    {
                        i--;
                        i--;
                        if (point)
                        {
                            Error e = new Error(line, "数字开头的数字、字母串");
                            errors.Add(e);
                        }
                        else
                        {
                            Error e = new Error(line, "实数的小数部分出现字母");
                            errors.Add(e);
                        }
                    }
                }
                else
                {
                    flag = false;
                }
            }//while end

            if (point)
            {
                Token token = new Token();
                token.content = str;
                token.type    = 3; //nufloat
                token.symb    = symbles.Count;
                token.Line    = line;
                tokens.Add(token);

                Symble sym = new Symble();
                sym.content = str;
                sym.type    = 3;
                sym.token   = token.symb;

                symbles.Add(sym);
            }
            else
            {
                Token token = new Token();
                token.content = str;
                token.Line    = line;
                token.type    = 4;//float
                token.symb    = symbles.Count;

                tokens.Add(token);

                Symble sym = new Symble();
                sym.content = str;
                sym.type    = 4;
                sym.token   = token.symb;

                symbles.Add(sym);
            }
        }