Skip to content

Instantly share code, notes, and snippets.

@washtubs
Last active February 20, 2022 23:09
Show Gist options
  • Save washtubs/3e8c9b6c12a59bcb290f to your computer and use it in GitHub Desktop.
Save washtubs/3e8c9b6c12a59bcb290f to your computer and use it in GitHub Desktop.
State machine
function get_state() {
return substr($0,1,1)
}
BEGIN {
#state machine spec
start_state="_BOF_"
state["_BOF_"]="H"
state["H"]="B"
state["B"]="D"
state["D"]="B|D|T"
state["T"]="_EOF_"
state["_EOF_"]="_BOF_"
final_states="_EOF_"
}
awk -f state_machine.awk -f record_proc.awk test
# test:6: State transition not present: B -> B
# test:7: State transition not present: B -> T
# you must implement get_state elsewhere.
function validate_state() {
transitions_str = state[previous_state]
split(transitions_str, transitions_ary, "|")
for (key in transitions_ary) {
if (transitions_ary[key] == current_state) {
return 1
}
}
return 0
}
BEGIN {
current_state = "_BOF_"
}
{
previous_state=current_state
current_state=get_state()
if (!validate_state()) {
print FILENAME":"FNR": State transition not present: "previous_state" -> "current_state > "/dev/stderr"
#exit 1
}
}
H
B
D
D
B
B error (should be D)
T
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment