⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 一个用C#来开发的C语言词法分析器
💻 CS
📖 第 1 页 / 共 2 页
字号:
                        }
                    }
                    else if (text[i] == '=')
                    {
                        str += text[i++];
                        Print(str, 4);
                    }
                    else
                    {
                        Print(str, 4);
                    }
                }
                else if (text[i] == '>')
                {
                    str += text[i++];
                    if (text[i] == '>')
                    {
                        str += text[i++];
                        if (text[i] == '=')
                        {
                            str += text[i++];
                            Print(str, 4);
                        }
                        else
                        {
                            Print(str, 4);
                        }
                    }
                    else if (text[i] == '=')
                    {
                        str += text[i++];
                        Print(str, 4);
                    }
                    else
                    {
                        Print(str, 4);
                    }
                }
                else if (text[i] == '/')
                {
                    str += text[i++];
                    if (text[i] == '/')
                    {
                        while (text[++i] != '\0')
                        {
                            if (text[i] == '\n') { i++; break; }
                        }
                    }
                    else if (text[i] == '*')
                    {
                        while (text[++i] != '\0')
                        {
                            if (text[i] == '*' && text[i + 1] == '/') { i += 2; break; }
                        }
                    }
                    else
                    {
                        Print(str, 4);
                    }
                }
                else if (text[i] == '\"')
                {
                    str += text[i];
                    while (text[++i] != '\0')
                    {
                        str += text[i];
                        if (text[i] == '\"') { Print(str, 9); break; }
                        if (text[i] == '\\')
                            if (text[i + 1] != '\0') str += text[++i]; ;
                    }
                    if (text[i++] == '\0') { Print(str, 5); break; }
                }
                else if (text[i] == '\'')
                {
                    int len = 3;
                    str += text[i];
                    while (text[++i] != '\0')
                    {
                        str += text[i];
                        if (text[i] == '\'') break;
                        if (text[i] == '\\')
                            if (text[i + 1] == '\0') continue;
                            else if (text[i + 1] >= '0' && text[i + 1] <= '7')
                            {
                                len++;
                                while (text[++i] != '\0')
                                {
                                    if (text[i] < '0' || text[i] > '7') break;
                                    str += text[i];
                                    len++;
                                }
                                i--;
                            }
                            else { str += text[++i]; len++; }
                    }
                    if (text[i++] == '\0') { Print(str, 5); break; }
                    else if (str.Length > len) Print(str, 5);
                    else Print(str, 3);
                }
                else if (text[i] == '#')
                {
                    str += text[i];
                    while (text[++i] != '\0')
                    {
                        str += text[i];
                        if (text[i] == '\n') { i++; break; }
                    }
                    showtext += (str + "\t\t预处理命令\r\n");
                }
                else
                {
                    str += text[i++];
                    Print(str, 5);
                }
                str = string.Empty;
            }
            textBox2.Text = showtext;
        }


        public bool IsBlank(char t)
        {
            if (t == ' ' || t == '\t' || t == '\n') return true;
            else return false;
        }


        public bool IsLetter(char t)
        {
            if ((t >= 'a' && t <= 'z') || (t >= 'A' && t <= 'Z')) return true;
            else return false;
        }


        public bool IsDigit(char t)
        {
            if (t >= '0' && t <= '9') return true;
            else return false;
        }


        public bool IsSinglePunctuation(char t)
        {
            foreach (char i in singlepunctuation)
                if (t == i) return true;
            return false;
        }

        public void CheckLetter(string str)
        {
            foreach (string i in letter)
                if (str == i) { Print(str, 1); return; }
            if (str == "true" || str == "false") { Print(str, 6); return; }
            Print(str, 2);
        }


        public void CheckDigit(string str)
        {
            bool checkp = false, checke = false;
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] == '.')
                {
                    if (!checkp) checkp = true;
                    else { Print(str, 5); return; }
                }
                if (str[i] == 'e')
                {
                    if (!checke) { checke = true; checkp = true; }
                    else { Print(str, 5); return; }
                }
            }
            if (str[str.Length - 1] == 'e' || str[str.Length - 1] == '-') Print(str, 5);
            else if (!checkp && !checke) Print(str, 7);
            else Print(str, 8);
        }
        

        void Print(string str, int i)
        {
            int j;
            showtext += str + '\t' + '\t';
            switch (i)
            {
                case 1:
                    showtext += "关键字\t\t-\r\n";
                    break;
                case 2:
                    j = 0;
                    foreach (string t in listBox1.Items)
                        if (t == str) break;
                        else j++;
                    if (j == listBox1.Items.Count) listBox1.Items.Add(str);
                    showtext += "标识符\t\t" + Convert.ToString(j) + "\r\n";
                    break;
                case 3:
                    j = 0;
                    foreach (string t in listBox2.Items)
                        if (t == str) break;
                        else j++;
                    if (j == listBox2.Items.Count) listBox2.Items.Add(str);
                    showtext += "字符型常量\t\t" + Convert.ToString(j) + "\r\n";
                    break;
                case 4:
                    showtext += "运算符/界符\t\t-\r\n";
                    break;
                case 5:
                    showtext += "ERROR\t\t-\r\n";
                    break;
                case 6:
                    j = 0;
                    foreach (string t in listBox2.Items)
                        if (t == str) break;
                        else j++;
                    if (j == listBox2.Items.Count) listBox2.Items.Add(str);
                    showtext += "布尔型常量\t\t" + Convert.ToString(j) + "\r\n";
                    break;
                case 7:
                    j = 0;
                    foreach (string t in listBox2.Items)
                        if (t == str) break;
                        else j++;
                    if (j == listBox2.Items.Count) listBox2.Items.Add(str);
                    showtext += "整型常量\t\t" + Convert.ToString(j) + "\r\n";
                    break;
                case 8:
                    j = 0;
                    foreach (string t in listBox2.Items)
                        if (t == str) break;
                        else j++;
                    if (j == listBox2.Items.Count) listBox2.Items.Add(str);
                    showtext += "浮点型常数\t\t" + Convert.ToString(j) + "\r\n";
                    break;
                case 9:
                    j = 0;
                    foreach (string t in listBox2.Items)
                        if (t == str) break;
                        else j++;
                    if (j == listBox2.Items.Count) listBox2.Items.Add(str);
                    showtext += "字符串型常数\t\t" + Convert.ToString(j) + "\r\n";
                    break;
            }
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            textBox2.Text = string.Empty;
            listBox1.Items.Clear();
            listBox2.Items.Clear();
        }

 
    }
}


⌨️ 快捷键说明

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