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

📄 main.cs

📁 C语言来构造编译器的,十分详细的源代码
💻 CS
📖 第 1 页 / 共 5 页
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class main : Form
    {

        public main()//系统生成
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
           
           
        }
        private void richTextBox3_TextChanged(object sender, EventArgs e)
        {

        }
        //以下开始有加入内容
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        private void yuyi_Click(object sender, EventArgs e)
        {
            checkmain();//对整个程序的编码进行检查并分配函数
        }
        //测试使用
        //string in_wzj;//导入inw的内容

        //int[] shuru = { 9, 32, 15, 32, 15, 32, 13, 32, 15, 32, 25 };//数组(测试)wenti
        //string[] shuru_s ={ "program", "a", "/", "b", "/", "s", "+", "z", "*", "q", ")" };
        //int[] shuru = { 9, 32, 15, 32, 13, 32, 15, 32, 25 };//数组(测试)
        //string[] shuru_s ={ "program",  "b", "*", "s", "+", "z", "*", "q", ")" };
        //int[] shuru = { 9,  32, 13, 32, 13, 32, 25 };//数组(测试)
        //string[] shuru_s ={ "program", "s", "+", "z", "+", "q", ")" };
        //int[] shuru = { 9, 32, 13, 32, 15, 32, 13, 32, 25 };//数组(测试)
        //string[] shuru_s ={ "program", "b", "+", "s", "*", "z", "+", "q", ")" };
        //int[] shuru = { 9, 32, 15, 32, 15, 32  };//数组(测试)
        //string[] shuru_s ={ "program", "b", "*", "s", "*", "z" };
        //int[] shuru = { 9, 32, 17, 32, 15, 32, 26 };//数组(测试)
        //string[] shuru_s ={ "program", "s", "=", "z", "*", "q", ";" };
        //int[] shuru = { 9, 32, 17, 32, 15, 32, 15, 32, 13, 32, 26, 32, 17, 32, 18, 32, 15, 32, 13, 32, 26 };//数组(测试)
        //string[] shuru_s ={ "program", "s", "=", "z", "*", "q", "*", "f", "+", "z", ";", "s", ">=", "z", "*", "q", "*", "f", "+", "z", ";" };
        //int[] shuru = { 9, 32, 21,32, 13, 32, 15, 32 ,13,32,25};//数组(测试)
        //string[] shuru_s ={ "program", "s", "<", "b", "+", "s", "*", "z", "+", "z", ")" };
        //int[] shuru = { 9, 32, 17, 27, 31, 27, 26 };//数组(测试)
        //string[] shuru_s ={ "program", "s", "=", "'", "g", "'", ";" };
        //int[] shuru = { 9, 32, 22, 30,  25 };//数组(测试)
        //string[] shuru_s ={ "program", "s", "==", "555", ")" };
        //int[] shuru = { 9, 32, 18, 27, 31, 27, 26 };//数组(测试)
        //string[] shuru_s ={ "program", "s", ">=", "'", "g", "'", ";" };
        //int[] shuru = { 9,6 ,24,32, 21, 32, 13, 32, 15, 32, 13, 32, 25 ,7,32,17,32,13,32,26,8};//数组(测试)
        //string[] shuru_s ={ "program", "while", "(", "s", "<", "b", "+", "s", "*", "z", "+", "z", ")", "do", "a", "=", "b", "+", "r", ";", "endwhile" };
        //int[] shuru = { 9, 6, 24, 32, 21, 32, 15, 32, 25, 7, 32, 17, 32, 26, 32, 17, 32, 26, 8 };//数组(测试)
        //string[] shuru_s ={ "program", "while", "(", "s", "<", "b", "*", "s", ")", "do", "a", "=", "b", ";", "a", "=", "b", ";", "endwhile" };
        //int[] shuru = { 9, 6, 24, 32, 21, 32, 13, 32, 15, 32, 13, 32, 25, 7, 32, 17, 32, 26, 32, 17, 32,15,32, 26, 8 };//数组(测试)
        //string[] shuru_s ={ "program", "while", "(", "s", "<", "b", "+", "s", "*", "z", "+", "z", ")", "do", "a", "=", "b", ";", "a", "=", "b", "*", "d", ";", "endwhile" };
        //int[] shuru = { 9, 6, 24, 32, 21, 32, 15, 32, 13, 32, 25, 7, 32, 17, 32, 15, 32, 26, 32, 17, 32, 26, 8 };//数组(测试)
        //string[] shuru_s ={ "program", "while", "(", "s", "<", "b", "*", "s", "+", "h", ")", "do", "a", "=", "b", "*", "s", ";", "c", "=", "b", ";", "endwhile" };
        int[] shuru = { 9, 32, 17, 30, 26, 0, 24, 32, 21, 32, 13, 32, 15, 32, 13, 32, 25, 1, 32, 17, 32, 26, 32, 17, 32, 15, 32, 26, 2, 32, 17, 30, 26, 3, 6, 24, 32, 21, 32, 13, 32, 15, 32, 13, 32, 25, 7, 32, 17, 32, 26, 32, 17, 27, 31, 27, 26, 8,11 };//数组(测试)
        string[] shuru_s ={ "program", "a", "=", "3", ";", "if", "(", "s", "<", "b", "+", "s", "*", "z", "+", "z", ")", "then", "a", "=", "b", ";", "a", "=", "b", "*", "d", ";", "else", "a", "=", "3", ";", "endif", "while", "(", "s", "<", "b", "+", "s", "*", "z", "+", "z", ")", "do", "a", "=", "b", ";", "a", "=", "'", "t", "'", ";", "endwhile", "endprogram" };
        //以下的部分为变量声明
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        int now;//目标部分的存储器,表示当前数组元素的值
        int tip = 0;//表示数组号
        string[] siyuanshi ={ "", "", "", "" }, siyuanshi1 ={ "", "", "", "" }, siyuanshi2 ={ "", "", "", "" }, siyuanshi3 ={ "", "", "", "" }, siyuanshi4 ={ "", "", "", "" };//四元式的存储数组
        int huancun;//就是t1,t2,t3......
        bool lian_chen=false,chen_chu=false;
        int hangshu = 1;//输出的四元式的行数
        int linshi_int,linshi_int1,yuxianpd,linshi_int2;
        int shuchu_yuansu = 0,shuchu_yuansu1=0;//传递给优化的数组的数组符
        string linshi;//四元式的跳转的行数,与行数配合使用
        int[] shuchu_i ={ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        string[] shuchu_s ={ "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "","", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" , "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""};//输出的初始化
        //以下开始是自定义函数
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        public void init()//初始化【完成】
            {
                scan();//对第一个数组元素进行复制,复制到now
            }
        //以上的部分为初始话,扫描数组
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        public void checkmain()//对整个程序的编码进行检查并分配函数【完成】
            {
                init();//初始化
                richTextBox3.AppendText("ywz提示;\n\n");//提示信息
                if (tip >= shuru.Length) tip = 0;//避免数组溢出(此句也可以注释掉)tip的值复位
                //由于在语义分析中对于declare的部分(声明)不进行四元式的转换,另外我们所定制的语言必须以声明开头,所以略过声明部分
                while (now != 9)  //在初始init()中执行的scan()所得到的值不为program时,不断检测
                {
                    //textBox1.Text = tip.ToString();//检测用
                    //textBox2.Text = shuru.Length.ToString();//检测用
                    xiayige();//使数组指向下一个元素赋给now,由于getnext()只执行了将数组号加一的功能,而没有将这时的shuru【】的值赋给now,所以如果不加scan()会产生数组号溢出的问题
                    //if (now == 9)//检测用
                    //{ init(); break; }//检测用
                    if (tip == shuru.Length - 1 && tip != 9)//当tip的值等于数组的长度-1(即数组号的最大值)且此数组不为program时
                    { richTextBox3.AppendText("\n\n执行的程序中缺少program!\n\n"); break; }//提示信息
                }
                //outw.AppendText("ahahahh");//检测用
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                //上方为program检测,下方为四元式生成
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                while (tip < shuru.Length - 1)//在tip的值在最大值以内执行循环
                {

                    if (tip < shuru.Length - 1)//判断tip的值在最大值以内
                        xiayige();//使数组指向下一个元素赋给now
                    if (now == 0) ifs();//if
                    if (now == 6) whiles();//while
                    if (now == 18 || now == 19 || now == 20 || now == 21 || now == 22 || now == 23) {bools();}
                    if (now == 17) {fuzhis();}
                    if (now == 15 || now == 16) { chenchus(); }
                    if (now == 13 || now == 14) { jiajians(); }
                    //MessageBox.Show("you program","dd",MessageBoxButtons.OK,MessageBoxIcon.Information);

                    //if() init(); 
                    //getnext();

                    if (now == 11) { shuchui(11); shuchus("endprogram"); }
                    if (tip >= shuru.Length - 1) { tip = 0; break; }//当tip的值达到最大值时跳出循环,并将tip的值复位
                }
                if (tip >= shuru.Length) tip = 0;//避免数组溢出(此句也可以注释掉)tip的值复位
                //textBox1.Text = tip.ToString();//检测用
                richTextBox3.AppendText("\n\n语义分析完成! \n\n四元式说明:\r\n空:__\r\n常数:###\r\n临时变量:T###\r\n变量:***\r\n运算符:@@\r\n行数:L###\r\n(###为数字,***为字母,@@为运算符)\r\n\n");//完成后的提示信息
                shuchu_yuansu = 0; shuchu_yuansu1 = 0;
                //for (int i = 0; i < shuchu_s.Length; i++) { richTextBox3.AppendText(shuchu_s[i]+" "); }//输出部分的检测
                //for (int i = 0; i < shuchu_i.Length; i++) { richTextBox3.AppendText(shuchu_i[i]+" "); }//输出部分的检测
            }
        //以上的部分为主程序
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        public void duru()//将词法分析的内容读入数组的模块【未完成】
            {
                //int i=0;
                //  string[]   strData   =   strLine.Split( ', '); 
                //  for(int   i=0;i <strData.Length;i++) 
                //  { 
                //          Response.Write(strData[i]+ " <br> "); 
                //  
                //  } 

                //in_wzj=inw.SelectedText;//获取选定的文本
                //in_wzj = inw.Text;//原始的获取输出
                //outw.AppendText(in_wzj);//追加输出
                //for (i = 0; i < inw.MaxLength; i++)
                //{ outw.Text = in_wzj; }

            }
        //以上部分是读入模块,以下两个是读出模块
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        public string youhua_s()//输出到优化部分的函数string【完成】
        {
            int i = 0;
            string p;
            p = shuchu_s[i];
            i++;
            return p;
        
        }
        public int youhua_i()//输出到优化部分的函数int【完成】
        {
            int i = 0, p;
            p = shuchu_i[i];
            i++;
            return p;
        
        }
        //以下是if分支判断部分
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        public string ifs()//if的功能【完成】
        {
            shuchui(0);shuchus("if ");//输出接口的基本块划分
            xiayige(); xiayige();//下两个是bool式子的第一个id
            switch (now)//判断now
            {

                case 32://如果是id(只可能是id)
                    xiayige();//下一个
                    if (now == 18 || now == 19 || now == 20 || now == 21 || now == 22 || now == 23) { bools(); }//判断产生bool式的条件
                    else { listBox1.Items.Add("错误 !while 后面应跟bool表达式"); break; }//如果不是bool式报错
                    xiayige();//下一个取then
                    shuchui(1); shuchus("then ");//输出接口的基本块划分
                   if (now == 1)//then
                    {
                        if (searchelse() == 0)//当没有else的情况,即为if then endif

                        {
                            textBox1.Text = searchelse().ToString();//测试用
                            yuxianpd = researchif();//进行出口的判断
                            linshi_int2 = hangshu + yuxianpd + 1;//计算出口
                            tiaozhuans(linshi_int2);//输出无条件天转的四元式
                            while (now != 3)//endif  当now不为endif的时候执行
                            {

                                if (now == 17) //=
                                {
                                    fuzhis(); //赋值计算

⌨️ 快捷键说明

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