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