Java 语法分析器LR0

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Stack;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableCellRenderer;

public class LR0 extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private char VN[]={'S','E','T','F'};//非终结符
	private char VT[]={'+','*','(',')','i','$','ε'};//终结符
	private char Total[]={'S','E','T','F','+','*','(',')','i','$'};
	/*private char Total[]={'S','E','C','c','d','$'};
	private char VN[]={'S','E','C'};//非终结符
	private char VT[]={'c','d','$','ε'};//终结符*/
	private String M[][]=new String[22][22];
	private String M1[][]=new String[22][22];
	private int N[][]=new int[22][22];
	private int Goto[][]=new int[22][Total.length];
	private String ACtion[][]=new String[22][VT.length-1];
	private boolean Visited[]=new boolean[22];
	private String G[]={"S->E","E->E+T","E->T","T->T*F","T->F","F->(E)","F->i"};//文法
	//private String G[]={"S->E","E->CC","C->cC","C->d"};//文法
	
	//建立表格
		private Object[] colname = {"步骤","分析栈","分析符号","剩余字符串","动作"};
		private Object[][] data=new Object[50][5];
		private JTable table;
		private JTextField j3;
		private JLabel j8;
		private JLabel j9;
		private JLabel j10;
		private JTextArea j5;
		private JScrollPane jsPan;
		private JScrollPane jsPane;
		private File imgFile = null;// 声明所选择文件
		private BufferedReader in1;
		private GridBagLayout layout;
	    private GridBagConstraints s;
	    private int h = 0;
	    private String getstr=null;
	    private int ONE_SECOND = 1000;
	    private String DEFAULT_TIME_FORMAT = "HH:mm:ss";
	    private String time;
		
	  //获取系统的日期
		public String Get_Date() {
			 Date date = new Date();    
			 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
		     String dateNowStr = sdf.format(date);  
		     return dateNowStr;
		}
		
		public void Get_Time() {
			 
		     Timer tmr = new Timer();
		     tmr.scheduleAtFixedRate(new JLabelTimerTask(),new Date(), ONE_SECOND);
		}
		
		//将表格清空
				public void Get_Clear() {
					int i;
					for(i=0;i<50;i++) {
						data[i]=new Object[]{"","","","",""};
					}
				}
				
	
	public void Init() {//初始化follow集,将#放到开始符号集中去
		M[0][0]="S->.E";
		int i=0,j=0;
		for(i=0;i<22;i++) {
			for(j=0;j<22;j++) {
				N[i][j]=0;
			}
		}
	/*	for(i=0;i<22;i++) {
			for(j=0;j<22;j++) {
				M1[i][j]="love";
			}
		}*/
		M1[0][0]="S->.E";
		
		for(i=0;i<22;i++) {
			Visited[i]=false;
		}
	}
	
	public int IsInTotal(char c) {//判断是否在总的字符中
		int i;
		int locate=999;
		for(i=0;iA.Bb,a]
			if(M[I][i]!=null) {//如果字符串非空
				j=3;
				while(M[I][i].charAt(j)!='.'&&jA.Bb,a]
			if(str[i]!=null) {//如果字符串非空
				j=3;
				while(str[i].charAt(j)!='.'&&j stack) {
		return stack.toString();
	}
	
	
		//输出堆栈中的内容
				public String Print_Stack1(Stack stack) {
					String s=stack.toString();
					int i;
					StringBuilder t=new StringBuilder();
					for(i=0;i stack = new Stack(); // 创建堆栈对象 
		Stack stack1 = new Stack();
		stack.push(0);
		while(true) {
			int s=stack.peek();
			if(ACtion[s][IsInVt(getstr.charAt(i))].charAt(0)=='s') {
				
				System.out.print(Print_Stack(stack));
				System.out.print("         ");
				System.out.print(Print_Stack1(stack1));
				System.out.print("         ");
				System.out.print(Print_Buffer(getstr,i));
				System.out.println();
				
        		data[h++]=new Object[]{h,Print_Stack(stack),Print_Stack1(stack1),Print_Buffer(getstr,i),"移入"};
        		
				stack1.push(getstr.charAt(i));
				stack.push(Integer.parseInt(ACtion[s][IsInVt(getstr.charAt(i))].substring(1,ACtion[s][IsInVt(getstr.charAt(i))].length())));
				i++;
			} else {
				if(ACtion[s][IsInVt(getstr.charAt(i))].charAt(0)=='r') {
					
					System.out.print(Print_Stack(stack));
					System.out.print("         ");
					System.out.print(Print_Stack1(stack1));
					System.out.print("         ");
					System.out.print(Print_Buffer(getstr,i));
					System.out.print("         ");
					
					j=Integer.parseInt(ACtion[s][IsInVt(getstr.charAt(i))].substring(1,ACtion[s][IsInVt(getstr.charAt(i))].length()));
					data[h++]=new Object[]{h,Print_Stack(stack),Print_Stack1(stack1),Print_Buffer(getstr,i),"根据"+G[j]+"归约"};
					k=Get_Number(j);
					while(k>0) {
						stack.pop();
						stack1.pop();
						k--;
					}
					stack1.push(G[j].charAt(0));
					t=stack.peek();
					stack.push(Goto[t][IsInTotal(G[j].charAt(0))]);
					System.out.print(G[j]);
					System.out.println();
				} else {
					if(ACtion[s][IsInVt(getstr.charAt(i))].equals("acc")) {
						System.out.print(Print_Stack(stack));
						System.out.print("         ");
						System.out.print(Print_Stack1(stack1));
						System.out.print("         ");
						System.out.print(Print_Buffer(getstr,i));
						System.out.print("         ");
						System.out.print(G[0]);
						System.out.println();
						data[h++]=new Object[]{h,Print_Stack(stack),Print_Stack1(stack1),Print_Buffer(getstr,i),"接受"};
						break;
					} else {
						System.out.println("ERROR");
					}
				}
				
			}
				
		}
		
	}
	
	public void Print1() {
		for(int i=0;i<22;i++) {
			for(int j=0;j<22;j++) {
				if(M[i][j]!=null){
					System.out.print(i+ " "+j+M[i][j]+" ");
			}
		}
			System.out.println();
	}
	}
	
	public void Print2() {
		for(int i=0;i<22;i++) {
			for(int j=0;j

你可能感兴趣的:(Java)