form1.cs

来自「一个用C#编写的解释器」· CS 代码 · 共 786 行 · 第 1/3 页

CS
786
字号
                        else
                        {
                            stemp += ch;
                            MyStreamReader.Read();
                            ch = (char)MyStreamReader.Peek();
                            while (MyStreamReader.Peek() != -1 && !IsListSeparatorOrBlank((char)MyStreamReader.Peek()))
                            {
                                stemp += ch;
                                MyStreamReader.Read();
                                ch = (char)MyStreamReader.Peek();
                            }
                            Out(ERROR, stemp);
                            CountError++;
                        }
                        IsDouble = false;
                    }
                    else switch (ch)
                        {
                            case '<':

                                stemp += ch;
                                MyStreamReader.Read();
                                ch = (char)MyStreamReader.Peek();
                                if (ch == '=')
                                { stemp += ch; Out(LE, ""); CountSign++; MyStreamReader.Read(); }
                                else if (ch == '>')
                                { stemp += ch; Out(NE, ""); CountSign++; MyStreamReader.Read(); }
                                else Out(LT, "");
                                CountSign++;
                                break;


                            case '=':
                                stemp += ch; Out(EQ, "");
                                MyStreamReader.Read();
                                CountSign++;
                                break;


                            case '(':
                                stemp += ch; Out(LP, "");
                                MyStreamReader.Read();
                                CountSign++;
                                break;

                            case '{':
                                stemp += ch; Out(LFP, "");
                                MyStreamReader.Read();
                                CountSign++;
                                break;

                            case ')':
                                stemp += ch; Out(RP, "");
                                MyStreamReader.Read();
                                CountSign++;
                                break;

                            case '}':
                                stemp += ch; Out(RFP, "");
                                MyStreamReader.Read();
                                CountSign++;
                                break;



                            case '>':
                                stemp += ch;
                                MyStreamReader.Read();
                                ch = (char)MyStreamReader.Peek();
                                if (ch == '=')
                                { stemp += ch; Out(LE, ""); CountSign++; MyStreamReader.Read(); }
                                else Out(GT, "");
                                CountSign++;
                                break;

                            case '*':
                                stemp += ch;
                                MyStreamReader.Read();
                                ch = (char)MyStreamReader.Peek();
                                if (ch == '/')
                                { stemp += ch; Out(RN, ""); CountSign++; MyStreamReader.Read(); }
                                else Out(MUL, "");
                                CountSign++;
                                break;
                            case '/':
                                stemp += ch;
                                MyStreamReader.Read();
                                ch = (char)MyStreamReader.Peek();
                                if (ch == '*')
                                { stemp += ch; Out(LN, ""); CountSign++; MyStreamReader.Read(); }
                                else Out(DIV, "");
                                CountSign++;
                                break;
                            case '-':
                                stemp += ch;
                                MyStreamReader.Read();
                                ch = (char)MyStreamReader.Peek();
                                if (ch == '-')
                                { stemp += ch; Out(DEC, ""); CountSign++; MyStreamReader.Read(); }
                                else Out(SUB, "");
                                CountSign++;
                                break;
                            case '+':
                                stemp += ch;
                                MyStreamReader.Read();
                                ch = (char)MyStreamReader.Peek();
                                if (ch == '+')
                                { stemp += ch; Out(INC, ""); CountSign++; MyStreamReader.Read(); }
                                else Out(ADD, "");
                                CountSign++;
                                break;

                            default:
                                stemp += ch;
                                MyStreamReader.Read();
                                ch = (char)MyStreamReader.Peek();
                                while (MyStreamReader.Peek() != -1 && !IsListSeparatorOrBlank((char)MyStreamReader.Peek()))
                                {
                                    stemp += ch;
                                    MyStreamReader.Read();
                                    ch = (char)MyStreamReader.Peek();
                                }
                                Out(ERROR, stemp);
                                CountError++;
                                break;

                        }


                    textBox7.Text = CountKeyword.ToString();
                    textBox8.Text = CountID.ToString();
                    textBox9.Text = CountInt.ToString();
                    textBox10.Text = CountSign.ToString();
                    textBox1.Text = CountError.ToString();
                    textBox6.Text = CountDouble.ToString();
                   
                    
                }

               
                MyStreamReader.Close();   




////////////////////////////////////////////////////////////////////////////

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            
            }
            
        }
     
        private bool isalpha(int c)
        {
            if ((c <= 'z' && c >= 'a') | (c <= 'Z' && c >= 'A'))
                return true;
            else return false;
        }  //判断是否为大小写字母
        private bool isalnum(int c)
        {
            if ((c <= 'z' && c >= 'a') | (c <= 'Z' && c >= 'A') | (c <= '9' && c >= '0'))
                return true;
            else return false;
        }  //判断是否为大小写字母或数字
        private bool isdigit(int c)
        {
            if (c <= '9' && c >= '0')
                return true;
            else return false;
        }  //判断是否为数字
        private bool isrelationsign(int c)  //判断是否为关系符
        {
            if (c == '>' | c == '<' | c == '=')
                return true;
            else return false;
        }
        private bool isoperator(int c)  //判断是否为关系符
        {
            if (c == '+' | c == '-' | c == '*' | c == '/' | c == '(' | c == ')')
                return true;
            else return false;
        }   

        class Keyword
        {
            public string key;
            public int value;
            public Keyword(string s, int v)
            {
                this.key = s;
                this.value = v;
            }
        }    //关键字类定义
        Keyword[] KeywordTable;
        Keyword[] SignTable;
        uint CountInt, CountID, CountKeyword, CountSign,CountError,CountDouble;
                              
        private int lookup(string s)
        {
            
            foreach (Keyword t in KeywordTable)
            {
                if (s == t.key)
                    return t.value;

            }
            return 0;
        }   //判断输入串是否在关键字表中
        private void Out(int c, string s)
        {
            listBox2.Items.Add( "(" + c + "," + s + ")" );
        }    //输出信息
        private bool IsBlank(char c)
        {
            if (c == ' ' | c == '\t' | c=='\n')
                return true;
            else return false;
        
        } //是空白符吗
        private bool IsListSeparatorOrBlank(char c)
        {
            if (c == ' ' | c == '\t' | c=='\n'|c==','|c==';'|c=='\r')
                return true;
            else return false;

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
            textBox2.Text += (@"D:\T1.txt");
            textBox5.Text += (@"D:\T2.txt");
            textBox3.Text += "分析结果是形如(CLASS,VALUE)的二元表达式,CLASS为类型码,VALUE为其值。若当前分析的串为关键字,则VALUE为空。另外,我们约定CLASS为-1时表示此串为非法串(查错功能)";
            textBox11.Text += "导入的关键字集格式要求:" + Environment.NewLine + "一行为一个关键字,不可以用空白符做关键字(即关键字所在行间不要有空行)" + Environment.NewLine + "例如:" + Environment.NewLine + "begin" + Environment.NewLine + "end" + Environment.NewLine + "if" + Environment.NewLine + "以上是合法的3个关键字";
            KeywordTable = new Keyword[12];
            KeywordTable[0] = new Keyword("begin", BEGIN);
            KeywordTable[1] = new Keyword("end", END);
            KeywordTable[2] = new Keyword("if", IF);
            KeywordTable[3] = new Keyword("then", THEN);
            KeywordTable[4] = new Keyword("else", ELSE);
            KeywordTable[5] = new Keyword("while", WHILE);
            KeywordTable[6] = new Keyword("do", DO);
            KeywordTable[7] = new Keyword("var", VAR);
            KeywordTable[8] = new Keyword("integer", INTEGER);
            KeywordTable[9] = new Keyword("procedure", PROCEDURE);
            KeywordTable[10] = new Keyword("parbegin", PARBEGIN);
            KeywordTable[11] = new Keyword("parend", PAREND);

            SignTable = new Keyword[21];
            SignTable[0] = new Keyword("<", LT);
            SignTable[1] = new Keyword("<=", LE);
            SignTable[2] = new Keyword("=", EQ);
            SignTable[3] = new Keyword("<>", NE);
            SignTable[4] = new Keyword(">", GT);
            SignTable[5] = new Keyword(">=", GE);
            SignTable[6] = new Keyword("+", ADD);
            SignTable[7] = new Keyword("-", SUB);
            SignTable[8] = new Keyword("*", MUL);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?