예제 #1
0
        //扫描一个单元
        public clsToken scan()
        {
            if (tokenstatus == false)//如果前面操作,没有误吞字符,那么读一个
            {
                while (readOneChar())
                {
                    //吞掉空格和换行符,已经制表符,遇到普通字符退出
                    if (peek == ' ' || peek == '\t')
                    {
                        continue;
                    }
                    else if (peek == '\n')
                    {
                        line++;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            else//否则用被吞字符
            {
                peek        = tokenchar;
                tokenstatus = false;
            }


            // 判断是否为数字数字
            if (isDigit(peek))
            {
                int v = 0;
                do
                {
                    v = v * 10 + peek - '0';
                    readOneChar();
                } while (isDigit(peek));
                if (isIdentifierStart(peek))
                {
                    //如数字后跟着的是字符的话,说明这是一个错误的标识符,而不是数字
                    //不管他,还是先读//读完之后,构造时,告诉程序,,这是个错误的标识符
                    StringBuilder b = new StringBuilder(v + "");
                    do
                    {
                        b.Append(peek);
                        readOneChar();
                    } while (isIdentifierStart(peek) || isDigit(peek));
                    Fortokenchar();
                    String  s = b.ToString();
                    clsWord w = new clsWord(s, Tag.error, line);
                    return(w);
                }
                else if (peek != '.')
                {
                    Fortokenchar();
                    return(new clsNum(v, line));
                }
                float x = v; float d = 10;
                for (; ;)
                {
                    readOneChar();
                    if (!isDigit(peek))
                    {
                        Fortokenchar();
                        break;
                    }
                    x += (peek - '0') / d;
                    d  = d * 10;
                }
                if (isIdentifierStart(peek))
                {
                    //如数字后跟着的是字符的话,说明这是一个错误的标识符,而不是数字
                    //不管他,还是先读//读完之后,构造时,告诉程序,,这是个错误的标识符
                    string        tmpc = x + "";
                    StringBuilder b    = new StringBuilder(tmpc);
                    do
                    {
                        b.Append(peek);
                        readOneChar();
                    } while (isIdentifierStart(peek) || isDigit(peek));
                    Fortokenchar();
                    String  s = b.ToString();
                    clsWord w = new clsWord(s, Tag.error, line);
                    return(w);
                }
                return(new clsRealNum(x, line));
            }


            // 判断保留字或标识符
            if (isIdentifierStart(peek))
            {
                StringBuilder b = new StringBuilder();
                do
                {
                    b.Append(peek);
                    readOneChar();
                } while (isIdentifierStart(peek) || isDigit(peek));
                Fortokenchar();
                String  s = b.ToString();
                clsWord w;
                if (words.Contains(s))
                {
                    w = (clsWord)words[s];
                    string  x    = w.lexeme;
                    int     y    = w.tag;
                    clsWord newW = new clsWord(x, y, line);
                    return(newW);
                }
                w = new clsWord(s, Tag.ID, line);
                //先把用户标识符,也放在words表里面,后面再改
                // words.Add(w.lexeme, w);
                return(w);
            }

            string stmp = "";

            stmp += peek;
            // 判断界限符
            if (symbol.Contains(stmp))
            {
                clsToken w = (clsToken)symbol[stmp];
                int      x = w.tag;
                if (x == Tag.delimiter)
                {
                    clsDelimiter d    = (clsDelimiter)symbol[stmp];
                    clsDelimiter newD = new clsDelimiter(d.sign, x, line);
                    return(newD);
                }
            }

            clsToken t = new clsToken(peek, line);

            peek = ' ';
            return(t);
        }
예제 #2
0
 //将保留字存入hash表
 public void PutReservedWord(clsWord t)
 {
     words.Add(t.lexeme, t);
 }