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
|
%define api.pure full
%define api.prefix {kalmia}
%locations
%define parse.error verbose
%param { yyscan_t scanner }
%code top {
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
}
%code requires {
typedef void* yyscan_t;
struct kai_attr_t {
char *key;
char *value;
struct kai_attr_t *next;
};
struct kai_tag_t {
char *type;
struct kai_attr_t *attrs;
struct kai_tag_t *children;
char *content;
struct kai_tag_t *next;
};
struct kai_tag_t * kai_parse_file(const char *filename);
void kai_tag_destroy(struct kai_tag_t *tag);
}
%parse-param { struct kai_tag_t **document }
%code {
/* flex definitions */
int yylex(YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_t scanner);
int kalmialex_init(yyscan_t *);
void kalmiaset_in(FILE *, yyscan_t);
int kalmialex_destroy(yyscan_t);
int yyerror(YYLTYPE *yyllocp, yyscan_t unused,
struct kai_tag_t **unused2, const char *msg);
#define TAG_MISMATCH(a, b) \
do { \
char buf[1024]; \
snprintf(buf, 1024, "Tag name mismatch: \"%s\" != \"%s\"", a, b); \
yyerror(&yylloc, NULL, NULL, (const char*) buf); \
YYABORT; \
} while (0)
/* create/destroy kai_attr_t */
struct kai_attr_t * kai_attr_new(char *key, char *value);
struct kai_attr_t * kai_attr_last(struct kai_attr_t *head);
void kai_attr_destroy(struct kai_attr_t *attr);
/* create/destroy kai_tag_t */
struct kai_tag_t * kai_tag_new(char *type, struct kai_attr_t *attrs);
struct kai_tag_t * kai_tag_last(struct kai_tag_t *head);
void kai_tag_destroy(struct kai_tag_t *tag);
}
%union {
char *string;
struct kai_attr_t *attr;
struct kai_tag_t *tag;
}
/* tokens */
%token PROLOG
%token S_TAG_OPEN E_TAG_OPEN TAG_CLOSE EMPTY_TAG_CLOSE
%token <string> NAME
%token <string> ATTR
%token <string> TEXT
%token <string> CONTENT
/* tag targets */
%type <tag> start_tag
%type <string> end_tag
%type <tag> empty_tag
%type <tag> tag
%type <tag> tags
/* attribute targets */
%type <attr> attribute
%type <attr> attributes
%%
document: PROLOG tag { *document = $2; }
tags:
tag { $$ = $1; }
| tags tag { $$ = $1; kai_tag_last($$)->next = $2; }
;
tag:
start_tag end_tag
{
$$ = $1;
if (strcmp($$->type, $2) != 0) TAG_MISMATCH($$->type, $2);
free($2);
}
| start_tag CONTENT end_tag
{
$$ = $1;
$$->content = $2;
if (strcmp($$->type, $3) != 0) TAG_MISMATCH($$->type, $3);
free($3);
}
| start_tag tags end_tag
{
$$ = $1;
$$->children = $2;
if (strcmp($$->type, $3) != 0) TAG_MISMATCH($$->type, $3);
free($3);
}
| empty_tag { $$ = $1; }
;
start_tag:
S_TAG_OPEN NAME TAG_CLOSE
{
$$ = kai_tag_new($2, NULL);
if ($$ == NULL) YYNOMEM;
}
| S_TAG_OPEN NAME attributes TAG_CLOSE
{
$$ = kai_tag_new($2, $3);
if ($$ == NULL) YYNOMEM;
}
;
empty_tag:
S_TAG_OPEN NAME EMPTY_TAG_CLOSE
{
$$ = kai_tag_new($2, NULL);
if ($$ == NULL) YYNOMEM;
}
| S_TAG_OPEN NAME attributes EMPTY_TAG_CLOSE
{
$$ = kai_tag_new($2, $3);
if ($$ == NULL) YYNOMEM;
}
;
end_tag:
E_TAG_OPEN NAME TAG_CLOSE { $$ = $2; }
;
attributes:
attribute { $$ = $1; }
| attributes attribute { $$ = $1; kai_attr_last($$)->next = $2; }
;
attribute:
ATTR '=' '"' TEXT '"'
{
$$ = kai_attr_new($1, $4);
if ($$ == NULL) YYNOMEM;
}
;
%%
/* print an error message */
int yyerror(
YYLTYPE *yyllocp,
yyscan_t unused,
struct kai_tag_t **unused2,
const char *msg)
{
fprintf(
stderr, "[%d:%d]: %s\n",
yyllocp->first_line, yyllocp->first_column, msg
);
return 1;
}
/* create a new kai_attr_t object */
struct kai_attr_t * kai_attr_new(char *key, char *value)
{
struct kai_attr_t *attr = malloc(sizeof(struct kai_attr_t));
if (attr == NULL) {
return NULL;
}
attr->key = key;
attr->value = value;
attr->next = NULL;
return attr;
}
/* get the end of a kai_attr_t list */
struct kai_attr_t * kai_attr_last(struct kai_attr_t *head)
{
struct kai_attr_t *ptr = head;
while (ptr->next != NULL) {
ptr = ptr->next;
}
return ptr;
}
/* destroy a kai_attr_t list */
void kai_attr_destroy(struct kai_attr_t *attr)
{
if (attr == NULL) {
return;
}
kai_attr_destroy(attr->next);
free(attr->key);
free(attr->value);
free(attr);
}
/* create a kai_tag_t object */
struct kai_tag_t * kai_tag_new(char *type, struct kai_attr_t *attrs)
{
struct kai_tag_t *tag = malloc(sizeof(struct kai_tag_t));
if (tag == NULL) {
return NULL;
}
tag->type = type;
tag->attrs = attrs;
tag->children = NULL;
tag->content = NULL;
tag->next = NULL;
return tag;
}
/* get the end of a kai_tag_t list */
struct kai_tag_t * kai_tag_last(struct kai_tag_t *head)
{
struct kai_tag_t *ptr = head;
while (ptr->next != NULL) {
ptr = ptr->next;
}
return ptr;
}
/* destroy a kai_tag_t list */
void kai_tag_destroy(struct kai_tag_t *tag)
{
if (tag == NULL) {
return;
}
free(tag->type);
kai_attr_destroy(tag->attrs);
kai_tag_destroy(tag->children);
free(tag->content);
kai_tag_destroy(tag->next);
free(tag);
}
/* parse a file into a document */
struct kai_tag_t * kai_parse_file(const char *filename)
{
FILE *file = fopen(filename, "r");
if (file == NULL) {
fprintf(stderr, "Could not open file \"%s\"\n", filename);
return NULL;
}
yyscan_t scanner;
struct kai_tag_t *document;
kalmialex_init(&scanner);
kalmiaset_in(file, scanner);
int result = kalmiaparse(scanner, &document);
kalmialex_destroy(scanner);
fclose(file);
if (result != 0) {
/* parse function will print error message */
return NULL;
}
return document;
}
|