Last active
February 20, 2022 23:09
-
-
Save washtubs/3e8c9b6c12a59bcb290f to your computer and use it in GitHub Desktop.
State machine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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