private void VarDclr_(int kind)
        {
            int type = Global.tokenArr[n].type;

            if (type == (int)Symbol.SEMICOLON)              //ε,结束声明变量
            {
                return;
            }
            else
            {
                if (type == (int)Symbol.BECOMES)                           //普通变量声明并赋值
                {
                    int record = n;                                        //记录=位置
                    exprArr.Add(Global.tokenArr[record - 1]);              //放入id
                    ++n;                                                   //=
                    Expr();
                    exprArr.Add(Global.tokenArr[record]);                  //放入=
                }
                else if (type == (int)Symbol.LBRAKET)                      //数组变量声明(与赋值)//------------------待修改:数组赋值
                {
                    int arr_record = n - 1;                                //arr_id
                    ++n;                                                   //[
                    int x = int.Parse(Global.tokenArr[n].src);
                    Global.idTable[Global.tokenArr[n - 2].src].lenght = x; //数组长度
                    ID.RefreshCount(x - 1);
                    n += 2;                                                //num]
                    VarDclr_2();                                           //数组赋值
                }
                VarDclr_3(kind);                                           //继续声明
            }
        }
        private void FunDclr()
        {
            //生成JUMP中间代码,获取JUMP中间代码的行号(为下标+1)
            Global.midcodeArr.Add(new MidCode(opArr["jump"]));
            int jump_record = Global.midcodeArr.Count;

            ++n;//type

            //记录函数id的起始指令行
            int id_record = n;

            HandleId(Global.tokenArr[id_record], Global.tokenArr[id_record - 1].type);
            Global.idTable[Global.tokenArr[id_record].src].vcodeAssignLine = jump_record + 1;
            n += 2;//id(

            //分配空间
            Global.midcodeArr.Add(new MidCode("ALC"));
            int alc_record = Global.midcodeArr.Count;

            Global.idTable[Global.tokenArr[id_record].src].vcodeAssignLine = alc_record;


            //保存现场
            Dictionary <string, ID> tempTable = new Dictionary <string, ID>(Global.idTable, null);//用于暂存符号表

            ID.RefreshCount(0 - ID.GetCount());
            ClearCase();
            int tempTempNum = tempNum;

            tempNum = 0;

            //参数处理
            VarListDclr();
            Global.idTable[Global.tokenArr[id_record].src].numOfVar = ID.GetCount(); //获取现在临时id表中的变量个数,即新加入的变量个数
            ++n;                                                                     //)

            //处理临时变量下标
            int tempParamNum = ID.GetCount();

            HandleParamOff(tempParamNum);
            ID.RefreshCount(0 - tempParamNum);



            //函数体处理
            FunBody(id_record);

            //修改JUMP中间指令的跳转行和ACL中间指令的参数
            Global.midcodeArr[jump_record - 1].op2 = "" + (Global.midcodeArr.Count + 1);
            Global.midcodeArr[alc_record - 1].op2  = "" + ID.GetCount();

            //恢复现场
            Global.idTable = tempTable;//还原符号表
            ID.RefreshCount(0 - ID.GetCount());
            ID.RefreshCount(tempTable.Count());
            tempNum = tempTempNum;
        }
Ejemplo n.º 3
0
        private void init()
        {
            //进行清理,以免之前就进行过语法分析,消除干扰
            n        = 0;
            isInExpr = false;
            errNum   = 0;
            errStr   = "";
            exprType = 0;
            ID.RefreshCount(0 - ID.GetCount());

            symTable.Clear();
            Global.idTable.Clear();

            symTable.Add("int");
            symTable.Add("real");
            symTable.Add("void");
            symTable.Add("if");
            symTable.Add("else");
            symTable.Add("while");
            symTable.Add("for");
            symTable.Add("read");
            symTable.Add("write");
            symTable.Add("return");
            symTable.Add("标识符");
            symTable.Add("整数");
            symTable.Add("小数");
            symTable.Add("++");
            symTable.Add("--");
            symTable.Add("+");
            symTable.Add("-");
            symTable.Add("*");
            symTable.Add("/");
            symTable.Add("=");
            symTable.Add("==");
            symTable.Add("!=");
            symTable.Add(">");
            symTable.Add(">=");
            symTable.Add("<");
            symTable.Add("<=");
            symTable.Add(",");
            symTable.Add(";");
            symTable.Add("(");
            symTable.Add(")");
            symTable.Add("{");
            symTable.Add("}");
            symTable.Add("[");
            symTable.Add("]");
            symTable.Add("EOF");

            //初始化语法树
            gTree      = new TreeView();
            gTree.Dock = DockStyle.Fill;
            begin      = new TreeNode("语法树");
            gTree.Nodes.Add(begin);
        }
Ejemplo n.º 4
0
        private void FunDclr(ref TreeNode root)
        {
            TreeNode tn = NewTreeNode(ref root, "函数声明");

            int type; //用于与返回值类型进行比较

            if (Global.tokenArr[n].type == (int)Symbol.INT)
            {
                type = (int)Symbol.INTEGER;
            }
            else if (Global.tokenArr[n].type == (int)Symbol.REAL)
            {
                type = (int)Symbol.REALNUM;
            }
            else
            {
                type = (int)Symbol.VOID;
            }
            Type(ref tn);

            //这里不用判别是不是id就可以加入符号表,因为已经符合FunDclr的要求了(通过了isFunDclr)
            int record = n;

            NewID(Global.tokenArr[n - 1].type, ref tn);//参数为函数返回值类型
            Global.idTable[Global.tokenArr[record].src].vcodeAssignLine = Global.tokenArr[record].lineNum;
            Global.idTable[Global.tokenArr[record].src].isDefined       = true;

            //保存现场
            Dictionary <string, ID> tempTable = new Dictionary <string, ID>(Global.idTable, null); //用于暂存符号表

            ID.RefreshCount(0 - ID.GetCount());                                                    //变量清零
            ClearCase();

            TreeNode tn2 = NewTreeNode(ref tn, "参数列表");

            Match((int)Symbol.LPAREN, ref tn2);
            VarListDclr(ref tn2);
            Global.idTable[Global.tokenArr[record].src].numOfVar = ID.GetCount();//获取现在临时id表中的变量个数,即新加入的变量个数
            Match((int)Symbol.RPAREN, ref tn2);

            int returnType = FunBody(ref tn);

            if ((type == (int)Symbol.REALNUM && returnType != (int)Symbol.REALNUM && returnType != (int)Symbol.INTEGER) || (type == (int)Symbol.INTEGER && returnType != (int)Symbol.INTEGER) || (type == (int)Symbol.VOID && returnType != (int)Symbol.VOID))
            {
                ++errNum;
                errStr += "line " + Global.tokenArr[record].lineNum + " : \t函数返回值类型与实际返回值类型不匹配\r\n";
            }

            //恢复现场
            Global.idTable[Global.tokenArr[record].src].isDefined = true; //已声明
            Global.idTable = tempTable;                                   //还原符号表
            ID.RefreshCount(tempTable.Count());
        }
Ejemplo n.º 5
0
        private void VarDclr_(int kind, ref TreeNode root)
        {
            int type = Global.tokenArr[n].type;

            if (type == (int)Symbol.SEMICOLON)          //ε,结束声明变量
            {
                return;
            }
            else
            {
                if (type == (int)Symbol.BECOMES) //普通变量声明并赋值
                {
                    int      record = n - 1;     //变量位置
                    TreeNode tn     = NewTreeNode(ref root, "普通变量初始化");
                    Match((int)Symbol.BECOMES, ref tn);
                    Expr(ref tn);
                    Global.idTable[Global.tokenArr[record].src].isDefined = true; //已声明
                }
                else if (type == (int)Symbol.LBRAKET)                             //数组变量声明(与赋值)
                {
                    TreeNode tn = NewTreeNode(ref root, "数组下标");

                    Match((int)Symbol.LBRAKET, ref tn);//[
                    if (Global.tokenArr[n].type == (int)Symbol.INTEGER)
                    {
                        int x = int.Parse(Global.tokenArr[n].src); //不会有异常,因为肯定是integer类型了
                        Global.idTable[Global.tokenArr[n - 2].src].lenght = x;
                        ID.RefreshCount(x - 1);                    //之前放Id的时候已经放过1长度了,在HandleId里
                        Match((int)Symbol.INTEGER, ref tn);        //]
                    }
                    else
                    {
                        ++errNum;
                        errStr += "line " + Global.tokenArr[n].lineNum + " : \t下标类型不为整型\r\n";
                    }
                    Match((int)Symbol.RBRAKET, ref tn);

                    VarDclr_2(Global.idTable[Global.tokenArr[n - 4].src].lenght, ref tn);
                }
                else if (type == (int)Symbol.COMMA)
                {
                    ;                                    //继续声明变量
                }
                else
                {
                    ++errNum;
                    errStr += "line " + Global.tokenArr[n].lineNum + " : \t未知错误\r\n";
                }

                VarDclr_3(kind, ref root);
            }
        }
        private void init()
        {
            //进行清理,以免之前就进行过语法分析,消除干扰
            Global.idTable.Clear();
            n        = 0;
            isInExpr = false;
            tempNum  = 0;
            opArr.Clear();
            Global.midcodeArr.Clear();
            midcodeGrid.Rows.Clear();
            midcodeGrid.Columns.Clear();
            ID.RefreshCount(0 - ID.GetCount());

            //创建Grid
            midcodeGrid.RowHeadersVisible = false;               //去除行头
            midcodeGrid.ScrollBars        = ScrollBars.Vertical; //不显示水平滚动条
            midcodeGrid.BorderStyle       = BorderStyle.None;    //去除边框显示
            midcodeGrid.ReadOnly          = true;
            string[] arr = { "Num", "OP", "ARG1", "ARG2", "RESULT" };
            for (int i = 0; i < arr.Length; i++)    //新建列
            {
                DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
                col.Name       = arr[i];
                col.HeaderText = arr[i];
                col.Width      = 100;
                midcodeGrid.Columns.Add(col);
            }
            //midcodeGrid.Columns[0].Width = 50;//行号一列宽度为50

            opArr.Add("+", "ADD");
            opArr.Add("-", "SUB");
            opArr.Add("*", "MUL");
            opArr.Add("/", "DIV");
            opArr.Add("<", "LES");
            opArr.Add("<=", "LEQ");
            opArr.Add(">", "GTR");
            opArr.Add(">=", "GEQ");
            opArr.Add("==", "EQL");
            opArr.Add("!=", "NEQ");
            opArr.Add("=", "ASN");
            opArr.Add("jump", "JMP");
            opArr.Add("jumpc", "JPC");
        }