-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.c
More file actions
338 lines (304 loc) · 6.2 KB
/
parse.c
File metadata and controls
338 lines (304 loc) · 6.2 KB
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
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<strings.h>
#include "parse.h"
#include "automata.h"
struct lexer *
lexer_create(char *pre, char *post, struct pattern *patterns, size_t npat,
struct token *tokens, size_t ntok)
{
struct lexer *lx = (struct lexer *) malloc(sizeof(struct lexer));
lx->pre = pre;
lx->post = post;
lx->patterns = patterns;
lx->npat = npat;
lx->tokens = tokens;
lx->ntok = ntok;
return lx;
}
void
lexer_destroy(struct lexer *lx)
{
free(lx->pre);
free(lx->post);
free(lx);
}
static char *
substr(char *s, int n)
{
int len = n + 1;
char *ss = (char *) malloc(sizeof(char) * len);
snprintf(ss, len, "%s", s);
return ss;
}
static char *
parse_id(char *input)
{
if (!isalpha(*input)) {
fprintf(stderr, "id must begin with letter: '%s'", input);
exit(1);
}
char *s = input + 1;
while (isalpha(*s) || isdigit(*s) || *s == '_') {
s++;
}
return substr(input, s - input);
}
static char *
parse_tonewline(char *input)
{
char *s = input;
while (*s != '\n') {
s++;
}
return substr(input, s - input);
}
/* skipws: skip whitespace */
static char *
skipws(char *s)
{
for (; isspace(*s); s++) {}
return s;
}
static char *
skiplinespace(char *s)
{
for (; *s == ' ' || *s == '\t'; s++) {}
return s;
}
struct stringresult {
char *s;
char *pos;
};
static struct stringresult
parse_defsraw(char *input)
{
if (strncmp(input, "%{", 2) != 0) {
return (struct stringresult){"", input};
}
input += 2;
char *pos = input;
for (; strncmp(pos, "%}", 2) != 0; pos++) {}
return (struct stringresult){
substr(input, pos - input),
pos + 2,
};
}
static char *
skipoptions(char *pos)
{
char *keyword = "%option";
if (strncmp(pos, keyword, strlen(keyword)) != 0) {
return pos;
}
pos += strlen(keyword);
pos = skiplinespace(pos);
char *id = parse_id(pos);
pos += strlen(id);
free(id);
return pos;
}
struct pattern *
pattern_create(char *name, char *pattern)
{
struct pattern *p = (struct pattern *) malloc(sizeof(struct pattern));
p->name = name;
p->pattern = pattern;
return p;
}
struct patternresult {
struct pattern *p;
char *pos;
};
struct patternresult
parse_pattern(char *pos)
{
char *name = parse_id(pos);
pos = pos + strlen(name);
pos = skiplinespace(pos);
char *pattern = parse_tonewline(pos);
pos += strlen(pattern);
return (struct patternresult){pattern_create(name, pattern), pos};
}
struct patternset {
struct pattern *patterns;
size_t npat;
char *pos;
};
static struct patternset
parse_defsproper(char *pos)
{
size_t npat = 0;
struct pattern *patterns = NULL;
for (; strncmp(pos, "%%", 2) != 0 ; npat++) {
struct patternresult res = parse_pattern(pos);
pos = res.pos;
patterns = (struct pattern *)
realloc(patterns, sizeof(struct pattern) * (npat + 1));
patterns[npat] = *res.p;
pos = skipws(pos);
}
return (struct patternset){patterns, npat, pos};
}
struct defsresult {
char *pre;
struct pattern *patterns;
size_t npat;
char *pos;
};
static struct defsresult
parse_defs(char *pos)
{
pos = skipws(pos);
if (*pos == '\0') {
fprintf(stderr, "EOF in defs\n");
exit(1);
}
struct stringresult raw = parse_defsraw(pos);
pos = raw.pos;
pos = skipws(pos);
pos = skipoptions(pos);
pos = skipws(pos);
struct patternset set = parse_defsproper(pos);
return (struct defsresult){
raw.s, set.patterns, set.npat, set.pos,
};
}
static struct token *
token_create(bool literal, char *name, char *action)
{
struct token *tk = (struct token *) malloc(sizeof(struct token));
tk->literal = literal;
tk->name = name;
tk->action = action;
return tk;
}
static struct stringresult
parse_action(char *input)
{
if (*input != '{') {
fprintf(stderr, "action must begin with '{' but has '%.*s\n",
10, input);
exit(1);
}
input++; /* skip '{' */
char *pos = input;
for (; *pos != '}'; pos++) {}
return (struct stringresult){
substr(input, pos - input),
pos + 1,
};
}
struct tknameresult {
bool literal;
char *name;
char *pos;
};
static struct tknameresult
parse_token_id(char *pos)
{
char *id = parse_id(++pos); /* skip '{' */
pos += strlen(id);
if (*pos != '}') {
fprintf(stderr, "token id must end in '}' but has '%.*s\n'", 5,
pos);
exit(1);
}
return (struct tknameresult){false, id, pos + 1}; /* '}' */
}
static struct tknameresult
parse_token_literal(char *input)
{
input++; /* skip '"' */
char *pos = input;
for (pos++; *pos != '"'; pos++) {}
return (struct tknameresult){
true,
substr(input, pos - input),
pos + 1,
};
}
static struct tknameresult
parse_token_pattern(char *pos)
{
char *id = parse_id(pos);
return (struct tknameresult){true, id, pos + strlen(id)};
}
struct tokenresult {
struct token *tk;
char *pos;
};
typedef struct tknameresult (*nameparser)(char *);
static nameparser
choose_nameparser(char *pos)
{
switch (*pos) {
case '{':
return &parse_token_id;
case '"':
return &parse_token_literal;
default:
return &parse_token_pattern;
}
}
static struct tokenresult
parse_token(char *pos)
{
struct tknameresult nameres = choose_nameparser(pos)(pos);
struct stringresult actionres = parse_action(skiplinespace(nameres.pos));
return (struct tokenresult){
token_create(nameres.literal, nameres.name, actionres.s),
actionres.pos,
};
}
struct rulesresult {
struct token *tokens;
size_t ntok;
char *pos;
};
static struct rulesresult
parse_rules(char *pos)
{
size_t ntok = 0;
struct token *tokens = NULL;
for (; *pos != '\0' && strncmp(pos, "%%", 2) != 0 ; ntok++) {
struct tokenresult res = parse_token(pos);
pos = res.pos;
tokens = (struct token *)
realloc(tokens, sizeof(struct token) * (ntok + 1));
tokens[ntok] = *res.tk;
pos = skipws(pos);
}
return (struct rulesresult){tokens, ntok, pos};
}
static char *
parse_toeof(char *input)
{
char *s = input;
while (*s != '\0') {
s++;
}
return substr(input, s - input);
}
struct lexer *
parse(char *pos)
{
struct defsresult def = parse_defs(pos);
pos = def.pos;
if (strncmp(pos, "%%", 2) != 0) {
fprintf(stderr, "invalid transition to rules: '%.*s'\n", 10,
pos);
exit(1);
}
pos = skipws(pos + 2); /* %% */
struct rulesresult res = parse_rules(pos);
pos = res.pos;
char *post = "";
if (strncmp(pos, "%%", 2) == 0) {
pos += 2;
post = parse_toeof(pos);
}
return lexer_create(def.pre, post, def.patterns, def.npat, res.tokens,
res.ntok);
}