-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen.c
More file actions
198 lines (190 loc) · 4 KB
/
gen.c
File metadata and controls
198 lines (190 loc) · 4 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
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>
#include<string.h>
#include "automata.h"
#include "parse.h"
#include "gen.h"
#include "lex_gen.c"
static void
gen_imports(FILE *out)
{
for (int i = 0; i < lex_gen_file_len; i++) {
fputc(lex_gen_file[i], out);
}
}
void
yyin(FILE *out)
{
fprintf(out,
"char *yyin = NULL;\n"
"\n"
"void\n"
"yy_scan_string(char *s)\n"
"{\n"
" int len = strlen(s) + 1;\n"
" yyin = (char *) malloc(sizeof(char) * len);\n"
" snprintf(yyin, len, \"%%s\", s);\n"
"}\n");
}
static void
fprintliteral(FILE *out, char *s, bool comment)
{
int len = strlen(s);
fprintf(out, "(char []){");
for (int i = 0; i < len; i++) {
fprintf(out, "%d,", s[i]);
}
fprintf(out, "0}");
if (comment) {
fprintf(out, " /* %s */", s);
}
}
static void
yyfsmlistprep(FILE *out, struct pattern *p, size_t npat, struct token *t,
size_t ntok)
{
fprintf(out,
"static struct fsm *\n"
"gettokenfsm(struct token *t, struct fsmlist *l)\n"
"{\n"
" if (t->literal) {\n"
" /* TODO: create token */\n"
" fprintf(stderr, \"literal tokens NOT IMPLEMENTED\\n\");\n"
" exit(1);\n"
" }\n"
" struct fsm *s = fsmlist_findfsm(l, t->name);\n"
" if (NULL == s) {\n"
" fprintf(stderr, \"cannot find pattern for '%%s'\\n\", t->name);\n"
" exit(1);\n"
" }\n"
" return fsm_copy(s);\n"
"}\n"
"\n"
"struct fsmlist *yyfsmlist = NULL;\n"
"unsigned long yyleng = 0;\n"
"char *yytext = NULL;\n"
"\n"
"static void\n"
"yyfsmlistprep()\n"
"{\n");
fprintf(out,
" struct pattern p[] = {\n");
for (int i = 0; i < npat; i++) {
fprintf(out,
" {\"%s\",\t", p[i].name);
fprintliteral(out, p[i].pattern, true);
fprintf(out, "},\n");
}
fprintf(out,
" };\n"
" struct fsmlist *l = NULL;\n"
" for (int i = 0; i < %lu; i++) {\n", npat);
fprintf(out,
" struct fsm *s = fsm_fromstring(p[i].pattern, l);\n"
" l = fsmlist_append(l, p[i].name, s);\n"
" };\n"
" struct token t[] = {\n");
for (int i = 0; i < ntok; i++) {
fprintf(out,
" {%s,\t\"%s\",\t", (t[i].literal ? "true" : "false"), t[i].name);
fprintliteral(out, t[i].action, false);
fprintf(out, "},\n");
}
fprintf(out,
" };\n"
" for (int i = 0; i < %lu; i++) {\n", ntok);
fprintf(out,
" yyfsmlist = fsmlist_append(yyfsmlist, t[i].name, gettokenfsm(&t[i], l));\n"
" };\n"
" fsmlist_destroy(l);\n"
"}\n");
}
static bool
tokens_hasname(struct token *tokens, size_t len, char *name)
{
for (int i = 0; i < len; i++) {
if (strcmp(tokens[i].name, name) == 0) {
return true;
}
}
return false;
}
static void
yylex(FILE *out, struct pattern *p, size_t npat, struct token *t, size_t ntok)
{
yyin(out);
fprintf(out,
"\n");
yyfsmlistprep(out, p, npat, t, ntok);
fprintf(out,
"\n"
"int\n"
"yylex()\n"
"{\n");
fprintf(out,
" if (NULL == yyfsmlist) {\n"
" yyfsmlistprep();\n"
" }\n"
" struct findresult *r = fsmlist_findnext(yyfsmlist, yyin);\n"
" yyleng = r->len; yytext = yyin;\n"
" yyin += yyleng;\n"
" if (yyleng == 0) {\n"
" assert(*yyin == '\\0');\n"
" return 0;\n"
" }\n"
" if (r->fsm == NULL) {\n"
" fprintf(stderr, \"unmatched '%%.*s'\\n\", (int) yyleng, yytext);\n"
" exit(1);\n"
" }\n"
" /* ⊢ r->fsm != NULL && yyleng > 0 */\n");
for (int i = 0; i < ntok; i++) {
char *els = i == 0 ? "" : "} else ";
struct token tk = t[i];
if (i == 0) {
fprintf(out,
" if (strcmp(r->fsm, \"%s\") == 0) {\n"
" %s\n", tk.name, tk.action);
} else {
fprintf(out,
" } else if (strcmp(r->fsm, \"%s\") == 0) {\n"
" %s\n", tk.name, tk.action);
}
}
fprintf(out,
" }\n"
" /* recurse if no return-action above */\n"
" return yylex();\n"
"}\n");
}
void
gen(FILE *out, struct lexer *lx, bool imports)
{
fprintf(out,
"/* BEGIN */\n"
"\n");
if (imports) {
fprintf(out,
"/* BEGIN lex_gen.c */\n");
gen_imports(out);
fprintf(out,
"/* END lex_gen.c */\n");
}
fprintf(out,
"/* BEGIN preamble */\n"
"%s\n"
"/* END preamble */\n", lx->pre);
fprintf(out,
"/* BEGIN lexer */\n");
yylex(out, lx->patterns, lx->npat, lx->tokens, lx->ntok);
fprintf(out,
"/* END lexer */\n");
fprintf(out,
"/* BEGIN postamble */\n"
"%s\n"
"/* END postamble */\n", lx->post);
fprintf(out,
"\n"
"/* END */\n");
}