📄 readfile.java
字号:
package io;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ReadFile {
int numOfState = 0;
public static Integer finalState[] = null;
public static ArrayList<String> alphaList = new ArrayList<String>();
static HashMap<String, Integer> alpha_map = new HashMap<String, Integer>();
public HashMap<String, Integer> tansition_map = new HashMap<String, Integer>();
static String fileName;
File dfaFile;
BufferedReader reader;
boolean isMatch = true;
public boolean isDFA = true;
public static String error = "";
private static ReadFile instance = null;
/* 这里使用了singleton实例化 */
public static ReadFile getInstance(String fileName) {
if (instance == null)
instance = new ReadFile(fileName);
return instance;
}
public ReadFile(String fileName) {
dfaFile = new File(fileName);
reader = null;
try {
reader = new BufferedReader(new FileReader(dfaFile));
String tempString = null;
Pattern regex;
Matcher matcher;
boolean isMatched;
//use regular expressions to judge if the input files are valid
String check_numOfState = "^\\d+$";
String check_finalState = "^[\\d\\s]+$";
String check_alpha = "^[a-z]{2}$";
String check_transition = "^\\d+\\-\\d+\\-\\w$";
try {
// first line
if (isDFA) {
tempString = reader.readLine();
regex = Pattern.compile(check_numOfState);
matcher = regex.matcher(tempString);
isMatched = matcher.matches();
if (isMatched) {
numOfState = Integer.parseInt(tempString);
} else {
error = "Invalid state number in " + tempString + "\n";
isDFA = false;
}
}
// second line
if (isDFA) {
tempString = reader.readLine();
regex = Pattern.compile(check_finalState);
matcher = regex.matcher(tempString);
isMatched = matcher.matches();
if (isMatched) {
finalState = new Integer[tempString.split(" ").length];
for (int i = 0; i < tempString.split(" ").length; i++) {
finalState[i] = Integer.parseInt(tempString
.split(" ")[i]);
if (finalState[i] >= numOfState) {
error += "Too big of final states in "
+ tempString + "\n";
isDFA = false;
}
}
} else {
error += "Invalid final states in " + tempString + "\n";
isDFA = false;
}
}
// third line
if (isDFA) {
tempString = reader.readLine();
regex = Pattern.compile(check_alpha);
matcher = regex.matcher(tempString);
isMatched = matcher.matches();
if (isMatched && isDFA) {
for (int i = 0; i < tempString.length(); i++) {
alphaList.add(tempString.substring(i, i + 1));
alpha_map.put(tempString.substring(i, i + 1), i);
}
} else {
error += "Invalid alpha in " + tempString + "\n";
isDFA = false;
}
}
// judge the following lines of the transition
if (isDFA) {
while ((tempString = reader.readLine()) != null) {
if (isDFA) {
regex = Pattern.compile(check_transition);
matcher = regex.matcher(tempString);
isMatched = matcher.matches();
if (!isMatched) {
error += "Invalid transition form" + tempString
+ "\n";
isDFA = false;
}
}
int start_state = 0;
if (isDFA) {
start_state = Integer.parseInt(tempString
.split("-")[0]);
if (start_state >= numOfState) {
error += "Too big of start state in "
+ tempString + "\n";
isDFA = false;
}
}
int end_state = 0;
if (isDFA) {
end_state = Integer
.parseInt(tempString.split("-")[1]);
if (end_state >= numOfState) {
error += "Too big of end state in "
+ tempString + "\n";
isDFA = false;
}
}
String alpha = null;
if (isDFA) {
alpha = tempString.split("-")[2];
if (alpha_map.get(alpha) == null) {
error += "Error in dfa file: invalid alpha in "
+ tempString + "\n";
isMatch = false;
isDFA = false;
}
}
tansition_map.put(start_state + alpha, end_state);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -