设为首页 加入收藏

TOP

C语言实现有限自动机FSM(一)
2013-10-17 09:05:35 来源: 作者: 【 】 浏览:361
Tags:语言 实现 有限 动机 FSM

  一、状态机实现的要素

  首先,分析一下一个普通的状态机究竟要实现哪些内容。

  状态机存储从开始时刻到现在的变化,并根据当前输入,决定下一个状态。这意味着,状态机要存储状态、获得输入(我们把它叫做跳转条件)、做出响应。

  如上所示,{s1, s2, s3}均为状态,箭头c1/a1表示在s1状态、输入为c1时,跳转到s2,并进行a1操作。

  最下方为一组输入,状态机应做出如下反应:

  当前状态 输入 下一个状态 动作

  s1 c1 s2 a1

  s2 c2 s3 a2

  s3 c1 s2 a3

  s2 c2 s3 a2

  s3 c1 s2 a3

  s2 c1 s_trap a_trap

  s_trap c1 s_trap a_trap

  当某个状态遇到不能识别的输入时,就默认进入陷阱状态,在陷阱状态中,不论遇到怎样的输入都不能跳出。

  为了表达上面这个自动机,我们定义它们的状态和输入类型:

  typedef int state;

  typedef int condition;

  #define

  STATES 4

  #define

  STATE1 0

  #define

  STATE2 1

  #define

  STATE3 2

  #define

  STATETRAP 3

  #define

  CONDITIONS 2

  #define

  CONDITION1 0

  #define

  CONDITION2 1

  总结一下,我们需要定义的有状态、输入、行为(动作+下一个状态),其中,行为的个数是"状态数*输入数量"(其中有一些是重复的);其中动作一般来说可以用一个函数指针来实现。

  二、具体设计

  在嵌入式环境中,由于存储空间比较小,因此把它们全部定义成宏。此外,为了降低执行时间的不确定性,我们使用O(1)的跳转表来模拟状态的跳转。

  首先定义跳转类型:

  typedef void (*actiontype)(state

  mystate, condition condition);

  typedef struct

  {

  state

  next;

  actiontype

  action;

  }

  trasition, * ptrasition;

  然后按照上图中的跳转关系,把三个跳转加一个陷阱跳转先定义出来:

  //

  (s1, c1, s2, a1)

  trasition

  t1 = {

  STATE2,

  action1

  };

  //

  (s2, c2, s3, a2)

  trasition

  t2 = {

  STATE3,

  action2

  };

  //

  (s3, c1, s2, a3)

  trasition

  t3 = {

  STATE2,

  action3

  };

  //

  (s, c, trap, a1)

  trasition

  tt = {

  STATETRAP,

  actiontrap

  };

  其中的动作,由用户自己完成,在这里仅定义一条输出语句。

  void action1(State

  state, Condition condition)

  {

  printf("Action

  1 triggered.\n");

  }

  1

  最后定义跳转表:

  asition

  transition_table[STATES][CONDITIONS] = {

  /*

  c1,  c2*/

  /*

  s1 */&t1,

  &tt,

  /*

  s2 */&tt,

  &t2,

  /*

  s3 */&t3,

  &tt,

  /*

  st */&tt,

  &tt,

  };

     

首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇string类的正确简明写法 下一篇C语言中常数的数据类型

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: