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