📄 dt_grammar.y
字号:
| DT_TOK_SUB { $$ = DT_TOK_INEG; } | DT_TOK_BNEG { $$ = DT_TOK_BNEG; } | DT_TOK_LNEG { $$ = DT_TOK_LNEG; } ;cast_expression: unary_expression | DT_TOK_LPAR type_name DT_TOK_RPAR cast_expression { $$ = OP2(DT_TOK_LPAR, dt_node_type($2), $4); } ;multiplicative_expression: cast_expression | multiplicative_expression DT_TOK_MUL cast_expression { $$ = OP2(DT_TOK_MUL, $1, $3); } | multiplicative_expression DT_TOK_DIV cast_expression { $$ = OP2(DT_TOK_DIV, $1, $3); } | multiplicative_expression DT_TOK_MOD cast_expression { $$ = OP2(DT_TOK_MOD, $1, $3); } ;additive_expression: multiplicative_expression | additive_expression DT_TOK_ADD multiplicative_expression { $$ = OP2(DT_TOK_ADD, $1, $3); } | additive_expression DT_TOK_SUB multiplicative_expression { $$ = OP2(DT_TOK_SUB, $1, $3); } ;shift_expression: additive_expression | shift_expression DT_TOK_LSH additive_expression { $$ = OP2(DT_TOK_LSH, $1, $3); } | shift_expression DT_TOK_RSH additive_expression { $$ = OP2(DT_TOK_RSH, $1, $3); } ;relational_expression: shift_expression | relational_expression DT_TOK_LT shift_expression { $$ = OP2(DT_TOK_LT, $1, $3); } | relational_expression DT_TOK_GT shift_expression { $$ = OP2(DT_TOK_GT, $1, $3); } | relational_expression DT_TOK_LE shift_expression { $$ = OP2(DT_TOK_LE, $1, $3); } | relational_expression DT_TOK_GE shift_expression { $$ = OP2(DT_TOK_GE, $1, $3); } ;equality_expression: relational_expression | equality_expression DT_TOK_EQU relational_expression { $$ = OP2(DT_TOK_EQU, $1, $3); } | equality_expression DT_TOK_NEQ relational_expression { $$ = OP2(DT_TOK_NEQ, $1, $3); } ;and_expression: equality_expression | and_expression DT_TOK_BAND equality_expression { $$ = OP2(DT_TOK_BAND, $1, $3); } ;exclusive_or_expression: and_expression | exclusive_or_expression DT_TOK_XOR and_expression { $$ = OP2(DT_TOK_XOR, $1, $3); } ;inclusive_or_expression: exclusive_or_expression | inclusive_or_expression DT_TOK_BOR exclusive_or_expression { $$ = OP2(DT_TOK_BOR, $1, $3); } ;logical_and_expression: inclusive_or_expression | logical_and_expression DT_TOK_LAND inclusive_or_expression { $$ = OP2(DT_TOK_LAND, $1, $3); } ;logical_xor_expression: logical_and_expression | logical_xor_expression DT_TOK_LXOR logical_and_expression { $$ = OP2(DT_TOK_LXOR, $1, $3); } ;logical_or_expression: logical_xor_expression | logical_or_expression DT_TOK_LOR logical_xor_expression { $$ = OP2(DT_TOK_LOR, $1, $3); } ;constant_expression: conditional_expression ;conditional_expression: logical_or_expression | logical_or_expression DT_TOK_QUESTION expression DT_TOK_COLON conditional_expression { $$ = OP3($1, $3, $5); } ;assignment_expression: conditional_expression | unary_expression assignment_operator assignment_expression { $$ = OP2($2, $1, $3); } ;assignment_operator: DT_TOK_ASGN { $$ = DT_TOK_ASGN; } | DT_TOK_MUL_EQ { $$ = DT_TOK_MUL_EQ; } | DT_TOK_DIV_EQ { $$ = DT_TOK_DIV_EQ; } | DT_TOK_MOD_EQ { $$ = DT_TOK_MOD_EQ; } | DT_TOK_ADD_EQ { $$ = DT_TOK_ADD_EQ; } | DT_TOK_SUB_EQ { $$ = DT_TOK_SUB_EQ; } | DT_TOK_LSH_EQ { $$ = DT_TOK_LSH_EQ; } | DT_TOK_RSH_EQ { $$ = DT_TOK_RSH_EQ; } | DT_TOK_AND_EQ { $$ = DT_TOK_AND_EQ; } | DT_TOK_XOR_EQ { $$ = DT_TOK_XOR_EQ; } | DT_TOK_OR_EQ { $$ = DT_TOK_OR_EQ; } ;expression: assignment_expression | expression DT_TOK_COMMA assignment_expression { $$ = OP2(DT_TOK_COMMA, $1, $3); } ;declaration: declaration_specifiers ';' { $$ = dt_node_decl(); dt_decl_free(dt_decl_pop()); yybegin(YYS_CLAUSE); } | declaration_specifiers init_declarator_list ';' { $$ = $2; dt_decl_free(dt_decl_pop()); yybegin(YYS_CLAUSE); } ;declaration_specifiers: d_storage_class_specifier | d_storage_class_specifier declaration_specifiers | type_specifier | type_specifier declaration_specifiers | type_qualifier | type_qualifier declaration_specifiers ;parameter_declaration_specifiers: storage_class_specifier | storage_class_specifier declaration_specifiers | type_specifier | type_specifier declaration_specifiers | type_qualifier | type_qualifier declaration_specifiers ;storage_class_specifier: DT_KEY_AUTO { dt_decl_class(DT_DC_AUTO); } | DT_KEY_REGISTER { dt_decl_class(DT_DC_REGISTER); } | DT_KEY_STATIC { dt_decl_class(DT_DC_STATIC); } | DT_KEY_EXTERN { dt_decl_class(DT_DC_EXTERN); } | DT_KEY_TYPEDEF { dt_decl_class(DT_DC_TYPEDEF); } ;d_storage_class_specifier: storage_class_specifier | DT_KEY_SELF { dt_decl_class(DT_DC_SELF); } | DT_KEY_THIS { dt_decl_class(DT_DC_THIS); } ;type_specifier: DT_KEY_VOID { $$ = dt_decl_spec(CTF_K_INTEGER, DUP("void")); } | DT_KEY_CHAR { $$ = dt_decl_spec(CTF_K_INTEGER, DUP("char")); } | DT_KEY_SHORT { $$ = dt_decl_attr(DT_DA_SHORT); } | DT_KEY_INT { $$ = dt_decl_spec(CTF_K_INTEGER, DUP("int")); } | DT_KEY_LONG { $$ = dt_decl_attr(DT_DA_LONG); } | DT_KEY_FLOAT { $$ = dt_decl_spec(CTF_K_FLOAT, DUP("float")); } | DT_KEY_DOUBLE { $$ = dt_decl_spec(CTF_K_FLOAT, DUP("double")); } | DT_KEY_SIGNED { $$ = dt_decl_attr(DT_DA_SIGNED); } | DT_KEY_UNSIGNED { $$ = dt_decl_attr(DT_DA_UNSIGNED); } | DT_KEY_STRING { $$ = dt_decl_spec(CTF_K_TYPEDEF, DUP("string")); } | DT_TOK_TNAME { $$ = dt_decl_spec(CTF_K_TYPEDEF, $1); } | struct_or_union_specifier | enum_specifier ;type_qualifier: DT_KEY_CONST { $$ = dt_decl_attr(DT_DA_CONST); } | DT_KEY_VOLATILE { $$ = dt_decl_attr(DT_DA_VOLATILE); } ;struct_or_union_specifier: struct_or_union_definition struct_declaration_list '}' { $$ = dt_scope_pop(); } | struct_or_union DT_TOK_IDENT { $$ = dt_decl_spec($1, $2); } | struct_or_union DT_TOK_TNAME { $$ = dt_decl_spec($1, $2); } ;struct_or_union_definition: struct_or_union '{' { dt_decl_sou($1, NULL); } | struct_or_union DT_TOK_IDENT '{' { dt_decl_sou($1, $2); } | struct_or_union DT_TOK_TNAME '{' { dt_decl_sou($1, $2); } ;struct_or_union: DT_KEY_STRUCT { $$ = CTF_K_STRUCT; } | DT_KEY_UNION { $$ = CTF_K_UNION; } ;struct_declaration_list: struct_declaration | struct_declaration_list struct_declaration ;init_declarator_list: init_declarator | init_declarator_list DT_TOK_COMMA init_declarator { $$ = LINK($1, $3); } ;init_declarator: declarator { $$ = dt_node_decl(); dt_decl_reset(); } ;struct_declaration: specifier_qualifier_list struct_declarator_list ';' { dt_decl_free(dt_decl_pop()); } ;specifier_qualifier_list: type_specifier | type_specifier specifier_qualifier_list { $$ = $2; } | type_qualifier | type_qualifier specifier_qualifier_list { $$ = $2; } ;struct_declarator_list: struct_declarator | struct_declarator_list DT_TOK_COMMA struct_declarator ;struct_declarator: declarator { dt_decl_member(NULL); } | DT_TOK_COLON constant_expression { dt_decl_member($2); } | declarator DT_TOK_COLON constant_expression { dt_decl_member($3); } ;enum_specifier: enum_definition enumerator_list '}' { $$ = dt_scope_pop(); } | DT_KEY_ENUM DT_TOK_IDENT { $$ = dt_decl_spec(CTF_K_ENUM, $2); } | DT_KEY_ENUM DT_TOK_TNAME { $$ = dt_decl_spec(CTF_K_ENUM, $2); } ;enum_definition: DT_KEY_ENUM '{' { dt_decl_enum(NULL); } | DT_KEY_ENUM DT_TOK_IDENT '{' { dt_decl_enum($2); } | DT_KEY_ENUM DT_TOK_TNAME '{' { dt_decl_enum($2); } ;enumerator_list: enumerator | enumerator_list DT_TOK_COMMA enumerator ;enumerator: DT_TOK_IDENT { dt_decl_enumerator($1, NULL); } | DT_TOK_IDENT DT_TOK_ASGN expression { dt_decl_enumerator($1, $3); } ;declarator: direct_declarator | pointer direct_declarator ;direct_declarator: DT_TOK_IDENT { $$ = dt_decl_ident($1); } | lparen declarator DT_TOK_RPAR { $$ = $2; } | direct_declarator array { dt_decl_array($2); } | direct_declarator function { dt_decl_func($1, $2); } ;lparen: DT_TOK_LPAR { dt_decl_top()->dd_attr |= DT_DA_PAREN; } ;pointer: DT_TOK_MUL { $$ = dt_decl_ptr(); } | DT_TOK_MUL type_qualifier_list { $$ = dt_decl_ptr(); } | DT_TOK_MUL pointer { $$ = dt_decl_ptr(); } | DT_TOK_MUL type_qualifier_list pointer { $$ = dt_decl_ptr(); } ;type_qualifier_list: type_qualifier | type_qualifier_list type_qualifier { $$ = $2; } ;parameter_type_list: parameter_list | DT_TOK_ELLIPSIS { $$ = dt_node_vatype(); } | parameter_list DT_TOK_COMMA DT_TOK_ELLIPSIS { $$ = LINK($1, dt_node_vatype()); } ;parameter_list: parameter_declaration | parameter_list DT_TOK_COMMA parameter_declaration { $$ = LINK($1, $3); } ;parameter_declaration: parameter_declaration_specifiers { $$ = dt_node_type(NULL); } | parameter_declaration_specifiers declarator { $$ = dt_node_type(NULL); } | parameter_declaration_specifiers abstract_declarator { $$ = dt_node_type(NULL); } ;type_name: specifier_qualifier_list { $$ = dt_decl_pop(); } | specifier_qualifier_list abstract_declarator { $$ = dt_decl_pop(); } ;abstract_declarator: pointer | direct_abstract_declarator | pointer direct_abstract_declarator ;direct_abstract_declarator: lparen abstract_declarator DT_TOK_RPAR { $$ = $2; } | direct_abstract_declarator array { dt_decl_array($2); } | array { dt_decl_array($1); $$ = NULL; } | direct_abstract_declarator function { dt_decl_func($1, $2); } | function { dt_decl_func(NULL, $1); } ;array: DT_TOK_LBRAC DT_TOK_RBRAC { $$ = NULL; } | DT_TOK_LBRAC { dt_scope_push(NULL, CTF_ERR); } constant_expression DT_TOK_RBRAC { dt_scope_pop(); $$ = $3; } | DT_TOK_LBRAC { dt_scope_push(NULL, CTF_ERR); } parameter_type_list DT_TOK_RBRAC { dt_scope_pop(); $$ = $3; } ;function: DT_TOK_LPAR DT_TOK_RPAR { $$ = NULL; } | DT_TOK_LPAR { dt_scope_push(NULL, CTF_ERR); } parameter_type_list DT_TOK_RPAR { dt_scope_pop(); $$ = $3; } ;%%
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -