-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
165 lines (131 loc) · 3.7 KB
/
main.c
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <errno.h>
#include "internal_allocators.h"
#include "visualize.h"
#include "parser.h"
#include "stream.h"
#include "tokenizer.h"
#include "fsm_compiler.h"
#include "fsm.h"
#include "util.h"
enum {
FSM_OUTPUT_FORMAT_NATIVE = 0,
FSM_OUTPUT_FORMAT_DOT = 1,
};
static void usage(const char * program, FILE * output)
{
const char * basename = program;
const char * search;
if ((search = strrchr(program, DIRECTORY_SEPARATOR)))
basename = search + 1;
fprintf(output, "Usage: %s file [options]\n", basename);
fprintf(output, "\n");
fprintf(output, "DESCRIPTION\n");
fprintf(output, "\tA command-line tool for converting FSA from one to another representation.\n");
fprintf(output, "\n");
fprintf(output, "OPTIONS\n");
fprintf(output, "\t--print-only\n");
fprintf(output, "\t\tPrint only the given FSM and exit.\n\n");
fprintf(output, "\t--format=[native|dot] (native by default)\n");
fprintf(output, "\t\tPrint the FSM in a given format, where format can be one of 'native', or 'dot'.\n\n");
fprintf(output, "\t--output=<file>\n");
fprintf(output, "\t\tPlace the output into <file>.\n\n");
fprintf(output, "\t--unite-initials\n");
fprintf(output, "\t\tUnite multiple initial states of the given FSM to the single state in output FSM.\n\n");
fflush(output);
}
static void print_fsm(FILE * output, struct fsm * fsm, int format, bool is_dfa)
{
switch (format)
{
case FSM_OUTPUT_FORMAT_NATIVE:
if (is_dfa)
generate_dfa_language(output, fsm);
else
generate_nfa_language(output, fsm);
break;
case FSM_OUTPUT_FORMAT_DOT:
if (is_dfa)
visualize_dfa(output, fsm);
else
visualize_nfa(output, fsm);
break;
}
}
static bool print_input_fsm_only = false;
static bool unite_initials = false;
static int fsm_output_format = FSM_OUTPUT_FORMAT_NATIVE;
int main(int argc, char * argv[])
{
if (argc < 2) {
usage(argv[0], stdout);
exit(EXIT_SUCCESS);
}
const char * input_file = argv[1];
const char * output_file = NULL;
atexit(drop_internal_allocators);
int i;
for(i = 2; i < argc; ++i) {
const char * arg = argv[i];
if (! memcmp(arg, "--output=", 9)) {
output_file = arg + 9;
continue;
}
if (! strcmp(arg, "--format=native")) {
continue;
}
if (! strcmp(arg, "--format=dot")) {
fsm_output_format = FSM_OUTPUT_FORMAT_DOT;
continue;
}
if (! strcmp(arg, "--print-only")) {
print_input_fsm_only = true;
continue;
}
if(! strcmp(arg, "--unite-initials")) {
unite_initials = true;
continue;
}
fprintf(stderr, "error: unknown option '%s'!\n", arg);
exit(EXIT_FAILURE);
}
init_symbols();
struct stream stream;
if(stream_init(&stream, input_file) == -1) {
fprintf(stderr, "error: cannot initialize stream (%s: %s)\n", input_file, strerror(errno));
exit(EXIT_FAILURE);
}
struct token * token_list = tokenize(&stream);
struct ast * tree = NULL;
(void)parse(token_list, &tree);
if (! tree) {
fprintf(stderr, "error: there is no any rules to construct the fsm!\n");
stream_destroy(&stream);
exit(EXIT_FAILURE);
}
struct fsm * nfa = fsm_compile(tree);
FILE * output = NULL;
if (output_file) {
output = fopen(output_file, "w");
if(! output)
fprintf(stderr, "warning: failed to open the output file '%s': %s!\n", output_file, strerror(errno));
}
if(! output)
output = stdout;
if(print_input_fsm_only) {
print_fsm(output, nfa, fsm_output_format, false);
stream_destroy(&stream);
exit(EXIT_FAILURE);
}
unsigned int options = 0;
if(unite_initials)
options |= CONV_OPTION_UNITE_INITIALS;
struct fsm * dfa = nfa_to_dfa(nfa, options);
print_fsm(output, dfa, fsm_output_format, true);
stream_destroy(&stream);
exit(EXIT_SUCCESS);
return 0;
}