日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

非确定有限状态自动机的构建(一)——NFA的定义和实现

發(fā)布時間:2024/6/21 综合教程 51 生活家
生活随笔 收集整理的這篇文章主要介紹了 非确定有限状态自动机的构建(一)——NFA的定义和实现 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

保留版權(quán),轉(zhuǎn)載需注明出處(http://blog.csdn.net/panjunbiao)。

非確定有限狀態(tài)自動機(Nondeterministic Finite Automata,NFA)由以下元素組成:

一個有限的狀態(tài)集合S
一個輸入符號集合Sigma,并且架設(shè)空字符epsilon不屬于Sigma
一個狀態(tài)遷移函數(shù),對于所給的每一個狀態(tài)和每一個屬于Sigma或{epsilon}的符號,輸出遷移狀態(tài)的集合。
一個S中的狀態(tài)s0作為開始狀態(tài)(初始狀態(tài))
S的一個子集F,作為接受狀態(tài)(結(jié)束狀態(tài))

例如,我們給定:

S={s0, s1, s2, s3, s4}
Sigma={a, b}
狀態(tài)遷移函數(shù)T,且T(s0, a} = {s1}, T(s1, a) = {s2}, T(s2, b) = {s3}, T(s3, b) = {s4}
s0為開始狀態(tài)
{s4}為接受狀態(tài)

這樣我們就得到一個很簡單的NFA,它可以用圖來表示,如下圖圖1:

NFA是一個識別器,例如圖1所示的NFA,我們從狀態(tài)s0開始,按順序輸入aabb,在輸入第一個符號a之后,狀態(tài)將從s0遷移到s1,輸入第二個符號a之后,狀態(tài)遷移到s2,輸入第三個符號b之后,狀態(tài)遷移到s3,輸入第四個符號b之后,狀態(tài)遷移到s4,而s4是接收狀態(tài),也就是說對我們剛才輸入的aabb字符串說yes,表明本NFA識別了所輸入的字符串。

所謂非確定,是指在某個狀態(tài)輸入同一個符號,狀態(tài)可以遷移到不同的下一個狀態(tài),例如圖2,在s0處輸入字符a,狀態(tài)既可以遷移為s1,也可以遷移為s3,準確的說是狀態(tài)遷移到了{s1,s3},因此圖2所示的NFA能夠接受的字符串包括aa和ab。

另外,NFA的特點還在于空符號也能進行狀態(tài)遷移,例如圖3的s0,不需要任何輸入字符就可以遷移到s1,因此圖3的NFA可以識別的語言為*a*b,即0到任意多個a,接著0到任意多個b。

NFA可以識別的語言與正則表達式所表達的語言是等價的,參考
http://en.wikipedia.org/wiki/Nondeterministic_finite_automaton

那么,NFA如何實現(xiàn)呢?我們先來看看NFA狀態(tài)節(jié)點的一種實現(xiàn):

/*
    This file is one of the component a Context-free Grammar Parser Generator,
    which accept a piece of text as the input, and generates a parser
    for the inputted context-free grammar.
    Copyright (C) 2013, Junbiao Pan (Email: panjunbiao@gmail.com)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package automata;

import java.util.*;

public class NFAState implements Comparable<NFAState> {
    private static int COUNT = 0;

    //狀態(tài)標識,每個NFA狀態(tài)節(jié)點都有唯一的數(shù)值標識
    private int id;

    public int getId() { return this.id; }

    //在創(chuàng)建NFA狀態(tài)對象的時候,通過靜態(tài)變量生成唯一標識
    public NFAState() {
        this.id = COUNT ++;
    }

    //遷移函數(shù),由于遷移函數(shù)需要兩個輸入:當(dāng)前狀態(tài)和輸入符號,因此在一個狀態(tài)對象內(nèi)部,
    //遷移函數(shù)都是針對本對象的,只需要輸入符號就可以了,這里通過Map接口實現(xiàn)遷移函數(shù)
    protected Map<Integer, Set<NFAState>> transition = new HashMap<Integer, Set<NFAState>>();
    public Map<Integer, Set<NFAState>> getTransition() { return this.transition; }

    //空字符遷移函數(shù),即從當(dāng)前節(jié)點經(jīng)過空字符輸入所能夠到達的下一個狀態(tài)節(jié)點
    protected Set<NFAState> epsilonTransition = new HashSet<NFAState>();
    public Set<NFAState> getEpsilonTransition() { return this.epsilonTransition; }

    //向遷移函數(shù)添加一個映射,不給定下一個狀態(tài)節(jié)點
    public NFAState addTransit(int input) {
        return addTransit(input, new NFAState());
    }

    //向遷移函數(shù)添加一個映射,給定下一個狀態(tài)節(jié)點
    public NFAState addTransit(int input, NFAState next) {
        Set<NFAState> states = this.transition.get(input);
        if (states == null) {
            states = new HashSet<NFAState>();
            this.transition.put(input, states);
        }
        states.add(next);
        return next;
    }

    //向遷移函數(shù)添加一個映射,不給定下一個狀態(tài)節(jié)點
    public NFAState addTransit(char input) {
        return addTransit(input, new NFAState());
    }

    //向遷移函數(shù)添加一個映射,給定下一個狀態(tài)節(jié)點
    //假定我們的上下文無關(guān)文法是大小寫不敏感的,當(dāng)輸入字符是char類型并且是字母時,
    //生成大寫字母和小寫字母兩個映射
    public NFAState addTransit(char input, NFAState next) {
        if (Character.isLetter(input)) {
            this.addTransit((int) (Character.toUpperCase(input)), next);
            this.addTransit((int)(Character.toLowerCase(input)), next);
            return next;
        }
        this.addTransit((int)input, next);
        return next;
    }

    //添加一個空字符的映射
    public NFAState addTransit(NFAState next) {
        this.epsilonTransition.add(next);
        return next;
    }

    //返回遷移函數(shù)
    public Set<NFAState> getTransition(int input) {
        return this.transition.get(input);
    }

}

再來看看NFA的實現(xiàn):

/*
    This file is one of the component a Context-free Grammar Parser Generator,
    which accept a piece of text as the input, and generates a parser
    for the inputted context-free grammar.
    Copyright (C) 2013, Junbiao Pan (Email: panjunbiao@gmail.com)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package automata;

import java.util.*;

import abnf.CharVal;
import abnf.NumVal;
import abnf.AbnfParser;
import abnf.RangedNumVal;
import abnf.Repeat;
import abnf.Repetition;
import abnf.Rule;
import abnf.RuleName;

public class NFA {
    //開始狀態(tài)startState
    private NFAState startState = null;
    public NFAState getStartState() { return startState; }

    //接收狀態(tài)acceptingStates
    private Set<NFAState> acceptingStates = new HashSet<NFAState>();
    public Set<NFAState> getAcceptingStates() { return acceptingStates; }
    public boolean accept(NFAState state) {
        return this.acceptingStates.contains(state);
    }
    public void addAcceptingState(NFAState state) {
        this.acceptingStates.add(state);
    }

    public NFA() {
        this(new NFAState(), new NFAState());
    }

    public NFA(NFAState startState) {
        this(startState, new NFAState());
    }

    public NFA(NFAState startState, NFAState acceptingState) {
        this.startState = startState;
        this.addAcceptingState(acceptingState);
    }

    //在上面的NFAState類實現(xiàn)中,新的狀態(tài)節(jié)點是在添加遷移映射的過程中生成的,
    //這個過程中NFA并沒有介入,因此NFA類不能直接得到狀態(tài)集S的成員
    //而是需要從狀態(tài)startState開始,不斷迭代找出所有的狀態(tài)節(jié)點
    protected void getStateSet(NFAState current, Set<NFAState> states) {
        if (states.contains(current)) return;
        states.add(current);

        Iterator<NFAState> it;

        it = current.getNextStates().iterator();
        while (it.hasNext()) {
            this.getStateSet(it.next(), states);
        }

        it = current.getEpsilonTransition().iterator();
        while (it.hasNext()) {
            this.getStateSet(it.next(), states);
        }

    }

    public Set<NFAState> getStateSet() {
        Set<NFAState> states = new HashSet<NFAState>();
        this.getStateSet(this.getStartState(), states);
        return states;
    }

}

這樣,我們可以從NFA類中獲得一個NFA的開始狀態(tài)startState和接受狀態(tài)集合acceptingStates,在每一個狀態(tài)節(jié)點NFAState中可以獲得狀態(tài)遷移函數(shù),因此NFA所定義的各個元素都實現(xiàn)了。

總結(jié)

以上是生活随笔為你收集整理的非确定有限状态自动机的构建(一)——NFA的定义和实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。