Esempio n. 1
0
        private void button_test_Click(object sender, EventArgs e)
        {
            clsLexer lex = new clsLexer(fileStreamReader);

            myTokenList.Clear();
            while (fileStreamReader.EndOfStream == false)
            {
                clsToken ct = lex.scan();
                myTokenList.Add(ct);
            }
            fileStreamReader.Close();
        }
Esempio n. 2
0
        private void button_save_Result_Click(object sender, EventArgs e)
        {
            string path = "result" + DateTime.Now.ToString("HH-mm-ss") + ".txt";

            using (StreamWriter sw = File.CreateText(path))
            {
                for (int i = 0; i < myTokenList.Count; i++)
                {
                    clsToken tmp   = myTokenList[i];
                    string   mytmp = tmp.toString();
                    sw.WriteLine(tmp.toString());
                }
            }
            MessageBox.Show("分词结果已经存储在" + path + "下!");
        }
Esempio n. 3
0
        private void listBox_code_DoubleClick(object sender, EventArgs e)
        {
            int    index = listBox_code.SelectedIndex + 1;
            string res   = "第" + index + "行:\n";

            for (int i = 0; i < myTokenList.Count; i++)
            {
                clsToken tmp   = myTokenList[i];
                string   mytmp = tmp.toString();
                if (tmp.line == index)
                {
                    res += tmp.toString();
                    res += "\n";
                }
            }
            MessageBox.Show(res);
        }
Esempio n. 4
0
        public void work()
        {
            while (fileStreamReader.EndOfStream == false)
            {
                clsToken ct = lex.scan();
                string   x  = ct.myString();

                if (isZuokuohao(x))
                {
                    //是左括号的话,说明进入了一个新的定义层,
                    //我们新建一个新的Hashtable,加入当前层中,并加入stack,并更新当前层,
                    //定义一个新的hashtable
                    Hashtable tmp = new Hashtable();
                    //定义其中的初始数目
                    tmp.Add(dangqianzibiaoshumu, 0);
                    //定义完毕后,要加入到当前层
                    //首先获得当前层的子表的个数
                    int cnt = (int)curtable[dangqianzibiaoshumu];
                    //构造标准化子表名称:字标编号+cnt
                    string tmpname = zibiaobianhao + cnt;
                    //更新计数器
                    curtable[dangqianzibiaoshumu] = cnt + 1;
                    //将这个心的hashtable加入当前表,关键字是标准化命名,值就是hashtable
                    curtable.Add(tmpname, tmp);
                    //加入stack
                    cur_Weizhi.Push(tmp);
                    //更新当前层
                    curtable = (Hashtable)cur_Weizhi.Peek();
                }

                if (isYoukuohao(x))
                {
                    //遇到右括号,代表着当前子空间已经结束了。
                    //如果遇到右括号,弹出栈,更新当前层
                    cur_Weizhi.Pop();
                    curtable = (Hashtable)cur_Weizhi.Peek();
                }

                //如果形如int啊  我们称之为定义符号,并进入定义声明阶段
                if (IsDingYiFu(x))
                {
                    while (true)
                    { //如果读到了定义符,我们接着读下一个词法单元
                        clsToken mytmpct = lex.scan();
                        string   tmpx    = mytmpct.myString();

                        if (isBiaozhifu(mytmpct))
                        {
                            //如果读取的词是标识符
                            //先在本层判重
                            if (curtable.Contains(tmpx))
                            {
                                //已经有了 拒绝定义,本版本直接报错。
                                Console.Write("error");
                            }
                            else
                            {
                                //否则不重复,加入其中
                                curtable.Add(tmpx, mytmpct);
                            }
                        }

                        //如果读到的是分号,推出变量声明阶段
                        if (IsFenhao(tmpx))
                        {
                            break;;
                        }
                    }
                }
            }
            fileStreamReader.Close();
        }
Esempio n. 5
0
 private bool isBiaozhifu(clsToken mytmpct)
 {
     return(mytmpct.tag == Tag.ID);
 }
Esempio n. 6
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);
        }