def p_relational_expression(p): ''' relational_expression : shift_expression | relational_expression '<' shift_expression | relational_expression '>' shift_expression | relational_expression LE_OP shift_expression | relational_expression GE_OP shift_expression ''' p[0] = ASTInternalNode('relational_expression', p[1:])
def p_struct_or_union_specifier(p): ''' struct_or_union_specifier : struct_or_union IDENTIFIER '{' struct_declaration_list '}' | struct_or_union '{' struct_declaration_list '}' | struct_or_union IDENTIFIER ''' if not p[2] == '{' and not p[2] in reserved_list: p[2] = ASTExternalNode('IDENTIFIER', p[2]) p[0] = ASTInternalNode('struct_or_union_specifier', p[1:])
def p_labeled_statement(p): ''' labeled_statement : IDENTIFIER ':' statement | CASE constant_expression ':' statement | DEFAULT ':' statement ''' if len(p) == 4 and not p[1] == 'default' and not p[1] in reserved_list: p[1] = ASTExternalNode('IDENTIFIER', p[1]) p[0] = ASTInternalNode('labeled_statement', p[1:])
def p_storage_class_specifier(p): ''' storage_class_specifier : TYPEDEF | EXTERN | STATIC | AUTO | REGISTER ''' p[0] = ASTInternalNode('storage_class_specifier', p[1:])
def p_statement(p): ''' statement : labeled_statement | compound_statement | expression_statement | selection_statement | iteration_statement | jump_statement ''' p[0] = ASTInternalNode('statement', p[1:])
def p_unary_operator(p): ''' unary_operator : '&' | '*' | '+' | '-' | '~' | '!' ''' p[0] = ASTInternalNode('unary_operator', p[1:])
def p_unary_expression(p): ''' unary_expression : postfix_expression | INC_OP unary_expression | DEC_OP unary_expression | unary_operator cast_expression | SIZEOF unary_expression | SIZEOF '(' type_name ')' ''' p[0] = ASTInternalNode('unary_expression', p[1:])
def p_identifier_list(p): ''' identifier_list : IDENTIFIER | identifier_list ',' IDENTIFIER ''' if len(p) == 2 and not p[1] in reserved_list: p[1] = ASTExternalNode('IDENTIFIER', p[1]) elif len(p) == 4 and not p[3] in reserved_list: p[3] = ASTExternalNode('IDENTIFIER', p[3]) p[0] = ASTInternalNode('identifier_list', p[1:])
def p_iteration_statement(p): ''' iteration_statement : WHILE '(' expression ')' statement | DO statement WHILE '(' expression ')' ';' | FOR '(' expression_statement expression_statement ')' statement | FOR '(' expression_statement expression_statement expression ')' statement | FOR '(' declaration expression_statement ')' statement | FOR '(' declaration expression_statement expression ')' statement ''' p[0] = ASTInternalNode('iteration_statement', p[1:])
def p_primary_expression(p): ''' primary_expression : IDENTIFIER | CONSTANT | STRING_LITERAL | '(' expression ')' ''' if re.match(r'(([_a-zA-Z])([0-9]|([_a-zA-Z]))*)', p[1]) and not p[1] in reserved_list: p[1] = ASTExternalNode('IDENTIFIER', str(p[1])) p[0] = ASTInternalNode('primary_expression', p[1:])
def p_jump_statement(p): ''' jump_statement : GOTO IDENTIFIER ';' | CONTINUE ';' | BREAK ';' | RETURN ';' | RETURN expression ';' ''' if len(p) == 4 and p[1] == 'goto' and not p[2] in reserved_list: p[2] = ASTExternalNode('IDENTIFIER', p[2]) p[0] = ASTInternalNode('jump_statement', p[1:])
def p_enum_specifier(p): ''' enum_specifier : ENUM '{' enumerator_list '}' | ENUM IDENTIFIER '{' enumerator_list '}' | ENUM '{' enumerator_list ',' '}' | ENUM IDENTIFIER '{' enumerator_list ',' '}' | ENUM IDENTIFIER ''' if not p[2] == '{' and not p[2] in reserved_list: p[2] = ASTExternalNode('IDENTIFIER', p[2]) p[0] = ASTInternalNode('enum_specifier', p[1:])
def p_declaration_specifiers(p): ''' declaration_specifiers : storage_class_specifier | storage_class_specifier declaration_specifiers | type_specifier | type_specifier declaration_specifiers | type_qualifier | type_qualifier declaration_specifiers | function_specifier | function_specifier declaration_specifiers ''' p[0] = ASTInternalNode('declaration_specifiers', p[1:])
def p_assignment_operator(p): ''' assignment_operator : '=' | MUL_ASSIGN | DIV_ASSIGN | MOD_ASSIGN | ADD_ASSIGN | SUB_ASSIGN | LEFT_ASSIGN | RIGHT_ASSIGN | AND_ASSIGN | XOR_ASSIGN | OR_ASSIGN ''' p[0] = ASTInternalNode('assignment_operator', p[1:])
def p_direct_abstract_declarator(p): ''' direct_abstract_declarator : '(' abstract_declarator ')' | '[' ']' | '[' assignment_expression ']' | direct_abstract_declarator '[' ']' | direct_abstract_declarator '[' assignment_expression ']' | '[' '*' ']' | direct_abstract_declarator '[' '*' ']' | '(' ')' | '(' parameter_type_list ')' | direct_abstract_declarator '(' ')' | direct_abstract_declarator '(' parameter_type_list ')' ''' p[0] = ASTInternalNode('direct_abstract_declarator', p[1:])
def p_postfix_expression(p): ''' postfix_expression : primary_expression | postfix_expression '[' expression ']' | postfix_expression '(' ')' | postfix_expression '(' argument_expression_list ')' | postfix_expression '.' IDENTIFIER | postfix_expression PTR_OP IDENTIFIER | postfix_expression INC_OP | postfix_expression DEC_OP | '(' type_name ')' '{' initializer_list '}' | '(' type_name ')' '{' initializer_list ',' '}' ''' if len(p) == 4 and not p[2] == '(' and not p[3] in reserved_list: p[3] = ASTExternalNode('IDENTIFIER', p[3]) p[0] = ASTInternalNode('postfix_expression', p[1:])
def p_type_specifier(p): ''' type_specifier : VOID | CHAR | SHORT | INT | LONG | FLOAT | DOUBLE | SIGNED | UNSIGNED | BOOL | struct_or_union_specifier | enum_specifier ''' p[0] = ASTInternalNode('type_specifier', p[1:])
def p_direct_declarator(p): ''' direct_declarator : IDENTIFIER | '(' declarator ')' | direct_declarator '[' type_qualifier_list assignment_expression ']' | direct_declarator '[' type_qualifier_list ']' | direct_declarator '[' assignment_expression ']' | direct_declarator '[' STATIC type_qualifier_list assignment_expression ']' | direct_declarator '[' type_qualifier_list STATIC assignment_expression ']' | direct_declarator '[' type_qualifier_list '*' ']' | direct_declarator '[' '*' ']' | direct_declarator '[' ']' | direct_declarator '(' parameter_type_list ')' | direct_declarator '(' identifier_list ')' | direct_declarator '(' ')' ''' if len(p) == 2 and not p[1] in reserved_list: p[1] = ASTExternalNode('IDENTIFIER', p[1]) p[0] = ASTInternalNode('direct_declarator', p[1:])
def p_selection_statement(p): ''' selection_statement : IF '(' expression ')' statement ELSE statement | IF '(' expression ')' statement | SWITCH '(' expression ')' statement ''' p[0] = ASTInternalNode('selection_statement', p[1:])
def p_expression_statement(p): ''' expression_statement : ';' | expression ';' ''' p[0] = ASTInternalNode('expression_statement', p[1:])
def p_block_item(p): ''' block_item : declaration | statement ''' p[0] = ASTInternalNode('block_item', p[1:])
def p_block_item_list(p): ''' block_item_list : block_item | block_item_list block_item ''' p[0] = ASTInternalNode('block_item_list', p[1:])
def p_compound_statement(p): ''' compound_statement : '{' '}' | '{' block_item_list '}' ''' p[0] = ASTInternalNode('compound_statement', p[1:])
def p_declaration_list(p): ''' declaration_list : declaration | declaration_list declaration ''' p[0] = ASTInternalNode('declaration_list', p[1:])
def p_designation(p): ''' designation : designator_list '=' ''' p[0] = ASTInternalNode('designation', p[1:])
def p_designator_list(p): ''' designator_list : designator | designator_list designator ''' p[0] = ASTInternalNode('designator_list', p[1:])
def p_function_specifier(p): ''' function_specifier : INLINE ''' p[0] = ASTInternalNode('function_specifier', p[1:])
def p_designator(p): ''' designator : '[' constant_expression ']' | '.' IDENTIFIER ''' if len(p) == 3 and not p[2] in reserved_list: p[2] = ASTExternalNode('IDENTIFIER', p[2]) p[0] = ASTInternalNode('designator', p[1:])
def p_type_qualifier(p): ''' type_qualifier : CONST | RESTRICT | VOLATILE ''' p[0] = ASTInternalNode('type_qualifier', p[1:])
def p_function_definition(p): ''' function_definition : declaration_specifiers declarator declaration_list compound_statement | declaration_specifiers declarator compound_statement ''' p[0] = ASTInternalNode('function_definition', p[1:])