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

📄 plxcompile.cs

📁 编译器
💻 CS
📖 第 1 页 / 共 3 页
字号:
                {
                    case vartype.varConst: table[tx].val = num; break;
                    case vartype.varInteger: table[tx].adr = dx;
                        table[tx].level = lev;
                        dx++;
                        break;
                    case vartype.varLogical: table[tx].adr = dx;
                        table[tx].level = lev;
                        dx++;
                        break;
                    case vartype.varProc: table[tx].level = lev; break;
                }
            }
        }
        void enter(vartype indent, int value)
        {
            if (position(id) != 0)
            {
                //error("%s redefine.", id);

            }
            else
            {
                tx++;

                table[tx].name = String.Copy(id);
                table[tx].kind = indent;

                switch (indent)
                {
                    case vartype.varConst: table[tx].val = num; break;
                    case vartype.varInteger: table[tx].adr = dx;
                        table[tx].level = lev;
                        dx++;
                        break;
                    case vartype.varLogical: table[tx].adr = dx;
                        table[tx].level = lev;
                        dx++;
                        break;
                    case vartype.varProc: table[tx].level = lev; break;
                }
            }
        }

        //寻找标识符-为0表示失败
        int position(string ident)
        {
            //strcpy(table[0].name, ident);
            table[0].name = String.Copy(ident);
            //table[0].name = ident.ToCharArray();
            int i = tx;
            while (table[i].name != ident) i--;
            return i;
        }

        //存储变量或者过程
        void vardeclaration(vartype varkind)
        {
            if (sym == ident)
            {
                enter(varkind);

                getsym();
            }
            else
            {
                error("未知名的定义");
            }
        }

        //常量定义函数
        void constdeclaration()
        {
            if (sym == ident)
            {
                getsym();

                if (sym == eql)
                {
                    getsym();

                    if (sym == number)
                    {
                        enter(vartype.varConst, num);

                        getsym();
                    }
                }
                else
                {
                    if (sym == becomes)
                    {
                        getsym();

                        error("常量定义使用赋值号!");
                    }
                    else
                    {
                        error("常量定义错误!");
                    }
                }
            }
            else
            {
                error("常量定义错误!");
            }
        }

        //分析程序入口
        void Prog()
        {
            if (sym == progsym)
            {
                getsym();

                block();

                if (sym == period)
                {
                    getsym();
                }
                else
                {
                    error("程序缺少结束符号!");
                }
            }
            else
            {
                error("程序缺少 'program' 标志符!");
            }
        }

        // block 函数 
        void block()
        {
            dx = 3;
            int tx0 = tx;

            table[tx0].adr = cx;         /*  让每个table的入口 */

            gen(fct.JMP, 0, 0);

            ds();

            while (sym == procsym)
            {
                getsym();

                if (sym == ident)
                {
                    enter(vartype.varProc);

                    getsym();

                    if (sym == semicolon)
                    {
                        getsym();

                        int oldlev = lev;
                        int olddx = dx;

                        lev++;

                        block();

                        lev = oldlev;
                        dx = olddx;

                        if (sym == semicolon)
                        {
                            getsym();
                        }
                        else
                        {
                            error("过程定义缺少结束分号!");
                        }
                    }
                    else
                    {
                        error("过程定义缺少开始分号!");
                    }
                }
                else
                {
                    error("不可识别的过程定义标识符!");
                }
            }

            code[table[tx0].adr].a = cx;   /* 回填,确定函数起始位置 */

            table[tx0].adr = cx;

            gen(fct.INT, 0, dx);

            ss();

            gen(fct.OPR, 0, 0);
        }

        //定义变量的过程
        void ds()
        {
            d();

            while (sym == semicolon)
            {
                getsym();

                d();
            }
        }

        //定义变量过程
        void d()
        {
            if (sym == intesym)
            {
                getsym();

                vardeclaration(vartype.varInteger);

                while (sym == comma)
                {
                    getsym();
                    vardeclaration(vartype.varInteger);
                }
            }
            else
            {
                if (sym == logisym)
                {
                    getsym();
                    vardeclaration(vartype.varLogical);

                    while (sym == comma)
                    {
                        getsym();
                        vardeclaration(vartype.varLogical);
                    }
                }
                else
                {
                    if (sym == constsym)
                    {
                        getsym();
                        constdeclaration();

                        while (sym == comma)
                        {
                            getsym();
                            constdeclaration();
                        }
                    }
                    else
                    {
                        /* 可空的路径 */
                    }
                }
            }
        }

        //语句过程 
        void ss()
        {
            if (sym == ident)
            {
                int index = position(id);

                if (index == 0)
                {
                    error(" 不可识别的标识符!");
                    getsym();
                }
                else
                {
                    int kind = (int)table[index].kind;

                    if (kind == (int)vartype.varInteger)
                    {
                        getsym();

                        if (sym == becomes)
                        {
                            getsym();
                            ae();

                            gen(fct.STO, lev - table[index].level, table[index].adr);

                        }
                        else
                        {
                            if (sym == eql)
                            {
                                getsym();

                                error("赋值语句不能使用 '=' 标识符!");
                            }
                            else
                            {
                                error("不可识别的赋值语句!");
                            }
                        }

                    }
                    else if (kind == (int)vartype.varLogical)
                    {
                        getsym();

                        if (sym == becomes)
                        {
                            getsym();
                            be();

                            gen(fct.STO, lev - table[index].level, table[index].adr);

                        }
                        else
                        {
                            if (sym == eql)
                            {
                                getsym();

                                error("赋值语句不能使用 '=' 标识符!");
                            }
                            else
                            {
                                error("不可识别的赋值语句!");
                            }
                        }
                    }
                    else
                    {
                        error("为过程变量!");
                    }
                }
            }
            else if (sym == ifsym)
            {
                getsym();

                be();

                if (sym == thensym)
                {
                    getsym();

                    int cx1 = cx;
                    gen(fct.JPC, 0, 0);

                    ss();

                    while (sym == semicolon)
                    {
                        getsym();

                        ss();
                    }

                    gen(fct.JMP, 0, 0);
                    int cx2 = cx - 1;

                    code[cx1].a = cx;

                    if (sym == elsesym)
                    {
                        getsym();
                        ss();

                        while (sym == semicolon)
                        {
                            getsym();

                            ss();
                        }

                        code[cx2].a = cx;
                    }

                    if (sym == endsym)
                    {
                        getsym();
                        code[cx2].a = cx;

                    }
                    else
                    {
                        error("if 语句缺乏end标记符!");
                    }
                }
                else
                {
                    error("if 语句缺少 then 语句!");
                }

            }
            else if (sym == whilesym)
            {
                int cx1 = cx;

                getsym();
                be();

                int cx2 = cx;

                gen(fct.JPC, 0, 0);

                if (sym == dosym)
                {
                    getsym();
                    ss();

                    while (sym == semicolon)
                    {
                        getsym();

                        ss();
                    }

                    gen(fct.JMP, 0, cx1);
                    code[cx2].a = cx;

                    if (sym == endsym)
                    {
                        getsym();

                    }
                    else
                    {
                        error("在 while 循环里需要一个 end 标识符!");
                    }

                }
                else
                {
                    error("while 缺乏do标识符!");
                }

            }
            else if (sym == repeasym)
            {
                int cx1 = cx;

                getsym();
                ss();

                while (sym == semicolon)
                {
                    getsym();

                    ss();
                }

                if (sym == untilsym)
                {
                    getsym();
                    be();

                    gen(fct.JPC, 0, cx1);
                }
                else
                {
                    error("repeat 缺乏until标识符!");
                }

            }
            else if (sym == writesym)
            {
                getsym();
                ae();

                gen(fct.SHO, 0, 0);

            }
            else if (sym == beginsym)
            {
                getsym();

                ss();

                while (sym == semicolon)
                {
                    getsym();

                    ss();
                }

                if (sym == endsym)
                {
                    getsym();

                }
                else
                {
                    error("语句缺乏 end 标识符!");
                }

            }
            else if (sym == callsym)
            {
                getsym();

                if (sym == ident)
                {
                    int index = position(id);

                    if (index == 0)
                    {
                        error("没有定义!");
                    }
                    else
                    {
                        if (table[index].kind != vartype.varProc)
                        {
                            error("被调用的不是过程!");
                        }
                        else
                        {
                            gen(fct.CAL, lev - table[index].level, table[index].adr);
                            getsym();
                        }
                    }
                }
                else
                {

⌨️ 快捷键说明

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