如何将 NFA/DFA 转换为 java?

发布于 2024-12-10 01:26:59 字数 170 浏览 5 评论 0原文

我有一个场景,我设计了 NFA 并使用 JFLAP 将其转换为 DFA。

我需要知道如何用Java编写它?

基本上如何在 Java 中实现这些状态转换。我见过一些使用 switch 和 if 语句执行此操作的示例,但我看不到与 DFA/NFA 设计以及如何使用它在 Java 中实现的任何关系。

I have a scenario where I have designed the NFA and using JFLAP I have converted it to DFA.

I need to know, how to code it in Java?

Basically how to implement those state transitions in Java. I have seen some examples which do this using switch and if statements, but I can't see any relation to DFA/NFA design and how to use it to implement in Java.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮凉 2024-12-17 01:26:59

如果您想在 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
}

if you want to use a more object oriented design over 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;}        


}

and then the loop will be

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
}
遮了一弯 2024-12-17 01:26:59

看一下 dk.brics.automaton

此 Java 包包含使用 Unicode 字母表 (UTF16) 的 DFA/NFA(有限状态自动机)实现,并支持标准正则表达式操作(串联、并集、Kleene 星形)和许多非标准操作(交集) 、补码等)

Take a look at dk.brics.automaton:

This Java package contains a DFA/NFA (finite-state automata) implementation with Unicode alphabet (UTF16) and support for the standard regular expression operations (concatenation, union, Kleene star) and a number of non-standard ones (intersection, complement, etc.)

梦里泪两行 2024-12-17 01:26:59

虽然你现在已经实现了它,但是有一个非常好的实现,很容易理解。使用有向图来维护 epsilon 转换并使用堆栈来跟踪表达式。查看来自 RS NFA.java 的链接。

Although you would have implemented it by now but there is very good implementation which is easy to digest. Use Digraph to maintain the epsilon transitions and stack to keep track of expressions. Check out this link from RS NFA.java .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文