| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 600 人关注过本帖
标题:制作一个简单的词法分析器
只看楼主 加入收藏
王祖品
Rank: 2
等 级:论坛游民
帖 子:31
专家分:20
注 册:2013-4-21
结帖率:27.27%
收藏
已结贴  问题点数:10 回复次数:1 
制作一个简单的词法分析器
try
            {
                string a;
                StreamReader st = new StreamReader(openFileDialog1.FileName, Encoding.Default);
                listBox1.Items.Clear();

                while (st.Peek() >= 0)
                {
                    int Count=0;
                    int ch = st.Read();
                   while(true)
                    {
                        while (ch == ' ' || ch == 10 || ch == 9)
                        {
                            ch = st.Read();
                        }

                        if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z')
                        {

                            a = null;

                            do
                            {
                                Count++;
                                a += (char)ch;
                                ch = st.Read();

                            } while (ch >= 'a' && ch <= 'z' || ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'Z');

                            if (a == "if")
                            {
                                listBox1.Items.Add("(ifsym, if)\n");
                            }
                            else if (a == "else")
                            {
                                listBox1.Items.Add("(elsesym, else)\n");
                            }
                            else if (a == "int")
                            {
                                listBox1.Items.Add("(intsym, int)\n");
                            }
                            else if (a == "for")
                            {
                                listBox1.Items.Add("(forsym, for)\n");
                            }
                            else if (a == "return")
                            {
                                listBox1.Items.Add("(returnsym, return)\n");
                            }
                            else if (a == "void")
                            {
                                listBox1.Items.Add("(voidsym, void)\n");
                            }
                            else if (a == "while")
                            {
                                listBox1.Items.Add("(whilesym, while)\n");
                            }
                            else if (a == "char")
                            {
                                listBox1.Items.Add("(charsym, char)\n");
                            }
                            else if (a == "const")
                            {
                                listBox1.Items.Add("(constsym, const)\n");
                            }
                            else if (a == "case")
                            {
                                listBox1.Items.Add("(casesym, case)\n");
                            }
                            else if (a == "float")
                            {
                                listBox1.Items.Add("(floatsym, float)\n");
                            }
                            else if (a == "do")
                            {
                                listBox1.Items.Add("(Tosym, to)\n");
                            }
                            else if (a == "break")
                            {
                                listBox1.Items.Add("(breaksym, break)\n");
                            }
                            else if (a == "static")
                            {
                                listBox1.Items.Add("(staticsym, static)\n");
                            }
                            else if (a == "continue")
                            {
                                listBox1.Items.Add("(continuesym, continue)\n");
                            }
                            else if (Count > 10)
                            {
                                string str = "(标识符定义有错误!, " + a + ")";
                                listBox1.Items.Add(str + "\n");
                            }
                            else
                            {
                                string ss = "(ident," + a + ")";
                                listBox1.Items.Add(ss + "\n");
                            }
                        }
                        if (ch >= '0' && ch <= '9')
                        {
                            do
                            {
                                ch = st.Read();

                            } while (ch >= '0' && ch <= '9');
                            if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z')
                            {
                                listBox1.Items.Add("数据定义有错误!");
                            }
                            else
                            {
                                listBox1.Items.Add("(number, )\n");
                            }
                        }
                        if (ch == '=')
                        {
                            ch = st.Read();

                            if (ch == '=')
                            {
                                listBox1.Items.Add("(eql, )\n");
                                ch = st.Read();

                            }

                            else
                            {
                                listBox1.Items.Add("(becomes, )\n");
                            }
                        }

                        if (ch == '>')
                        {

                            ch = st.Read();

                            if (ch == '=')
                            {
                                listBox1.Items.Add("(geq)\n");
                                ch = st.Read();
                            }
                            else
                            {
                                listBox1.Items.Add("(gtr, )\n");
                            }

                        }

                        if (ch == '<')
                        {

                            ch = st.Read();

                            if (ch == '=')
                            {
                                listBox1.Items.Add("(leq, )\n");
                                ch = st.Read();
                            }
                            else
                            {
                                listBox1.Items.Add("(lss, )\n");
                            }

                        }


                        if (ch == ';')
                        {
                            listBox1.Items.Add("(semicolon, )\n");
                            break;
                        }
                        if (ch == '+')
                        {
                            listBox1.Items.Add("(plus, )\n");
                            ch = st.Read();
                        }
                        if (ch == '-')
                        {
                            listBox1.Items.Add("(minus, )\n");
                            ch = st.Read();
                        }
                        if (ch == '*')
                        {
                            listBox1.Items.Add("(times, )\n");
                            ch = st.Read();
                        }
                        if (ch == '/')
                        {
                            listBox1.Items.Add("(slash, )\n");
                            ch = st.Read();
                        }
                        if (ch == '(')
                        {
                            listBox1.Items.Add("(lparen, )\n");
                            ch = st.Read();
                        }
                        if (ch == ')')
                        {
                            listBox1.Items.Add("(rparen, )\n");
                            ch = st.Read();
                        }
                        if (ch == '{')
                        {
                            listBox1.Items.Add("(lbparen, )\n");
                            ch = st.Read();
                        }
                        if (ch == '}')
                        {
                            listBox1.Items.Add("(rbparen, )\n");
                            ch = st.Read();
                        }
                        if (ch == ',')
                        {
                            listBox1.Items.Add("(comma, )");
                            ch = st.Read();
                        }
                        if (ch == '.')
                        {
                            listBox1.Items.Add("(period, )");
                            ch = st.Read();
                        }
                    }
                }

                st.Close();
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
     这个程序卡住了,运行不了,请高手帮忙一下,看看哪里出现了问题。
搜索更多相关主题的帖子: 制作 
2013-10-30 22:33
Osiris9
Rank: 4
来 自:九柱
等 级:业余侠客
帖 子:28
专家分:225
注 册:2013-10-25
收藏
得分:7 
...

干旱时死去................丰水时重生!
2013-11-02 01:37
快速回复:制作一个简单的词法分析器
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019236 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved