0d7c9c17fe618ec68f562ca94e506ebae77a1604
[babeltrace.git] / formats / ctf / metadata / ctf-parser.y
1 %{
2 /*
3 * ctf.y
4 *
5 * Common Trace Format Metadata Grammar.
6 */
7
8 #include <stdio.h>
9 #include <unistd.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <assert.h>
13 #include <helpers/list.h>
14 #include <glib.h>
15 #include "ctf-parser.h"
16 #include "ctf-ast.h"
17
18 #define printf_dbg(fmt, args...) fprintf(stderr, "%s: " fmt, __func__, args)
19 #define printf_dbg_noarg(fmt) fprintf(stderr, "%s: " fmt, __func__)
20
21 int yyparse(void);
22 int yylex(void);
23
24 static CDS_LIST_HEAD(allocated_strings);
25 int yydebug;
26
27 struct scope;
28 struct scope {
29 struct scope *parent;
30 GHashTable *types;
31 };
32
33 struct gc_string {
34 struct cds_list_head gc;
35 char s[];
36 };
37
38 struct scope root_scope;
39 struct scope *cs = &root_scope; /* current scope */
40
41 char *strredup(char **dest, const char *src)
42 {
43 size_t len = strlen(src) + 1;
44
45 *dest = realloc(*dest, len);
46 if (!*dest)
47 return NULL;
48 strcpy(*dest, src);
49 return *dest;
50 }
51
52 static struct gc_string *gc_string_alloc(size_t len)
53 {
54 struct gc_string *gstr;
55
56 gstr = malloc(sizeof(*gstr) + len);
57 cds_list_add(&gstr->gc, &allocated_strings);
58 return gstr;
59 }
60
61 void setstring(const char *src)
62 {
63 yylval.gs = gc_string_alloc(strlen(src) + 1);
64 strcpy(yylval.gs->s, src);
65 }
66
67 static void init_scope(struct scope *scope, struct scope *parent)
68 {
69 scope->parent = parent;
70 scope->types = g_hash_table_new_full(g_str_hash, g_str_equal,
71 (GDestroyNotify) free, NULL);
72 }
73
74 static void finalize_scope(struct scope *scope)
75 {
76 g_hash_table_destroy(scope->types);
77 }
78
79 static void push_scope(void)
80 {
81 struct scope *ns;
82
83 printf_dbg_noarg("push scope\n");
84 ns = malloc(sizeof(struct scope));
85 init_scope(ns, cs);
86 cs = ns;
87 }
88
89 static void pop_scope(void)
90 {
91 struct scope *os;
92
93 printf_dbg_noarg("pop scope\n");
94 os = cs;
95 cs = os->parent;
96 finalize_scope(os);
97 free(os);
98 }
99
100 int lookup_type(struct scope *s, const char *id)
101 {
102 int ret;
103
104 ret = (int) g_hash_table_lookup(s->types, id);
105 printf_dbg("lookup %p %s %d\n", s, id, ret);
106 return ret;
107 }
108
109 int is_type(const char *id)
110 {
111 struct scope *it;
112 int ret = 0;
113
114 for (it = cs; it != NULL; it = it->parent) {
115 if (lookup_type(it, id)) {
116 ret = 1;
117 break;
118 }
119 }
120 printf_dbg("is type %s %d\n", id, ret);
121 return ret;
122 }
123
124 static void add_type(const char *id)
125 {
126 char *type_id = NULL;
127
128 printf_dbg("add type %s\n", id);
129 if (lookup_type(cs, id))
130 return;
131 strredup(&type_id, id);
132 g_hash_table_insert(cs->types, type_id, type_id);
133 }
134
135 void yyerror(const char *str)
136 {
137 fprintf(stderr, "error %s\n", str);
138 }
139
140 int yywrap(void)
141 {
142 return 1;
143 }
144
145 static void free_strings(void)
146 {
147 struct gc_string *gstr, *tmp;
148
149 cds_list_for_each_entry_safe(gstr, tmp, &allocated_strings, gc)
150 free(gstr);
151 }
152
153 int main(int argc, char **argv)
154 {
155 yydebug = 1;
156 init_scope(&root_scope, NULL);
157 yyparse();
158 finalize_scope(&root_scope);
159 free_strings();
160 return 0;
161 }
162
163 %}
164
165 %start file
166 %token CHARACTER_CONSTANT_START SQUOTE STRING_LITERAL_START DQUOTE ESCSEQ CHAR_STRING_TOKEN LSBRAC RSBRAC LPAREN RPAREN LBRAC RBRAC RARROW STAR PLUS MINUS LT GT TYPEASSIGN COLON SEMICOLON DOTDOTDOT DOT EQUAL COMMA CONST CHAR DOUBLE ENUM EVENT FLOATING_POINT FLOAT INTEGER INT LONG SHORT SIGNED STREAM STRING STRUCT TRACE TYPEALIAS TYPEDEF UNSIGNED VARIANT VOID _BOOL _COMPLEX _IMAGINARY DECIMAL_CONSTANT OCTAL_CONSTANT HEXADECIMAL_CONSTANT
167 %token <gs> IDENTIFIER ID_TYPE
168 %token ERROR
169 %union
170 {
171 long long ll;
172 char c;
173 struct gc_string *gs;
174 struct ctf_node *n;
175 }
176
177 %%
178
179 file:
180 declaration
181 | file declaration
182 ;
183
184 keywords:
185 VOID
186 | CHAR
187 | SHORT
188 | INT
189 | LONG
190 | FLOAT
191 | DOUBLE
192 | SIGNED
193 | UNSIGNED
194 | _BOOL
195 | _COMPLEX
196 | FLOATING_POINT
197 | INTEGER
198 | STRING
199 | ENUM
200 | VARIANT
201 | STRUCT
202 | CONST
203 | TYPEDEF
204 | EVENT
205 | STREAM
206 | TRACE
207 ;
208
209 /* 1.5 Constants */
210
211 c_char_sequence:
212 c_char
213 | c_char_sequence c_char
214 ;
215
216 c_char:
217 CHAR_STRING_TOKEN
218 | ESCSEQ
219 ;
220
221 /* 1.6 String literals */
222
223 s_char_sequence:
224 s_char
225 | s_char_sequence s_char
226 ;
227
228 s_char:
229 CHAR_STRING_TOKEN
230 | ESCSEQ
231 ;
232
233 /* 2: Phrase structure grammar */
234
235 postfix_expression:
236 IDENTIFIER
237 | ID_TYPE
238 | keywords
239 | DECIMAL_CONSTANT
240 | OCTAL_CONSTANT
241 | HEXADECIMAL_CONSTANT
242 | STRING_LITERAL_START DQUOTE
243 | STRING_LITERAL_START s_char_sequence DQUOTE
244 | CHARACTER_CONSTANT_START c_char_sequence SQUOTE
245 | LPAREN unary_expression RPAREN
246 | postfix_expression LSBRAC unary_expression RSBRAC
247 | postfix_expression DOT IDENTIFIER
248 | postfix_expression DOT ID_TYPE
249 | postfix_expression RARROW IDENTIFIER
250 | postfix_expression RARROW ID_TYPE
251 ;
252
253 unary_expression:
254 postfix_expression
255 | PLUS postfix_expression
256 | MINUS postfix_expression
257 ;
258
259 unary_expression_or_range:
260 unary_expression DOTDOTDOT unary_expression
261 | unary_expression
262 ;
263
264 /* 2.2: Declarations */
265
266 declaration:
267 declaration_specifiers SEMICOLON
268 | event_declaration
269 | stream_declaration
270 | trace_declaration
271 | declaration_specifiers TYPEDEF declaration_specifiers type_declarator_list SEMICOLON
272 | TYPEDEF declaration_specifiers type_declarator_list SEMICOLON
273 | declaration_specifiers TYPEDEF type_declarator_list SEMICOLON
274 | TYPEALIAS declaration_specifiers abstract_declarator_list COLON declaration_specifiers abstract_type_declarator_list SEMICOLON
275 | TYPEALIAS declaration_specifiers abstract_declarator_list COLON type_declarator_list SEMICOLON
276 ;
277
278 event_declaration:
279 event_declaration_begin event_declaration_end
280 | event_declaration_begin ctf_assignment_expression_list event_declaration_end
281 ;
282
283 event_declaration_begin:
284 EVENT LBRAC
285 {
286 push_scope();
287 }
288 ;
289
290 event_declaration_end:
291 RBRAC SEMICOLON
292 {
293 pop_scope();
294 }
295 ;
296
297
298 stream_declaration:
299 stream_declaration_begin stream_declaration_end
300 | stream_declaration_begin ctf_assignment_expression_list stream_declaration_end
301 ;
302
303 stream_declaration_begin:
304 STREAM LBRAC
305 {
306 push_scope();
307 }
308 ;
309
310 stream_declaration_end:
311 RBRAC SEMICOLON
312 {
313 pop_scope();
314 }
315 ;
316
317
318 trace_declaration:
319 trace_declaration_begin trace_declaration_end
320 | trace_declaration_begin ctf_assignment_expression_list trace_declaration_end
321 ;
322
323 trace_declaration_begin:
324 TRACE LBRAC
325 {
326 push_scope();
327 }
328 ;
329
330 trace_declaration_end:
331 RBRAC SEMICOLON
332 {
333 pop_scope();
334 }
335 ;
336
337 declaration_specifiers:
338 CONST
339 | type_specifier
340 | declaration_specifiers CONST
341 | declaration_specifiers type_specifier
342 ;
343
344 type_declarator_list:
345 type_declarator
346 | type_declarator_list COMMA type_declarator
347 ;
348
349 abstract_type_declarator_list:
350 abstract_type_declarator
351 | abstract_type_declarator_list COMMA abstract_type_declarator
352 ;
353
354 type_specifier:
355 VOID
356 | CHAR
357 | SHORT
358 | INT
359 | LONG
360 | FLOAT
361 | DOUBLE
362 | SIGNED
363 | UNSIGNED
364 | _BOOL
365 | _COMPLEX
366 | ID_TYPE
367 | FLOATING_POINT LBRAC RBRAC
368 | FLOATING_POINT LBRAC ctf_assignment_expression_list RBRAC
369 | INTEGER LBRAC RBRAC
370 | INTEGER LBRAC ctf_assignment_expression_list RBRAC
371 | STRING LBRAC RBRAC
372 | STRING LBRAC ctf_assignment_expression_list RBRAC
373 | ENUM enum_type_specifier
374 | VARIANT variant_type_specifier
375 | STRUCT struct_type_specifier
376 ;
377
378 struct_type_specifier:
379 struct_declaration_begin struct_or_variant_declaration_list struct_declaration_end
380 | IDENTIFIER struct_declaration_begin struct_or_variant_declaration_list struct_declaration_end
381 | ID_TYPE struct_declaration_begin struct_or_variant_declaration_list struct_declaration_end
382 | IDENTIFIER
383 | ID_TYPE
384 ;
385
386 struct_declaration_begin:
387 LBRAC
388 {
389 push_scope();
390 }
391 ;
392
393 struct_declaration_end:
394 RBRAC
395 {
396 pop_scope();
397 }
398 ;
399
400 variant_type_specifier:
401 variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
402 | LT IDENTIFIER GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
403 | LT ID_TYPE GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
404 | IDENTIFIER variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
405 | IDENTIFIER LT IDENTIFIER GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
406 | IDENTIFIER LT IDENTIFIER GT
407 | IDENTIFIER LT ID_TYPE GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
408 | IDENTIFIER LT ID_TYPE GT
409 | ID_TYPE variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
410 | ID_TYPE LT IDENTIFIER GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
411 | ID_TYPE LT IDENTIFIER GT
412 | ID_TYPE LT ID_TYPE GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
413 | ID_TYPE LT ID_TYPE GT
414 ;
415
416 variant_declaration_begin:
417 LBRAC
418 {
419 push_scope();
420 }
421 ;
422
423 variant_declaration_end:
424 RBRAC
425 {
426 pop_scope();
427 }
428 ;
429
430 type_specifier_or_integer_constant:
431 declaration_specifiers
432 | DECIMAL_CONSTANT
433 | OCTAL_CONSTANT
434 | HEXADECIMAL_CONSTANT
435 ;
436
437 enum_type_specifier:
438 LBRAC enumerator_list RBRAC
439 | LT type_specifier_or_integer_constant GT LBRAC enumerator_list RBRAC
440 | IDENTIFIER LBRAC enumerator_list RBRAC
441 | IDENTIFIER LT type_specifier_or_integer_constant GT LBRAC enumerator_list RBRAC
442 | ID_TYPE LBRAC enumerator_list RBRAC
443 | ID_TYPE LT type_specifier_or_integer_constant GT LBRAC enumerator_list RBRAC
444 | LBRAC enumerator_list COMMA RBRAC
445 | LT type_specifier_or_integer_constant GT LBRAC enumerator_list COMMA RBRAC
446 | IDENTIFIER LBRAC enumerator_list COMMA RBRAC
447 | IDENTIFIER LT type_specifier_or_integer_constant GT LBRAC enumerator_list COMMA RBRAC
448 | IDENTIFIER
449 | IDENTIFIER LT type_specifier_or_integer_constant GT
450 | ID_TYPE LBRAC enumerator_list COMMA RBRAC
451 | ID_TYPE LT type_specifier_or_integer_constant GT LBRAC enumerator_list COMMA RBRAC
452 | ID_TYPE
453 | ID_TYPE LT type_specifier_or_integer_constant GT
454 ;
455
456 struct_or_variant_declaration_list:
457 /* empty */
458 | struct_or_variant_declaration_list struct_or_variant_declaration
459 ;
460
461 struct_or_variant_declaration:
462 specifier_qualifier_list struct_or_variant_declarator_list SEMICOLON
463 | specifier_qualifier_list TYPEDEF specifier_qualifier_list type_declarator_list SEMICOLON
464 | TYPEDEF specifier_qualifier_list type_declarator_list SEMICOLON
465 | specifier_qualifier_list TYPEDEF type_declarator_list SEMICOLON
466 | TYPEALIAS specifier_qualifier_list abstract_declarator_list COLON specifier_qualifier_list abstract_type_declarator_list SEMICOLON
467 | TYPEALIAS specifier_qualifier_list abstract_declarator_list COLON type_declarator_list SEMICOLON
468 ;
469
470 specifier_qualifier_list:
471 CONST
472 | type_specifier
473 | specifier_qualifier_list CONST
474 | specifier_qualifier_list type_specifier
475 ;
476
477 struct_or_variant_declarator_list:
478 struct_or_variant_declarator
479 | struct_or_variant_declarator_list COMMA struct_or_variant_declarator
480 ;
481
482 struct_or_variant_declarator:
483 declarator
484 | COLON unary_expression
485 | declarator COLON unary_expression
486 ;
487
488 enumerator_list:
489 enumerator
490 | enumerator_list COMMA enumerator
491 ;
492
493 enumerator:
494 IDENTIFIER
495 | ID_TYPE
496 | keywords
497 | STRING_LITERAL_START DQUOTE
498 | STRING_LITERAL_START s_char_sequence DQUOTE
499 | IDENTIFIER EQUAL unary_expression_or_range
500 | ID_TYPE EQUAL unary_expression_or_range
501 | keywords EQUAL unary_expression_or_range
502 | STRING_LITERAL_START DQUOTE EQUAL unary_expression_or_range
503 | STRING_LITERAL_START s_char_sequence DQUOTE EQUAL unary_expression_or_range
504 ;
505
506 abstract_declarator_list:
507 abstract_declarator
508 | abstract_declarator_list COMMA abstract_declarator
509 ;
510
511 abstract_declarator:
512 direct_abstract_declarator
513 | pointer direct_abstract_declarator
514 ;
515
516 direct_abstract_declarator:
517 /* empty */
518 | IDENTIFIER
519 | LPAREN abstract_declarator RPAREN
520 | direct_abstract_declarator LSBRAC type_specifier_or_integer_constant RSBRAC
521 | direct_abstract_declarator LSBRAC RSBRAC
522 ;
523
524 declarator:
525 direct_declarator
526 | pointer direct_declarator
527 ;
528
529 direct_declarator:
530 IDENTIFIER
531 | LPAREN declarator RPAREN
532 | direct_declarator LSBRAC type_specifier_or_integer_constant RSBRAC
533 ;
534
535 type_declarator:
536 direct_type_declarator
537 | pointer direct_type_declarator
538 ;
539
540 direct_type_declarator:
541 IDENTIFIER
542 {
543 add_type($1->s);
544 }
545 | LPAREN type_declarator RPAREN
546 | direct_type_declarator LSBRAC type_specifier_or_integer_constant RSBRAC
547 ;
548
549 abstract_type_declarator:
550 direct_abstract_type_declarator
551 | pointer direct_abstract_type_declarator
552 ;
553
554 direct_abstract_type_declarator:
555 /* empty */
556 | IDENTIFIER
557 {
558 add_type($1->s);
559 }
560 | LPAREN abstract_type_declarator RPAREN
561 | direct_abstract_type_declarator LSBRAC type_specifier_or_integer_constant RSBRAC
562 | direct_abstract_type_declarator LSBRAC RSBRAC
563 ;
564
565 pointer:
566 STAR
567 | STAR pointer
568 | STAR type_qualifier_list pointer
569 ;
570
571 type_qualifier_list:
572 CONST
573 | type_qualifier_list CONST
574 ;
575
576 /* 2.3: CTF-specific declarations */
577
578 ctf_assignment_expression_list:
579 ctf_assignment_expression SEMICOLON
580 | ctf_assignment_expression_list ctf_assignment_expression SEMICOLON
581 ;
582
583 ctf_assignment_expression:
584 unary_expression EQUAL unary_expression
585 | unary_expression TYPEASSIGN type_specifier
586 | declaration_specifiers TYPEDEF declaration_specifiers type_declarator_list
587 | TYPEDEF declaration_specifiers type_declarator_list
588 | declaration_specifiers TYPEDEF type_declarator_list
589 | TYPEALIAS declaration_specifiers abstract_declarator_list COLON declaration_specifiers abstract_type_declarator_list
590 | TYPEALIAS declaration_specifiers abstract_declarator_list COLON type_declarator_list
591 ;
This page took 0.046029 seconds and 3 git commands to generate.