Exemplo n.º 1
0
        String found(String a, GrammarSheet c)
        {
            bool   unendoccur = false;
            string less       = "";

            for (int m = 0; m < 18; m++)
            {
                for (int n = 0; n < 25; n++)
                {
                    if (c.grammarSheetColumn[m] == a)
                    {
                        unendoccur = true;
                        if (c.grammarSheetUnit[m, n].productionSubstring[0] != "error" && c.grammarSheetUnit[m, n].productionSubstring[0] != "synch")
                        {
                            less = less + c.grammarSheetUnit[m, n] + ",";
                        }
                    }
                }
            }
            if (!unendoccur)
            {
                less = less + a;
            }
            return(less);
        }
Exemplo n.º 2
0
 //语法分析表输出,输入为两个字符串,分别为左右栈的栈顶字符串,输出为Production类型的生成式
 Production Grammarsheet_Return_Production(string a, string b, GrammarSheet c)
 {
     for (int m = 0; m < 18; m++)
     {
         for (int n = 0; n < 25; n++)
         {
             if (c.grammarSheetColumn[m] == a && c.grammarSheetRow[n] == b)
             {
                 return(c.grammarSheetUnit[m, n]);
             }
         }
     }
     return(c.grammarSheetUnit[10, 0]);//default情况,返回
 }
Exemplo n.º 3
0
        //语法分析表输出,输入为两个字符串,分别为左右栈的栈顶字符串,输出为Production类型的生成式的表达式
        public string Grammarsheet_Return(string a, string b, GrammarSheet c)
        {
            string prostring = "";

            for (int m = 0; m < 18; m++)
            {
                for (int n = 0; n < 25; n++)
                {
                    if (c.grammarSheetColumn[m] == a && c.grammarSheetRow[n] == b)
                    {//如果匹配成功,输出产生式
                        for (int i = 0; i < 10; i++)
                        {
                            if (c.grammarSheetUnit[m, n].productionSubstring[i] != null)
                            {
                                prostring = prostring + c.grammarSheetUnit[m, n].productionSubstring[i] + " ";
                            }
                        }
                    }
                }
            }
            return(prostring);
        }
Exemplo n.º 4
0
 private void Compiler_Load(object sender, EventArgs e)
 {
     myLex = new Lex(); //初始化
     gr = new GrammarSheet();
     tr = new Translation();
     nResult = 0;
     myString = new String[6];
     syntaxString = new String[6];
     fileChange = false;
     fileChick = false;
     filePress = false;
     fileLock = false;
     fileTemp = false;
     thisStatus.Text = "  程序加载成功";
     //高亮
     keywords1 = new Hashtable();
     keywords2 = new Hashtable();
     keywords1.Add("int", '1');
     keywords1.Add("real", '1');
     keywords2.Add("if", '1');
     keywords2.Add("then", '1');
     keywords2.Add("else", '1');
     keywords2.Add("while", '1');
 }
Exemplo n.º 5
0
 private void Compiler_Load(object sender, EventArgs e)
 {
     myLex           = new Lex(); //初始化
     gr              = new GrammarSheet();
     tr              = new Translation();
     nResult         = 0;
     myString        = new String[6];
     syntaxString    = new String[6];
     fileChange      = false;
     fileChick       = false;
     filePress       = false;
     fileLock        = false;
     fileTemp        = false;
     thisStatus.Text = "  程序加载成功";
     //高亮
     keywords1 = new Hashtable();
     keywords2 = new Hashtable();
     keywords1.Add("int", '1');
     keywords1.Add("real", '1');
     keywords2.Add("if", '1');
     keywords2.Add("then", '1');
     keywords2.Add("else", '1');
     keywords2.Add("while", '1');
 }
Exemplo n.º 6
0
        //过程栈和输入栈进行怼的过程,treestack为过程栈,b为输入栈
        public void match(Stack <string[]> b, GrammarSheet c, int nResult /*判定是否是文件结束*/)
        {
            int[] errorshowrow = new int[100]; //显示错误所在的行
            c.outcount = -1;                   //每次进入match函数,序号初始化为-1


            //p,q用于输出两个栈的内容
            Stack <Node>     p = new Stack <Node>();
            Stack <string[]> q = new Stack <string[]>();
            Stack <Node>     r = new Stack <Node>();//用于反向输出的保存



            while (!(treestack.Count == 0))
            {
                c.outcount++;//process,inout,action的计数+1
                //清空process,input,action使得每多读一个token都不是在原来的输出中写内容
                c.process[outcount] = "";
                c.input[outcount]   = "";
                c.action[outcount]  = "";
                //p,q用于输出两个栈的内容
                p = new Stack <Node>(treestack);
                q = new Stack <string[]>(b);

                while (!(p.Count == 0))
                {
                    if (p.Peek().type != "empty")
                    {
                        r.Push(p.Peek());
                    }
                    p.Pop();
                }
                while (!(r.Count == 0))
                {
                    if (r.Peek().type != "")
                    {
                        c.process[c.outcount] = c.process[c.outcount] + r.Peek().type + " ";//用字符串m获取过程栈中字符串
                    }
                    r.Pop();
                }
                c.process[c.outcount] = c.process[c.outcount];//空一行
                //	printf("%-30s", m.c_str());
                while (!(q.Count == 0))
                {
                    if (q.Peek()[2] != "empty")
                    {
                        c.input[c.outcount] = c.input[c.outcount] + q.Peek()[2] + " ";//用字符串n获取输入栈中字符串
                    }
                    q.Pop();
                }
                c.input[c.outcount] = c.input[c.outcount];
                //	printf("%-30s", n.c_str());
                //把empty符号删除,因为empty表示空

                if (treestack.Peek().type == "empty")
                {
                    treestack.Pop();
                }
                if (b.Peek()[2] == "empty")
                {
                    b.Pop();
                }
                //如果某个栈只剩$了,而另一个不可能变成$,那么就结束并报错
                if ((treestack.Peek().type == "$" && b.Peek()[2] != "$") || (treestack.Peek().type != "$" && b.Peek()[2] == "$"))
                {
                    c.action[c.outcount] = c.action[c.outcount] + "该语句出错";
                    c.programright       = false;
                    break;
                }
                //当两个栈的栈顶不同时
                if (treestack.Peek().type != b.Peek()[2])
                {
                    Production k = Grammarsheet_Return_Production(treestack.Peek().type, b.Peek()[2], c);

                    //假如能在符号表中找到生成式,说明分析过程还是正确的
                    if (k.productionSubstring[0] != "error" && k.productionSubstring[0] != "synch")
                    {
                        //构建临时list临时存放生成式的node,用于反向放入treestack中
                        List <Node> temp = new List <Node>();
                        //输出动作
                        c.action[c.outcount] = c.action[c.outcount] + "输出 ";
                        c.action[c.outcount] = c.action[c.outcount] + Grammarsheet_Return(treestack.Peek().type, b.Peek()[2], c);
                        //构建list
                        Production currentproduction = Grammarsheet_Return_Production(treestack.Peek().type, b.Peek()[2], c);
                        for (int i = 2; i < 10; i++)
                        {
                            if (currentproduction.productionSubstring[i] != "" && currentproduction.productionSubstring[i] != null)
                            {
                                //获取当前孩子节点中的某个
                                Node currentnode = new Node("", currentproduction.productionSubstring[i], "", "", "");
                                temp.Add(currentnode);//
                                //取栈顶作为父节点来加入他的孩子
                                Node tmpnode = treestack.Peek();
                                tmpnode.Add(currentnode);
                                //计算当前节点所在的层
                                currentnode.layer = tmpnode.layer + 1;
                                //计算list的高
                                if (currentnode.layer >= treeroot.layerheight)
                                {
                                    treeroot.layerheight = currentnode.layer;
                                }
                            }
                        }
                        treestack.Pop();
                        //treestack放入生成式节点
                        for (int i = temp.Count - 1; i >= 0; i--)
                        {
                            if (k.productionSubstring[i] != "" && k.productionSubstring[i] != null)
                            {
                                treestack.Push(temp[i]);
                            }
                        }

                        //清空临时list
                        temp.Clear();
                    }

                    //如果没有对应的生成式,则弹出输入栈中最上方的字符并报错
                    else if (k.productionSubstring[0] == "error")
                    {
                        //输出动作
                        c.action[c.outcount] = c.action[c.outcount] + "出错";
                        c.errorshow          = c.errorshow + "第" + b.Peek()[4] + "行 " + "缺少或多余" + found(b.Peek()[2], c) + "\r\n" + "\r\n";
                        c.programright       = false;
                        b.Pop();
                    }

                    //如果对应的生成式为错误处理,则弹出过程栈中最上方的非终结符并报错
                    else if (k.productionSubstring[0] == "synch")
                    {
                        //输出动作
                        c.action[c.outcount] = c.action[c.outcount] + "出错";
                        c.errorshow          = c.errorshow + "第" + b.Peek()[4] + "行 " + "缺少或多余" + found(b.Peek()[2], c) + "\r\n" + "\r\n";
                        c.programright       = false;
                        treestack.Pop();
                    }
                }

                //两个栈的栈顶相同
                else if (treestack.Peek().type == b.Peek()[2])
                {
                    treestack.Peek().name        = b.Peek()[1];
                    treestack.Peek().symbolname  = b.Peek()[1];
                    treestack.Peek().value       = b.Peek()[3];
                    treestack.Peek().symbolvalue = b.Peek()[3];
                    treestack.Peek().lineNo      = b.Peek()[4];
                    treestack.Peek().columnNo    = b.Peek()[5];
                    if (treestack.Peek().type == "}")
                    {
                        c.action[c.outcount] = c.action[c.outcount] + "匹配 " + treestack.Peek().type + "该句子正确";
                    }
                    else
                    {
                        c.action[c.outcount] = c.action[c.outcount] + "匹配 " + treestack.Peek().type;
                    }

                    treestack.Pop();
                    b.Pop();
                }
            }
        }
Exemplo n.º 7
0
 String found(String a,GrammarSheet c)
 {
     bool unendoccur = false;
     string less="";
     for (int m = 0; m < 18; m++)
     {
         for (int n = 0; n < 25; n++)
         {
             if (c.grammarSheetColumn[m] == a )
             {
                 unendoccur = true;
                 if (c.grammarSheetUnit[m, n].productionSubstring[0] != "error" && c.grammarSheetUnit[m, n].productionSubstring[0] != "synch")
                     less = less + c.grammarSheetUnit[m, n] + ",";
             }
         }
     }
     if (!unendoccur)
         less = less + a;
     return less;
 }
Exemplo n.º 8
0
        //过程栈和输入栈进行怼的过程,treestack为过程栈,b为输入栈
        public void match(Stack<string[]> b, GrammarSheet c, int nResult/*判定是否是文件结束*/)
        {
            int[] errorshowrow = new int[100];//显示错误所在的行
            c.outcount = -1;//每次进入match函数,序号初始化为-1


            //p,q用于输出两个栈的内容
            Stack<Node> p = new Stack<Node>();
            Stack<string[]> q = new Stack<string[]>();
            Stack<Node> r = new Stack<Node>();//用于反向输出的保存



            while (!(treestack.Count == 0))
            {
                c.outcount++;//process,inout,action的计数+1
                //清空process,input,action使得每多读一个token都不是在原来的输出中写内容
                c.process[outcount] = "";
                c.input[outcount] = "";
                c.action[outcount] = "";
                //p,q用于输出两个栈的内容
                p = new Stack<Node>(treestack);
                q = new Stack<string[]>(b);

                while (!(p.Count == 0))
                {
                    if (p.Peek().type != "empty")
                        r.Push(p.Peek());
                    p.Pop();
                }
                while (!(r.Count == 0))
                {
                    if (r.Peek().type != "")
                        c.process[c.outcount] = c.process[c.outcount] + r.Peek().type + " ";//用字符串m获取过程栈中字符串
                    r.Pop();
                }
                c.process[c.outcount] = c.process[c.outcount];//空一行
                //	printf("%-30s", m.c_str());
                while (!(q.Count == 0))
                {
                    if (q.Peek()[2] != "empty")
                        c.input[c.outcount] = c.input[c.outcount] + q.Peek()[2] + " ";//用字符串n获取输入栈中字符串
                    q.Pop();
                }
                c.input[c.outcount] = c.input[c.outcount];
                //	printf("%-30s", n.c_str());
                //把empty符号删除,因为empty表示空

                if (treestack.Peek().type == "empty")
                {
                    treestack.Pop();
                }
                if (b.Peek()[2] == "empty")
                {
                    b.Pop();
                }
                //如果某个栈只剩$了,而另一个不可能变成$,那么就结束并报错
                if ((treestack.Peek().type == "$" && b.Peek()[2] != "$") || (treestack.Peek().type != "$" && b.Peek()[2] == "$"))
                {
                    c.action[c.outcount] = c.action[c.outcount] + "该语句出错";
                    c.programright = false;
                    break;
                }
                //当两个栈的栈顶不同时
                if (treestack.Peek().type != b.Peek()[2])
                {

                    Production k = Grammarsheet_Return_Production(treestack.Peek().type, b.Peek()[2], c);

                    //假如能在符号表中找到生成式,说明分析过程还是正确的
                    if (k.productionSubstring[0] != "error" && k.productionSubstring[0] != "synch")
                    {
                        //构建临时list临时存放生成式的node,用于反向放入treestack中
                        List<Node> temp = new List<Node>();
                        //输出动作
                        c.action[c.outcount] = c.action[c.outcount] + "输出 ";
                        c.action[c.outcount] = c.action[c.outcount] + Grammarsheet_Return(treestack.Peek().type, b.Peek()[2], c);
                        //构建list
                        Production currentproduction = Grammarsheet_Return_Production(treestack.Peek().type, b.Peek()[2], c);
                        for (int i = 2; i < 10; i++)
                        {
                            if (currentproduction.productionSubstring[i] != "" && currentproduction.productionSubstring[i] != null)
                            {
                                //获取当前孩子节点中的某个
                                Node currentnode = new Node("", currentproduction.productionSubstring[i], "", "", "");
                                temp.Add(currentnode);//
                                //取栈顶作为父节点来加入他的孩子
                                Node tmpnode = treestack.Peek();
                                tmpnode.Add(currentnode);
                                //计算当前节点所在的层
                                currentnode.layer = tmpnode.layer + 1;
                                //计算list的高
                                if (currentnode.layer >= treeroot.layerheight)
                                    treeroot.layerheight = currentnode.layer;
                            }
                        }
                        treestack.Pop();
                        //treestack放入生成式节点
                        for (int i = temp.Count - 1; i >= 0; i--)
                        {
                            if (k.productionSubstring[i] != "" && k.productionSubstring[i] != null)
                            {
                                treestack.Push(temp[i]);
                            }
                        }

                        //清空临时list
                        temp.Clear();
                    }

                    //如果没有对应的生成式,则弹出输入栈中最上方的字符并报错
                    else if (k.productionSubstring[0] == "error")
                    {
                        //输出动作	
                        c.action[c.outcount] = c.action[c.outcount] + "出错";
                        c.errorshow = c.errorshow + "第" + b.Peek()[4] + "行 " + "缺少或多余" +found(b.Peek()[2],c)+ "\r\n" + "\r\n";
                        c.programright = false;
                        b.Pop();
                    }

                    //如果对应的生成式为错误处理,则弹出过程栈中最上方的非终结符并报错
                    else if (k.productionSubstring[0] == "synch")
                    {
                        //输出动作		
                        c.action[c.outcount] = c.action[c.outcount] + "出错";
                        c.errorshow = c.errorshow + "第" + b.Peek()[4] + "行 " + "缺少或多余" +found(b.Peek()[2],c)+ "\r\n" + "\r\n";
                        c.programright = false;
                        treestack.Pop();
                    }
                }

                //两个栈的栈顶相同
                else if (treestack.Peek().type == b.Peek()[2])
                {
                    treestack.Peek().name = b.Peek()[1];
                    treestack.Peek().symbolname = b.Peek()[1];
                    treestack.Peek().value = b.Peek()[3];
                    treestack.Peek().symbolvalue = b.Peek()[3];
                    treestack.Peek().lineNo = b.Peek()[4];
                    treestack.Peek().columnNo = b.Peek()[5];
                    if (treestack.Peek().type == "}")
                    {
                        c.action[c.outcount] = c.action[c.outcount] + "匹配 " + treestack.Peek().type + "该句子正确";
                    }
                    else
                    {
                        c.action[c.outcount] = c.action[c.outcount] + "匹配 " + treestack.Peek().type;
                    }

                    treestack.Pop();
                    b.Pop();
                }
            }
        }
Exemplo n.º 9
0
 //语法分析表输出,输入为两个字符串,分别为左右栈的栈顶字符串,输出为Production类型的生成式
 Production Grammarsheet_Return_Production(string a, string b, GrammarSheet c)
 {
     for (int m = 0; m < 18; m++)
     {
         for (int n = 0; n < 25; n++)
         {
             if (c.grammarSheetColumn[m] == a && c.grammarSheetRow[n] == b)
             {
                 return c.grammarSheetUnit[m, n];
             }
         }
     }
     return c.grammarSheetUnit[10, 0];//default情况,返回
 }
Exemplo n.º 10
0
 //语法分析表输出,输入为两个字符串,分别为左右栈的栈顶字符串,输出为Production类型的生成式的表达式
 public string Grammarsheet_Return(string a, string b, GrammarSheet c)
 {
     string prostring = "";
     for (int m = 0; m < 18; m++)
     {
         for (int n = 0; n < 25; n++)
         {
             if (c.grammarSheetColumn[m] == a && c.grammarSheetRow[n] == b)
             {//如果匹配成功,输出产生式
                 for (int i = 0; i < 10; i++)
                 {
                     if (c.grammarSheetUnit[m, n].productionSubstring[i] != null)
                         prostring = prostring + c.grammarSheetUnit[m, n].productionSubstring[i] + " ";
                 }
             }
         }
     }
     return prostring;
 }