Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added basic support of Graphviz output #17

Merged
merged 1 commit into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 26 additions & 24 deletions lib/dfa.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@

static int dfa_state_set_deadend(struct dfa *dfa, size_t state, int deadend);
static int dfa_state_calc_deadend(struct dfa *dfa, size_t state);
static void dfa_print(const struct dfa *dfa);

static int max_to_bps(size_t max)
{
Expand Down Expand Up @@ -1086,20 +1085,22 @@
}


static void dfa_print_char(const unsigned char a)
static void dfa_print_char(FILE *stream, const unsigned char a)

Check warning on line 1088 in lib/dfa.c

View check run for this annotation

Codecov / codecov/patch

lib/dfa.c#L1088

Added line #L1088 was not covered by tests
{
if (isprint(a)) {
if (a == '"' || a == '-' || a == '\\')
printf("\\");
printf("%c", a);
fprintf(stream, "\\");
fprintf(stream, "%c", a);

Check warning on line 1093 in lib/dfa.c

View check run for this annotation

Codecov / codecov/patch

lib/dfa.c#L1092-L1093

Added lines #L1092 - L1093 were not covered by tests
} else {
printf("0x%02X", a);
fprintf(stream, "0x%02X", a);

Check warning on line 1095 in lib/dfa.c

View check run for this annotation

Codecov / codecov/patch

lib/dfa.c#L1095

Added line #L1095 was not covered by tests
}
}

void dfa_print(const struct dfa *src)
void dfa_graphviz(FILE *stream, const struct dfa *src)

Check warning on line 1099 in lib/dfa.c

View check run for this annotation

Codecov / codecov/patch

lib/dfa.c#L1099

Added line #L1099 was not covered by tests
{
printf("#dfa states: %zu, first: %zu \n", src->state_cnt, src->first_index);
fprintf(stream, "digraph DFA{\n");
fprintf(stream, "#dfa states: %zu, first: %zu \n",
src->state_cnt, src->first_index);

Check warning on line 1103 in lib/dfa.c

View check run for this annotation

Codecov / codecov/patch

lib/dfa.c#L1101-L1103

Added lines #L1101 - L1103 were not covered by tests

for (size_t i = 0; i < src->state_cnt; i++) {
printf("{node [shape = %s, %s]; \"%zu\";}\n",
Expand All @@ -1109,11 +1110,9 @@
}

for (size_t i = 0; i < src->state_cnt; i++) {
struct to_and_symb {size_t to; unsigned char symb;} qu[256];
struct to_and_symb {size_t to; unsigned char symb;} qu[256];

Check warning on line 1113 in lib/dfa.c

View check run for this annotation

Codecov / codecov/patch

lib/dfa.c#L1113

Added line #L1113 was not covered by tests



printf("#node[%zu]\n", i);
fprintf(stream, "#node[%zu]\n", i);

Check warning on line 1115 in lib/dfa.c

View check run for this annotation

Codecov / codecov/patch

lib/dfa.c#L1115

Added line #L1115 was not covered by tests
for (unsigned int j = 0; j < 256; j++) {
qu[j].to = dfa_get_trans(src, i, j);

Expand Down Expand Up @@ -1148,11 +1147,12 @@

unsigned int ub, db;
int started = 0;
printf(" \"%zu\" -> \"%zu\" [label = \"", i, qu[d_bnd].to);
fprintf(stream, " \"%zu\" -> \"%zu\" [label = \"",

Check warning on line 1150 in lib/dfa.c

View check run for this annotation

Codecov / codecov/patch

lib/dfa.c#L1150

Added line #L1150 was not covered by tests
i, qu[d_bnd].to);
if (total_trans > 1)
printf("[");
fprintf(stream, "[");

Check warning on line 1153 in lib/dfa.c

View check run for this annotation

Codecov / codecov/patch

lib/dfa.c#L1153

Added line #L1153 was not covered by tests
if (total_trans > 256/2 && total_trans != 256)
printf("^");
fprintf(stream, "^");

Check warning on line 1155 in lib/dfa.c

View check run for this annotation

Codecov / codecov/patch

lib/dfa.c#L1155

Added line #L1155 was not covered by tests
for (db = 0; db < 256;)
if (symbs[db] == 0) {
db++;
Expand All @@ -1161,19 +1161,19 @@

ub--;
if (db == ub) {
dfa_print_char(db);
dfa_print_char(stream, db);

Check warning on line 1164 in lib/dfa.c

View check run for this annotation

Codecov / codecov/patch

lib/dfa.c#L1164

Added line #L1164 was not covered by tests
} else if (ub - db == 1) {
dfa_print_char(db);
dfa_print_char(ub);
dfa_print_char(stream, db);
dfa_print_char(stream, ub);

Check warning on line 1167 in lib/dfa.c

View check run for this annotation

Codecov / codecov/patch

lib/dfa.c#L1166-L1167

Added lines #L1166 - L1167 were not covered by tests
} else {
dfa_print_char(db);
printf("-");
dfa_print_char(stream, db);
fprintf(stream, "-");

Check warning on line 1170 in lib/dfa.c

View check run for this annotation

Codecov / codecov/patch

lib/dfa.c#L1169-L1170

Added lines #L1169 - L1170 were not covered by tests
if (isprint(ub)) {
if (ub == '\\' || ub == '"' || ub == ']')
printf("\\");
printf("%c", ub);
fprintf(stream, "\\");
fprintf(stream, "%c", ub);

Check warning on line 1174 in lib/dfa.c

View check run for this annotation

Codecov / codecov/patch

lib/dfa.c#L1173-L1174

Added lines #L1173 - L1174 were not covered by tests
} else {
printf("\\x%02X", ub);
fprintf(stream, "\\x%02X", ub);

Check warning on line 1176 in lib/dfa.c

View check run for this annotation

Codecov / codecov/patch

lib/dfa.c#L1176

Added line #L1176 was not covered by tests
}
}
if (!started) {
Expand All @@ -1184,11 +1184,13 @@

j = u_bnd - 1;
if (total_trans > 1)
printf("]");
printf("\"];\n");
fprintf(stream, "]");
fprintf(stream, "\"];\n");

Check warning on line 1188 in lib/dfa.c

View check run for this annotation

Codecov / codecov/patch

lib/dfa.c#L1187-L1188

Added lines #L1187 - L1188 were not covered by tests
}

}

fprintf(stream, "}\n");

Check warning on line 1193 in lib/dfa.c

View check run for this annotation

Codecov / codecov/patch

lib/dfa.c#L1193

Added line #L1193 was not covered by tests
}

#define QUEUE_BLOCK_SIZE (4096 / sizeof(size_t) - 3)
Expand Down
9 changes: 9 additions & 0 deletions lib/dfa.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <stdint.h>
#include <stddef.h>
#include <stdio.h>

/** flag that shows if the state is final */
#define DFA_FLAG_FINAL (0x01)
Expand Down Expand Up @@ -284,4 +285,12 @@ int dfa_save_to_file(const struct dfa *dfa, char *filename);
*/
int dfa_load_from_file(struct dfa *dfa, char *filename);

/**
* Print the dfa in Graphviz format.
*
* @param stream Output stream.
* @param dfa DFA to be printed.
*/
void dfa_graphviz(FILE *stream, const struct dfa *dfa);

#endif /** REFA_DFA_H @} */
27 changes: 21 additions & 6 deletions tools/re2fa.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ static struct argp_option options[] = {
{"verbose", 'v', 0, 0, "Verbose output", 2},
{"join", 'j', 0, 0, "Join inputs into one output", 1},
{"minimize", 'm', 0, 0, "Minimize automaton", 1},
{"print-gv", 'g', 0, 0, "Print Graphviz representation of automaton", 2},
{0}
};

Expand All @@ -52,6 +53,7 @@ struct arguments {
int verbose;
int join;
int minimize;
int gv;

int thread_cnt;
};
Expand Down Expand Up @@ -110,6 +112,9 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state)
case 'v':
args->verbose = 1;
break;
case 'g':
args->gv = 1;
break;
case ARGP_KEY_ARG:
args->input = realloc(args->input,
sizeof(char *) * (args->input_cnt + 1));
Expand Down Expand Up @@ -142,6 +147,7 @@ int main(int argc, char **argv)
arguments.output_path = NULL;
arguments.output_type = FAT_DFA_FILE;
arguments.verbose = 0;
arguments.gv = 0;
arguments.join = 0;
arguments.minimize = 0;
arguments.thread_cnt = 1;
Expand Down Expand Up @@ -227,9 +233,10 @@ int main(int argc, char **argv)
size_t before = dfa[i].state_cnt;
dfa_minimize(&dfa[i]);
if (arguments.verbose) {
printf("dfa minimized %zu->%zu\n",
before,
dfa[i].state_cnt);
fprintf(stderr,
"dfa minimized %zu->%zu\n",
before,
dfa[i].state_cnt);
}
}

Expand All @@ -242,11 +249,19 @@ int main(int argc, char **argv)

if (arguments.verbose)
for (int j = 0; j < dfa_cnt; j++) {
printf("[dfa]\n"
" state cnt: %zu, bps: %d\n",
dfa[j].state_cnt, dfa[j].bps);
fprintf(stderr, "[dfa]\n"
" state cnt: %zu\n",
dfa[j].state_cnt);
}

if (arguments.gv) {
if (dfa_cnt == 1) {
dfa_graphviz(stdout, &dfa[0]);
} else {
fprintf(stderr, "You can print only 1 DFA\n");
}
}

if (dfa_cnt > 1 && arguments.join) {
main_dfa_join(dfa, dfa_cnt, arguments.thread_cnt);

Expand Down