summaryrefslogtreecommitdiff
path: root/lex.c
blob: 0cdd1c48ec7b7de7e04e74fa7287362a5c553b62 (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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#define _XOPEN_SOURCE 700

#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#define MAYBEADDLINE(buf, lineno, filename) do { if (needline) { \
	char ppline[LINE_MAX] = {0}; \
	sprintf(ppline, "#line %d \"%s\"\n", lineno, filename); \
	buf = append(buf, ppline); \
	needline = 0; \
	} } while (0)

#define RESETLINE(buf) do { \
	needline = 1; \
	buf = append(buf, "%%\n"); \
	} while (0)

#define OUTPUTFILENAME "lex.yy.c"

#define MIN_POSITIONS	2500
#define MIN_STATES	500
#define MIN_TRANSITIONS	2000
#define MIN_NODES	1000
#define MIN_CLASSES	1000
#define MIN_OUTPUTSIZE	3000

struct definition {
	char *name;
	char *substitute;
};

struct parser {
	enum { UNSPECIFIED, POINTER, ARRAY } yytexttype;
	int maxpositions;
	int maxstates;
	int maxtransitions;
	int maxnodes;
	int maxclasses;
	int maxoutput;

	int npositions;
	int nstates;
	int ntransitions;
	int nnodes;
	int nclasses;
	int outputsize;

	char *states;
	char *top;
	char *init;
	char *bottom;

	size_t ndefs;
	char *defs;
};

char *append(char *buf, const char *add)
{
	if (buf == NULL) {
		return strdup(add);
	}

	size_t oldlen = buf ? strlen(buf) : 0;
	size_t newlen = strlen(add);
	char *tmp = realloc(buf, oldlen + newlen);
	if (tmp == NULL) {
		perror("lex");
		exit(EXIT_FAILURE);
	}
	buf = tmp;
	memcpy(buf + oldlen, add, newlen);
	return buf;
}

static int add_directive(struct parser *p, const char *buf)
{
	if (!strcmp(buf, "%array\n")) {
		p->yytexttype = ARRAY;
		return 1;
	}

	if (!strcmp(buf, "%pointer\n")) {
		p->yytexttype = POINTER;
		return 1;
	}

	switch (buf[1]) {
	case 'p':
		p->npositions = atoi(buf + 2);
		break;

	case 'n':
		p->nstates = atoi(buf + 2);
		break;

	case 'a':
		p->ntransitions = atoi(buf + 2);
		break;

	case 'e':
		p->nnodes = atoi(buf + 2);
		break;

	case 'k':
		p->nclasses = atoi(buf + 2);
		break;

	case 'o':
		p->outputsize = atoi(buf + 2);
		break;

	case 'x':
	case 'X':
		/* exclusive start condition */
		break;

	case 's':
	case 'S':
		/* start condition */
		break;

	default:
		return 0;
	}

	return 1;
}

static int add_definition(struct parser *p, const char *buf)
{
	(void)p; (void)buf;
	return 1;
}

struct parser *parse(struct parser *parser, const char *file)
{
	struct parser p = {0};
	FILE *input = stdin;
	char *buf = NULL;
	int line = 0;
	size_t n = 0;
	ssize_t len = 0;
	int needline = 1;
	enum { DEFINITIONS, RULES, SUBROUTINES } section = DEFINITIONS;

	if (strcmp("-", file)) {
		if ((input = fopen(file, "r")) == NULL) {
			fprintf(stderr, "Couldn't open %s: %s\n", file,
				strerror(errno));
			return parser;
		}
	}

	while (!feof(input)) {
		len = getline(&buf, &n, input);
		if (len == -1) {
			if (errno == 0) {
				buf = NULL;
				n = 0;
				continue;
			}
			perror("lex");
			exit(EXIT_FAILURE);
		}
		line++;

		if (section == DEFINITIONS) {
			if (!strcmp("%%\n", buf)) {
				section = RULES;
			} else if (!strcmp("%{\n", buf)) {
				MAYBEADDLINE(p.top, line, file);
				while ((len = getline(&buf, &n, input)) != -1
					&& strcmp("%}\n", buf))
				{
					line++;
					p.top = append(p.top, buf);
				}
				line++;
				RESETLINE(p.top);
			} else if (isblank(buf[0])) {
				MAYBEADDLINE(p.top, line, file);
			} else if (buf[0] == '%') {
				if (!add_directive(&p, buf)) {
					fprintf(stderr, "lex: %s:%d: unknown directive %s", file, line, buf);
					return NULL;
				}
			} else {
				add_definition(&p, buf);
			}
		} else if (section == RULES) {
			if (!strcmp("%%\n", buf)) {
				section = SUBROUTINES;
			}
		} else if (section == SUBROUTINES) {
			if (!strcmp("%%\n", buf)) {
				fprintf(stderr, "%s:%d:Unexpected %%%% "
					"after processing user subroutines\n",
					file, line);
				free(buf);
				free(p.top);
				free(p.init);
				free(p.bottom);
				return parser;
			}
			MAYBEADDLINE(p.bottom, line, file);
			p.bottom = append(p.bottom, buf);
		}
	}

	if (input != stdin) {
		fclose(input);
	}

	if (section == DEFINITIONS) {
		fprintf(stderr, "%s:%d reach EOF without rules section\n", file, line);
		return parser;
	}

	/* merge successful stuff */
	if (parser == NULL) {
		parser = calloc(1, sizeof(*parser));
	}

	if (p.top) {
		parser->top = append(parser->top, p.top);
		free(p.top);
	}

	if (p.init) {
		parser->init = append(parser->init, p.init);
		free(p.init);
	}

	if (p.bottom) {
		parser->bottom = append(parser->bottom, p.bottom);
		free(p.bottom);
	}

	return parser;
}

int output(struct parser *p, FILE *f)
{
	/* write the data */
	if (p->top) {
		fprintf(f, "%s\n", p->top);
	}

	fprintf(f, "#include <stdio.h>\n");
	fprintf(f, "\n");

	fprintf(f, "static struct {\n");
	fprintf(f, "\tconst struct {\n");
	fprintf(f, "\t\tint INITIAL;\n");
	for (int i = 0; i < p->nstates; i++) {
		fprintf(f, "\t\tint %s;\n", p->states[i]);
	}
	fprintf(f, "\t} state;\n");
	fprintf(f, "\tint condition;\n");
	fprintf(f, "} YY = {\n");
	fprintf(f, "\t{ 0, ");
	for (int i = 0; i < p->nstates; i++) {
		fprintf(f, "%d, ", i + 1);
	}
	fprintf(f, "},\n");
	fprintf(f, "};\n");
	fprintf(f, "\n");

	fprintf(f, "#define ECHO fputs(yytext, yyout)\n");
	fprintf(f, "#define REJECT /* something complex */\n");
	fprintf(f, "#define BEGIN YY.condition = YY.state.\n");
	fprintf(f, "\n");

	if (p->yytexttype == ARRAY) {
		fprintf(f, "char yytext[YYARRAYSIZE];\n");
	} else {
		fprintf(f, "char *yytext = NULL;\n");
	}
	fprintf(f, "int yyleng = 0;\n");
	fprintf(f, "FILE *yyin = NULL;\n");
	fprintf(f, "FILE *yyout = NULL;\n");
	fprintf(f, "\n");

	/* int yymore(void) */
	/* int yyless(int n) */
	/* int input(void) */
	/* int unput(int c) */

	fprintf(f, "int yylex(void)\n{\n");
	if (p->init) {
		fputs(p->init, f);
	}
	/* generated code */
	fprintf(f, "}\n");

	if (p->bottom) {
		fputs(p->bottom, f);
	}
	return 0;
}

int main(int argc, char *argv[])
{
	struct parser *parser = NULL;
	FILE *outfile = NULL;
	int verbose = 0;
	int c;

	while ((c = getopt(argc, argv, "tnv")) != -1) {
		switch (c) {
		case 't':
			/* Output to stdout */
			outfile = stdout;
			break;

		case 'n':	/* MUTEX: v */
			/* Suppress statistics summary */
			verbose = -1;
			break;

		case 'v':	/* MUTEX: n */
			/* Show statistics summary */
			verbose = 1;
			break;
			
		default:
			return 1;
		}
	}

	if (outfile == NULL) {
		if ((outfile = fopen(OUTPUTFILENAME, "w")) == NULL) {
			perror("lex: Couldn't open output file");
			return 1;
		}
	}

	if (optind >= argc) {
		parser = parse(parser, "-");
	} else {
		while (optind < argc) {
			parser = parse(parser, argv[optind++]);
		}
	}

	if (parser) {
		output(parser, outfile);
		/* if (verbose == 1 || (verbose == 0 && parser->tables_are_set) { */
		/* output_statistics(parser); */
		/* } */
	}

	if (outfile != stdout) {
		fclose(outfile);
	}

	return 0;
}