From: Matthias Kruk Date: Tue, 21 Jul 2020 15:39:09 +0000 (+0900) Subject: grammar: Add production rules for grammatical structures on pages 299, 300 of "The... X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=f7ae0f029bd26f1b13182c2e59355ae77891773f;p=ccc grammar: Add production rules for grammatical structures on pages 299, 300 of "The C Programming Language, 2nd Edition" --- diff --git a/src/grammar.c b/src/grammar.c index 0722685..f4d1cd5 100644 --- a/src/grammar.c +++ b/src/grammar.c @@ -2314,6 +2314,791 @@ static struct rule *_and_expression_productions[] = { NULL }; +static struct rule _equality_expression_production_A[] = { + { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_RELATIONAL_EXPRESSION, + .name = "relational-expression" + }, { + 0 + } +}; + +static struct rule _equality_expression_production_B[] = { + { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_EQUALITY_EXPRESSION, + .name = "equality-expression" + }, { + .type = RULE_TERMINAL, + .terminal = TOKEN_EQ, + .name = "op" + }, { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_RELATIONAL_EXPRESSION, + .name = "relational-expression" + }, { + 0 + } +}; + +static struct rule _equality_expression_production_C[] = { + { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_EQUALITY_EXPRESSION, + .name = "equality-expression" + }, { + .type = RULE_TERMINAL, + .terminal = TOKEN_NEQ, + .name = "op" + }, { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_RELATIONAL_EXPRESSION, + .name = "relational-expression" + }, { + 0 + } +}; + +static struct rule *_equality_expression_productions[] = { + _equality_expression_production_A, + _equality_expression_production_B, + _equality_expression_production_C, + NULL +}; + +static struct rule _relational_expression_production_A[] = { + { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_SHIFT_EXPRESSION, + .name = "shift-expression" + }, { + 0 + } +}; + +static struct rule _relational_expression_production_B[] = { + { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_RELATIONAL_EXPRESSION, + .name = "relational-expression" + }, { + .type = RULE_TERMINAL, + .terminal = TOKEN_LT, + .name = "op" + }, { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_SHIFT_EXPRESSION, + .name = "shift-expression" + }, { + 0 + } +}; + +static struct rule _relational_expression_production_C[] = { + { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_RELATIONAL_EXPRESSION, + .name = "relational-expression" + }, { + .type = RULE_TERMINAL, + .terminal = TOKEN_GT, + .name = "op" + }, { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_SHIFT_EXPRESSION, + .name = "shift-expression" + }, { + 0 + } +}; + +static struct rule _relational_expression_production_D[] = { + { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_RELATIONAL_EXPRESSION, + .name = "relational-expression" + }, { + .type = RULE_TERMINAL, + .terminal = TOKEN_LE, + .name = "op" + }, { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_SHIFT_EXPRESSION, + .name = "shift-expression" + }, { + 0 + } +}; + +static struct rule _relational_expression_production_E[] = { + { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_RELATIONAL_EXPRESSION, + .name = "relational-expression" + }, { + .type = RULE_TERMINAL, + .terminal = TOKEN_GE, + .name = "op" + }, { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_SHIFT_EXPRESSION, + .name = "shift-expression" + }, { + 0 + } +}; + +static struct rule *_relational_expression_productions[] = { + _relational_expression_production_A, + _relational_expression_production_B, + _relational_expression_production_C, + _relational_expression_production_D, + _relational_expression_production_E, + NULL +}; + +static struct rule _shift_expression_production_A[] = { + { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_ADDITIVE_EXPRESSION, + .name = "additive-expression" + }, { + 0 + } +}; + +static struct rule _shift_expression_production_B[] = { + { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_SHIFT_EXPRESSION, + .name = "shift-expression" + }, { + .type = RULE_TERMINAL, + .terminal = TOKEN_SHL, + .name = "op" + }, { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_ADDITIVE_EXPRESSION, + .name = "additive-expression" + }, { + 0 + } +}; + +static struct rule _shift_expression_production_C[] = { + { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_SHIFT_EXPRESSION, + .name = "shift-expression" + }, { + .type = RULE_TERMINAL, + .terminal = TOKEN_SHR, + .name = "op" + }, { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_ADDITIVE_EXPRESSION, + .name = "additive-expression" + }, { + 0 + } +}; + +static struct rule *_shift_expression_productions[] = { + _shift_expression_production_A, + _shift_expression_production_B, + _shift_expression_production_C, + NULL +}; + +static struct rule _additive_expression_production_A[] = { + { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_MULTIPLICATIVE_EXPRESSION, + .name = "multiplicative-expression" + }, { + 0 + } +}; + +static struct rule _additive_expression_production_B[] = { + { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_ADDITIVE_EXPRESSION, + .name = "additive-expression" + }, { + .type = RULE_TERMINAL, + .terminal = TOKEN_ADD, + .name = "op" + }, { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_MULTIPLICATIVE_EXPRESSION, + .name = "multiplicative-expression" + }, { + 0 + } +}; + +static struct rule _additive_expression_production_C[] = { + { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_ADDITIVE_EXPRESSION, + .name = "additive-expression" + }, { + .type = RULE_TERMINAL, + .terminal = TOKEN_SUB, + .name = "op" + }, { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_MULTIPLICATIVE_EXPRESSION, + .name = "multiplicative-expression" + }, { + 0 + } +}; + +static struct rule *_additive_expression_productions[] = { + _additive_expression_production_A, + _additive_expression_production_B, + _additive_expression_production_C, + NULL +}; + +static struct rule _multiplicative_expression_production_A[] = { + { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_CAST_EXPRESSION, + .name = "cast-expression" + }, { + 0 + } +}; + +static struct rule _multiplicative_expression_production_B[] = { + { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_MULTIPLICATIVE_EXPRESSION, + .name = "multiplicative-expression" + }, { + .type = RULE_TERMINAL, + .terminal = TOKEN_MUL, + .name = "op" + }, { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_CAST_EXPRESSION, + .name = "cast-expression" + }, { + 0 + } +}; + +static struct rule _multiplicative_expression_production_C[] = { + { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_MULTIPLICATIVE_EXPRESSION, + .name = "multiplicative-expression" + }, { + .type = RULE_TERMINAL, + .terminal = TOKEN_DIV, + .name = "op" + }, { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_CAST_EXPRESSION, + .name = "cast-expression" + }, { + 0 + } +}; + +static struct rule _multiplicative_expression_production_D[] = { + { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_MULTIPLICATIVE_EXPRESSION, + .name = "multiplicative-expression" + }, { + .type = RULE_TERMINAL, + .terminal = TOKEN_MOD, + .name = "op" + }, { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_CAST_EXPRESSION, + .name = "cast-expression" + }, { + 0 + } +}; + +static struct rule *_multiplicative_expression_productions[] = { + _multiplicative_expression_production_A, + _multiplicative_expression_production_B, + _multiplicative_expression_production_C, + _multiplicative_expression_production_D, + NULL +}; + +static struct rule _cast_expression_production_A[] = { + { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_UNARY_EXPRESSION, + .name = "unary-expression" + }, { + 0 + } +}; + +static struct rule _cast_expression_production_B[] = { + { + .type = RULE_TERMINAL, + .terminal = TOKEN_LPAREN, + .name = "lparen" + }, { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_TYPE_NAME, + .name = "type-name" + }, { + .type = RULE_TERMINAL, + .terminal = TOKEN_RPAREN, + .name = "rparen" + }, { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_CAST_EXPRESSION, + .name = "cast-expression" + }, { + 0 + } +}; + +static struct rule *_cast_expression_productions[] = { + _cast_expression_production_A, + _cast_expression_production_B, + NULL +}; + +static struct rule _unary_expression_production_A[] = { + { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_POSTFIX_EXPRESSION, + .name = "postfix-expression" + }, { + 0 + } +}; + +static struct rule _unary_expression_production_B[] = { + { + .type = RULE_TERMINAL, + .terminal = TOKEN_INC, + .name = "prefix" + }, { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_UNARY_EXPRESSION, + .name = "unary-expression" + }, { + 0 + } +}; + +static struct rule _unary_expression_production_C[] = { + { + .type = RULE_TERMINAL, + .terminal = TOKEN_DEC, + .name = "prefix" + }, { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_UNARY_EXPRESSION, + .name = "unary-expression" + }, { + 0 + } +}; + +static struct rule _unary_expression_production_D[] = { + { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_UNARY_OPERATOR, + .name = "unary-operator" + }, { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_CAST_EXPRESSION, + .name = "cast-expression" + }, { + 0 + } +}; + +static struct rule _unary_expression_production_E[] = { + { + .type = RULE_TERMINAL, + .terminal = TOKEN_SIZEOF, + .name = "sizeof" + }, { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_UNARY_EXPRESSION, + .name = "unary-expression" + }, { + 0 + } +}; + +static struct rule _unary_expression_production_F[] = { + { + .type = RULE_TERMINAL, + .terminal = TOKEN_SIZEOF, + .name = "sizeof" + }, { + .type = RULE_TERMINAL, + .terminal = TOKEN_LPAREN, + .name = "lparen" + }, { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_TYPE_NAME, + .name = "type-name" + }, { + .type = RULE_TERMINAL, + .terminal = TOKEN_RPAREN, + .name = "rparen" + }, { + 0 + } +}; + +static struct rule *_unary_expression_productions[] = { + _unary_expression_production_A, + _unary_expression_production_B, + _unary_expression_production_C, + _unary_expression_production_D, + _unary_expression_production_E, + _unary_expression_production_F, + NULL +}; + +static struct rule _unary_operator_production_A[] = { + { + .type = RULE_TERMINAL, + .terminal = TOKEN_AND, + .name = "token" + }, { + 0 + } +}; + +static struct rule _unary_operator_production_B[] = { + { + .type = RULE_TERMINAL, + .terminal = TOKEN_MUL, + .name = "token" + }, { + 0 + } +}; + +static struct rule _unary_operator_production_C[] = { + { + .type = RULE_TERMINAL, + .terminal = TOKEN_ADD, + .name = "token" + }, { + 0 + } +}; + +static struct rule _unary_operator_production_D[] = { + { + .type = RULE_TERMINAL, + .terminal = TOKEN_SUB, + .name = "token" + }, { + 0 + } +}; + +static struct rule _unary_operator_production_E[] = { + { + .type = RULE_TERMINAL, + .terminal = TOKEN_COMPLEMENT, + .name = "token" + }, { + 0 + } +}; + +static struct rule _unary_operator_production_F[] = { + { + .type = RULE_TERMINAL, + .terminal = TOKEN_NOT, + .name = "token" + }, { + 0 + } +}; + +static struct rule *_unary_operator_productions[] = { + _unary_operator_production_A, + _unary_operator_production_B, + _unary_operator_production_C, + _unary_operator_production_D, + _unary_operator_production_E, + _unary_operator_production_F, + NULL +}; + +static struct rule _postfix_expression_production_A[] = { + { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_PRIMARY_EXPRESSION, + .name = "primary-expression" + }, { + 0 + } +}; + +static struct rule _postfix_expression_production_B[] = { + { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_POSTFIX_EXPRESSION, + .name = "postfix-expression" + }, { + .type = RULE_TERMINAL, + .terminal = TOKEN_LBRACKET, + .name = "lbracket" + }, { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_EXPRESSION, + .name = "expression" + }, { + .type = RULE_TERMINAL, + .terminal = TOKEN_RBRACKET, + .name = "rbracket" + }, { + 0 + } +}; + +static struct rule _postfix_expression_production_C[] = { + { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_POSTFIX_EXPRESSION, + .name = "postfix-expression" + }, { + .type = RULE_TERMINAL, + .terminal = TOKEN_LPAREN, + .name = "lparen" + }, { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_ARGUMENT_EXPRESSION_LIST, + .flags = RULE_FLAG_OPTIONAL, + .name = "argument-expression-list" + }, { + .type = RULE_TERMINAL, + .terminal = TOKEN_RPAREN, + .name = "rparen" + }, { + 0 + } +}; + +static struct rule _postfix_expression_production_D[] = { + { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_POSTFIX_EXPRESSION, + .name = "postfix-expression" + }, { + .type = RULE_TERMINAL, + .terminal = TOKEN_MEMBER, + .name = "op" + }, { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_IDENTIFIER, + .name = "identifier" + }, { + 0 + } +}; + +static struct rule _postfix_expression_production_E[] = { + { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_POSTFIX_EXPRESSION, + .name = "postfix-expression" + }, { + .type = RULE_TERMINAL, + .terminal = TOKEN_DMEMBER, + .name = "op" + }, { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_IDENTIFIER, + .name = "identifier" + }, { + 0 + } +}; + +static struct rule _postfix_expression_production_F[] = { + { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_POSTFIX_EXPRESSION, + .name = "postfix-expression" + }, { + .type = RULE_TERMINAL, + .terminal = TOKEN_INC, + .name = "op" + }, { + 0 + } +}; + +static struct rule _postfix_expression_production_G[] = { + { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_POSTFIX_EXPRESSION, + .name = "postfix-expression" + }, { + .type = RULE_TERMINAL, + .terminal = TOKEN_DEC, + .name = "op" + }, { + 0 + } +}; + +static struct rule *_postfix_expression_productions[] = { + _postfix_expression_production_A, + _postfix_expression_production_B, + _postfix_expression_production_C, + _postfix_expression_production_D, + _postfix_expression_production_E, + _postfix_expression_production_F, + _postfix_expression_production_G, + NULL +}; + +static struct rule _primary_expression_production_A[] = { + { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_IDENTIFIER, + .name = "identifier" + }, { + 0 + } +}; + +static struct rule _primary_expression_production_B[] = { + { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_CONSTANT, + .name = "constant" + }, { + 0 + } +}; + +static struct rule _primary_expression_production_C[] = { + { + .type = RULE_TERMINAL, + .terminal = TOKEN_LPAREN, + .name = "lparen" + }, { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_EXPRESSION, + .name = "expression" + }, { + .type = RULE_TERMINAL, + .terminal = TOKEN_RPAREN, + .name = "rparen" + }, { + 0 + } +}; + +static struct rule *_primary_expression_productions[] = { + _primary_expression_production_A, + _primary_expression_production_B, + _primary_expression_production_C, + NULL +}; + +static struct rule _argument_expression_list_production_A[] = { + { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_ASSIGNMENT_EXPRESSION, + .name = "assignment-expression" + }, { + 0 + } +}; + +static struct rule _argument_expression_list_production_B[] = { + { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_ARGUMENT_EXPRESSION_LIST, + .name = "argument-expression-list" + }, { + .type = RULE_TERMINAL, + .terminal = TOKEN_COMMA, + .name = "comma" + }, { + .type = RULE_NONTERMINAL, + .nonterminal = SYNTAX_NODE_ASSIGNMENT_EXPRESSION, + .name = "assignment-expression" + }, { + 0 + } +}; + +static struct rule *_argument_expression_list_productions[] = { + _argument_expression_list_production_A, + _argument_expression_list_production_B, + NULL +}; + +static struct rule _constant_production_A[] = { + { + .type = RULE_TERMINAL, + .terminal = TOKEN_CHAR_LITERAL, + .name = "token" + }, { + 0 + } +}; + +static struct rule _constant_production_B[] = { + { + .type = RULE_TERMINAL, + .terminal = TOKEN_FLOAT_LITERAL, + .name = "token" + }, { + 0 + } +}; + +static struct rule _constant_production_C[] = { + { + .type = RULE_TERMINAL, + .terminal = TOKEN_NUMBER, + .name = "token" + }, { + 0 + } +}; + +static struct rule _constant_production_D[] = { + { + .type = RULE_TERMINAL, + .terminal = TOKEN_STRING, + .name = "token" + }, { + 0 + } +}; + +static struct rule *_constant_productions[] = { + _constant_production_A, + _constant_production_B, + _constant_production_C, + _constant_production_D, + NULL +}; + static struct syntax_node_descriptor _node_descriptors[] = { /* order needs to be in sync with syntax_node_type_t */ { @@ -2446,48 +3231,18 @@ static struct syntax_node_descriptor _node_descriptors[] = { }, { .type = SYNTAX_NODE_SELECTION_STATEMENT, .productions = _selection_statement_productions - }, { - .type = SYNTAX_NODE_IF_STATEMENT, - .productions = _if_statement_productions - }, { - .type = SYNTAX_NODE_IF_ELSE_STATEMENT, - .productions = _if_else_statement_productions - }, { - .type = SYNTAX_NODE_SWITCH_STATEMENT, - .productions = _switch_statement_productions }, { .type = SYNTAX_NODE_ITERATION_STATEMENT, .productions = _iteration_statement_productions - }, { - .type = SYNTAX_NODE_WHILE_STATEMENT, - .productions = _while_statement_productions - }, { - .type = SYNTAX_NODE_DO_WHILE_STATEMENT, - .productions = _do_while_statement_productions - }, { - .type = SYNTAX_NODE_FOR_STATEMENT, - .productions = _for_statement_productions }, { .type = SYNTAX_NODE_JUMP_STATEMENT, .productions = _jump_statement_productions - }, { - .type = SYNTAX_NODE_GOTO_STATEMENT, - .productions = _goto_statement_productions - }, { - .type = SYNTAX_NODE_CONTINUE_STATEMENT, - .productions = _continue_statement_productions - }, { - .type = SYNTAX_NODE_BREAK_STATEMENT, - .productions = _break_statement_productions - }, { - .type = SYNTAX_NODE_RETURN_STATEMENT, - .productions = _return_statement_productions }, { .type = SYNTAX_NODE_EXPRESSION, .productions = _expression_productions }, { .type = SYNTAX_NODE_ASSIGNMENT_EXPRESSION, - .productions = _assignement_expression_productions + .productions = _assignment_expression_productions }, { .type = SYNTAX_NODE_ASSIGNMENT_OPERATOR, .productions = _assignment_operator_productions @@ -2525,7 +3280,7 @@ static struct syntax_node_descriptor _node_descriptors[] = { .type = SYNTAX_NODE_ADDITIVE_EXPRESSION, .productions = _additive_expression_productions }, { - .type = SYNTAX_NODE_MULTIPLICATIVE_EXRESSION, + .type = SYNTAX_NODE_MULTIPLICATIVE_EXPRESSION, .productions = _multiplicative_expression_productions }, { .type = SYNTAX_NODE_CAST_EXPRESSION,