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

📄 form1.cs

📁 一个用C#来开发的C语言词法分析器
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace cffxq
{
    public partial class Form1 : Form
    {
        //32个关键字
        string[] letter ={ "char", "int", "float", "double", "signed", "unsigned", "short", "long", "struct", "union", "enum", "void", "typedef", "sizeof", "const", "volatile", "static", "extern", "auto", "register", "if", "else", "switch", "case", "default", "do", "while", "for", "break", "continue", "return", "goto" };
        //13个单符号
        char[] singlepunctuation ={ '(', ')', '{', '}', '[', ']', ':', ';', ',', '?', '~' };
        string showtext;


        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            StreamReader sr;
            openFileDialog1.Filter = "c语言文件(*.c)|*.c|c++语言文件(*.cpp)|*.cpp|c语言头文件(*.h)|*.h|所有文件(*.*)|*.*";
            DialogResult dr = openFileDialog1.ShowDialog();
            if (dr != DialogResult.OK) return;
            sr = new StreamReader(openFileDialog1.FileName);
            richTextBox1.Text = sr.ReadToEnd();
            sr.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = string.Empty;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            MessageBox.Show("信息安全    052412    奚正豪");
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            int i = 0;
            string text, str = string.Empty;
            text = richTextBox1.Text + '\0';
            showtext = string.Empty;
            listBox1.Items.Clear();
            listBox2.Items.Clear();

            while (text[i] != '\0')
            {
                if (IsBlank(text[i])) { i++; continue; }
                else if (IsLetter(text[i]) || text[i] == '_')
                {
                    while (IsLetter(text[i]) || IsDigit(text[i]) || text[i] == '_')
                    {
                        str += text[i++];
                    }
                    CheckLetter(str);
                }
                else if (IsDigit(text[i]))
                {
                    while (IsDigit(text[i]) || text[i] == '.' || text[i] == 'e')
                    {
                        str += text[i++];
                        if (text[i - 1] == 'e' && text[i] == '-') str += text[i++];
                    }
                    CheckDigit(str);
                }
                else if (IsSinglePunctuation(text[i]))
                {
                    str += text[i++];
                    Print(str, 4);
                }
                else if (text[i] == '.')
                {
                    str += text[i++];
                    if (text[i] >= '0' && text[i] <= '9')
                    {
                        while (IsDigit(text[i]) || text[i] == '.' || text[i] == 'e')
                        {
                            str += text[i++];
                            if (text[i - 1] == 'e' && text[i] == '-') str += text[i++];
                        }
                        CheckDigit(str);
                    }
                    else
                    {
                        Print(str, 4);
                    }
                }
                else 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++];
                    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++];
                        Print(str, 4);
                    }
                    else
                    {
                        Print(str, 4);
                    }
                }
                else 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++];
                    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++];
                        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] == '+')
                    {
                        str += text[i++];
                        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] == '|')
                    {
                        str += text[i++];
                        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] == '-')
                    {
                        str += text[i++];
                        Print(str, 4);
                    }
                    else if (text[i] == '=')
                    {
                        str += text[i++];
                        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] == '<')
                    {
                        str += text[i++];
                        if (text[i] == '=')
                        {
                            str += text[i++];
                            Print(str, 4);
                        }
                        else
                        {
                            Print(str, 4);

⌨️ 快捷键说明

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