-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakescanner.c
316 lines (270 loc) · 9.83 KB
/
makescanner.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/***********************************************************************
SPL, the Shakespeare Programming Language
Copyright (C) 2001 Karl Hasselström and Jon Åslund
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
USA.
***********************************************************************/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRING_LENGTH 4096
int main(int argc, char *argv[]);
void insert_file(char *filename);
void rules_from_file(char *filename, char *token);
void rule_for_word(char *word, char *token);
void remove_newline(char *string);
void make_regexp(char *string);
void remove_repeated_whitespace(char *string);
void trim_whitespace(char *string);
int main(int argc, char *argv[])
{
char filename[STRING_LENGTH];
char *include_path;
/* Command line parameters */
if (argc < 2) {
include_path = (char *) malloc(sizeof(char)*2);
strcpy(include_path, ".");
}
else {
include_path = (char *) malloc(sizeof(char)*strlen(argv[1]));
strcpy(include_path, argv[1]);
}
/* User code at top of file */
sprintf(filename, "%s/%s", include_path, "user_code_top.metaflex");
insert_file(filename);
/* Definitions */
sprintf(filename, "%s/%s", include_path, "roman_numbers.metaflex");
insert_file(filename);
/* Separator */
printf("\n%%%%\n");
/* Rules */
/* - Wordlist rules */
sprintf(filename, "%s/%s", include_path, "article.wordlist");
rules_from_file(filename, "ARTICLE");
sprintf(filename, "%s/%s", include_path, "be.wordlist");
rules_from_file(filename, "BE");
sprintf(filename, "%s/%s", include_path, "character.wordlist");
rules_from_file(filename, "CHARACTER");
sprintf(filename, "%s/%s", include_path, "first_person.wordlist");
rules_from_file(filename, "FIRST_PERSON");
sprintf(filename, "%s/%s", include_path, "first_person_possessive.wordlist");
rules_from_file(filename, "FIRST_PERSON_POSSESSIVE");
sprintf(filename, "%s/%s", include_path, "first_person_reflexive.wordlist");
rules_from_file(filename, "FIRST_PERSON_REFLEXIVE");
sprintf(filename, "%s/%s", include_path, "negative_adjective.wordlist");
rules_from_file(filename, "NEGATIVE_ADJECTIVE");
sprintf(filename, "%s/%s", include_path, "negative_comparative.wordlist");
rules_from_file(filename, "NEGATIVE_COMPARATIVE");
sprintf(filename, "%s/%s", include_path, "negative_noun.wordlist");
rules_from_file(filename, "NEGATIVE_NOUN");
sprintf(filename, "%s/%s", include_path, "neutral_adjective.wordlist");
rules_from_file(filename, "NEUTRAL_ADJECTIVE");
sprintf(filename, "%s/%s", include_path, "neutral_noun.wordlist");
rules_from_file(filename, "NEUTRAL_NOUN");
sprintf(filename, "%s/%s", include_path, "nothing.wordlist");
rules_from_file(filename, "NOTHING");
sprintf(filename, "%s/%s", include_path, "positive_adjective.wordlist");
rules_from_file(filename, "POSITIVE_ADJECTIVE");
sprintf(filename, "%s/%s", include_path, "positive_comparative.wordlist");
rules_from_file(filename, "POSITIVE_COMPARATIVE");
sprintf(filename, "%s/%s", include_path, "positive_noun.wordlist");
rules_from_file(filename, "POSITIVE_NOUN");
sprintf(filename, "%s/%s", include_path, "second_person.wordlist");
rules_from_file(filename, "SECOND_PERSON");
sprintf(filename, "%s/%s", include_path, "second_person_possessive.wordlist");
rules_from_file(filename, "SECOND_PERSON_POSSESSIVE");
sprintf(filename, "%s/%s", include_path, "second_person_reflexive.wordlist");
rules_from_file(filename, "SECOND_PERSON_REFLEXIVE");
sprintf(filename, "%s/%s", include_path, "third_person_possessive.wordlist");
rules_from_file(filename, "THIRD_PERSON_POSSESSIVE");
/* - Single word rules */
printf("\n /* single word rules */\n");
rule_for_word("and", "AND");
rule_for_word("as", "AS");
rule_for_word("enter", "ENTER");
rule_for_word("exeunt", "EXEUNT");
rule_for_word("exit", "EXIT");
rule_for_word("heart", "HEART");
rule_for_word("if not", "IF_NOT");
rule_for_word("if so", "IF_SO");
rule_for_word("less", "LESS");
rule_for_word("let us", "LET_US");
rule_for_word("listen to", "LISTEN_TO");
rule_for_word("mind", "MIND");
rule_for_word("more", "MORE");
rule_for_word("not", "NOT");
rule_for_word("open", "OPEN");
rule_for_word("proceed to", "PROCEED_TO");
rule_for_word("recall", "RECALL");
rule_for_word("remember", "REMEMBER");
rule_for_word("return to", "RETURN_TO");
rule_for_word("speak", "SPEAK");
rule_for_word("than", "THAN");
rule_for_word("the cube of", "THE_CUBE_OF");
rule_for_word("the difference between", "THE_DIFFERENCE_BETWEEN");
rule_for_word("the factorial of", "THE_FACTORIAL_OF");
rule_for_word("the product of", "THE_PRODUCT_OF");
rule_for_word("the quotient between", "THE_QUOTIENT_BETWEEN");
rule_for_word("the remainder of the quotient between", "THE_REMAINDER_OF_THE_QUOTIENT_BETWEEN");
rule_for_word("the square of", "THE_SQUARE_OF");
rule_for_word("the square root of", "THE_SQUARE_ROOT_OF");
rule_for_word("the sum of", "THE_SUM_OF");
rule_for_word("twice", "TWICE");
rule_for_word("we must", "WE_MUST");
rule_for_word("we shall", "WE_SHALL");
/* - Other rules */
sprintf(filename, "%s/%s", include_path, "terminals.metaflex");
printf("\n /* rules for terminals from file %s */", filename);
insert_file(filename);
/* Separator */
printf("\n%%%%\n");
/* User code */
sprintf(filename, "%s/%s", include_path, "user_code_bottom.metaflex");
insert_file(filename);
/* We did it, no problemas */
free(include_path);
return 0;
}
void insert_file(char *filename)
{
FILE *file;
int c;
/* Open file */
file = fopen(filename,"r");
if (file == NULL) {
fprintf(stderr, "Could not find %s.\n", filename);
exit(1);
}
/* An initial newline is nice */
printf("\n");
/* Copy file to stdout */
while((c = getc(file)) != EOF) {
putchar(c);
}
fclose(file);
}
void rules_from_file(char *filename, char *token)
{
FILE *file;
char *s, string[STRING_LENGTH];
/* Open file */
file = fopen(filename,"r");
if (file == NULL) {
fprintf(stderr, "Could not find %s.\n", filename);
exit(1);
}
/* An initial newline is nice */
printf("\n /* rules from file %s */\n", filename);
/* For each line */
while(!feof(file)) {
/* Get line */
s = fgets(string, sizeof(string), file);
if (s == NULL)
break;
/* Write the rule */
remove_newline(string);
rule_for_word(string, token);
}
fclose(file);
}
void rule_for_word(char *word, char *token)
{
char regexp[STRING_LENGTH];
strcpy(regexp, word);
make_regexp(regexp);
printf("%s {\n yylval.str = newstr(yytext); return %s;\n}\n", regexp, token);
}
void remove_newline(char *string)
{
while((string[strlen(string) - 1] == '\n') ||
(string[strlen(string) - 1] == '\r')) {
string[strlen(string) - 1] = '\0';
}
}
void make_regexp(char *string)
{
char temp_string[STRING_LENGTH];
char *read = string, *write = temp_string;
char c;
char space_string[] = "\"[[:space:]]+\"";
/* Remove repeated whitespace */
remove_repeated_whitespace(string);
/* Remove whitespace from beginning and end */
trim_whitespace(string);
/* Replace space with [:space:], and quote */
*write = '\"';
write++;
while(*read != '\0') {
c = *read;
read++;
if(isspace((int) c)) {
strcpy(write, space_string);
write += sizeof(space_string) - 1;
}
else {
*write = c;
write++;
}
}
*write = '\"';
write++;
*write = '\0';
/* Copy working string to original string */
strcpy(string, temp_string);
}
void remove_repeated_whitespace(char *string)
{
char *read = string, *write = string;
int last_was_white = 0;
char c;
while(*read != '\0') {
c = *read;
read++;
if(isspace((int) c)) {
if(!last_was_white) {
last_was_white = 1;
*write = ' ';
write++;
}
}
else {
last_was_white = 0;
*write = c;
write++;
}
}
*write = '\0';
}
void trim_whitespace(char *string)
{
char *read = string, *write = string;
char c;
/* Remove whitespace at the end */
read = string + strlen(string) - 1;
while(isspace((int) *read))
read--;
write = read + 1;
*write = '\0';
/* Remove whitespace at the beginning */
for(read = string; isspace((int) *read); read++)
continue;
write = string;
while(*read != '\0') {
c = *read;
read++;
*write = c;
write++;
}
*write = '\0';
}