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;
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
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());
        }
Exemplo n.º 4
0
        private void HandleId(Token token, int type, int len = -1, int assign = -1)
        {
            if (Global.idTable.ContainsKey(token.src))
            {
                ++errNum;
                errStr += "line " + token.lineNum + " : \t变量 \"" + token.src + "\" 重复定义\r\n";
                return;
            }

            ID id = new ID(token.src, ID.GetCount(), type, len, assign);

            Global.idTable.Add(token.src, id);
            //ID.RefreshCount(len);
        }
        public void midcodeGenerate()
        {
            init();
            Global.midcodeArr.Add(new MidCode("ALC"));
            Program();

            if (isFunMain())
            {
                FunMain();
            }

            Global.midcodeArr[0].op2 = "" + ID.GetCount();
            ++n;//EOF
        }
        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");
        }
        private void HandleId(Token token, int type, int len = -1, int assign = -1)
        {
            ID id = new ID(token.src, ID.GetCount(), type, len, assign);

            Global.idTable.Add(token.src, id);
        }