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

📄 themain.java

📁 不好意思
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			 * if (c == 'i') return 2;
			 */
			if (string.equals("{"))
				return 3;
			if (string.equals("}"))
				return 4;
			if (string.equals("("))
				return 5;
			if (string.equals(")"))
				return 6;
			if (string.equals("+"))
				return 7;
			if (string.equals("-"))
				return 8;
			if (string.equals("*"))
				return 9;
			if (string.equals("/"))
				return 10;
			if (string.equals(">"))
				return 11;
			if (string.equals("="))
				return 12;
			if (string.equals("<"))
				return 13;
			if (string.equals(";"))
				return 14; // $ 代表空产生式
			else
				return 2;//如果不是以上所有情况的话,那么就是标识符,则定位为2

		}

		int getBig() {//为栈顶的每一个元素进行定位,即为其编号,用来与输入的变量进行比较
			if (cs.peek().equals("S"))
				return 0;
			if (cs.peek().equals("G"))
				return 1;
			if (cs.peek().equals("R"))
				return 2;
			if (cs.peek().equals("B"))
				return 3;
			if (cs.peek().equals("T"))
				return 4;
			if (cs.peek().equals("E"))
				return 5;
			if (cs.peek().equals("F"))
				return 6;
			if (cs.peek().equals("d"))
				return 0;
			if (cs.peek().equals("w"))
				return 1;
			if (cs.peek().equals("i"))
				return 2;
			if (cs.peek().equals("{"))
				return 3;
			if (cs.peek().equals("}"))
				return 4;
			if (cs.peek().equals("("))
				return 5;
			if (cs.peek().equals(")"))
				return 6;
			if (cs.peek().equals("+"))
				return 7;
			if (cs.peek().equals("-"))
				return 8;
			if (cs.peek().equals("*"))
				return 9;
			if (cs.peek().equals("/"))
				return 10;
			if (cs.peek().equals(">"))
				return 11;
			if (cs.peek().equals("="))
				return 12;
			if (cs.peek().equals("<"))
				return 13;
			if (cs.peek().equals(";"))
				return 14; // $ 代表空产生式
			else
				return 15;
		}

		int match() {//如果栈顶元素为一个非终结符,则返回一个0,若为终结符,则返回1
			if (cs.peek().equals("S") || cs.peek().equals("G")
					|| cs.peek().equals("R") || cs.peek().equals("B")
					|| cs.peek().equals("T") || cs.peek().equals("E")
					|| cs.peek().equals("F"))
				return 0;
			else
				return 1;
		}

		void YJudge() {//开始语法分析
			cs.push("#");
			
			int judge = this.match();//judge用来判断栈顶元素,是否为终结符或非终结符
			
			while (count <= text1.getText().length()) {//只要没有取完,则继续取下去
				if (string.equals("do")) {//如果找到关键字do,则继续往下做,否则,查找下个变量

					for (int i = 0; i < s[0][0].length(); i++) {//将对应二维数组中的内容以
						cs.push(s[0][0].substring(i, i + 1));//字符的形式入栈
					}

					cs.pop();//将第一个字符出栈,因为已经匹配do关键字了
					string = this.getString(count);//取下一个
					judge = this.match();//判断栈顶元素的形式
					
					while (cs.peek() != "#") {//只要栈顶元素不是#,则继续
						while (judge == 1) {//如果栈顶是终结符
							if (this.getBig() == this.getLittle()) {//通过”定位系统“,查看其是否相等
								cs.pop();//相等则出栈,并取下个字符
								judge = this.match();
								
								string = this.getString(count);

							} else if (cs.peek() != "#") {//不匹配则出错
								text2.append("程序中不含有正确的DO-WHILE循环1!" + "\n");
								return;
							} else {
								text2.append("程序中含有正确的DO-WHILE循环1!" + "\n");
								return;
							}

						}
						while (judge == 0) {//如果栈顶为非终结符
							if (this.getBig() == 3 && this.getLittle() == 1//因为文法中,B能推导出空产生式,因此特殊对待
									|| this.getBig() == 3
									&& this.getLittle() == 13
									|| this.getBig() == 3
									&& this.getLittle() == 6
									|| this.getBig() == 3
									&& this.getLittle() == 11
									|| this.getBig() == 3
									&& this.getLittle() == 12
									|| this.getBig() == 3
									&& this.getLittle() == 14) {
								
								cs.pop();
								judge = this.match();
							} else if (!s[this.getBig()][this.getLittle()]
									.equals("@")) {//否则,查找预测分析表中对应的位置是否有值
													//即是否不为 @,若不是,则继续,是则出错
								int k = this.getBig();//用两个变量分别保存数组下标的值,因为后面会发生变化
								int m = this.getLittle();
								for (int i = 0; i < s[k][m].length(); i++) {

									if (i == 0) {//将栈顶元素取出
										cs.pop();
									}

									
									cs.push(s[k][m].substring(i, i + 1));//并将对应的值以字符形式,存入栈中
									

								}
								judge = this.match();

							} else if (cs.peek() != "#") {//出错
								text2.append("程序中不含有正确的DO-WHILE循环2!" + "\n");
								return;
							}
						}

					}

				} else {//如果不是DO关键字,则取下一个变量
					string = this.getString(count);
				}
			}
		}

	}

	class Display {//输出三地址形式
		char c = getChar(0);

		int count = 0;
		

		int k = 0;

		String output = new String();

		public boolean isLetter() {//以下几个方法也同词法分析
			if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
				return true;
			else
				return false;
		}

		public boolean isNumber() {//同上
			if (c >= '0' && c <= '9')
				return true;
			else
				return false;

		}

		public boolean isOperate() {//同上
			Operater oper = new Operater();
			for (int i = 0; i < oper.y; i++) {
				if (c == oper.op[i])
					return true;
			}
			return false;

		}

		public boolean isFenJie() {//同上
			if (c == ';') {

				return true;
			} else
				return false;

		}

		public char getChar(int i) {//同上
			char[] ch = text1.getText().toCharArray();
			if (i < (text1.getText().length())) {
				c = ch[i];
		
				i++;
			}
			return c;
		}

		public String getString(int m) {//此处的getString()同语法分析
			String output = new String();
			c = getChar(m);
			if (isLetter() || isNumber()) {
				while (isLetter() || isNumber()) {
					output = output + c;
					c = getChar(++m);
					count = m;

				}
				return output;
		/*	} else if (isNumber()) {
				while (isNumber()) {
					output = output + c;
					c = getChar(++m);
					count = m;

				}
				return output;*/
			} else if (isOperate()) {
				output = "" + c;
				count = m + 1;
				// c = getChar();
				return output;
			} else if (isFenJie()) {
				output = "" + c;
				count = m + 1;
				// c = getChar();
				return output;
			} else
				return "";
		}
		
		

		String precede(String op1, String op2) { // 判断优先级程序段
			if (op1.equals("+") || op1.equals("-")) {
				if (op2.equals("+") || op2.equals("-") || op2.equals(")")
						|| op2.equals(";"))
					return ">";
				else
					return "<";
			} else if (op1.equals("*") || op1.equals("/")) {
				if (op2.equals("("))
					return "<";
				else
					return ">";
			} else if (op1.equals("(")) {
				if (op2.equals(")"))
					return "=";
				else if (op2.equals(";"))
					return "ERROR$";
				else
					return "<";
			} else if (op1.equals(")")) {
				if (op2.equals("("))
					return "ERROR(";
				else
					return ">";
			} else if (op1.equals(";")) {
				if (op2.equals(";"))
					return "=";
				else if (op2.equals(")"))
					return "ERROR)";
				else
					return "<";
			}
			return "ERROR优先级错误";
		}

		int tnum = 0;

		String string = this.getString(count);
		/*String string1 = this.getString(count1);
		
		while(string*/

		public String operate(String a, String thera, String b) {//输出三地址的方法

			tnum++;
			String t1 = "t" + tnum;
			text2.append(t1 + "=" + a + thera + b + "\n");

			return t1;
		}

		int judge() {//为每个符号进行定位操作

			if (string.equals("+"))
				return 0;
			if (string.equals("-"))
				return 1;
			if (string.equals("*"))
				return 2;
			if (string.equals("/"))
				return 3;
			if (string.equals(";"))
				return 4; 
			if(string.equals("{"))
				return 6;
			if(string.equals("}"))
				return 7;
			if(string.equals("="))
				return 8;
			else              //若都不是以上的符号,则为标识符,定位为5
				return 5;

		}

		public void EvaluateExpression() {//开始进行分析,用的是以前数据结果中的内容
			
			Stack<String> OPTR, OPND;//定义两个栈,数据栈和符号栈
			String a, b, optr, theta;
			OPTR = new Stack<String>();
			OPTR.push(";");//以分号做为结束标志
			OPND = new Stack<String>();
			optr = (String) OPTR.peek();

			
			while (!string.equals("=")) {//将光标定位在等号后面
				string = this.getString(count);
				
				 if(this.judge() == 5) {
					head4 = string;
				}
			}
			string = this.getString(count);
			//OPTR.push(string);
			while (!optr.equals(";") || !string.equals(";")) {//判别条件
				if (this.judge() == 5) {//如果不是符号,则进数据栈
					OPND.push(string);
					System.out.println(OPND.peek());
					string = this.getString(count);
				}
				else if (precede(optr, string).equals("<")) {//符号栈顶元素的优先级小,则进栈
					OPTR.push(string);
					optr = (String)OPTR.peek();
					System.out.println(OPTR.peek());
					string = this.getString(count);

				} else if (precede(optr, string).equals("=")) {//如果相等,则取出
					OPTR.pop();
					string = this.getString(count);
				} else if (precede(optr, string).equals(">")) {//如果大于,进行出栈操作
					theta = (String) OPTR.pop();
					b = (String) OPND.pop();
					a = (String) OPND.pop();
					System.out.println(b);
					System.out.println(a);
					head5 = operate(a,theta,b);
					OPND.push(head5);
					System.out.println(OPND.peek());
					
				}
				optr = (String) OPTR.peek();
				

			}
			while(!string.equals("(")){
				string = this.getString(count);
			}
			head1 = this.getString(count);
			head2 = this.getString(count);
			head3 = this.getString(count);
			//head5 = optr;
			
		}

	}
}

⌨️ 快捷键说明

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