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

📄 ganalysis.cs

📁 关于CMM语言的解释器
💻 CS
📖 第 1 页 / 共 3 页
字号:
                case EMPTY:  //空语句
                    return new TreeNode("( Empty Statement )");

                case COMPLEX:  //复合语句
                    return GetComplexNode(st);

                case DECLARE: //声明语句
                    return GetDeclareNode(st);

                case INPUT:  //输入语句
                    return GetInputNode(st);

                case OUTPUT:  //输出语句
                    return GetOutputNode(st);

                case ASSIGN:  //赋值语句
                    return GetAssignNode(st);

                case LOOP:   //循环语句
                    return GetLoopNode(st);

                case SELECT:  //选择语句
                    return GetSelectNode(st);

                case ELSE_BR: //if语句的else分支
                case ERROR:
                    return new TreeNode("(Error Statement : '" + st.Con + "' )");

                default:
                    return null;
            }
        }

        //获得复合语句语法树节点
        private TreeNode GetComplexNode(CMMStatement st)
        {
            TreeNode rootNode = new TreeNode("{}");
            string cmpsts = st.Els[0];
            while (true)
            {
                CMMStatement cmpsst = SeparateFirstStatement(ref cmpsts);
                if (cmpsst.Type == NULLST)
                    break;
                else
                {
                    TreeNode stNode = GetNode(cmpsst);
                    if (stNode != null)
                        rootNode.Nodes.Add(stNode);
                }
            }
            return rootNode;
        }

        //获得声明语句语法树节点
        private TreeNode GetDeclareNode(CMMStatement st)
        {
            TreeNode rootNode = new TreeNode();
            if (st.Els[2].Equals(NOTARR))
                rootNode.Text = st.Els[1];
            else
                rootNode.Text = st.Els[1] + "[" + st.Els[2] + "]";
            rootNode.Nodes.Add(st.Els[0]); //ID
            if (!st.Els[3].Equals(NOTASS)) //声明-赋值语句
            {
                rootNode.Nodes.Add("=");
                rootNode.Nodes.Add(GetArthmExpNode(st.Els[3]));
            }
            return rootNode;
        }

        //获得输入语句语法树节点
        private TreeNode GetInputNode(CMMStatement st)
        {
            TreeNode rootNode = new TreeNode("read");
            if (st.Els[1].Equals(NOTARR))
                rootNode.Nodes.Add(st.Els[0]);
            else
                rootNode.Nodes.Add(st.Els[0] + "[" + st.Els[1].Trim() + "]");
            return rootNode;
        }

        //获得输出语句语法树节点
        private TreeNode GetOutputNode(CMMStatement st)
        {
            TreeNode rootNode = new TreeNode("write");
            rootNode.Nodes.Add(GetArthmExpNode(st.Els[0]));
            return rootNode;
        }

        //获得赋值语句语法树节点
        private TreeNode GetAssignNode(CMMStatement st)
        {
            TreeNode rootNode = new TreeNode("= (Assign)");
            if (st.Els[1].Equals(NOTARR))
                rootNode.Nodes.Add(st.Els[0]);
            else
                rootNode.Nodes.Add(st.Els[0] + "[" + st.Els[1] + "]");
            rootNode.Nodes.Add(GetArthmExpNode(st.Els[2]));
            return rootNode;
        }

        //获得循环语句语法树节点
        private TreeNode GetLoopNode(CMMStatement st)
        {
            TreeNode rootNode = new TreeNode("while");
            TreeNode expNode = new TreeNode("Condition");
            expNode.Nodes.Add(GetRelatExpNode(new CMMStatement(st.Els[0])));
            TreeNode stNode = new TreeNode("Loop Statement");
            stNode.Nodes.Add(GetNode(new CMMStatement(st.Els[1])));
            rootNode.Nodes.Add(expNode);
            rootNode.Nodes.Add(stNode);
            return rootNode;
        }

        //获得选择语句语法树节点
        private TreeNode GetSelectNode(CMMStatement st)
        {
            TreeNode rootNode = new TreeNode("if");
            TreeNode expNode = new TreeNode("Condition");
            expNode.Nodes.Add(GetRelatExpNode(new CMMStatement(st.Els[0])));
            TreeNode thenNode = new TreeNode("Then Branch");
            thenNode.Nodes.Add(GetNode(new CMMStatement(st.Els[1])));
            rootNode.Nodes.Add(expNode);
            rootNode.Nodes.Add(thenNode);
            if (st.Els[2].Length != 0)
            {
                TreeNode elseNode = new TreeNode("Else Branch");
                elseNode.Nodes.Add(GetNode(new CMMStatement(st.Els[2])));
                rootNode.Nodes.Add(elseNode);
            }
            return rootNode;
        }

        //获得关系表达式语法树节点
        private TreeNode GetRelatExpNode(CMMStatement exp)
        {
            AnalyzeRelatExp(ref exp);
            if (exp.Type == ERROR)
                return new TreeNode("(Error Rlt Exp : '" + exp.Con + "' )");
            TreeNode rootNode = new TreeNode(exp.Els[0]);
            rootNode.Nodes.Add(GetArthmExpNode(exp.Els[1]));
            rootNode.Nodes.Add(GetArthmExpNode(exp.Els[2]));
            return rootNode;
        }

        //获得算术表达式语法树节点
        private TreeNode GetArthmExpNode(string exp)
        {
            ArthmExp athExp = new ArthmExp(exp);
            if (!athExp.IsArthmExp())
                return new TreeNode("(Error Ath Exp : '" + exp + "' )");

            athExp.Con = Regex.Replace(athExp.Con, @" ", "").ToString();
            TreeNode rootNode = new TreeNode("Exp : " + athExp.Con);

            Term[] terms = ArthmExp.GetTerm(athExp);
            for (int i = 0; i < terms.Length; i++)
            {
                Term term = terms[i];
                TreeNode termNode;
                //若第一项符号为'+',出于习惯不显示 '+'
                if (i == 0 && term.Sign.Equals("+")) ;
                else
                {
                    //添加项符号
                    rootNode.Nodes.Add(term.Sign);
                }
                //添加项
                termNode = new TreeNode("Term : " + term.Con);

                Factor[] factors = ArthmExp.GetFactor(term);
                int factorNum = factors.Length;

                //第一个因子
                termNode.Nodes.Add(GetFactorNode(factors[0].Con));
                if (factorNum > 1)
                {
                    for (int j = 1; j < factorNum; j++)
                    {
                        //添加因子符号
                        termNode.Nodes.Add(factors[j].Sign);
                        //添加因子
                        termNode.Nodes.Add(GetFactorNode(factors[j].Con));
                    }
                }
                rootNode.Nodes.Add(termNode);
            }

            return rootNode;
        }

        //获得因子语法树节点
        private TreeNode GetFactorNode(string factor)
        {
            int factorType = Factor.GetFactorType(factor);
            if (factorType == INTG
                || factorType == REAL
                || factorType == ID
                || factorType == ARR_ID)
                return new TreeNode(factor);
            else if (factorType == ARTHM_EXP)
            {
                string exp = "";
                GetBracketContent(ref factor, ref exp);
                return GetArthmExpNode(exp);
            }
            else
                return new TreeNode("(Error Factor : )" + factor);

        }



        //语法错误报错
        private void dealError(string statement, string cause)
        {
            errorNum++;
            resultText += errorNum + ".语句:  " + statement
                        + "\r\n  错误:  " + cause + " \r\n";
        }



        //获取与左括号匹配的右括号之间(不包括)的语句(辅助方法)。
        //注:sourceCode第一个字符必须为左括号,操作完后bracketCode不含括号
        public bool GetBracketContent(ref string sourceCode, ref string bracketCode)
        {
            string leftBracket = sourceCode.Substring(0, 1);
            string rightBracket;
            if (leftBracket.Equals("(")) rightBracket = ")";
            else if (leftBracket.Equals("[")) rightBracket = "]";
            else if (leftBracket.Equals("{")) rightBracket = "}";
            else return false;

            WordAnalysis wordAnalysis = new WordAnalysis();
            sourceCode = sourceCode.Remove(0, 1);
            int count = 1;

            while (count != 0)
            {
                int rtIndex = sourceCode.IndexOf(rightBracket);

                if (rtIndex < 0) //如果找不到右括号
                    return false;

                //增加的代码片断
                string addCode = sourceCode.Substring(0, rtIndex + 1);
                bracketCode += addCode;
                sourceCode = sourceCode.Remove(0, addCode.Length);

                ArrayList sourceCodeWord = wordAnalysis.GetWordLst(addCode);
                foreach (string word in sourceCodeWord)
                {
                    if (word.Equals(leftBracket))
                        count++;
                    else if (word.Equals(rightBracket))
                        count--;
                }
            }
            //若跳出循环,则括号匹配成功
            bracketCode = bracketCode.Remove(bracketCode.Length - 1); //删除右括号
            return true;
        }


        WordAnalysis wordAnalysis = new WordAnalysis();

        //返回结果
        private string resultText;

        //语法树
        private TreeView trv;

        //语法错误数(私有)
        private int errorNum;


        //语法错误数(公共访问)
        public int ErrorNum
        {
            get { return errorNum; }
            set { errorNum = 0; } //只允许归0操作
        }


        //CMM语言的保留字
        public string[] ReservedWords = new string[]
        {
                "if" , "else" , "while" , "read" , "write" , "int" , "real"
        };


        //符号(分隔保留字,标识符,整数,实数),详见词法分析开发文档
        public string[] Symbols = new string[] 
        { 
                "==" , "<>" ,"<", ";", "=" ,  "+" , "-" , "*" , "/" ,"(" , ")", "{" , "}" ,  
                "[" , "]" , " " , "\t" , "\r" ,"\n"  
        };


        //算术运算符
        string[] Oprts = { "+", "-", "*", "/" };

        //比较运算符
        string[] Compars = { "<>", "==", "<" };


        //词句类型常量
        const int ERROR = -1;        //错误

        const int RSVWORD = -11;     //保留字
        const int ID = -12;          //标识符
        const int ARR_ID = -13;      //数组标识符
        const int INTG = -14;        //整数
        const int REAL = -15;        //实数
        const int OPRT = -16;        //算术运算符 + - * /
        const int COMPAR = -17;      //比较运算符 < == <>
        const int SEMI = -18;        //分号
        const int BRACKET = -19;     //括号 ()[]{}
        const int EQUAL = -20;       //等号
        const int SYMBOL = -20;      //符号

        const int NULLST = -31;      //null语句
        const int EMPTY = -32;       //空语句
        const int COMPLEX = -33;     //复合语句
        const int DECLARE = -34;     //声明语句
        const int ASSIGN = -37;      //赋值语句
        const int INPUT = -39;       //输入语句
        const int OUTPUT = -41;      //输出语句
        const int SELECT = -42;      //if选择语句
        const int ELSE_BR = -44;     //if-else选择语句的else分支
        const int LOOP = -45;        //while循环语句
        const int ARTHM_EXP = -46;   //算术表达式
        const int RELAT_EXP = -47;   //关系表达式

        const string NOTARR = "NotArr"; //非数组
        const string NOTASS = "NotAss"; //未赋值




    }
}

⌨️ 快捷键说明

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