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