我正在测试 Ragel 中状态动作的功能。我有以下 Ragel 程序:
ragelScaffolding.rl:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
char *p, *pe;
int cs;
void runRagelMachine(char instructions[], int instructionLen){
p = instructions;
pe = p + instructionLen;
%%{
machine test;
action testToAction1{
puts("1");
}
action testFromAction1{
puts("f1");
}
action testToAction2{
puts("2");
}
test = (
start: (
any -> s1
),
s1: (
any -> s2
)$to(testToAction1) $from(testFromAction1),
s2: (
any -> final
)$to(testToAction2)
);
main := test;
write data;
write init;
write exec;
}%%
}
int main(){
char buf[1024];
runRagelMachine(buf, 1024);
}
我希望这会输出以下内容:
1
f1
2
但相反,它输出:
1
f1
1
2
f1
2
这告诉我它两次运行这些操作。我一直在思考为什么会出现这种情况并阅读文档,但我似乎无法弄清楚为什么会发生这种情况。这发生在使用 Ragel 6.9 和 7 编译(以及使用 gcc 编译 C)时。该文档说明了以下内容:
当状态机进入指定状态时,状态动作就会被执行,要么是通过转换的自然移动,要么是通过基于动作的控制转移,例如 fgoto。它们在转换中的动作之后但在当前字符前进并针对输入块的末尾进行测试之前执行。
但是其中没有关于执行两次操作的内容。我非常感谢您对此事的任何帮助或澄清。
提前致谢。