aboutsummaryrefslogtreecommitdiff
path: root/src/fsm2dot.c
blob: 8138781fc658972dc89ad84fea2c8aa90ef69a6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// fsm2dot.c --- print dot digraph of a finite state machine
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include "utils.h"
#include "string-builder.h"
#include "fsm.h"

struct string_builder sb = {0};

enum determination {
    DETERMINED = 0,
  UNDETERMINED = 1,
};

void
print_dot (const char *filename, enum determination determination)
{
  FILE *fp = fopen (filename, "w");
  fprintf (fp, "digraph fsm {" "\n"); {
    fprintf (fp, "\t" "fontname=\"Aporetic Serif,serif\"" "\n");
    fprintf (fp, "\t" "node [fontname=\"Aporetic Sans,sans-serif\"]" "\n");
    fprintf (fp, "\t" "edge [fontname=\"Aporetic Serif Mono,mono\"]" "\n");
    fprintf (fp, "\t" "rankdir=LR" "\n");
    fprintf (fp, "\t" "concentrate=true" "\n");
    fprintf (fp, "\t" "node [shape = circle]" "\n");

    for (state from = STATE_COUNT - 1; from > 0; --from) {
      for (state to = determination; to < STATE_COUNT; ++to) {
        STRING_BUILDER_RESET (sb);
        for (symbol input = 0; input < ALPHABET_SYMBOL_COUNT; ++input) {
          state transition = state_transition_table[from][input];
          if (transition != to) continue;

          symbol input_from = input;
          do input += 1;
          while (input < ALPHABET_SYMBOL_COUNT &&
                 state_transition_table[from][input] == to);
          input -= 1;

          if (input_from == C2A ('"') || input_from == C2A ('\\'))
            STRING_BUILDER_APPEND_CHAR (sb, '\\');
          STRING_BUILDER_APPEND_CHAR (sb, A2C (input_from));
          if (input != input_from) {
            STRING_BUILDER_APPEND_CHAR (sb, '-');
            if (input == C2A ('"') || input == C2A ('\\'))
              STRING_BUILDER_APPEND_CHAR (sb, '\\');
            STRING_BUILDER_APPEND_CHAR (sb, A2C (input));
          }
        }
        if (sb.count) {
          int level = from/(determination ? 5 : 4);
          // int level = 0;

          void print_state (state state) {
            bool final = getbit (bitarray_of_final_states, state);
            fprintf (fp, "\t" "s%dl%d [label = \"%d\"%s]" "\n",
                     state, level, state,
                     final ? " shape = doublecircle" : "");
          }

          STRING_BUILDER_APPEND_NULL (sb);
          print_state (from);
          print_state (to);
          fprintf (fp, sb.count == 3 && sb.items[0] != '\\' ||
                       sb.count > 3
                       ? "\t" "s%dl%d -> s%dl%d [label = \"[%s]\"]" "\n"
                       : "\t" "s%dl%d -> s%dl%d [label = \"%s\"]" "\n",
                   from, level, to, level, sb.string);
        }
      }
    }
  } fprintf (fp, "}" "\n");
}

int
main (int argc, char *argv[])
{
  const char *progname = *argv++;

  if (argc != 3) {
    fprintf (stderr, "Usage: %s undetermined.dot determined.dot\n", progname);
    return EXIT_FAILURE;
  }

  const char *undetermined = *argv++;
  const char *determined = *argv++;

  print_dot (undetermined, UNDETERMINED);
  print_dot (  determined,   DETERMINED);

  return EXIT_SUCCESS;
}
// fsm2dot.c ends here