C State Machine Compiler
by Ring Yee (RingYee@Gmail.com)
2008-09-10
Introduction:
Generate C source code of Hierarchical state machine by state table scripts
Reference:
1. Hierarchical State Machines - a Fundamentally Important Way of Design
by Miro Samek, Ph.D. 2003
2. State-Oriented Programming
by Miro Samek and Paul Montgomery
3. The Care and Feeding of the State Map Compiler
by Robert Martin, 1993
Required environment:
Python-2.5.2
How to use:
python smc.py xxx.sm
After generate:
Two folders and four files are generated:
folder: one is 'hsm', the other's name is same with %HSMName
files: gen_hsm.c, gen_hsm.h; gen_hsm_eng.c, gen_hsm_eng.h
gen_hsm_eng.c and gen_hsm_eng.h:
The two files include all source files located in folder
'hsm'
gen_hsm.c, gen_hsm.h:
The two files include all source files located in folder
which's name is same with %HSMName
To avoid compiling error caused by header including, Make sure
ONLY C source file includes the two header files: gen_hsm.h and
gen_hsm_eng.h
Now you can use the HSM with the following functions:
step1: Create a FSM
<%FSMName> * NEW(<%FSMName>) ("fsm name")
To new an FSM which's name is 'fsm name'
<%FSMName> *pFSM = NULL;
pFSM = NEW(<%FSMName>) ("fsm name");
step2: Init this FSM
MCALL(HSM, Init, (<%FSMName> *)pFsm) END_CALL
To init the created FSM
MCALL(HSM, Init, pFSM) END_CALL;
step3: Handler the event
MCALL(HSM, OnEvt, (<%FSMName> *)pFsm), (Event *)pEvt END_CALL;
To precoss the event: pEvt
MCALL(HSM, OnEvt, pFSM)
,pEvt
END_CALL;
step4: after finish running the fsm, destroy it
void DEL(<%FSMName>) (<%FSMName> **ppFSM)
To destroy an FSM
DEL(<%FSMName>) (&pFSM);
NOTE:
The type definition of Event is included in gen_hsm_eng.h. You MUST
create/destroy an event with the following:
Event *pEvt = NULL;
/* if the event has no event data,
leaves pEvtData NULL and evtDataLen is 0 */
pEvt = NEW(Event) (evtId, pEvtData, evtDataLen);
/* precoss the event.... */
DEL(Event) (&pEvt);
In default, the macro of _SUPPORT_DBG is defined in gen_hsm_eng.h. If
the event is not handled by each state, it will be delivered to
topmost state and assertion works
You can remove the macro in gen_hsm_eng.h to avoid assertion. But it is
not recommanded
Rules for writing SM file:
The sign '#' means one line comments
[Header:]
KEYWORDS:
%HSMName: name
%Context: context
%Header: header1, header2...
%Version: version
NOTE:
Name for %HSMName and %Context MUST be in the format:
[A-Za-z_][A-Za-z0-9_]*
The name of %HSMName and %Context MUST NOT be the same
If %HSMName, %Context and %Version are not defined,
the default value will be used:
_SMC_Default_Name, _SMC_Default_Ctxt and _SMC_Default_V0.01
You MUST typedef your FSM context as <%Context>. For example,
if value of <%Context> is FsmContext, you must typedef the type
in your header:
typedef struct xxxxx {
structure member define;
}FsmContext;
And this header MUST be one of the value of <%Header>
%Header is to include the header files which declare the name of
%Context, and the functions used in the SM file. (These functions
MUST be implemented by yourself)
If there is no headers needed to be included, this field can be
not present or just leaves the vaule as blank
If there are more than one header files to be included, they MUST
be separated by sign ','
The value of %Version will be included as file comments in the
generated source files
All the definition of each value MUST be in one line, following is
invalid:
%Header: header1,
header2
%Version:This is
the version of the SM file
It shall be written as:
%Header: header1, header2
%Version:This is the version of the SM file
[Body:]
The body MUST begin with sign '%%' and end with '%%' also. Each sign
MUST be in one line
In the body, the SM table MUST be present, each state MUST be the format:
State[InitSubstate]:SuperState >EntryFunction <ExitFunction
{
TriggledEvent NextState {action1, action2}
TriggledEvent NextState {action1, action2}
#other transitions
}
Name of state, triggled event and function MUST be in the format:
[A-Za-z_][A-Za-z0-9_]*
The <State> MUST be present as state name.
The <InitSubstate> is optional if <State> has no init sub state. when the
init sub state is present, it MUST be surrounded by sign '[' and ']'
If <State> has parent state <SuperState>, the sign ':' MUST be used to
separated them.
The entry function and exit function are optional for the <State>. If they
are present, the sign ' '(whitespace or tab) MUST be used to separated each
one, and the entry function MUST be started with sign '>', exit function
MUST be started with sign '<'
Statement of "State[InitSubstate]:SuperState >EntryFunction <ExitFunction"
MUST locate in one line. Then followed a line with sign '{' to represent
beginning to transitions definition.
The transition is as the following:
+------+ TriggledEvt/ +---------+
|State |----------------------->|NextState|
+------+ (action1, action2) +---------+
When <TriggledEvent> comes, the actions in {action1, action2} are executed
at first, and then following state transition: transit to <NextState>.
<TriggledEvent>, <NextState> and <{acitons}> MUST be separated with sign
' '(whitespace or tab).
The actions MUST be surrounded wiht sign '{' and '}'. If no actions in this
transition, just leaves it blank.
Each transition MUST locate in one line
The definition of state MUST be end with sign '}' in one line
Your effort:
After the %Context is defiend, you MUST implement the Init and DeInit
function to init and deinit context of this FSM. The name rule for the two
functions is: Init%Context and DeInit%Context.
For example,if %Context is defined as 'FsmContext', the init and deinit
functions MUST like:
InitFsmContext and DeInitFsmContext
If the Entry or Exit function of some states are present, you MUST implement
these functions
If the actions are present for some transitions, you MUST implement these
functions
NOTE:
The parameter for all function is unfied with: %Context *pCtxt. And
the return type for each function is 'void'
Example:
#--------------------Watch.sm
%HSMName: Watch
%Context: WatchCtxt #init func is: void InitWatchCtxt(WatchCtxt *)
#deinit func is: void DeInitWatchCtxt(WatchCtxt *)
%Header: watch_hsm.h
%Version: V0.01 2008/09/02
# State machine:
# +----------------------------------------------------------------------+
# | Top |
# | INIT_ST |
# | | |
# | V |
#
没有合适的资源?快使用搜索试试~ 我知道了~
StateMachineCompiler for C 根据状态表生成【层次状态机】【C代码】
共8个文件
py:4个
txt:1个
c:1个
4星 · 超过85%的资源 需积分: 49 32 下载量 65 浏览量
2008-09-24
08:51:18
上传
评论 1
收藏 20KB RAR 举报
温馨提示
Python实现,根据状态表生成C代码的【层次状态机】,亦可退化成【平面状态机】。使用C模拟C++的一些特性。 2009.12.3: 里面有readme,在研究之前先读一下。 对于号称“专业研究。。。”的fazai001(无激发)同学,本人表示不屑。 压缩包内有现成的例子,使用如下: 安装python2.5.2 在该目录的cmd下,运行python smc.sm watch.sm 生成两个目录,以及gen_xxx.c, gen_xxx.h几只文件 用vc6创建空的工程,加入gen_xxx.c, watch_hsm.c等三支文件就可以编译运行了 完整的状态机转换图在watch.sm文件中。
资源推荐
资源详情
资源评论
收起资源包目录
HSMC.rar (8个子文件)
watch.sm 4KB
watch_hsm.h 972B
smcg.py 64KB
watch_hsm.c 3KB
smc.py 3KB
smcr.py 2KB
readme.txt 15KB
smcp.py 12KB
共 8 条
- 1
资源评论
- shijia1322020-02-13有参考价值,可以使用
rty
- 粉丝: 3
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功