Commit | Line | Data |
---|---|---|
953192ba MD |
1 | %{ |
2 | /* | |
3 | * filter-parser.y | |
4 | * | |
5 | * LTTng filter expression parser | |
6 | * | |
ab5be9fa | 7 | * Copyright 2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
953192ba | 8 | * |
ab5be9fa | 9 | * SPDX-License-Identifier: LGPL-2.1-only |
953192ba MD |
10 | * |
11 | * Grammar inspired from http://www.quut.com/c/ANSI-C-grammar-y.html | |
12 | */ | |
13 | ||
14 | #include <stdio.h> | |
15 | #include <unistd.h> | |
16 | #include <string.h> | |
17 | #include <stdlib.h> | |
18 | #include <assert.h> | |
953192ba | 19 | #include <inttypes.h> |
953192ba | 20 | #include "filter-ast.h" |
95b9bd90 | 21 | #include "filter-parser.h" |
e4d2f27a JR |
22 | #include "filter-bytecode.h" |
23 | #include "memstream.h" | |
953192ba | 24 | |
edf4b93e | 25 | #include <common/compat/errno.h> |
a187da1a DG |
26 | #include <common/macros.h> |
27 | ||
8ab7c0d9 MD |
28 | #define WIDTH_u64_SCANF_IS_A_BROKEN_API "20" |
29 | #define WIDTH_o64_SCANF_IS_A_BROKEN_API "22" | |
30 | #define WIDTH_x64_SCANF_IS_A_BROKEN_API "17" | |
31 | #define WIDTH_lg_SCANF_IS_A_BROKEN_API "4096" /* Hugely optimistic approximation */ | |
32 | ||
e4d2f27a JR |
33 | #ifdef DEBUG |
34 | static const int print_xml = 1; | |
35 | #define dbg_printf(fmt, args...) \ | |
36 | printf("[debug filter_parser] " fmt, ## args) | |
37 | #else | |
38 | static const int print_xml = 0; | |
39 | #define dbg_printf(fmt, args...) \ | |
40 | do { \ | |
41 | /* do nothing but check printf format */ \ | |
42 | if (0) \ | |
43 | printf("[debug filter_parser] " fmt, ## args); \ | |
44 | } while (0) | |
45 | #endif | |
46 | ||
a187da1a | 47 | LTTNG_HIDDEN |
953192ba | 48 | int yydebug; |
a187da1a | 49 | LTTNG_HIDDEN |
953192ba MD |
50 | int filter_parser_debug = 0; |
51 | ||
a187da1a | 52 | LTTNG_HIDDEN |
9039edd4 | 53 | int yyparse(struct filter_parser_ctx *parser_ctx, yyscan_t scanner); |
a187da1a | 54 | LTTNG_HIDDEN |
9039edd4 | 55 | int yylex(union YYSTYPE *yyval, yyscan_t scanner); |
a187da1a | 56 | LTTNG_HIDDEN |
953192ba | 57 | int yylex_init_extra(struct filter_parser_ctx *parser_ctx, yyscan_t * ptr_yy_globals); |
a187da1a | 58 | LTTNG_HIDDEN |
953192ba | 59 | int yylex_destroy(yyscan_t yyparser_ctx); |
a187da1a | 60 | LTTNG_HIDDEN |
953192ba MD |
61 | void yyrestart(FILE * in_str, yyscan_t parser_ctx); |
62 | ||
63 | struct gc_string { | |
64 | struct cds_list_head gc; | |
65 | size_t alloclen; | |
66 | char s[]; | |
67 | }; | |
68 | ||
69 | static const char *node_type_to_str[] = { | |
70 | [ NODE_UNKNOWN ] = "NODE_UNKNOWN", | |
71 | [ NODE_ROOT ] = "NODE_ROOT", | |
72 | [ NODE_EXPRESSION ] = "NODE_EXPRESSION", | |
73 | [ NODE_OP ] = "NODE_OP", | |
74 | [ NODE_UNARY_OP ] = "NODE_UNARY_OP", | |
75 | }; | |
76 | ||
a187da1a | 77 | LTTNG_HIDDEN |
953192ba MD |
78 | const char *node_type(struct filter_node *node) |
79 | { | |
80 | if (node->type < NR_NODE_TYPES) | |
81 | return node_type_to_str[node->type]; | |
82 | else | |
83 | return NULL; | |
84 | } | |
85 | ||
86 | static struct gc_string *gc_string_alloc(struct filter_parser_ctx *parser_ctx, | |
87 | size_t len) | |
88 | { | |
89 | struct gc_string *gstr; | |
90 | size_t alloclen; | |
91 | ||
92 | /* TODO: could be faster with find first bit or glib Gstring */ | |
93 | /* sizeof long to account for malloc header (int or long ?) */ | |
94 | for (alloclen = 8; alloclen < sizeof(long) + sizeof(*gstr) + len; | |
95 | alloclen *= 2); | |
96 | ||
be61ee34 MD |
97 | gstr = zmalloc(alloclen); |
98 | if (!gstr) { | |
99 | goto end; | |
100 | } | |
953192ba MD |
101 | cds_list_add(&gstr->gc, &parser_ctx->allocated_strings); |
102 | gstr->alloclen = alloclen; | |
be61ee34 | 103 | end: |
953192ba MD |
104 | return gstr; |
105 | } | |
106 | ||
107 | /* | |
108 | * note: never use gc_string_append on a string that has external references. | |
109 | * gsrc will be garbage collected immediately, and gstr might be. | |
110 | * Should only be used to append characters to a string literal or constant. | |
111 | */ | |
52dc69b2 | 112 | static |
953192ba MD |
113 | struct gc_string *gc_string_append(struct filter_parser_ctx *parser_ctx, |
114 | struct gc_string *gstr, | |
115 | struct gc_string *gsrc) | |
116 | { | |
117 | size_t newlen = strlen(gsrc->s) + strlen(gstr->s) + 1; | |
118 | size_t alloclen; | |
119 | ||
120 | /* TODO: could be faster with find first bit or glib Gstring */ | |
121 | /* sizeof long to account for malloc header (int or long ?) */ | |
122 | for (alloclen = 8; alloclen < sizeof(long) + sizeof(*gstr) + newlen; | |
123 | alloclen *= 2); | |
124 | ||
125 | if (alloclen > gstr->alloclen) { | |
126 | struct gc_string *newgstr; | |
127 | ||
128 | newgstr = gc_string_alloc(parser_ctx, newlen); | |
129 | strcpy(newgstr->s, gstr->s); | |
130 | strcat(newgstr->s, gsrc->s); | |
131 | cds_list_del(&gstr->gc); | |
132 | free(gstr); | |
133 | gstr = newgstr; | |
134 | } else { | |
135 | strcat(gstr->s, gsrc->s); | |
136 | } | |
137 | cds_list_del(&gsrc->gc); | |
138 | free(gsrc); | |
139 | return gstr; | |
140 | } | |
141 | ||
a187da1a | 142 | LTTNG_HIDDEN |
953192ba MD |
143 | void setstring(struct filter_parser_ctx *parser_ctx, YYSTYPE *lvalp, const char *src) |
144 | { | |
145 | lvalp->gs = gc_string_alloc(parser_ctx, strlen(src) + 1); | |
146 | strcpy(lvalp->gs->s, src); | |
147 | } | |
148 | ||
149 | static struct filter_node *make_node(struct filter_parser_ctx *scanner, | |
150 | enum node_type type) | |
151 | { | |
152 | struct filter_ast *ast = filter_parser_get_ast(scanner); | |
153 | struct filter_node *node; | |
154 | ||
be61ee34 | 155 | node = zmalloc(sizeof(*node)); |
953192ba MD |
156 | if (!node) |
157 | return NULL; | |
158 | memset(node, 0, sizeof(*node)); | |
159 | node->type = type; | |
160 | cds_list_add(&node->gc, &ast->allocated_nodes); | |
161 | ||
162 | switch (type) { | |
163 | case NODE_ROOT: | |
164 | fprintf(stderr, "[error] %s: trying to create root node\n", __func__); | |
165 | break; | |
166 | ||
167 | case NODE_EXPRESSION: | |
168 | break; | |
169 | case NODE_OP: | |
170 | break; | |
171 | case NODE_UNARY_OP: | |
172 | break; | |
173 | ||
174 | case NODE_UNKNOWN: | |
175 | default: | |
176 | fprintf(stderr, "[error] %s: unknown node type %d\n", __func__, | |
177 | (int) type); | |
178 | break; | |
179 | } | |
180 | ||
181 | return node; | |
182 | } | |
183 | ||
184 | static struct filter_node *make_op_node(struct filter_parser_ctx *scanner, | |
185 | enum op_type type, | |
186 | struct filter_node *lchild, | |
187 | struct filter_node *rchild) | |
188 | { | |
189 | struct filter_ast *ast = filter_parser_get_ast(scanner); | |
190 | struct filter_node *node; | |
191 | ||
be61ee34 | 192 | node = zmalloc(sizeof(*node)); |
953192ba MD |
193 | if (!node) |
194 | return NULL; | |
195 | memset(node, 0, sizeof(*node)); | |
196 | node->type = NODE_OP; | |
197 | cds_list_add(&node->gc, &ast->allocated_nodes); | |
198 | node->u.op.type = type; | |
199 | node->u.op.lchild = lchild; | |
200 | node->u.op.rchild = rchild; | |
201 | return node; | |
202 | } | |
203 | ||
52dc69b2 | 204 | static |
9039edd4 | 205 | void yyerror(struct filter_parser_ctx *parser_ctx, yyscan_t scanner, const char *str) |
953192ba MD |
206 | { |
207 | fprintf(stderr, "error %s\n", str); | |
208 | } | |
953192ba MD |
209 | |
210 | #define parse_error(parser_ctx, str) \ | |
211 | do { \ | |
9039edd4 | 212 | yyerror(parser_ctx, parser_ctx->scanner, YY_("parse error: " str "\n")); \ |
953192ba MD |
213 | YYERROR; \ |
214 | } while (0) | |
215 | ||
216 | static void free_strings(struct cds_list_head *list) | |
217 | { | |
218 | struct gc_string *gstr, *tmp; | |
219 | ||
220 | cds_list_for_each_entry_safe(gstr, tmp, list, gc) | |
221 | free(gstr); | |
222 | } | |
223 | ||
224 | static struct filter_ast *filter_ast_alloc(void) | |
225 | { | |
226 | struct filter_ast *ast; | |
227 | ||
be61ee34 | 228 | ast = zmalloc(sizeof(*ast)); |
953192ba MD |
229 | if (!ast) |
230 | return NULL; | |
231 | memset(ast, 0, sizeof(*ast)); | |
232 | CDS_INIT_LIST_HEAD(&ast->allocated_nodes); | |
233 | ast->root.type = NODE_ROOT; | |
234 | return ast; | |
235 | } | |
236 | ||
237 | static void filter_ast_free(struct filter_ast *ast) | |
238 | { | |
239 | struct filter_node *node, *tmp; | |
240 | ||
241 | cds_list_for_each_entry_safe(node, tmp, &ast->allocated_nodes, gc) | |
242 | free(node); | |
37600d79 | 243 | free(ast); |
953192ba MD |
244 | } |
245 | ||
a187da1a | 246 | LTTNG_HIDDEN |
953192ba MD |
247 | int filter_parser_ctx_append_ast(struct filter_parser_ctx *parser_ctx) |
248 | { | |
9039edd4 | 249 | return yyparse(parser_ctx, parser_ctx->scanner); |
953192ba MD |
250 | } |
251 | ||
a187da1a | 252 | LTTNG_HIDDEN |
953192ba MD |
253 | struct filter_parser_ctx *filter_parser_ctx_alloc(FILE *input) |
254 | { | |
255 | struct filter_parser_ctx *parser_ctx; | |
256 | int ret; | |
257 | ||
258 | yydebug = filter_parser_debug; | |
259 | ||
be61ee34 | 260 | parser_ctx = zmalloc(sizeof(*parser_ctx)); |
953192ba MD |
261 | if (!parser_ctx) |
262 | return NULL; | |
263 | memset(parser_ctx, 0, sizeof(*parser_ctx)); | |
264 | ||
265 | ret = yylex_init_extra(parser_ctx, &parser_ctx->scanner); | |
266 | if (ret) { | |
267 | fprintf(stderr, "yylex_init error\n"); | |
268 | goto cleanup_parser_ctx; | |
269 | } | |
270 | /* Start processing new stream */ | |
271 | yyrestart(input, parser_ctx->scanner); | |
272 | ||
273 | parser_ctx->ast = filter_ast_alloc(); | |
274 | if (!parser_ctx->ast) | |
275 | goto cleanup_lexer; | |
276 | CDS_INIT_LIST_HEAD(&parser_ctx->allocated_strings); | |
277 | ||
278 | if (yydebug) | |
279 | fprintf(stdout, "parser_ctx input is a%s.\n", | |
280 | isatty(fileno(input)) ? "n interactive tty" : | |
281 | " noninteractive file"); | |
282 | ||
283 | return parser_ctx; | |
284 | ||
285 | cleanup_lexer: | |
286 | ret = yylex_destroy(parser_ctx->scanner); | |
287 | if (!ret) | |
288 | fprintf(stderr, "yylex_destroy error\n"); | |
289 | cleanup_parser_ctx: | |
290 | free(parser_ctx); | |
291 | return NULL; | |
292 | } | |
293 | ||
a187da1a | 294 | LTTNG_HIDDEN |
953192ba MD |
295 | void filter_parser_ctx_free(struct filter_parser_ctx *parser_ctx) |
296 | { | |
297 | int ret; | |
298 | ||
299 | free_strings(&parser_ctx->allocated_strings); | |
300 | filter_ast_free(parser_ctx->ast); | |
301 | ret = yylex_destroy(parser_ctx->scanner); | |
302 | if (ret) | |
303 | fprintf(stderr, "yylex_destroy error\n"); | |
304 | free(parser_ctx); | |
305 | } | |
306 | ||
e4d2f27a JR |
307 | LTTNG_HIDDEN |
308 | int filter_parser_ctx_create_from_filter_expression( | |
309 | const char *filter_expression, struct filter_parser_ctx **ctxp) | |
310 | { | |
311 | int ret; | |
312 | struct filter_parser_ctx *ctx = NULL; | |
313 | FILE *fmem = NULL; | |
314 | ||
315 | assert(filter_expression); | |
316 | assert(ctxp); | |
317 | ||
318 | /* | |
319 | * Casting const to non-const, as the underlying function will use it in | |
320 | * read-only mode. | |
321 | */ | |
322 | fmem = lttng_fmemopen((void *) filter_expression, | |
323 | strlen(filter_expression), "r"); | |
324 | if (!fmem) { | |
325 | fprintf(stderr, "Error opening memory as stream\n"); | |
326 | ret = -LTTNG_ERR_FILTER_NOMEM; | |
327 | goto error; | |
328 | } | |
329 | ctx = filter_parser_ctx_alloc(fmem); | |
330 | if (!ctx) { | |
331 | fprintf(stderr, "Error allocating parser\n"); | |
332 | ret = -LTTNG_ERR_FILTER_NOMEM; | |
333 | goto filter_alloc_error; | |
334 | } | |
335 | ret = filter_parser_ctx_append_ast(ctx); | |
336 | if (ret) { | |
337 | fprintf(stderr, "Parse error\n"); | |
338 | ret = -LTTNG_ERR_FILTER_INVAL; | |
339 | goto parse_error; | |
340 | } | |
341 | if (print_xml) { | |
342 | ret = filter_visitor_print_xml(ctx, stdout, 0); | |
343 | if (ret) { | |
344 | fflush(stdout); | |
345 | fprintf(stderr, "XML print error\n"); | |
346 | ret = -LTTNG_ERR_FILTER_INVAL; | |
347 | goto parse_error; | |
348 | } | |
349 | } | |
350 | ||
351 | dbg_printf("Generating IR... "); | |
352 | fflush(stdout); | |
353 | ret = filter_visitor_ir_generate(ctx); | |
354 | if (ret) { | |
355 | fprintf(stderr, "Generate IR error\n"); | |
356 | ret = -LTTNG_ERR_FILTER_INVAL; | |
357 | goto parse_error; | |
358 | } | |
359 | dbg_printf("done\n"); | |
360 | ||
361 | dbg_printf("Validating IR... "); | |
362 | fflush(stdout); | |
363 | ret = filter_visitor_ir_check_binary_op_nesting(ctx); | |
364 | if (ret) { | |
365 | ret = -LTTNG_ERR_FILTER_INVAL; | |
366 | goto parse_error; | |
367 | } | |
368 | ||
369 | /* Normalize globbing patterns in the expression. */ | |
370 | ret = filter_visitor_ir_normalize_glob_patterns(ctx); | |
371 | if (ret) { | |
372 | ret = -LTTNG_ERR_FILTER_INVAL; | |
373 | goto parse_error; | |
374 | } | |
375 | ||
376 | /* Validate strings used as literals in the expression. */ | |
377 | ret = filter_visitor_ir_validate_string(ctx); | |
378 | if (ret) { | |
379 | ret = -LTTNG_ERR_FILTER_INVAL; | |
380 | goto parse_error; | |
381 | } | |
382 | ||
383 | /* Validate globbing patterns in the expression. */ | |
384 | ret = filter_visitor_ir_validate_globbing(ctx); | |
385 | if (ret) { | |
386 | ret = -LTTNG_ERR_FILTER_INVAL; | |
387 | goto parse_error; | |
388 | } | |
389 | ||
390 | dbg_printf("done\n"); | |
391 | ||
392 | dbg_printf("Generating bytecode... "); | |
393 | fflush(stdout); | |
394 | ret = filter_visitor_bytecode_generate(ctx); | |
395 | if (ret) { | |
396 | fprintf(stderr, "Generate bytecode error\n"); | |
397 | ret = -LTTNG_ERR_FILTER_INVAL; | |
398 | goto parse_error; | |
399 | } | |
400 | dbg_printf("done\n"); | |
401 | dbg_printf("Size of bytecode generated: %u bytes.\n", | |
402 | bytecode_get_len(&ctx->bytecode->b)); | |
403 | ||
404 | /* No need to keep the memory stream. */ | |
405 | if (fclose(fmem) != 0) { | |
406 | fprintf(stderr, "fclose (%d) \n", errno); | |
407 | ret = -LTTNG_ERR_FILTER_INVAL; | |
408 | } | |
409 | ||
410 | *ctxp = ctx; | |
411 | return 0; | |
412 | ||
413 | parse_error: | |
414 | filter_ir_free(ctx); | |
415 | filter_parser_ctx_free(ctx); | |
416 | filter_alloc_error: | |
417 | if (fclose(fmem) != 0) { | |
418 | fprintf(stderr, "fclose (%d) \n", errno); | |
419 | } | |
420 | error: | |
421 | return ret; | |
422 | } | |
423 | ||
953192ba MD |
424 | %} |
425 | ||
52dc69b2 SM |
426 | %code provides |
427 | { | |
428 | #include "common/macros.h" | |
429 | ||
430 | LTTNG_HIDDEN | |
431 | void setstring(struct filter_parser_ctx *parser_ctx, YYSTYPE *lvalp, const char *src); | |
432 | } | |
433 | ||
953192ba MD |
434 | %define api.pure |
435 | /* %locations */ | |
436 | %parse-param {struct filter_parser_ctx *parser_ctx} | |
9039edd4 ZT |
437 | %parse-param {yyscan_t scanner} |
438 | %lex-param {yyscan_t scanner} | |
953192ba MD |
439 | %start translation_unit |
440 | %token CHARACTER_CONSTANT_START SQUOTE STRING_LITERAL_START DQUOTE | |
441 | %token ESCSEQ CHAR_STRING_TOKEN | |
e90d8561 | 442 | %token DECIMAL_CONSTANT OCTAL_CONSTANT HEXADECIMAL_CONSTANT FLOAT_CONSTANT |
953192ba MD |
443 | %token LSBRAC RSBRAC LPAREN RPAREN LBRAC RBRAC RARROW |
444 | %token STAR PLUS MINUS | |
445 | %token MOD_OP DIV_OP RIGHT_OP LEFT_OP | |
446 | %token EQ_OP NE_OP LE_OP GE_OP LT_OP GT_OP AND_OP OR_OP NOT_OP | |
447 | %token ASSIGN COLON SEMICOLON DOTDOTDOT DOT EQUAL COMMA | |
448 | %token XOR_BIN AND_BIN OR_BIN NOT_BIN | |
449 | ||
586dc72f | 450 | %token <gs> IDENTIFIER GLOBAL_IDENTIFIER |
953192ba MD |
451 | %token ERROR |
452 | %union | |
453 | { | |
454 | long long ll; | |
455 | char c; | |
456 | struct gc_string *gs; | |
457 | struct filter_node *n; | |
458 | } | |
459 | ||
460 | %type <gs> s_char s_char_sequence c_char c_char_sequence | |
461 | ||
462 | %type <n> primary_expression | |
bff988fa MD |
463 | %type <n> prefix_expression |
464 | %type <n> prefix_expression_rec | |
953192ba MD |
465 | %type <n> postfix_expression |
466 | %type <n> unary_expression | |
467 | %type <n> unary_operator | |
468 | %type <n> multiplicative_expression | |
469 | %type <n> additive_expression | |
470 | %type <n> shift_expression | |
471 | %type <n> relational_expression | |
472 | %type <n> equality_expression | |
473 | %type <n> and_expression | |
474 | %type <n> exclusive_or_expression | |
475 | %type <n> inclusive_or_expression | |
476 | %type <n> logical_and_expression | |
477 | %type <n> logical_or_expression | |
478 | %type <n> expression | |
bff988fa | 479 | %type <n> identifiers |
953192ba MD |
480 | |
481 | %% | |
482 | ||
483 | ||
484 | /* 1.5 Constants */ | |
485 | ||
486 | c_char_sequence: | |
487 | c_char | |
488 | { $$ = $1; } | |
489 | | c_char_sequence c_char | |
490 | { $$ = gc_string_append(parser_ctx, $1, $2); } | |
491 | ; | |
492 | ||
493 | c_char: | |
494 | CHAR_STRING_TOKEN | |
495 | { $$ = yylval.gs; } | |
496 | | ESCSEQ | |
497 | { | |
498 | parse_error(parser_ctx, "escape sequences not supported yet"); | |
499 | } | |
500 | ; | |
501 | ||
502 | /* 1.6 String literals */ | |
503 | ||
504 | s_char_sequence: | |
505 | s_char | |
506 | { $$ = $1; } | |
507 | | s_char_sequence s_char | |
508 | { $$ = gc_string_append(parser_ctx, $1, $2); } | |
509 | ; | |
510 | ||
511 | s_char: | |
512 | CHAR_STRING_TOKEN | |
513 | { $$ = yylval.gs; } | |
514 | | ESCSEQ | |
515 | { | |
516 | parse_error(parser_ctx, "escape sequences not supported yet"); | |
517 | } | |
518 | ; | |
519 | ||
bff988fa MD |
520 | primary_expression: |
521 | DECIMAL_CONSTANT | |
953192ba MD |
522 | { |
523 | $$ = make_node(parser_ctx, NODE_EXPRESSION); | |
524 | $$->u.expression.type = AST_EXP_CONSTANT; | |
8ab7c0d9 MD |
525 | if (sscanf(yylval.gs->s, "%" WIDTH_u64_SCANF_IS_A_BROKEN_API SCNu64, |
526 | &$$->u.expression.u.constant) != 1) { | |
527 | parse_error(parser_ctx, "cannot scanf decimal constant"); | |
528 | } | |
953192ba MD |
529 | } |
530 | | OCTAL_CONSTANT | |
531 | { | |
532 | $$ = make_node(parser_ctx, NODE_EXPRESSION); | |
533 | $$->u.expression.type = AST_EXP_CONSTANT; | |
8ab7c0d9 MD |
534 | if (!strcmp(yylval.gs->s, "0")) { |
535 | $$->u.expression.u.constant = 0; | |
536 | } else if (sscanf(yylval.gs->s, "0%" WIDTH_o64_SCANF_IS_A_BROKEN_API SCNo64, | |
537 | &$$->u.expression.u.constant) != 1) { | |
538 | parse_error(parser_ctx, "cannot scanf octal constant"); | |
539 | } | |
953192ba MD |
540 | } |
541 | | HEXADECIMAL_CONSTANT | |
542 | { | |
543 | $$ = make_node(parser_ctx, NODE_EXPRESSION); | |
544 | $$->u.expression.type = AST_EXP_CONSTANT; | |
8ab7c0d9 MD |
545 | if (sscanf(yylval.gs->s, "0x%" WIDTH_x64_SCANF_IS_A_BROKEN_API SCNx64, |
546 | &$$->u.expression.u.constant) != 1) { | |
547 | parse_error(parser_ctx, "cannot scanf hexadecimal constant"); | |
548 | } | |
953192ba | 549 | } |
e90d8561 MD |
550 | | FLOAT_CONSTANT |
551 | { | |
552 | $$ = make_node(parser_ctx, NODE_EXPRESSION); | |
553 | $$->u.expression.type = AST_EXP_FLOAT_CONSTANT; | |
8ab7c0d9 MD |
554 | if (sscanf(yylval.gs->s, "%" WIDTH_lg_SCANF_IS_A_BROKEN_API "lg", |
555 | &$$->u.expression.u.float_constant) != 1) { | |
556 | parse_error(parser_ctx, "cannot scanf float constant"); | |
557 | } | |
e90d8561 | 558 | } |
953192ba MD |
559 | | STRING_LITERAL_START DQUOTE |
560 | { | |
561 | $$ = make_node(parser_ctx, NODE_EXPRESSION); | |
562 | $$->u.expression.type = AST_EXP_STRING; | |
563 | $$->u.expression.u.string = ""; | |
564 | } | |
565 | | STRING_LITERAL_START s_char_sequence DQUOTE | |
566 | { | |
567 | $$ = make_node(parser_ctx, NODE_EXPRESSION); | |
568 | $$->u.expression.type = AST_EXP_STRING; | |
569 | $$->u.expression.u.string = $2->s; | |
570 | } | |
571 | | CHARACTER_CONSTANT_START c_char_sequence SQUOTE | |
572 | { | |
573 | $$ = make_node(parser_ctx, NODE_EXPRESSION); | |
574 | $$->u.expression.type = AST_EXP_STRING; | |
575 | $$->u.expression.u.string = $2->s; | |
576 | } | |
577 | | LPAREN expression RPAREN | |
578 | { | |
579 | $$ = make_node(parser_ctx, NODE_EXPRESSION); | |
580 | $$->u.expression.type = AST_EXP_NESTED; | |
581 | $$->u.expression.u.child = $2; | |
582 | } | |
583 | ; | |
584 | ||
bff988fa MD |
585 | identifiers |
586 | : IDENTIFIER | |
587 | { | |
588 | $$ = make_node(parser_ctx, NODE_EXPRESSION); | |
589 | $$->u.expression.type = AST_EXP_IDENTIFIER; | |
590 | $$->u.expression.u.identifier = yylval.gs->s; | |
591 | } | |
592 | | GLOBAL_IDENTIFIER | |
593 | { | |
594 | $$ = make_node(parser_ctx, NODE_EXPRESSION); | |
595 | $$->u.expression.type = AST_EXP_GLOBAL_IDENTIFIER; | |
596 | $$->u.expression.u.identifier = yylval.gs->s; | |
597 | } | |
598 | ; | |
599 | ||
600 | prefix_expression_rec | |
601 | : LSBRAC unary_expression RSBRAC | |
602 | { | |
603 | $$ = $2; | |
604 | } | |
605 | | LSBRAC unary_expression RSBRAC prefix_expression_rec | |
606 | { | |
607 | $$ = $2; | |
608 | $$->u.expression.pre_op = AST_LINK_BRACKET; | |
609 | $$->u.expression.prev = $4; | |
610 | } | |
611 | ; | |
612 | ||
613 | prefix_expression | |
614 | : identifiers | |
615 | { | |
616 | $$ = $1; | |
617 | } | |
618 | | identifiers prefix_expression_rec | |
661dfdd1 MD |
619 | { |
620 | $$ = $1; | |
621 | $$->u.expression.pre_op = AST_LINK_BRACKET; | |
bff988fa | 622 | $$->u.expression.next_bracket = $2; |
661dfdd1 | 623 | } |
bff988fa MD |
624 | ; |
625 | ||
626 | postfix_expression | |
627 | : prefix_expression | |
953192ba | 628 | { |
bff988fa MD |
629 | $$ = $1; |
630 | } | |
631 | | postfix_expression DOT prefix_expression | |
632 | { | |
633 | $$ = $3; | |
953192ba | 634 | $$->u.expression.post_op = AST_LINK_DOT; |
953192ba MD |
635 | $$->u.expression.prev = $1; |
636 | } | |
bff988fa | 637 | | postfix_expression RARROW prefix_expression |
953192ba | 638 | { |
bff988fa | 639 | $$ = $3; |
953192ba | 640 | $$->u.expression.post_op = AST_LINK_RARROW; |
953192ba MD |
641 | $$->u.expression.prev = $1; |
642 | } | |
643 | ; | |
644 | ||
645 | unary_expression | |
646 | : postfix_expression | |
647 | { $$ = $1; } | |
bff988fa MD |
648 | | primary_expression |
649 | { $$ = $1; } | |
953192ba MD |
650 | | unary_operator unary_expression |
651 | { | |
652 | $$ = $1; | |
653 | $$->u.unary_op.child = $2; | |
654 | } | |
655 | ; | |
656 | ||
657 | unary_operator | |
658 | : PLUS | |
659 | { | |
660 | $$ = make_node(parser_ctx, NODE_UNARY_OP); | |
661 | $$->u.unary_op.type = AST_UNARY_PLUS; | |
662 | } | |
663 | | MINUS | |
664 | { | |
665 | $$ = make_node(parser_ctx, NODE_UNARY_OP); | |
666 | $$->u.unary_op.type = AST_UNARY_MINUS; | |
667 | } | |
668 | | NOT_OP | |
669 | { | |
670 | $$ = make_node(parser_ctx, NODE_UNARY_OP); | |
671 | $$->u.unary_op.type = AST_UNARY_NOT; | |
672 | } | |
ab78f161 CB |
673 | | NOT_BIN |
674 | { | |
675 | $$ = make_node(parser_ctx, NODE_UNARY_OP); | |
bff988fa | 676 | $$->u.unary_op.type = AST_UNARY_BIT_NOT; |
ab78f161 | 677 | } |
953192ba MD |
678 | ; |
679 | ||
680 | multiplicative_expression | |
681 | : unary_expression | |
682 | { $$ = $1; } | |
683 | | multiplicative_expression STAR unary_expression | |
684 | { | |
685 | $$ = make_op_node(parser_ctx, AST_OP_MUL, $1, $3); | |
686 | } | |
687 | | multiplicative_expression DIV_OP unary_expression | |
688 | { | |
689 | $$ = make_op_node(parser_ctx, AST_OP_DIV, $1, $3); | |
690 | } | |
691 | | multiplicative_expression MOD_OP unary_expression | |
692 | { | |
693 | $$ = make_op_node(parser_ctx, AST_OP_MOD, $1, $3); | |
694 | } | |
695 | ; | |
696 | ||
697 | additive_expression | |
698 | : multiplicative_expression | |
699 | { $$ = $1; } | |
700 | | additive_expression PLUS multiplicative_expression | |
701 | { | |
702 | $$ = make_op_node(parser_ctx, AST_OP_PLUS, $1, $3); | |
703 | } | |
704 | | additive_expression MINUS multiplicative_expression | |
705 | { | |
706 | $$ = make_op_node(parser_ctx, AST_OP_MINUS, $1, $3); | |
707 | } | |
708 | ; | |
709 | ||
710 | shift_expression | |
711 | : additive_expression | |
712 | { $$ = $1; } | |
713 | | shift_expression LEFT_OP additive_expression | |
714 | { | |
116d3c01 | 715 | $$ = make_op_node(parser_ctx, AST_OP_BIT_LSHIFT, $1, $3); |
953192ba MD |
716 | } |
717 | | shift_expression RIGHT_OP additive_expression | |
718 | { | |
116d3c01 | 719 | $$ = make_op_node(parser_ctx, AST_OP_BIT_RSHIFT, $1, $3); |
953192ba MD |
720 | } |
721 | ; | |
722 | ||
831b702b | 723 | and_expression |
953192ba MD |
724 | : shift_expression |
725 | { $$ = $1; } | |
831b702b MD |
726 | | and_expression AND_BIN shift_expression |
727 | { | |
728 | $$ = make_op_node(parser_ctx, AST_OP_BIT_AND, $1, $3); | |
729 | } | |
730 | ; | |
731 | ||
732 | exclusive_or_expression | |
733 | : and_expression | |
734 | { $$ = $1; } | |
735 | | exclusive_or_expression XOR_BIN and_expression | |
736 | { | |
737 | $$ = make_op_node(parser_ctx, AST_OP_BIT_XOR, $1, $3); | |
738 | } | |
739 | ; | |
740 | ||
741 | inclusive_or_expression | |
742 | : exclusive_or_expression | |
743 | { $$ = $1; } | |
744 | | inclusive_or_expression OR_BIN exclusive_or_expression | |
745 | { | |
746 | $$ = make_op_node(parser_ctx, AST_OP_BIT_OR, $1, $3); | |
747 | } | |
748 | ; | |
749 | ||
750 | relational_expression | |
751 | : inclusive_or_expression | |
752 | { $$ = $1; } | |
753 | | relational_expression LT_OP inclusive_or_expression | |
953192ba MD |
754 | { |
755 | $$ = make_op_node(parser_ctx, AST_OP_LT, $1, $3); | |
756 | } | |
831b702b | 757 | | relational_expression GT_OP inclusive_or_expression |
953192ba MD |
758 | { |
759 | $$ = make_op_node(parser_ctx, AST_OP_GT, $1, $3); | |
760 | } | |
831b702b | 761 | | relational_expression LE_OP inclusive_or_expression |
953192ba MD |
762 | { |
763 | $$ = make_op_node(parser_ctx, AST_OP_LE, $1, $3); | |
764 | } | |
831b702b | 765 | | relational_expression GE_OP inclusive_or_expression |
953192ba MD |
766 | { |
767 | $$ = make_op_node(parser_ctx, AST_OP_GE, $1, $3); | |
768 | } | |
769 | ; | |
770 | ||
771 | equality_expression | |
772 | : relational_expression | |
773 | { $$ = $1; } | |
774 | | equality_expression EQ_OP relational_expression | |
775 | { | |
776 | $$ = make_op_node(parser_ctx, AST_OP_EQ, $1, $3); | |
777 | } | |
778 | | equality_expression NE_OP relational_expression | |
779 | { | |
780 | $$ = make_op_node(parser_ctx, AST_OP_NE, $1, $3); | |
781 | } | |
782 | ; | |
783 | ||
953192ba | 784 | logical_and_expression |
831b702b | 785 | : equality_expression |
953192ba | 786 | { $$ = $1; } |
831b702b | 787 | | logical_and_expression AND_OP equality_expression |
953192ba MD |
788 | { |
789 | $$ = make_op_node(parser_ctx, AST_OP_AND, $1, $3); | |
790 | } | |
791 | ; | |
792 | ||
793 | logical_or_expression | |
794 | : logical_and_expression | |
795 | { $$ = $1; } | |
796 | | logical_or_expression OR_OP logical_and_expression | |
797 | { | |
798 | $$ = make_op_node(parser_ctx, AST_OP_OR, $1, $3); | |
799 | } | |
800 | ; | |
801 | ||
802 | expression | |
803 | : logical_or_expression | |
804 | { $$ = $1; } | |
805 | ; | |
806 | ||
807 | translation_unit | |
808 | : expression | |
809 | { | |
810 | parser_ctx->ast->root.u.root.child = $1; | |
811 | } | |
812 | ; |