//该函数对每个符号串进行语法分析,生成最终的操作表 public static List <Operation> Grammar(List <SignTable> rawOperations) { List <Operation> result = new List <Operation>(); foreach (SignTable raw in rawOperations) { //遇到赋值语句,则要分析出:左值是?右值是? if (raw[raw.size() - 1].id == "1") { int i = 0; for (; i < raw.size(); i++) { if (raw[i].id == "=") { break; } } if (i < raw.size()) { //等号之前的变量标记为左值 for (int j = i - 1; j >= 0; j--) { if (raw[j].type == "id") { raw[j].type = "结果"; } } //等号之后的变量标记位右值 for (int j = i + 1; j < raw.size(); j++) { if (raw[j].type == "id") { raw[j].type = "args"; } } } AssignOperation oper = new AssignOperation(raw); result.Add(oper); } //遇到函数定义语句,要分析出,函数名是?,变量是? else if (raw[raw.size() - 1].id == "2") { int i = 0; for ( ; i < raw.size(); i++) { if (raw[i].type == "保留字") {//标记上该id为函数名 raw[i + 1].type = "funcname"; } else if (raw[i].id == "(") { break; } } //把前括号到后括号的内容都标记为args for (int j = i + 1; raw[j].id != ")"; j++) { raw[j].type = "args"; } FunctionDefinationOpration oper = new FunctionDefinationOpration(raw); result.Add(oper); } //遇到循环的语句,要分析出,循环判断条件是?判断左值是?右值是? else { CirculateOperation oper = new CirculateOperation(); //分析括号之内的条件 bool isleft = true; //标记应当为左值,假如遇到不等号,切换为右值 for (int i = 2; raw[i].id != ")"; i++) { if (raw[i].type == "id") { if (isleft) { raw[i].type = "ConditionLeft"; oper.ConditionLeft = raw[i]; } else { raw[i].type = "ConditionRight"; oper.ConditionRight = raw[i]; } } else if (raw[i].type == "op") { isleft = !isleft; raw[i].type = raw[i].id; oper.type = raw[i]; } } result.Add(oper); } } return(result); }
//测试用 public void test() { //测试print函数 //FunctionBuilder foobuilder = Lex.TotalFunctionList.getFunctionBuilderByName("print"); //SignTable s = new SignTable(); //Sign a1 = new Sign(new DadaInt("", "12456"), "args"); //Sign a2 = new Sign(new DadaInt("", "12456"), "args"); //Sign a3 = new Sign(new DadaInt("", "12456"), "args"); //s.Add(a1); //s.Add(a1); //s.Add(a1); //Function f = foobuilder.build(s); //f.run(); /* * a=1; * while(a<100){ * a=a+1: * print(a); * } * */ //a=1;===================================== SignTable st1 = new SignTable(); st1.Add(new Sign("a", "结果")); st1.Add(new Sign(new DadaInt("", "1"))); AssignOperation ass = new AssignOperation(st1); //while(a<100) CirculateOperation cop = new CirculateOperation(); SignTable condition = new SignTable(); condition.Add(new Sign("a", "ConditionLeft")); condition.Add(new Sign("", "<")); condition.Add(new Sign(new DadaInt("", "1000"), "ConditionRight")); cop.setCondition(condition); //{ List <Operation> cb = new List <Operation>(); //a=a+1: SignTable st2 = new SignTable(); st2.Add(new Sign("a", "结果")); st2.Add(new Sign("a")); st2.Add(new Sign("", "+")); st2.Add(new Sign(new DadaInt("", "1"))); AssignOperation ass2 = new AssignOperation(st2); //print(a); FunctionBuilder print = Lex.TotalFunctionList.getFunctionBuilderByName("print"); SignTable st3 = new SignTable(); st3.Add(new Sign(print)); st3.Add(new Sign("a", "args")); AssignOperation ass3 = new AssignOperation(st3); //} cb.Add(ass2); cb.Add(ass3); cop.setCirculateBody(cb); //测试开始执行 ass.doSomethings(Lex.TotalSignList); cop.doSomethings(Lex.TotalSignList); }