diff options
| author | 2026-11-08 05:23:31 +0000 | |
|---|---|---|
| committer | 2026-11-08 05:24:55 +0000 | |
| commit | 3f36aca66e1ac6c872300059e8db691a473614d4 (patch) | |
| tree | 48c65f463c487d281c7c4091c7ba01dd4a2ff8f1 /src/fsm2dot.c | |
| download | lab-02-3f36aca66e1ac6c872300059e8db691a473614d4.tar.gz | |
Diffstat (limited to 'src/fsm2dot.c')
| -rw-r--r-- | src/fsm2dot.c | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/src/fsm2dot.c b/src/fsm2dot.c new file mode 100644 index 0000000..8138781 --- /dev/null +++ b/src/fsm2dot.c | |||
| @@ -0,0 +1,96 @@ | |||
| 1 | // fsm2dot.c --- print dot digraph of a finite state machine | ||
| 2 | #include <stdlib.h> | ||
| 3 | #include <stdio.h> | ||
| 4 | #include <stdint.h> | ||
| 5 | #include <stdbool.h> | ||
| 6 | #include <string.h> | ||
| 7 | #include "utils.h" | ||
| 8 | #include "string-builder.h" | ||
| 9 | #include "fsm.h" | ||
| 10 | |||
| 11 | struct string_builder sb = {0}; | ||
| 12 | |||
| 13 | enum determination { | ||
| 14 | DETERMINED = 0, | ||
| 15 | UNDETERMINED = 1, | ||
| 16 | }; | ||
| 17 | |||
| 18 | void | ||
| 19 | print_dot (const char *filename, enum determination determination) | ||
| 20 | { | ||
| 21 | FILE *fp = fopen (filename, "w"); | ||
| 22 | fprintf (fp, "digraph fsm {" "\n"); { | ||
| 23 | fprintf (fp, "\t" "fontname=\"Aporetic Serif,serif\"" "\n"); | ||
| 24 | fprintf (fp, "\t" "node [fontname=\"Aporetic Sans,sans-serif\"]" "\n"); | ||
| 25 | fprintf (fp, "\t" "edge [fontname=\"Aporetic Serif Mono,mono\"]" "\n"); | ||
| 26 | fprintf (fp, "\t" "rankdir=LR" "\n"); | ||
| 27 | fprintf (fp, "\t" "concentrate=true" "\n"); | ||
| 28 | fprintf (fp, "\t" "node [shape = circle]" "\n"); | ||
| 29 | |||
| 30 | for (state from = STATE_COUNT - 1; from > 0; --from) { | ||
| 31 | for (state to = determination; to < STATE_COUNT; ++to) { | ||
| 32 | STRING_BUILDER_RESET (sb); | ||
| 33 | for (symbol input = 0; input < ALPHABET_SYMBOL_COUNT; ++input) { | ||
| 34 | state transition = state_transition_table[from][input]; | ||
| 35 | if (transition != to) continue; | ||
| 36 | |||
| 37 | symbol input_from = input; | ||
| 38 | do input += 1; | ||
| 39 | while (input < ALPHABET_SYMBOL_COUNT && | ||
| 40 | state_transition_table[from][input] == to); | ||
| 41 | input -= 1; | ||
| 42 | |||
| 43 | if (input_from == C2A ('"') || input_from == C2A ('\\')) | ||
| 44 | STRING_BUILDER_APPEND_CHAR (sb, '\\'); | ||
| 45 | STRING_BUILDER_APPEND_CHAR (sb, A2C (input_from)); | ||
| 46 | if (input != input_from) { | ||
| 47 | STRING_BUILDER_APPEND_CHAR (sb, '-'); | ||
| 48 | if (input == C2A ('"') || input == C2A ('\\')) | ||
| 49 | STRING_BUILDER_APPEND_CHAR (sb, '\\'); | ||
| 50 | STRING_BUILDER_APPEND_CHAR (sb, A2C (input)); | ||
| 51 | } | ||
| 52 | } | ||
| 53 | if (sb.count) { | ||
| 54 | int level = from/(determination ? 5 : 4); | ||
| 55 | // int level = 0; | ||
| 56 | |||
| 57 | void print_state (state state) { | ||
| 58 | bool final = getbit (bitarray_of_final_states, state); | ||
| 59 | fprintf (fp, "\t" "s%dl%d [label = \"%d\"%s]" "\n", | ||
| 60 | state, level, state, | ||
| 61 | final ? " shape = doublecircle" : ""); | ||
| 62 | } | ||
| 63 | |||
| 64 | STRING_BUILDER_APPEND_NULL (sb); | ||
| 65 | print_state (from); | ||
| 66 | print_state (to); | ||
| 67 | fprintf (fp, sb.count == 3 && sb.items[0] != '\\' || | ||
| 68 | sb.count > 3 | ||
| 69 | ? "\t" "s%dl%d -> s%dl%d [label = \"[%s]\"]" "\n" | ||
| 70 | : "\t" "s%dl%d -> s%dl%d [label = \"%s\"]" "\n", | ||
| 71 | from, level, to, level, sb.string); | ||
| 72 | } | ||
| 73 | } | ||
| 74 | } | ||
| 75 | } fprintf (fp, "}" "\n"); | ||
| 76 | } | ||
| 77 | |||
| 78 | int | ||
| 79 | main (int argc, char *argv[]) | ||
| 80 | { | ||
| 81 | const char *progname = *argv++; | ||
| 82 | |||
| 83 | if (argc != 3) { | ||
| 84 | fprintf (stderr, "Usage: %s undetermined.dot determined.dot\n", progname); | ||
| 85 | return EXIT_FAILURE; | ||
| 86 | } | ||
| 87 | |||
| 88 | const char *undetermined = *argv++; | ||
| 89 | const char *determined = *argv++; | ||
| 90 | |||
| 91 | print_dot (undetermined, UNDETERMINED); | ||
| 92 | print_dot ( determined, DETERMINED); | ||
| 93 | |||
| 94 | return EXIT_SUCCESS; | ||
| 95 | } | ||
| 96 | // fsm2dot.c ends here | ||
