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