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

📄 dfa.java

📁 这是一个用来解析文件从而得到一个DFA的设计的原理程序1
💻 JAVA
字号:
package logic;

import io.ReadFile;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;
import ui.MainBoard;

public class DFA {
	String input;
	int state;
	boolean isMatch = false;
	Pattern regex;
	Matcher matcher;
	boolean isMatched;
	String check_string = null;

	public DFA() {
		if (ReadFile.getInstance(MainBoard.filePathText.getText()).isDFA) {
			//use the regular expression to judge the input string
			check_string = "^[" + ReadFile.alphaList.get(0) + "|"
					+ ReadFile.alphaList.get(1) + "]*$";
			try {
				input = MainBoard.checkText.getText();
				regex = Pattern.compile(check_string);
				matcher = regex.matcher(input);
				isMatched = matcher.matches();
				if (!isMatched) {
					JOptionPane.showMessageDialog(null,
							"Invalid string to check !");
				} else {
					//get the trace of the string in the DFA transition
					MainBoard.trace = "(" + "0" + " , " + input + ")";
					try {
						while (!input.equals("")) {
							state = getNextState(state, input);
							input = input.substring(1);
							MainBoard.trace += " |- " + "("
									+ String.valueOf(state) + " , " + input
									+ ")";
						}
					} catch (Exception e) {
						JOptionPane.showMessageDialog(null,
								"Logic error with your DFA!");
					}
					//check the last alpha, whether it is the valid final states
					for (int i = 0; i < ReadFile.finalState.length; i++) {
						if (state == ReadFile.finalState[i]) {
							isMatch = true;
						}
					}

					if (isMatch) {
						JOptionPane.showMessageDialog(null, "String accepted!");
					} else {
						JOptionPane.showMessageDialog(null,
								"String not accepted!");
					}
				}
			} catch (Exception e) {
				// TODO: handle exception
				JOptionPane.showMessageDialog(null, "Invalid string to check");
			}
		} else {
			JOptionPane.showMessageDialog(null, "Invalid DFA file" + "\n"
					+ ReadFile.error);
		}
	}
	
	//the method to get the next state
	int getNextState(int state, String str) {
		int next_state = 0;
		next_state = ReadFile.getInstance(MainBoard.filePathText.getText()).tansition_map
				.get(state + str.substring(0, 1));
		return next_state;
	}

}

⌨️ 快捷键说明

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