当执行一个动作时,我希望该动作在某些条件下触发跳转到另一个状态。为了代码可读性,我还想在机器实例化之外定义操作。
如何在机器实例化范围之外访问由 ragel 生成的状态标签?
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
static int cs=1;
%%{
machine simpleMachine;
write data;
}%%
//The code for ActionA and ActionB is defined outside the scope
//of the instatiation for readability
void a(void)
{ //Called for ActionA
}
void b(void)
{ //Called for ActionB
int var2=0;
if (var2==0)
{
%%{
fgoto STATE_A;
#This fgoto generates a ragel parse error
#how to use the STATE_A label outside
#of the machine definition?
}%%
}
}
void ExecuteSM(const char *p)
{ const char *pe = p + strlen( p );
int var1=0;
%%{
EVENT1 = 'a';
EVENT2 = 'b';
EVENT3= 'c';
action ActionA
{ a();
if (var1==0)
{
fgoto STATE_B; //This fgoto compiles OK
}
}
action ActionB
{
b();//I'd like to execute an fgoto in the function b() but I get a
//parse error
}
myfsm := (
#STATE EVENT ACTION NEXT_STATE
start:( EVENT1 >ActionA ->STATE_A),
STATE_A:( EVENT2 >ActionB ->STATE_B),
STATE_B:( EVENT3)
);
write init nocs;
write exec;
}%%
}
int main()
{
ExecuteSM("abc");
return 0;
}