public class DFALexer { public static boolean dfaRecognize(Map<String, Object> dfa, String inputStr) { return true } public static Map<String, Object> json_file2map(String fileName) { JSONParser parser = new JSONParser(); Map<String, Object> dfa = new HashMap<>(); try { Object obj = parser.parse(new FileReader(fileName)); JSONObject dfaJson = (JSONObject) obj; List<Integer> endStates = new ArrayList<>(); JSONArray endStatesJson = (JSONArray) dfaJson.get("end"); for (Object stateObj : endStatesJson) { endStates.add(((Long) stateObj).intValue()); } List<String> symbols = new ArrayList<>(); JSONArray symbolsJson = (JSONArray) dfaJson.get("syms"); for (Object symbolObj : symbolsJson) { symbols.add((String) symbolObj); } List<List<Integer>> transitions = new ArrayList<>(); JSONArray transitionsJson = (JSONArray) dfaJson.get("transfer"); for (Object transitionObj : transitionsJson) { JSONArray transitionJson = (JSONArray) transitionObj; List<Integer> transition = new ArrayList<>(); for (Object valueObj : transitionJson) { if (valueObj instanceof String) { transition.add((int) ((String) valueObj).charAt(0)); } else { transition.add(((Long) valueObj).intValue()); } } transitions.add(transition); } dfa.put("end", endStates); dfa.put("start", ((Long) dfaJson.get("start")).intValue()); dfa.put("syms", symbols); dfa.put("transfer", transitions); } catch (Exception e) { e.printStackTrace(); } return dfa; }在这段代码实现DFA识别方法,要求:inpuStr被成功接收则返回true,不成功接收则返回false
时间: 2023-06-20 15:08:31 浏览: 190
JavaScript引用类型之基本包装类型实例分析【Boolean、Number和String】
public class DFALexer {
public static boolean dfaRecognize(Map<String, Object> dfa, String inputStr) {
int currentState = (int)dfa.get("start");
List<List<Integer>> transitions = (List<List<Integer>>)dfa.get("transfer");
List<Integer> endStates = (List<Integer>)dfa.get("end");
List<String> symbols = (List<String>)dfa.get("syms");
for (int i = 0; i < inputStr.length(); i++) {
char c = inputStr.charAt(i);
int symbolIndex = symbols.indexOf(String.valueOf(c));
if (symbolIndex == -1) {
return false;
}
currentState = transitions.get(currentState).get(symbolIndex);
}
return endStates.contains(currentState);
}
public static Map<String, Object> json_file2map(String fileName) {
JSONParser parser = new JSONParser();
Map<String, Object> dfa = new HashMap<>();
try {
Object obj = parser.parse(new FileReader(fileName));
JSONObject dfaJson = (JSONObject) obj;
List<Integer> endStates = new ArrayList<>();
JSONArray endStatesJson = (JSONArray) dfaJson.get("end");
for (Object stateObj : endStatesJson) {
endStates.add(((Long) stateObj).intValue());
}
List<String> symbols = new ArrayList<>();
JSONArray symbolsJson = (JSONArray) dfaJson.get("syms");
for (Object symbolObj : symbolsJson) {
symbols.add((String) symbolObj);
}
List<List<Integer>> transitions = new ArrayList<>();
JSONArray transitionsJson = (JSONArray) dfaJson.get("transfer");
for (Object transitionObj : transitionsJson) {
JSONArray transitionJson = (JSONArray) transitionObj;
List<Integer> transition = new ArrayList<>();
for (Object valueObj : transitionJson) {
if (valueObj instanceof String) {
transition.add((int) ((String) valueObj).charAt(0));
} else {
transition.add(((Long) valueObj).intValue());
}
}
transitions.add(transition);
}
dfa.put("end", endStates);
dfa.put("start", ((Long) dfaJson.get("start")).intValue());
dfa.put("syms", symbols);
dfa.put("transfer", transitions);
} catch (Exception e) {
e.printStackTrace();
}
return dfa;
}
}
需要注意的是,这里的DFA是由一个JSON文件转换而来的,JSON文件应该包含以下属性:
- start:DFA的起始状态
- end:DFA的终止状态
- syms:DFA的输入字符集
- transfer:DFA的转移函数,用二维数组表示
如果JSON文件格式有误,会抛出异常并返回空的DFA。
阅读全文