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

📄 re2enfa.java

📁 Regular Expression to Epsilon-NFA
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    			}
    			
			 
    		}else if(Re.charAt(i)== '|'){
    			n= Final.getLast();
    			m= Final.get(Final.size()-2);
    			String t = list.getLast();
    			
    			/*.......... s4--(e)-->s5   and   s2--(e)--->s5 .....*/
    			for(int j=0 ; j<t.length(); j++){
    				if(t.charAt(j)=='e'){
    					if(t.charAt(j+1)== 'n'){
    						list.set(n,list.getLast().substring(0,(j+1))+(n+1));  // snsnen > snsne5
    						list.set(m,list.get(m).substring(0,(j+1))+(n+1));    						    							    							
    					}else{    					 						
    							list.set(n,list.getLast()+"e"+(n+1)); // snsne2 > snsne2e5
    							list.set(m,list.get(m)+"e"+(n+1));    				
    					}
    					break;
    				}	
    			}
    			
    			list.add("sn_sn_en");
    			
    			if(Final.size()==3){
    				list.add(0,"sn_sn_e1");
    				shiftList(1);    				
    			}else{
    				list.add(Final.size()-2,"sn_sn_e"+(m));
    				shiftList(Final.size()-1);
    			}
    			
    		    			    			    			
    			Final.set((Final.size()-1),(list.size()-1));
    			Final.set(Final.size()-2,Final.get(Final.size()-2)+1);
    			if(Final.size()==3){    			
    				list.set(0,list.getFirst()+"e"+(Final.get(1)+1));  
    			}else{
    				list.set(Final.get(Final.size()-3)+1,list.get(Final.get(Final.size()-3)+1)+"e"+Final.getLast());      				
    			}    			
    			Final.remove(Final.size()-2);    			    			
    		}
    	}
    	
    
    	System.out.println("::::::::::::::::::::::::::::::::::::::");
    	System.out.println("state at__input=0__input=1__epsilon");
    	for(int i= 0; i<list.size() ;i++){
    			System.out.println("s"+i+" is "+list.get(i));
    		}
    	System.out.println("::::::::::::::::::::::::::::::::::::::");
    }
    
    public void shiftList(int m){ 
    	String plusValue="";
    	while(m!=list.size()){   			
    		String l = list.get(m);
    		for(int i=0; i<l.length() ; i++ ){
    			if(l.charAt(i)=='s'|| l.charAt(i)=='e'){
    				int a=i;
    				if(l.charAt(i+1)!='n'){
    					for(int j=i ; j<l.length()-1; j++){
    						if(l.charAt(j+1)== '_' || l.charAt(j+1)=='e' || l.charAt(j+1)== 's'){					
    							break;
    						}else{
    							plusValue += l.charAt(j+1);
    							a++;
							}
    					}
    					list.set(m,list.get(m).substring(0,(i+1)) + (Integer.parseInt(plusValue)+1)+list.get(m).substring(a+1,l.length()));
    					a=i;
    					plusValue="";
    				}
    			}
    		}
    		m++;
    	}    	  
    }        
}

class InfixToPostfix {
	private StringBuffer postfix = new StringBuffer();
	private String repostfix;	
	public InfixToPostfix(){
	}
	public InfixToPostfix(String RE){
		char []exp = newRE(RE).toCharArray();
		postfix(exp);
		newPostfix(getPostfix());
	}
	public String newRE(String RE){ 	//  put . in RE
		StringBuffer newInfix = new StringBuffer();
		for(int i =0; i <RE.length() ;i++){
			if(i < (RE.length()-1)){ 	// protect ErrorOutArray
				if(RE.charAt(i)==')' && RE.charAt(i+1)=='('){			// case : )+( 
					newInfix.append(RE.substring(i,i+1)+".");
				}
				else if(RE.charAt(i)==')' && ID(RE.charAt(i+1))==0){			// case : )+binary like )0 or )1
					newInfix.append(RE.substring(i,i+1)+".");
				} 
				else if(RE.charAt(i)==')' && ID(RE.charAt(i+1))==4){			// case : ){ and )[
					newInfix.append(RE.substring(i,i+1)+".");
				} 											
				else if( ID(RE.charAt(i))== 0 && ID(RE.charAt(i+1)) == 0){ 			// case: binary+binary like 00, 10
					newInfix.append(RE.substring(i,i+1)+".");
				}
				else if(ID(RE.charAt(i))==0 && RE.charAt(i+1)=='('){			// case; binary+( like 0( or 1(
					newInfix.append(RE.substring(i,i+1)+".");
				}
				else if(ID(RE.charAt(i))==0 && ID(RE.charAt(i+1)) == 4){			// case; binary+[ or binary+{ like 0[ or 1{
					newInfix.append(RE.substring(i,i+1)+".");
				}
				else if(ID(RE.charAt(i))== 1 && ID(RE.charAt(i+1)) == 0){ 		// case; regex+binary  like ?0 ,+0
					newInfix.append(RE.substring(i,i+1)+".");
				}
				else if(ID(RE.charAt(i))== 1 && RE.charAt(i+1)== '('){  		// case: regex+( like *(
					newInfix.append(RE.substring(i,i+1)+".");
				}
				else if(ID(RE.charAt(i))== 1 && ID(RE.charAt(i+1)) == 4){  		// case: regex+[ or regex+{ like *(
					newInfix.append(RE.substring(i,i+1)+".");
				}
				else if(ID(RE.charAt(i))== 5 && RE.charAt(i+1)=='('){			// case : ]( and }(
					newInfix.append(RE.substring(i,i+1)+".");
				}
				else if(ID(RE.charAt(i))== 5 && ID(RE.charAt(i+1))==4){			// case : ]{, ][,}[ and}{
					newInfix.append(RE.substring(i,i+1)+".");
				}
				else if(ID(RE.charAt(i))== 5 && ID(RE.charAt(i+1))==0){			// case : ]+binary or }+binary like ]0 or }1
					newInfix.append(RE.substring(i,i+1)+".");
				}
				else{
					newInfix.append(RE.substring(i,i+1));						// othercase
				}
			}
			else{ newInfix.append(RE.substring(i,i+1)); 						// push last char RE
			}			
		}
		System.out.println("New RE = "+new String(newInfix));
		return new String(newInfix);
	}
    public void postfix(char[] c) {
		Stack<String> st = new Stack<String>();
		for(int i =0 ; i<c.length ;i++){
			if(c[i] == '('){  					// push ( in stack
				st.push("(");
			}
			else if(ID(c[i])== 0){  		// send binary to postfix
				postfix.append(c[i]);
			}
			else if(ID(c[i])==1){  		// send these reg to postfix . Can check reg in ID() method 
				postfix.append(c[i]);
			}
			else if (ID(c[i])==2){		// push . in stack
				st.push(".");
			}
			else if(ID(c[i])==3){	 		// find |
				
				while(st.lastElement()!="("){
					postfix.append(st.pop()); // send data of top stack to postfix when top stack isn't (
				}			 	
				st.push("|");					// push | in stack	
			}
			else if(c[i] == ')'){ 
				while(st.lastElement()!= "("){	 		
					postfix.append(st.pop());  // send  data  to postfix and stop send it when top stack is ( 
				}
				if(st.lastElement()=="("){  	// when found ( , delete ( on top stack
					st.pop();
				}
			}
			else if(ID(c[i])== 4){
				if(c[i]=='[')
					st.push("[");
				if(c[i] == '{')
					st.push("{");
			}
			else if(ID(c[i])== 5){
				if(c[i] == ']'){
					while(st.lastElement()!= "["){	 		
						postfix.append(st.pop());  // send  data  to postfix and stop send it when top stack is [ 
					}
					if(st.lastElement()=="[" ){  	// when found [ on stack  
						postfix.append("[]");    	// print []
						st.pop();					// delete [ on stack
					}
				}
				if(c[i] =='}'){
					while(st.lastElement() != "{"){ 
						postfix.append(st.pop());   // send data to postfix and stop send it when top stack is {
					}
					if(st.lastElement()=="{"){		// when found [ on stack
						postfix.append("{}");		// print {}
						st.pop();					// delete [ on stack
					}	
				}
			
			}
			else if(ID(c[i])==6){
				while(st.lastElement() != "{"){		
					postfix.append(st.pop());		// send data of top stack to postfix when top stack isn't {
				}
				st.push(",");						// push , in stack
			}
			else if(ID(c[i])==7){
				while(st.lastElement() != "["){		
					postfix.append(st.pop());		// send data of top stack to postfix when top stack isn't [
				}
				st.push("-");						// push - in stack
			}
			else{}// for update new case
			if(i == c.length-1){			// print all stack
				while(!st.empty())
				postfix.append(st.pop());
			}			
		}
    }
    public  String getPostfix(){
    	return new String(postfix);
    }  
    public void newPostfix(String postfix){ // deleate[  and { 
    	repostfix="";
        for(int i=0; i<postfix.length();i++){
        	if(postfix.charAt(i)!= '{' && postfix.charAt(i)!='['){
        		repostfix+= postfix.charAt(i);
        	}
        }
    }
    public String getNewPostfix(){
    	return repostfix;
    }
    public int ID(char c){  	
		if( (c == '0'|| c== '1') || c=='^'|| c=='E'){
    		return 0;
    	}else if( c== '*' || c== '?' || c == '+') {
    		return 1;
    	}else if( c== '.'){
    		return 2;
    	}else if( c== '|'){                               
    		return 3;
    	}else if( c== '{' || c == '['){                               
    		return 4;
    	}else if( c== ']' || c== '}'){                               
    		return 5;
    	}else if(c==','){
    		return 6;
    	}else if(c=='-'){
    		return 7;		
    	}	
    	else{return 8;}   // update new case
    	
    }    
}

class SVG {
	private int DFA[][] ;
	private String circle;
	private String transport;
	private String start="";
	private int initx =50;
	private int inity =300;
	private double px,py;
	LinkedList<Integer> word = new LinkedList<Integer>();
    public SVG() {
    }
    
    public SVG(String t, String RE){
    	text(t);
    	drawCircle(getArray());
    	String end ="</svg>";
    	write("RE_to_e-NFA.svg",(getStart()+functionRE("RE ="+RE)+drawCircle(getArray())+drawTransport(getArray())+end));

    }
    public void writeStart(int maxStat){
    	start="<?xml version=\"1.0\"?> \n <svg  xmlns=\"http://www.w3.org/2000/svg\" "
    				+"width=\""+(500+ (maxStat-5)*100)+"\" height=\"600\"  >\n"
    				+"<defs>\n"
    				+"<marker id=\"transport\" refX=\"5\" refY=\"2\" markerUnits=\"userSpaceOnUse\" markerWidth=\"10\" markerHeight=\"10\" orient=\"auto\">\n"
    				+"<path class=\"edge\" d=\"M0 0 5 2 0 4z\"/>\n"
    				+"</marker>\n"
    				+"</defs>" ; 	
    }
    public String getStart(){
    	return start;
    }
    public void	text(String t){
    					
			
    	/////////// create sizeDFA /////////////
    	int row=0;
    	for(int i=0; i<t.length();i++){
    		if(t.charAt(i)=='\n'){
    			row++;
    		}
    	}
    	DFA = new int[row][5];
    	/////////////////////////////////////////
    	writeStart(row);
    	

		/////////// insert data in array//////////
    	row= 0; //reset row
    	int col =0 ;
    	int i=0;
    	word.add(0);
    	for(int j=0 ; j < t.length() ; j++){
    		if(t.charAt(j)== ','|| t.charAt(j)=='\n'){    			
    			word.add(j);
    		}
    	}
    	
    	while(i<word.size()-1){
    		if(col != 1){
    			if(word.get(i)==0){
    				if(t.charAt(word.get(i))== 'n'){
    					DFA[row][col] = 0;
    				}else{    				
	 					DFA[row][col] = Integer.parseInt(t.substring(word.get(i),word.get(i+1)))+1;
    				}
 				}else{
 					if(t.charAt(word.get(i)+1)=='n'){
    					DFA[row][col] = 0;		
    				}else{
 						DFA[row][col] = Integer.parseInt(t.substring(word.get(i)+1,word.get(i+1)))+1;
    				}
 				}
    		}else{
    			if(word.get(i)==0){
    				if(t.charAt(word.get(i))== 'n'){
    					DFA[row][col] = 0;
    				}else{
 						DFA[row][col] = Integer.parseInt(t.substring(word.get(i),word.get(i+1)));
    				}
 				}else{
 					if(t.charAt(word.get(i)+1)=='n'){
    					DFA[row][col] = 0;		
    				}else{
 						DFA[row][col] = Integer.parseInt(t.substring(word.get(i)+1,word.get(i+1)));
    				}
 				}
    		}
 			
    		
    		//System.out.println(row+","+col+" "+DFA[row][col]);

⌨️ 快捷键说明

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