Java编译原理写算符优先算法


import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.UIManager;
public class Operator_Priority extends JFrame {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private char VN[]={'E','T','F','P'};//非终结符
	private char VT[]={'+','*','↑','i','(',')','#'};//终结符
	private String[] FirstVt=new String[VN.length];//单个非终结符的first集
	private String[] LastVt=new String[VN.length];
	private char M[][]=new char[VT.length][VT.length];
	
	private String G[]={"E->E+T|T","T->T*F|F","F->P↑F|P","P->(E)|i"};//文法
	private String G1[]={"E->E+T|T","T->T*F|F","F->P↑F|P","P->(E)|i","E->#E#"};
	private char s1[]=new char[50];
	
	
	
	
	public int IsInVn(char ch) {//判断字符是否属于非终结符
		int judge=999;
		for(int i=0;i...aQ的形式
				LastVt[IsInVn(s[0])]+=s[j-1];
			} else {
				if(IsInVn(s[j])!=999) {
					LastVt[IsInVn(s[0])]+=LastVt[IsInVn(s[0])]+LastVt[IsInVn(s[j])];
				}
			}
		}
}
	
	
	public void Get_LastVt() {//求解单个非终结符first集
		int i,j;
		for(i=0;i') {
				do {
					 Q=s1[j];
					if(IsInVt(s1[j-1])!=999) {
						j=j-1;
					} else {
						j=j-2;
					}
				} while(M[IsInVt(s1[j])][IsInVt(Q)]!='<');
				
				String s2="";
				for(h=j+1;h<=k;h++) {
					s2+=s1[h];
				}
				Print_S(k);
				N=Get_Vn(s2);
				k=j+1;
			    s1[k]=N;
			    Print_S(k);
			}
			
			if(M[IsInVt(s1[j])][IsInVt(s.charAt(i))]=='<'||M[IsInVt(s1[j])][IsInVt(s.charAt(i))]=='=') {
				k=k+1;
				s1[k]=s.charAt(i);
			} else {
				System.out.print("Error");
			}
			
		} while(s.charAt(i)!='#');
		
	}
	public void Print() {
		for(int i=0;i

Java编译原理写算符优先算法_第1张图片
 
 

你可能感兴趣的:(Java)