-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.c
170 lines (147 loc) · 4.09 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
166
167
168
169
170
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#include "dict.h"
#include "list.h"
#include "grambit.h"
#include "grammar.h"
#include "choose.h"
#include "gstr.h"
#include "rxutil.h"
#define RMUTT_VERSION "2.6"
extern int yyparse(void);
extern FILE *yyin;
GRAMMAR *grammar;
char *topRule = NULL;
int maxStackDepth = -1;
int dynamic = 0;
void version() {
fprintf(stderr, "rmutt version %s\n", RMUTT_VERSION);
}
void usage() {
version();
fprintf(stderr, "usage: rmutt -[bdeirsv] grammar\n");
fprintf(stderr, " -s number max stack depth\n");
fprintf(stderr, " -r number random seed\n");
fprintf(stderr, " -i number iteration\n");
fprintf(stderr, " -I file read the iteration from a file\n");
fprintf(stderr,
" -e name name of rule to expand (default: first)\n");
fprintf(stderr, " -b name=value bind name to value in the grammar\n");
fprintf(stderr,
" -d dynamic variable scoping (default: lexical)\n");
fprintf(stderr, " -v print version number and exit\n");
}
int main(int argc, char **argv) {
GRAMBIT *t;
char *result;
FILE *in = stdin;
char *rte = NULL;
char c;
LIST *bindings = list_new();
int i, len;
srandom(time(NULL));
while ((c = getopt(argc, argv, "vs:r:i:I:e:db:")) != EOF) {
i = 0;
switch (c) {
case 'v':
version();
exit(1);
case 's':
/* maximum stack depth */
maxStackDepth = atoi(optarg);
break;
case 'r':
srandom(atoi(optarg));
break;
case 'i':
choose_setIterationString(optarg);
break;
case 'I': {
FILE *of = fopen(optarg, "r");
if (!of) {
fprintf(stderr, "error: unable to open iteration file %s\n",
optarg);
exit(-1);
}
choose_setIterationFile(of);
}
break;
case 'e':
rte = strdup(optarg);
break;
case 'b': {
int i;
char *name, *value;
for (i = 0; *(optarg + i) != '='; i++) {
if (!*(optarg + i)) {
usage();
exit(-1);
}
}
if (!*(optarg + i + 1)) {
usage();
}
name = (char *) calloc(sizeof(char), i + 1);
strncpy(name, optarg, i);
value = (char *) strdup(optarg + i + 1);
list_add(bindings, binding_new(name, literal_new(value),
GLOBAL_SCOPE));
}
break;
case 'd':
dynamic++;
break;
default:
usage();
exit(-1);
}
}
if (argc - optind == 1) {
in = fopen(argv[optind], "r");
if (!in) {
fprintf(stderr, "error: unable to open file %s\n", argv[optind]);
exit(-1);
}
}
yyin = in;
#ifdef DEBUG
fprintf(stderr,"parsing\n");
#endif
/* call the parser (puts result in "grammar") */
yyparse();
#ifdef DEBUG
fprintf(stderr,"done\n");
fflush(stdout);
#endif
if (!grammar) {
#ifdef DEBUG
fprintf(stderr,"no grammar returned from yyparse\n");
fflush(stdout);
#endif
exit(-1);
}
/* add the bindings from the command line */
len = list_length(bindings);
for (i = 0; i < len; i++) {
grammar_add(grammar, list_get(bindings, i));
}
/* if there's no top rule, the grammar is empty */
if (!topRule) {
fprintf(stderr, "rmutt error: empty grammar\n");
exit(-1);
}
/* determine which rule to expand */
if (rte) {
t = label_new(rte); /* the one from the command line, or */
} else {
t = label_new(topRule); /* the (default) top rule */
}
/* produce the output */
result = grammar_produce(grammar, t);
fputs(result, stdout);
/*free(result);
grambit_free(t);*/
return 0;
}