非确定有限状态自动机的构建(一)——NFA的定义和实现
保留版权,转载需注明出处(http://blog.csdn.net/panjunbiao)。
非确定有限状态自动机(Nondeterministic Finite Automata,NFA)由以下元素组成:
- 一个有限的状态集合S
- 一个输入符号集合Sigma,并且架设空字符epsilon不属于Sigma
- 一个状态迁移函数,对于所给的每一个状态和每一个属于Sigma或{epsilon}的符号,输出迁移状态的集合。
- 一个S中的状态s0作为开始状态(初始状态)
- S的一个子集F,作为接受状态(结束状态)
- S={s0, s1, s2, s3, s4}
- Sigma={a, b}
- 状态迁移函数T,且T(s0, a} = {s1}, T(s1, a) = {s2}, T(s2, b) = {s3}, T(s3, b) = {s4}
- s0为开始状态
- {s4}为接受状态
http://en.wikipedia.org/wiki/Nondeterministic_finite_automaton
/*
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; //状态标识,每个NFA状态节点都有唯一的数值标识
private int id; public int getId() { return this.id; } //在创建NFA状态对象的时候,通过静态变量生成唯一标识
public NFAState() {
this.id = COUNT ++;
} //迁移函数,由于迁移函数需要两个输入:当前状态和输入符号,因此在一个状态对象内部,
//迁移函数都是针对本对象的,只需要输入符号就可以了,这里通过Map接口实现迁移函数
protected Map<Integer, Set<NFAState>> transition = new HashMap<Integer, Set<NFAState>>();
public Map<Integer, Set<NFAState>> getTransition() { return this.transition; } //空字符迁移函数,即从当前节点经过空字符输入所能够到达的下一个状态节点
protected Set<NFAState> epsilonTransition = new HashSet<NFAState>();
public Set<NFAState> getEpsilonTransition() { return this.epsilonTransition; } //向迁移函数添加一个映射,不给定下一个状态节点
public NFAState addTransit(int input) {
return addTransit(input, new NFAState());
} //向迁移函数添加一个映射,给定下一个状态节点
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;
} //向迁移函数添加一个映射,不给定下一个状态节点
public NFAState addTransit(char input) {
return addTransit(input, new NFAState());
} //向迁移函数添加一个映射,给定下一个状态节点
//假定我们的上下文无关文法是大小写不敏感的,当输入字符是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;
} //返回迁移函数
public Set<NFAState> getTransition(int input) {
return this.transition.get(input);
} }
再来看看NFA的实现:
/*
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 {
//开始状态startState
private NFAState startState = null;
public NFAState getStartState() { return startState; } //接收状态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类实现中,新的状态节点是在添加迁移映射的过程中生成的,
//这个过程中NFA并没有介入,因此NFA类不能直接得到状态集S的成员
//而是需要从状态startState开始,不断迭代找出所有的状态节点
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的开始状态startState和接受状态集合acceptingStates,在每一个状态节点NFAState中可以获得状态迁移函数,因此NFA所定义的各个元素都实现了。
非确定有限状态自动机的构建(一)——NFA的定义和实现的更多相关文章
- 非确定有限状态自动机的构建(二)——将CharVal转换为NFA
保留版权,转载注明出处:潘军彪的个人博客(http://blog.csdn.net/panjunbiao/article/details/9378933) 将上下文无关文法读入内存之后,可以将它转换成 ...
- K:有限状态自动机
有限状态自动机是一种特殊的状态机.它表示有限个状态以及在这些状态之间的转移和动作等行为的数学模型.有限状态自动机分为两种,一种是 确定有限状态自动机(DFA) ,一种是 非确定有限状态自动机(NF ...
- <轻量算法>根据核密度估计检测波峰算法 ---基于有限状态自动机和递归实现
原创博客,转载请联系博主! 希望我思考问题的思路,也可以给大家一些启发或者反思! 问题背景: 现在我们的手上有一组没有明确规律,但是分布有明显聚簇现象的样本点,如下图所示: 图中数据集是显然是个3维的 ...
- Trie 前缀树或字典树 确定有限状态自动机
https://zh.wikipedia.org/wiki/Trie 应用 trie树常用于搜索提示.如当输入一个网址,可以自动搜索出可能的选择.当没有完全匹配的搜索结果,可以返回前缀最相似的可能.[ ...
- 简聊DFA(确定性有限状态自动机)
状态机理论最初的发展在数字电路设计领域.而在软件设计领域,状态机设计的理论俨然已经自成一体. 状态机是软件编程中的一个重要概念,比这个概念更重要的是对它的灵活应用.在一个思路清晰而且高效的程序中,必然 ...
- 用C语言实现有限状态自动机FSM
摘要:状态机模式是一种行为模式,在<设计模式>这本书中对其有详细的描述,通过多态实现不同状态的调转行为的确是一种很好的方法,只可惜在嵌入式环境下,有时只能写纯C代码,并且还需要考虑代码的重 ...
- DFA确定有限状态自动机
DFA 在计算理论中,确定有限状态自动机或确定有限自动机(英语:deterministic finite automaton, DFA)是一个能实现状态转移的自动机.对于一个给定的属于该自动机的状态和 ...
- 【Codeforces 506E】Mr.Kitayuta’s Gift&&【BZOJ 4214】黄昏下的礼物 dp转有限状态自动机+矩阵乘法优化
神题……胡乱讲述一下思维过程……首先,读懂题.然后,转化问题为构造一个长度为|T|+n的字符串,使其内含有T这个子序列.之后,想到一个简单的dp.由于是回文串,我们就增量构造半个回文串,设f(i,j, ...
- 51NOD 1292 1277(KMP算法,字符串中的有限状态自动机)
在前两天的CCPC网络赛中...被一发KMP题卡了住了...遂决定,哪里跌倒就在哪里爬起来...把个KMP恶补一发,连带着把AC自动机什么的也整上. 首先,介绍设定:KMP算法计划解决的基本问题是,两 ...
随机推荐
- android Graphics(三):区域(Range)
前言:最近几天对画图的研究有些缓慢,项目开始写代码了,只能在晚上空闲的时候捯饬一下自己的东西,今天给大家讲讲区域的相关知识,已经想好后面两篇的内容了,这几天有时间赶紧写出来给大家.有关界面开发的东东内 ...
- linux嵌入式: 实现自己的tree命令
//# cat treecmd.c #include<stdio.h> #include<dirent.h> #include<sys/stat.h> #inclu ...
- Redis中的value包含中文显示的问题?
linux 系统 redis不识别中文 如何显示中文 在Redis中存储的value值是中文“马拉斯加”Shell下get获取后展示的结果为:\xc2\xed\xc0\xad\xcb\xb9\xbc ...
- python3.4.3将汉字转换为大写拼音首字母
from pypinyin import pinyin a=pinyin(u'杨强',type=FIRST_LETTER) --->此时返回一个列表并赋给a(元素也是列表) b=[] - ...
- ZOJ 2968 Difference Game 【贪心 + 二分】
题意: 有Ga.Gb两堆数字,初始时两堆数量相同.从一一堆中移一一个数字到另一一堆的花费定义为两堆之间数 量差的绝对值,初始时共有钱C.求移动后Ga的最小小值减Gb的最大大值可能的最大大值. 思路: ...
- bresenham算法的FPGA的实现2
在上一篇里http://www.cnblogs.com/sepeng/p/4045593.html <bresenham算法的FPGA的实现1>已经做了一个整体框架的布局,但是那个程序只是 ...
- Linux下查看二进制文件命令
一.在Linux下查看二进制文件的软件: xxd hexdump 二.编辑: 1.biew 2.hexedit 3.vim Vim 来编辑二进制文件.Vim 本非为此而设计的,因而有若干局限.但你能读 ...
- Page的生命周期及相关事件苛
(1)请求页面:页请求发生在页生命周期开始之前. (2)开始:在开始阶段,将设置页属性,如Request和Response.在此阶段,页还将确定请求是回发请求还是新请求,并设置IsPostBack属性 ...
- Eclipse中JBOSS5.1无法启动的问题解决办法
今天在Eclipse中启动JBoss 5.1时遇到这样的一个错误: …… ERROR [AbstractKernelController] Error installing to Instantiat ...
- 1 #安装php
#安装php #备注:php5..3以后的版本源码不需要打php-fpm补丁,该补丁已经集成进5..3中强制启用fastcgi. [root@dba01 nginx-]# cd [root@dba01 ...