[英]How to convert NFA/DFA to java?
在我设计NFA并使用JFLAP的情况下,我将其转换为DFA。
我需要知道,如何用Java编写代码?
基本上,如何在Java中实现这些状态转换。 我已经看到了一些使用switch和if语句执行此操作的示例,但是我看不到与DFA / NFA设计以及如何使用它在Java中实现有关。
如果您想在while(true)switch(state){...}上使用更多面向对象的设计
public class State{
private Map<Character,State> transitions=new HashMap<Character,State>();
public void addTransition(char ch,State st){
transitions.put(ch,st);
}
public State next(char ch){
return transitions.get(ch);
}
private boolean fin=false;
public boolean isFinal(){return fin;}
public boolean setFinal(boolean f){fin=f;}
}
然后循环将是
State currState=startState;
while(currState!=null && input.hasNextChar()){//you can also end directly when final state is reached
char next = input.nextChar();//get next character
currState = currState.next(next);
}
if(currState!=null && currState.isFinal()){
// reached final state
}else{
// to bad didn't match
}
该Java软件包包含带有Unicode字母(UTF16)的DFA / NFA(有限状态自动机)实现,并支持标准的正则表达式操作(连接,并集,Kleene星号)和许多非标准的操作(交集,补码,等等。)
尽管您现在已经实现了,但是有一个很好的实现,很容易理解。 使用Digraph可以保持epsilon转换并进行堆栈以跟踪表达式。 从RS NFA.java签出此链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.