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

📄 compiler.java

📁 上海复旦大学计算机系学子写的JAVA版本编译原理词法、语法分析程序(LR0)
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
							//row--;
							int temp = i;
							i = find(i,content);
							row += (i - temp);
							state = 0;
						}
						
						break;		
					case 12://标志符是 #
						int id1 = i;
						int id2, id3;
						String id = "";
						while(c != '<'){
							id += c;
							i++;
							c = content.charAt(i);
						}
						id2 = i + 1;
						if(id.trim().equals("include")){
							programStr.add("#include<");
							styleStr.add("#include<");
							while(c != '>' && ( c != '\n')){
					    			i++;
								c = content.charAt(i);
							}
							id3 = i;
							if(c == '>'){
								error_text.append("info: 头文件引入文法 \n");
								programStr.add(content.substring(id2, id3));
								styleStr.add("C");
								programStr.add(">");
								styleStr.add(">");
							}
						}else
							error_text.appendText("error: " + "line " + line + ", row " + row + " 语法错误\n");
						
						state = 0;
						break;
					case 13://检测指数表示方式
						if(c == '+' || c == '-' || isDigit(c)){
							i++;
							c = content.charAt(i);
							while(isDigit(c)){
								i++;
								c = content.charAt(i);
							}
							if(isLetter(c) || c == '.'){
								error_text.appendText("error; line " + line + " row " + row + " 指数格式错误!\n");
								state = 0;
								int temp = i;
								i = find(i,content);
								row += (i - temp);
								
							}else{
								end = i;
								programStr.add(content.substring(begin, end));
								styleStr.add("K");
								i--;
								error_text.appendText("info: 指数 " + content.substring(begin, end) + '\n');
							}
							state = 0;
						}
						break;
					case 14:// 逻辑运算发 &&
						if(c == '&'){
							error_text.appendText("info: 逻辑AND运算符 '&&' \n");
							/***new**/
							programStr.add("&&");
							styleStr.add("&&");
						}
						else{
							i--;
							error_text.appendText("info: & 不明运算符. \n");
						}
						state = 0;
						break;
					case 15:// 逻辑运算发 ||
						if(c == '|'){
							error_text.appendText("info: 逻辑OR运算符 '||' \n");
							/***new**/
							programStr.add("||");
							styleStr.add("||");
						}
						else{
							i--;
							error_text.appendText("info: '|' 不明运算符. \n");
						}
						state = 0;
						break;
					case 16:
						error_text.appendText("info: 引号 " + '"' + '\n');
						/***new**/
						programStr.add('"'+"");
						styleStr.add('"'+"");
						int id_1 = i;
						i++;
						while(content.charAt(i)!='"')
							i++;
						int id_2 = i;
						String str =content.substring(id_1, id_2);
						programStr.add(str);
						styleStr.add("Q");
						error_text.appendText("字符串: " + str + '\n');
						programStr.add('"'+"");
						styleStr.add('"'+"");
						error_text.appendText("info: 引号 " + '"' + '\n');
						//i--;
						state = 0;
						break;
					
				}
			}
		}
		error_text.appendText("Have checked lexical! \n");
		
		test();
		
	}
	boolean isLetter(char c){
		if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_')
			return true;
		return false;
	}
	boolean isDigit(char c){
		if(c >= '0' && c <= '9') return true;
		return false;
	}
	boolean isKey(String str){
		if(str.equals("int") || str.equals("real") || str.equals("for") || str.equals("while") || str.equals("do") || str.equals("begin") || str.equals("end") || str.equals("if") || str.equals("then")||
				str.equals("AND") || str.equals("OR") || str.equals("NOT") || str.equals("repeat") || str.equals("until") || str.equals("read") || str.equals("write") || str.equals("return") || str.equals("true")
				|| str.equals("false") || str.equals("boolean") || str.equals("program") || str.equals("const") || str.equals("to"))
			return true;
		return false;
	}
	int find(int begin, String str){//寻找分隔符空格、括号、回车等。
		if(begin >= str.length())
			return str.length();
		for(int i = begin; i < str.length(); i++){
			char c = str.charAt(i);
			if(c == '\n' || c == ',' || c == ' ' || c == '\t' || c == '{' || c =='}' || c == '(' || c == ')' || c == ';' || c == '=' || c == '+'|| c == '-' || c == '*' || c == '/') 
				return i - 1;	
		}
		return str.length();
	}
	/*
	 * 测试styleStr programStr[]内容是否符合要求!
	 * */
	void test(){
		String str = "";
		for(int i = 0; i < styleStr.size(); i++)
			str += '{' + (String)(programStr.get(i)) + ", " + (String)(styleStr.get(i)) + '}';
		System.out.println(str);
	}
	/*********************************/
	/*   对经过语法分析的程序进行语法分析   */
	/*********************************/
	void check_syntax(){
		styleStr.add("@");//添加结束条件
		if(styleStr.size() == 1){
			error_text.appendText("空栈!无内容!");
			return;
		}
		int index = 0;
		int state = 0;
		stateStack.add(""+state);
		//String str = ((String)styleStack.get(index));	
		boolean jmp = true;
		while(index < styleStr.size()){
			/********************************************/
			/****************输出本时刻状态信息**************/
			/********************************************/
			String str = ((String)styleStr.get(index)); 
			error_text.appendText("State : " + state + " ");
			for(int i = 0; i < styleStack.size(); i++)
				error_text.appendText((String)styleStack.get(i)+" ");
			error_text.appendText("   ");
			for(int i = 0; i < stateStack.size(); i++)
				error_text.appendText((String)stateStack.get(i)+" ");
			error_text.appendText("\n");
			/***************************************/
			switch( state ){
				case -1:
					error_text.appendText(("Error info: case -1 " + stateStack.get(stateStack.size() - 1) + " " + styleStack.get(styleStack.size() - 1)));
					jmp = false;
					break;
				case 0:
					
					if(str.equals("#include<")){
						state = 3;
						styleStack.add(styleStr.get(index));
						stateStack.add(""+state);
						index++;
					}else{
						error_text.appendText(("Error info: case 0 " + stateStack.get(stateStack.size() - 1) + " " +  styleStack.get(styleStack.size() - 1)));
						jmp = false;
					}
					break;
				case 1:
					
					if(styleStr.get(index).equals("@")){
						error_text.appendText("Syntax all right!" + '\n');
						index++;
					}else{
						error_text.appendText(("Error info: case 1" + stateStack.get(stateStack.size() - 1) + " " +  styleStack.get(styleStack.size() - 1)));
						
					}
					jmp = false;
					break;
				case 2:
					
					if(styleStr.get(index).equals("type")){
						state = 5;
						styleStack.add(styleStr.get(index));
						stateStack.add(""+state);
						index++;
					}else {
						error_text.appendText(("Error info: case 2 " + stateStack.get(stateStack.size() - 1) + " " +  styleStack.get(styleStack.size() - 1)));
						jmp = false;
					}
					break;
				case 3:
					
					if(styleStr.get(index).equals("C")){
						state = 6;
						styleStack.add(styleStr.get(index));
						stateStack.add(""+state);
						index++;
					}else{
						error_text.appendText(("Error info: case 3 " + stateStack.get(stateStack.size() - 1) + " " +  styleStack.get(styleStack.size() - 1)));
						jmp = false;
					}
					break;
				case 4:
					
					if(styleStr.get(index).equals("@")){
						styleStack.remove(styleStack.size() - 1);
						stateStack.remove(stateStack.size() - 1);
						styleStack.remove(styleStack.size() - 1);
						stateStack.remove(stateStack.size() - 1);
						styleStack.add("S");
						stateStack.add(""+gotoState(Integer.parseInt((String)stateStack.get(stateStack.size() - 1)) ,"S"));
						state = Integer.parseInt(((String)stateStack.get(stateStack.size() - 1)));
					}else{
						error_text.appendText(("Error info: case 4 " + stateStack.get(stateStack.size() - 1) + " " +  styleStack.get(styleStack.size() - 1)));
						jmp = false;
					}
					
					break;
				case 5:
					
					if(styleStr.get(index).equals("C")){
						state = 7;
						styleStack.add(styleStr.get(index));
						stateStack.add(""+state);
						index++;
					}else{
						error_text.appendText(("Error info: case 5" + stateStack.get(stateStack.size() - 1) + " " +  styleStack.get(styleStack.size() - 1)));
						jmp = false;
					}
					break;
				case 6:
					
					if(styleStr.get(index).equals(">")){
						state = 8;
						styleStack.add(styleStr.get(index));
						stateStack.add(""+state);
						index++;
					}else{
						error_text.appendText(("Error info: case 6 " + stateStack.get(stateStack.size() - 1) + " " +  styleStack.get(styleStack.size() - 1)));
						jmp = false;
					}
					break;
				case 7:
					
					if(styleStr.get(index).equals("(")){
						state = 9;
						styleStack.add(styleStr.get(index));
						stateStack.add(""+state);
						index++;
					}else{
						error_text.appendText(("Error info: case 7 " + stateStack.get(stateStack.size() - 1) + " " +  styleStack.get(styleStack.size() - 1)));
						jmp = false;
					}
					break;
				case 8:
					
					if(styleStr.get(index).equals("type")){
						styleStack.remove(styleStack.size() - 1);
						stateStack.remove(stateStack.size() - 1);
						styleStack.remove(styleStack.size() - 1);
						stateStack.remove(stateStack.size() - 1);
						styleStack.remove(styleStack.size() - 1);
						stateStack.remove(stateStack.size() - 1);
						styleStack.add("H");
						stateStack.add("" + gotoState(Integer.parseInt((String)stateStack.get(stateStack.size() - 1)) ,"H"));
						state = Integer.parseInt(((String)stateStack.get(stateStack.size() - 1)));
					}else if(styleStr.get(index).equals("#include<")){
						state = 3;
						styleStack.add(styleStr.get(index));
						stateStack.add(""+state);
						index++;
					}
					else{
						error_text.appendText(("Error info: case 8 " + stateStack.get(stateStack.size() - 1) + " " +  styleStack.get(styleStack.size() - 1)));
						jmp = false;
					}
					break;
				case 9:
					
					if(styleStr.get(index).equals("Z")){
						state = 11;
						styleStack.add(styleStr.get(index));
						stateStack.add(""+state);
						index++;
					}else if(styleStr.get(index).equals("type")){
						state = 12;
						styleStack.add(styleStr.get(index));
						stateStack.add(""+state);
						index++;
					}else if(styleStr.get(index).equals(")")){
						/****************************/
						/**********Z --> 空的规约 *****/
						/****************************/
						styleStack.add("Z");
						stateStack.add(""+gotoState(Integer.parseInt((String)stateStack.get(stateStack.size() - 1)) ,"Z"));
						state = Integer.parseInt(((String)stateStack.get(stateStack.size() - 1)));
					}else{
						error_text.appendText(("Error info: case 9 " + stateStack.get(stateStack.size() - 1) + " " +  styleStack.get(styleStack.size() - 1)));
						jmp = false;
					}
					break;
				case 10:
					
					if(styleStr.get(index).equals("type")){//规约
						styleStack.remove(styleStack.size() - 1);
						stateStack.remove(stateStack.size() - 1);
						styleStack.remove(styleStack.size() - 1);
						stateStack.remove(stateStack.size() - 1);
						styleStack.remove(styleStack.size() - 1);
						stateStack.remove(stateStack.size() - 1);
						styleStack.remove(styleStack.size() - 1);
						stateStack.remove(stateStack.size() - 1);
						styleStack.add("H");
						stateStack.add(""+gotoState(Integer.parseInt((String)stateStack.get(stateStack.size() - 1)) ,"H"));
						state = Integer.parseInt(((String)stateStack.get(stateStack.size() - 1)));
					}else{
						jmp = false;
						error_text.appendText(("Error info: case 10" + stateStack.get(stateStack.size() - 1) + " " +  styleStack.get(styleStack.size() - 1)));
					}
					break;
				case 11:
					
					if(styleStr.get(index).equals(")")){
						state = 13;
						styleStack.add(styleStr.get(index));
						stateStack.add(""+state);
						index++;
					}else{
						jmp = false;
						error_text.appendText(("Error info: case 11 " + stateStack.get(stateStack.size() - 1) + " " +  styleStack.get(styleStack.size() - 1)));
					}
					break;
				case 12:
					
					if(styleStr.get(index).equals("C")){
						state = 14;
						styleStack.add(styleStr.get(index));
						stateStack.add(""+state);
						index++;
					}else{
						jmp = false;
						error_text.appendText(("Error info: case 12 " + stateStack.get(stateStack.size() - 1) + " " +  styleStack.get(styleStack.size() - 1)));
					}
					break;

⌨️ 快捷键说明

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