Ejemplo n.º 1
0
        public static void Production_Initialize(Production[] Production)
        {
            for (int i = 0; i < 36; i++)
                Production[i] = new Production();// C#中基本都要new来实例化才能使用

            //0、synch
            Production[0].productionSubstring[0] = "synch";
            //1、program->compoundstmt
            Production[1].productionSubstring[0] = "program";
            Production[1].productionSubstring[1] = "->";
            Production[1].productionSubstring[2] = "compoundstmt";
            //2、stmt->decl
            Production[2].productionSubstring[0] = "stmt";
            Production[2].productionSubstring[1] = "->";
            Production[2].productionSubstring[2] = "decl";
            //3、stmt->ifstmt
            Production[3].productionSubstring[0] = "stmt";
            Production[3].productionSubstring[1] = "->";
            Production[3].productionSubstring[2] = "ifstmt";
            //4、stmt->whilestmt
            Production[4].productionSubstring[0] = "stmt";
            Production[4].productionSubstring[1] = "->";
            Production[4].productionSubstring[2] = "whilestmt";
            //5、stmt->assgstmt
            Production[5].productionSubstring[0] = "stmt";
            Production[5].productionSubstring[1] = "->";
            Production[5].productionSubstring[2] = "assgstmt";
            //6、stmt->compoundstmt
            Production[6].productionSubstring[0] = "stmt";
            Production[6].productionSubstring[1] = "->";
            Production[6].productionSubstring[2] = "compoundstmt";
            //7、compoundstmt->{ stmts }
            Production[7].productionSubstring[0] = "compoundstmt";
            Production[7].productionSubstring[1] = "->";
            Production[7].productionSubstring[2] = "{";
            Production[7].productionSubstring[3] = "stmts";
            Production[7].productionSubstring[4] = "}";
            //8、stmts->stmt stmts
            Production[8].productionSubstring[0] = "stmts";
            Production[8].productionSubstring[1] = "->";
            Production[8].productionSubstring[2] = "stmt";
            Production[8].productionSubstring[3] = "stmts";
            //9、stmts->empty
            Production[9].productionSubstring[0] = "stmts";
            Production[9].productionSubstring[1] = "->";
            Production[9].productionSubstring[2] = "empty";
            //10、ifstmt->if ( boolexpr ) then stmt else stmt
            Production[10].productionSubstring[0] = "ifstmt";
            Production[10].productionSubstring[1] = "->";
            Production[10].productionSubstring[2] = "if";
            Production[10].productionSubstring[3] = "(";
            Production[10].productionSubstring[4] = "boolexpr";
            Production[10].productionSubstring[5] = ")";
            Production[10].productionSubstring[6] = "then";
            Production[10].productionSubstring[7] = "stmt";
            Production[10].productionSubstring[8] = "else";
            Production[10].productionSubstring[9] = "stmt";
            //11、whilestmt->while ( boolexpr ) stmt
            Production[11].productionSubstring[0] = "whilestmt";
            Production[11].productionSubstring[1] = "->";
            Production[11].productionSubstring[2] = "while";
            Production[11].productionSubstring[3] = "(";
            Production[11].productionSubstring[4] = "boolexpr";
            Production[11].productionSubstring[5] = ")";
            Production[11].productionSubstring[6] = "stmt";
            //12、assgstmt->ID = arithexpr;
            Production[12].productionSubstring[0] = "assgstmt";
            Production[12].productionSubstring[1] = "->";
            Production[12].productionSubstring[2] = "ID";
            Production[12].productionSubstring[3] = "=";
            Production[12].productionSubstring[4] = "arithexpr";
            Production[12].productionSubstring[5] = ";";
            //13、decl->type list;
            Production[13].productionSubstring[0] = "decl";
            Production[13].productionSubstring[1] = "->";
            Production[13].productionSubstring[2] = "type";
            Production[13].productionSubstring[3] = "list";
            Production[13].productionSubstring[4] = ";";
            //14、type->int
            Production[14].productionSubstring[0] = "type";
            Production[14].productionSubstring[1] = "->";
            Production[14].productionSubstring[2] = "int";
            //15、type->real
            Production[15].productionSubstring[0] = "type";
            Production[15].productionSubstring[1] = "->";
            Production[15].productionSubstring[2] = "real";
            //16、list->ID list1
            Production[16].productionSubstring[0] = "list";
            Production[16].productionSubstring[1] = "->";
            Production[16].productionSubstring[2] = "ID";
            Production[16].productionSubstring[3] = "list1";
            //17、list1->, list 
            Production[17].productionSubstring[0] = "list1";
            Production[17].productionSubstring[1] = "->";
            Production[17].productionSubstring[2] = ",";
            Production[17].productionSubstring[3] = "list";
            //18、list1->empty 
            Production[18].productionSubstring[0] = "list1";
            Production[18].productionSubstring[1] = "->";
            Production[18].productionSubstring[2] = "empty";
            //19、boolexpr->arithexpr boolop arithexpr
            Production[19].productionSubstring[0] = "boolexpr";
            Production[19].productionSubstring[1] = "->";
            Production[19].productionSubstring[2] = "arithexpr";
            Production[19].productionSubstring[3] = "boolop";
            Production[19].productionSubstring[4] = "arithexpr";
            //20、boolop-><
            Production[20].productionSubstring[0] = "boolop";
            Production[20].productionSubstring[1] = "->";
            Production[20].productionSubstring[2] = "<";
            //21、boolop->> 
            Production[21].productionSubstring[0] = "boolop";
            Production[21].productionSubstring[1] = "->";
            Production[21].productionSubstring[2] = ">";
            //22、boolop-><=  
            Production[22].productionSubstring[0] = "boolop";
            Production[22].productionSubstring[1] = "->";
            Production[22].productionSubstring[2] = "<=";
            //23、boolop->>=  
            Production[23].productionSubstring[0] = "boolop";
            Production[23].productionSubstring[1] = "->";
            Production[23].productionSubstring[2] = ">=";
            //24、boolop==
            Production[24].productionSubstring[0] = "boolop";
            Production[24].productionSubstring[1] = "->";
            Production[24].productionSubstring[2] = "==";
            //25、arithexpr->multexpr arithexprprime
            Production[25].productionSubstring[0] = "arithexpr";
            Production[25].productionSubstring[1] = "->";
            Production[25].productionSubstring[2] = "multexpr";
            Production[25].productionSubstring[3] = "arithexprprime";
            //26、arithexprprime->+ multexpr arithexprprime 
            Production[26].productionSubstring[0] = "arithexprprime";
            Production[26].productionSubstring[1] = "->";
            Production[26].productionSubstring[2] = "+";
            Production[26].productionSubstring[3] = "multexpr";
            Production[26].productionSubstring[4] = "arithexprprime";
            //27、arithexprprime->- multexpr arithexprprime 
            Production[27].productionSubstring[0] = "arithexprprime";
            Production[27].productionSubstring[1] = "->";
            Production[27].productionSubstring[2] = "-";
            Production[27].productionSubstring[3] = "multexpr";
            Production[27].productionSubstring[4] = "arithexprprime";
            //28、arithexprprime->empty
            Production[28].productionSubstring[0] = "arithexprprime";
            Production[28].productionSubstring[1] = "->";
            Production[28].productionSubstring[2] = "empty";
            //29、multexpr->simpleexpr  multexprprime
            Production[29].productionSubstring[0] = "multexpr";
            Production[29].productionSubstring[1] = "->";
            Production[29].productionSubstring[2] = "simpleexpr";
            Production[29].productionSubstring[3] = "multexprprime";
            //30、multexprprime->* simpleexpr multexprprime 
            Production[30].productionSubstring[0] = "multexprprime";
            Production[30].productionSubstring[1] = "->";
            Production[30].productionSubstring[2] = "*";
            Production[30].productionSubstring[3] = "simpleexpr";
            Production[30].productionSubstring[4] = "multexprprime";
            //31、multexprprime->/ simpleexpr multexprprime 
            Production[31].productionSubstring[0] = "multexprprime";
            Production[31].productionSubstring[1] = "->";
            Production[31].productionSubstring[2] = "/";
            Production[31].productionSubstring[3] = "simpleexpr";
            Production[31].productionSubstring[4] = "multexprprime";
            //32、multexprprime->empty
            Production[32].productionSubstring[0] = "multexprprime";
            Production[32].productionSubstring[1] = "->";
            Production[32].productionSubstring[2] = "empty";
            //33、simpleexpr->ID 
            Production[33].productionSubstring[0] = "simpleexpr";
            Production[33].productionSubstring[1] = "->";
            Production[33].productionSubstring[2] = "ID";
            //34、simpleexpr->NUM 
            Production[34].productionSubstring[0] = "simpleexpr";
            Production[34].productionSubstring[1] = "->";
            Production[34].productionSubstring[2] = "NUM";
            //35、simpleexpr->( arithexpr )
            Production[35].productionSubstring[0] = "simpleexpr";
            Production[35].productionSubstring[1] = "->";
            Production[35].productionSubstring[2] = "(";
            Production[35].productionSubstring[3] = "arithexpr";
            Production[35].productionSubstring[4] = ")";
        }
Ejemplo n.º 2
0
        //语法表初始化
        public void Grammarsheet_Initialize()
        {

            Production[] productionRule = new Production[36];			 //语法表达式
            //语法表达式初始化
            Production.Production_Initialize(productionRule);


            //初始化表格,先把表格中所有单元设为字符串error
            for (int m = 0; m < 18; m++)
                for (int n = 0; n < 25; n++)
                {
                    grammarSheetUnit[m, n] = new Production();
                    grammarSheetUnit[m, n].productionSubstring[0] = "error";
                }

            //初始化列
            grammarSheetColumn[0] = "program";
            grammarSheetColumn[1] = "stmt";
            grammarSheetColumn[2] = "compoundstmt";
            grammarSheetColumn[3] = "stmts";
            grammarSheetColumn[4] = "ifstmt";
            grammarSheetColumn[5] = "whilestmt";
            grammarSheetColumn[6] = "assgstmt";
            grammarSheetColumn[7] = "decl";
            grammarSheetColumn[8] = "type";
            grammarSheetColumn[9] = "list";
            grammarSheetColumn[10] = "list1";
            grammarSheetColumn[11] = "boolexpr";
            grammarSheetColumn[12] = "boolop";
            grammarSheetColumn[13] = "arithexpr";
            grammarSheetColumn[14] = "arithexprprime";
            grammarSheetColumn[15] = "multexpr";
            grammarSheetColumn[16] = "multexprprime";
            grammarSheetColumn[17] = "simpleexpr";
            //初始化行
            grammarSheetRow[0] = "{";
            grammarSheetRow[1] = "}";
            grammarSheetRow[2] = "(";
            grammarSheetRow[3] = ")";
            grammarSheetRow[4] = "if";
            grammarSheetRow[5] = "then";
            grammarSheetRow[6] = "else";
            grammarSheetRow[7] = "while";
            grammarSheetRow[8] = "int";
            grammarSheetRow[9] = "real";
            grammarSheetRow[10] = "ID";
            grammarSheetRow[11] = "NUM";
            grammarSheetRow[12] = ",";
            grammarSheetRow[13] = ";";
            grammarSheetRow[14] = "+";
            grammarSheetRow[15] = "-";
            grammarSheetRow[16] = "*";
            grammarSheetRow[17] = "/";
            grammarSheetRow[18] = "=";
            grammarSheetRow[19] = "<";
            grammarSheetRow[20] = ">";
            grammarSheetRow[21] = "<=";
            grammarSheetRow[22] = ">=";
            grammarSheetRow[23] = "==";
            grammarSheetRow[24] = "$";

            //把生成式填入其中
            //-------------------------------------第一行
            grammarSheetUnit[0, 0] = productionRule[1];
            grammarSheetUnit[0, 24] = productionRule[0];
            //-------------------------------------第二行
            grammarSheetUnit[1, 0] = productionRule[6];
            grammarSheetUnit[1, 4] = productionRule[3];
            grammarSheetUnit[1, 7] = productionRule[4];
            grammarSheetUnit[1, 8] = productionRule[2];
            grammarSheetUnit[1, 9] = productionRule[2];
            grammarSheetUnit[1, 10] = productionRule[5];
            grammarSheetUnit[1, 1] = productionRule[0];
            grammarSheetUnit[1, 6] = productionRule[0];
            //-------------------------------------第三行
            grammarSheetUnit[2, 0] = productionRule[7];
            grammarSheetUnit[2, 1] = productionRule[0];
            grammarSheetUnit[2, 4] = productionRule[0];
            grammarSheetUnit[2, 6] = productionRule[0];
            grammarSheetUnit[2, 7] = productionRule[0];
            grammarSheetUnit[2, 8] = productionRule[0];
            grammarSheetUnit[2, 9] = productionRule[0];
            grammarSheetUnit[2, 10] = productionRule[0];
            grammarSheetUnit[2, 24] = productionRule[0];
            //-------------------------------------第四行
            grammarSheetUnit[3, 0] = productionRule[8];
            grammarSheetUnit[3, 1] = productionRule[9];
            grammarSheetUnit[3, 4] = productionRule[8];
            grammarSheetUnit[3, 7] = productionRule[8];
            grammarSheetUnit[3, 8] = productionRule[8];
            grammarSheetUnit[3, 9] = productionRule[8];
            grammarSheetUnit[3, 10] = productionRule[8];
            //-------------------------------------第五行
            grammarSheetUnit[4, 4] = productionRule[10];
            grammarSheetUnit[4, 0] = productionRule[0];
            grammarSheetUnit[4, 1] = productionRule[0];
            grammarSheetUnit[4, 6] = productionRule[0];
            grammarSheetUnit[4, 7] = productionRule[0];
            grammarSheetUnit[4, 8] = productionRule[0];
            grammarSheetUnit[4, 9] = productionRule[0];
            grammarSheetUnit[4, 10] = productionRule[0];
            //-------------------------------------第六行
            grammarSheetUnit[5, 7] = productionRule[11];
            grammarSheetUnit[5, 0] = productionRule[0];
            grammarSheetUnit[5, 1] = productionRule[0];
            grammarSheetUnit[5, 4] = productionRule[0];
            grammarSheetUnit[5, 6] = productionRule[0];
            grammarSheetUnit[5, 8] = productionRule[0];
            grammarSheetUnit[5, 9] = productionRule[0];
            grammarSheetUnit[5, 10] = productionRule[0];
            //-------------------------------------第七行
            grammarSheetUnit[6, 10] = productionRule[12];
            grammarSheetUnit[6, 0] = productionRule[0];
            grammarSheetUnit[6, 1] = productionRule[0];
            grammarSheetUnit[6, 4] = productionRule[0];
            grammarSheetUnit[6, 6] = productionRule[0];
            grammarSheetUnit[6, 7] = productionRule[0];
            grammarSheetUnit[6, 8] = productionRule[0];
            grammarSheetUnit[6, 9] = productionRule[0];
            //-------------------------------------第八行
            grammarSheetUnit[7, 8] = productionRule[13];
            grammarSheetUnit[7, 9] = productionRule[13];
            grammarSheetUnit[7, 0] = productionRule[0];
            grammarSheetUnit[7, 1] = productionRule[0];
            grammarSheetUnit[7, 4] = productionRule[0];
            grammarSheetUnit[7, 6] = productionRule[0];
            grammarSheetUnit[7, 7] = productionRule[0];
            grammarSheetUnit[7, 10] = productionRule[0];
            //-------------------------------------第九行
            grammarSheetUnit[8, 8] = productionRule[14];
            grammarSheetUnit[8, 9] = productionRule[15];
            grammarSheetUnit[8, 10] = productionRule[0];
            //-------------------------------------第十行
            grammarSheetUnit[9, 10] = productionRule[16];
            grammarSheetUnit[9, 13] = productionRule[0];
            //-------------------------------------第十一行
            grammarSheetUnit[10, 12] = productionRule[17];
            grammarSheetUnit[10, 13] = productionRule[18];
            //-------------------------------------第十二行
            grammarSheetUnit[11, 2] = productionRule[19];
            grammarSheetUnit[11, 10] = productionRule[19];
            grammarSheetUnit[11, 11] = productionRule[19];
            grammarSheetUnit[11, 1] = productionRule[0];
            //-------------------------------------第十三行
            grammarSheetUnit[12, 19] = productionRule[20];
            grammarSheetUnit[12, 20] = productionRule[21];
            grammarSheetUnit[12, 21] = productionRule[22];
            grammarSheetUnit[12, 22] = productionRule[23];
            grammarSheetUnit[12, 23] = productionRule[24];
            grammarSheetUnit[12, 2] = productionRule[0];
            grammarSheetUnit[12, 10] = productionRule[0];
            grammarSheetUnit[12, 11] = productionRule[0];
            //-------------------------------------第十四行
            grammarSheetUnit[13, 2] = productionRule[25];
            grammarSheetUnit[13, 10] = productionRule[25];
            grammarSheetUnit[13, 11] = productionRule[25];
            grammarSheetUnit[13, 19] = productionRule[0];
            grammarSheetUnit[13, 20] = productionRule[0];
            grammarSheetUnit[13, 21] = productionRule[0];
            grammarSheetUnit[13, 22] = productionRule[0];
            grammarSheetUnit[13, 23] = productionRule[0];
            //-------------------------------------第十五行
            grammarSheetUnit[14, 3] = productionRule[28];
            grammarSheetUnit[14, 13] = productionRule[28];
            grammarSheetUnit[14, 14] = productionRule[26];
            grammarSheetUnit[14, 15] = productionRule[27];
            grammarSheetUnit[14, 19] = productionRule[28];
            grammarSheetUnit[14, 20] = productionRule[28];
            grammarSheetUnit[14, 21] = productionRule[28];
            grammarSheetUnit[14, 22] = productionRule[28];
            grammarSheetUnit[14, 23] = productionRule[28];
            //-------------------------------------第十六行
            grammarSheetUnit[15, 2] = productionRule[29];
            grammarSheetUnit[15, 10] = productionRule[29];
            grammarSheetUnit[15, 11] = productionRule[29];
            grammarSheetUnit[15, 3] = productionRule[0];
            grammarSheetUnit[15, 13] = productionRule[0];
            grammarSheetUnit[15, 14] = productionRule[0];
            grammarSheetUnit[15, 15] = productionRule[0];
            grammarSheetUnit[15, 19] = productionRule[0];
            grammarSheetUnit[15, 20] = productionRule[0];
            grammarSheetUnit[15, 21] = productionRule[0];
            grammarSheetUnit[15, 22] = productionRule[0];
            grammarSheetUnit[15, 23] = productionRule[0];
            //-------------------------------------第十七行
            grammarSheetUnit[16, 3] = productionRule[32];
            grammarSheetUnit[16, 13] = productionRule[32];
            grammarSheetUnit[16, 14] = productionRule[32];
            grammarSheetUnit[16, 15] = productionRule[32];
            grammarSheetUnit[16, 16] = productionRule[30];
            grammarSheetUnit[16, 17] = productionRule[31];
            grammarSheetUnit[16, 19] = productionRule[32];
            grammarSheetUnit[16, 20] = productionRule[32];
            grammarSheetUnit[16, 21] = productionRule[32];
            grammarSheetUnit[16, 22] = productionRule[32];
            grammarSheetUnit[16, 23] = productionRule[32];
            //-------------------------------------第十八行
            grammarSheetUnit[17, 2] = productionRule[35];
            grammarSheetUnit[17, 10] = productionRule[33];
            grammarSheetUnit[17, 11] = productionRule[34];
            grammarSheetUnit[17, 3] = productionRule[0];
            grammarSheetUnit[17, 13] = productionRule[0];
            grammarSheetUnit[17, 14] = productionRule[0];
            grammarSheetUnit[17, 15] = productionRule[0];
            grammarSheetUnit[17, 16] = productionRule[0];
            grammarSheetUnit[17, 17] = productionRule[0];
            grammarSheetUnit[17, 19] = productionRule[0];
            grammarSheetUnit[17, 20] = productionRule[0];
            grammarSheetUnit[17, 21] = productionRule[0];
            grammarSheetUnit[17, 22] = productionRule[0];
            grammarSheetUnit[17, 23] = productionRule[0];
        }