我不明白 Ragel 认为什么是“最终”状态。IIRC 用户指南说,在机器简化之前是最终状态的状态在之后仍然是最终状态。究竟什么时候是状态决赛,人们如何认识到这一点?
应用:
我正在使用状态机语法来实现字符串查找器——查找长度大于 n 的 ASCII 字符串,并打印它们。这意味着实现最大长度匹配器,如下所示。
尽管点输出没有显示最终状态,但 EOF 转换的行为会有所不同,具体取决于使用的风格{$%@}eof
。我不明白为什么会这样。例如,在下面的has_string
状态中,使用%eof
而不是@eof
导致从终止匹配状态的生成/合成状态之一调用commit_nonstring_eof
和动作。commit_string_eof
这是一个工作状态机。请注意,最右侧的节点退出动作只有一个动作:commit_nonstring_eof
.
这是一个损坏的状态机。请注意,最右侧的节点退出操作同时调用
commit_string_eof
和commit_nonstring_eof
action commit_string { }
action commit_string_eof { }
action commit_nonstring_eof { }
action set_mark { }
action reset {
/* Force the machine back into state 1. This happens after
* an incomplete match when some graphical characters are
* consumed, but not enough for use to keep the string. */
fgoto start;
}
# Matching classes union to 0x00 .. 0xFF
graphic = (0x09 | 0x20 .. 0x7E);
non_graphic = (0x00 .. 0x08 | 0x0A .. 0x1F | 0x7F .. 0xFF);
collector = (
start: (
# Set the mark if we have a graphic character,
# otherwise go to non_graphic state and consume input
graphic @set_mark -> has_glyph |
non_graphic -> no_glyph
) $eof(commit_nonstring_eof),
no_glyph: (
# Consume input until a graphic character is encountered
non_graphic -> no_glyph |
graphic @set_mark -> has_glyph
) $eof(commit_nonstring_eof),
has_glyph: (
# We already matched one graphic character to get here
# from start or no_glyph. Try to match N-1 before allowing
# the string to be committed. If we don't get to N-1,
# drop back to the start state
graphic{3} $lerr(reset) -> has_string
) @eof(commit_nonstring_eof),
has_string: (
# Already consumed our quota of N graphic characters;
# consume input until we run out of graphic characters
# then reset the machine. All exiting edges should commit
# the string. We differentiate between exiting on a non-graphic
# input that shouldn't be added to the string and exiting
# on a (graphic) EOF that should be added.
graphic* non_graphic -> start
) %from(commit_string) @eof(commit_string_eof) // okay
#) %from(commit_string) %eof(commit_string_eof) // bad
); #$debug;
main := (collector)+;