form1.cs

来自「实现了此法分析器和语法分析器 有详细的代码和文档」· CS 代码 · 共 2,012 行 · 第 1/5 页

CS
2,012
字号
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace program2
{
    public partial class Form1 : Form
    {
        private string path;

        public Form1()
        {
            InitializeComponent();
            path = Directory.GetCurrentDirectory();
        }
        public string[,] strArray1 = new string[1000, 2];
        public string[,] strArray2 = new string[16, 2];
        public char[] char1;
        public string str2 = "", c1;
        public static int j = 0, m = 1, e = 0;
        public static int n = 0;
        public int f = 1, h = 1, g = 1;
        public int ascii;
//词法分析器----------------------------------------------------------------------------------------------------------------------------------------------
        private void button1_Click(object sender, EventArgs e)
        {
            mpname();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            int i,k;
            for (i = 0; i < 1000; i++)
            {
                for (k = 0; k < 2; k++)
                {
                    strArray1[i, k] = "";
                }
            }
            str2 = "";
            char1 = richTextBox1.Text.ToCharArray();
            getchar();
        }
        public void scan()//拼读一个单词模块
        {
            if (ascii <= 122 & ascii >= 97 | ascii <= 90 & ascii >= 65) // ASCII A-Z/a-z=65-90/97-122
            {
                if (h == 0)//上一个字符是数字
                {
                    j = j + 1;
                    str2 = "";
                    str2 = str2 + c1;
                    strArray1[j, 0] = str2;
                    strArray1[j, 1] = "17";//字母数字串
                    h = 1;                //h为字母数字串字母与数字拼接时的接口
                    f = 1;                //f为区分一行的开始是字母还是空格的标志量
                    g = 0;                //g为数字字母串数字与字母断开时的接口
                }
                else
                {
                    str2 = str2 + c1;
                    strArray1[j, 0] = str2;
                    strArray1[j, 1] = "17";//字母串
                    look(j);
                    f = 0;
                    g = 0;
                }
            }
            else if (ascii <= 57 & ascii >= 48) // ASCII 0-9=48-57
            {
                if (g == 0)//上一个字符是字母
                {
                    str2 = str2 + c1;
                    strArray1[j, 0] = str2;
                    strArray1[j, 1] = "17";//字母串
                    h = 0;
                    f = 0;
                }
                else
                {
                    str2 = str2 + c1;
                    strArray1[j, 0] = str2;
                    strArray1[j, 1] = "18";//数字串
                    h = 0;
                    f = 0;
                }
            }
            else if (c1 == "<")
            {
                if (f == 0)
                {
                    j = j + 1;
                }
                strArray1[j, 0] = c1;
                strArray1[j, 1] = "28";//"<"
                j = j + 1;
                str2 = "";
                g = 1;
            }
            else if (c1 == ">")
            {
                if (strArray1[j - 1, 0] == "<")
                {
                    j = j - 1;
                    strArray1[j, 0] = strArray1[j, 0] + c1;
                    strArray1[j, 1] = "25";//"<>"
                    j = j + 1;
                    g = 1;
                }
                else
                {
                    if (f == 0)
                    {
                        j = j + 1;
                    }
                    strArray1[j, 0] = c1;
                    strArray1[j, 1] = "26";//">"
                    j = j + 1;
                    str2 = "";
                    g = 1;
                }
            }
            else if (c1 == "+")
            {
                if (f == 0)
                {
                    j = j + 1;
                }
                strArray1[j, 0] = c1;
                strArray1[j, 1] = "19";//"+"
                j = j + 1;
                str2 = "";
                g = 1;
                f = 1;
                h = 1;
            }
            else if (c1 == "-")
            {
                if (f == 0)
                {
                    j = j + 1;
                }
                str2 = c1;
                strArray1[j, 0] = c1;
                strArray1[j, 1] = "20";//"-"
                j = j + 1;
                str2 = "";
                g = 1;
                f = 1;
                h = 1;
            }
            else if (c1 == "*")
            {
                if (f == 0)
                {
                    j = j + 1;
                }
                strArray1[j, 0] = c1;
                strArray1[j, 1] = "21";//"*"
                j = j + 1;
                str2 = "";
                g = 1;
                f = 1;
                h = 1;
            }
            else if (c1 == "/")
            {
                if (f == 0)
                {
                    j = j + 1;
                }
                strArray1[j, 0] = c1;
                strArray1[j, 1] = "22";//"/"
                j = j + 1;
                str2 = "";
                g = 1;
                f = 1;
                h = 1;
            }
            else if (c1 == ":")
            {
                if (f == 0)
                {
                    j = j + 1;
                }
                strArray1[j, 0] = c1;
                strArray1[j, 1] = "0";
                j = j + 1;
                str2 = "";
            }
            else if (c1 == "=")
            {

                if (strArray1[j - 1, 0] == ":" | strArray1[j - 1, 0] == "<" | strArray1[j - 1, 0] == ">")
                {
                    j = j - 1;
                    strArray1[j, 0] = strArray1[j, 0] + c1;
                    if (strArray1[j, 0] == ":=")
                        strArray1[j, 1] = "23";
                    if (strArray1[j, 0] == "<=")
                        strArray1[j, 1] = "29";
                    if (strArray1[j, 0] == ">=")
                        strArray1[j, 1] = "27";
                    j = j + 1;
                    g = 1;
                    f = 1;
                }
                else
                {
                    if (f == 0)
                    {
                        j = j + 1;
                    }
                    strArray1[j, 0] = c1;
                    strArray1[j, 1] = "24";
                    j = j + 1;
                    str2 = "";
                    g = 1;
                }
            }
            else if (c1 == ",")
            {
                if (f == 0)
                {
                    j = j + 1;
                }
                strArray1[j, 0] = c1;
                strArray1[j, 1] = "32";
                j = j + 1;
                f = 1;
                h = 1;
                str2 = "";
            }
            else if (c1 == ";")
            {
                if (f == 0)
                {
                    j = j + 1;
                }
                strArray1[j, 0] = c1;
                strArray1[j, 1] = "33";
                str2 = "";
                h = 1;
            }
            else if (c1 == "(")
            {
                if (f == 0)
                {
                    j = j + 1;
                }
                strArray1[j, 0] = c1;
                strArray1[j, 1] = "30";
                str2 = "";
                j = j + 1;
                f = 1;
                g = 1;
                h = 1;
            }
            else if (c1 == ")")
            {
                if (f == 0)
                {
                    j = j + 1;
                }
                strArray1[j, 0] = c1;
                strArray1[j, 1] = "31";
                str2 = "";
                g = 1;
                h = 1;
            }
            else if (c1 == ".")
            {
                if (f == 0)
                {
                    j = j + 1;
                }
                strArray1[j, 0] = c1;
                strArray1[j, 1] = "34";
                str2 = "";
            }
            else if (c1 == "\n")
            {
                if (f == 0 | h==0 )
                {
                    j = j + 1;
                }
                strArray1[j, 0] = "Enter";
                strArray1[j, 1] = "35";
                str2 = "";
                j = j + 1;
                m = m + 1;
                f = 1;
                if (h == 0)
                    h = 1;
            }
            else if (c1 == " ")
            {
                g = 1;
                getbc();
            }
            else
            {
                error();
            }

            getchar();
        }
        public void look(int v)//查保留字表模块
        {
            int u;
            for (u = 0; u < 16; u++)
            {
                if (strArray1[v, 0] == strArray2[u, 1])
                {
                    strArray1[v, 1] = strArray2[u, 0];
                    break;
                }
            }

        }
        public void error()//错误处理模块
        {
            string m1 = "";
            m1 = m.ToString();
            MessageBox.Show("在第" + m1 + "行有非法字符“" + c1 + "”");
            StreamWriter e_SW = new StreamWriter(path+@"\file\error.txt", true);
            e_SW.WriteLine("在第" + m1 + "行有非法字符“" + c1 + "”");
            e_SW.Close();
        }
        public void getbc()//剔除空格模块
        {
            if (f == 0)
            {
                j = j + 1;
                f = 1;
            }
            str2 = "";
        }
        public void getchar()//从源程序文件读一个字符模块
        {

            if (n <= char1.Length - 1)
            {
                c1 = char1[n].ToString();
                c1 = c1.ToLower();
                ASCIIEncoding asciiencoding = new ASCIIEncoding();
                ascii = (int)asciiencoding.GetBytes(char1)[n];
                n = n + 1;
                scan();
            }
            else
            {
                n = 0;
                Outword();
            }
        }
        public void mpname()//判断源程序文件是否存在模块
        {
            FileInfo m_FileInfo = new FileInfo(path+@"\file\" + textBox1.Text);
            if (m_FileInfo.Exists)
            {
                StreamReader m_SR = new StreamReader(path+@"\file\" + textBox1.Text);
                string m_Data = m_SR.ReadToEnd();
                richTextBox1.Text = m_Data;
                m_SR.Close();
            }
            else
            {
                MessageBox.Show("源文件不存在!");
            }
        }
        public void Outword()//输出单词模块
        {
            int i;
            string str1 = "";
            for (i = 0; i <= j; i++)
            {

                StreamWriter m_SW = new StreamWriter(path+@"\file\456.txt", true);
                m_SW.WriteLine(strArray1[i, 0] + "           " + strArray1[i, 1]);
                m_SW.Close();
                str1 = str1 + strArray1[i, 0] + "        " + strArray1[i, 1] + "\n";
            }
            j = 0;
            richTextBox2.Text = str1;
        }
        public void initiate()//初始化模块
        {
            int u, v;
            StreamWriter m_SW = new StreamWriter(path+@"\file\456.txt", false);
            m_SW.WriteLine();
            m_SW.Close();
            StreamWriter e_SW = new StreamWriter(path+@"\file\error.txt", false);
            e_SW.WriteLine();
            e_SW.Close();
            StreamWriter y_SW = new StreamWriter(path+@"\file\yuan4.txt", false);
            y_SW.WriteLine();
            y_SW.Close();
            StreamReader n_SR = new StreamReader(path+@"\file\word.txt");

⌨️ 快捷键说明

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