def p_constant_declaration(p): """ constant_declaration : CONSTANT constant_definition | empty """ if len(p) == 2: p[0] = Node(type='constant_declaration', children=[p[1]]) else: p[0] = Node(type='constant_declaration', children=[p[2]], leaf=p[1])
def p_expression_table(p): """ expression_table : expression ',' expression_table | expression """ if len(p) == 2: p[0] = Node(type='expression_table', children=[p[1]]) else: p[0] = Node(type='expression_table', children=[p[1], p[3]], leaf=p[2])
def p_boolean_factor(p): """ boolean_factor : boolean_value | NOT boolean_factor """ if len(p) == 2: p[0] = Node(type='boolean_factor', children=[p[1]]) else: p[0] = Node(type='boolean_factor', children=[p[2]], leaf=p[1])
def p_identifier_table(p): """ identifier_table : identifier ',' identifier_table | identifier """ if len(p) == 2: p[0] = Node(type='identifier_table', children=[p[1]]) else: p[0] = Node(type='identifier_table', children=[p[1], p[3]], leaf=p[2])
def p_variable_declaration(p): """ variable_declaration : VAR variable_definition | empty """ if len(p) == 2: p[0] = Node(type='variable_declaration', children=p[1]) else: p[0] = Node(type='variable_declaration', children=[p[2]], leaf=p[1])
def p_clause_table(p): """ clause_table : execution_clause ';' clause_table | execution_clause """ if len(p) == 2: p[0] = Node(type='clause_table', children=[p[1]]) else: p[0] = Node(type='clause_table', children=[p[1], p[3]], leaf=p[2])
def p_boolean_term(p): """ boolean_term : boolean_term AND boolean_factor | boolean_factor """ if len(p) == 2: p[0] = Node(type='boolean_term', children=[p[1]]) else: p[0] = Node(type='boolean_term', children=[p[1], p[3]], leaf=p[2])
def p_factor(p): """ factor : arithmetic_value | '(' arithmetic_expression ')' """ if len(p) == 2: p[0] = Node(type='factor', children=[p[1]]) else: p[0] = Node(type='factor', children=[p[2]], leaf=[p[1], p[3]])
def p_term(p): """ term : term '*' factor | term '/' factor | factor """ if len(p) == 2: p[0] = Node(type='term', children=[p[1]]) else: p[0] = Node(type='term', children=[p[1], p[3]], leaf=p[2])
def p_input_variable_table(p): """ input_variable_table : variable | variable ',' input_variable_table """ if len(p) == 2: p[0] = Node(type='input_variable_table', children=[p[1]]) else: p[0] = Node(type='input_variable_table', children=[p[1], p[3]], leaf=p[2])
def p_procedure_declaration(p): """ procedure_declaration : PROCEDURE identifier '(' formal_parameter_table ')' subroutine | empty """ if len(p) == 2: p[0] = Node(type='procedure_declaration', children=[p[1]]) else: p[0] = Node(type='procedure_declaration', children=[p[2], p[4], p[6]], leaf=[p[1], p[3], p[5]])
def p_boolean_expression(p): """ boolean_expression : boolean_expression OR boolean_term | boolean_term """ if len(p) == 2: p[0] = Node(type='boolean_expression', children=[p[1]]) else: p[0] = Node(type='boolean_expression', children=[p[1], p[3]], leaf=p[2])
def p_real_parameter_table(p): """ real_parameter_table : expression ',' real_parameter_table | expression """ if len(p) == 2: p[0] = Node(type='real_parameter_table', children=[p[1]]) else: p[0] = Node(type='real_parameter_table', children=[p[1], p[3]], leaf=p[2])
def p_if_clause(p): """ if_clause : IF boolean_expression THEN execution_clause | IF boolean_expression THEN execution_clause ELSE execution_clause """ if len(p) == 5: p[0] = Node(type='if_clause', children=[p[2], p[4]], leaf=[p[1], p[3]]) else: p[0] = Node(type='if_clause', children=[p[2], p[4], p[6]], leaf=[p[1], p[3], p[5]])
def p_variable_definition(p): """ variable_definition : identifier_table ':' type ';' | identifier_table ':' type ';' variable_definition """ if len(p) == 5: p[0] = Node(type='variable_definition', children=[p[1], p[3]], leaf=[p[2], p[4]]) else: p[0] = Node(type='variable_definition', children=[p[1], p[3], p[5]], leaf=[p[2], p[4]])
def p_formal_parameter_table(p): """ formal_parameter_table : variable ':' simple_type | variable ':' simple_type ',' formal_parameter_table """ if len(p) == 4: p[0] = Node(type='formal_parameter_table', children=[p[1], p[3]], leaf=p[2]) else: p[0] = Node(type='formal_parameter_table', children=[p[1], p[3], p[5]], leaf=[p[2], p[4]])
def p_constant_definition(p): """ constant_definition : identifier '=' constant ';' constant_definition | identifier '=' constant ';' """ if len(p) == 6: p[0] = Node(type='constant_definition', children=[p[1], p[3], p[5]], leaf=[p[2], p[4]]) else: p[0] = Node(type='constant_definition', children=[p[1], p[3]], leaf=[p[2], p[4]])
def p_arithmetic_expression(p): """ arithmetic_expression : arithmetic_expression '+' term | arithmetic_expression '-' term | '+' term | '-' term | term """ if len(p) == 2: p[0] = Node(type='arithmetic_expression', children=[p[1]]) elif len(p) == 3: p[0] = Node(type='arithmetic_expression', children=[p[2]], leaf=p[1]) else: p[0] = Node(type='arithmetic_expression', children=[p[1], p[3]], leaf=p[2])
def p_index(p): """ index : integer | integer ELLIPSIS integer | index ',' integer | index ',' integer ELLIPSIS integer """ if len(p) == 2: p[0] = Node(type='index', children=[p[1]]) elif len(p) == 4: p[0] = Node(type="index", children=[p[1], p[3]], leaf=p[2]) else: p[0] = Node(type="index", children=[p[1], p[3], p[5]], leaf=[p[2], p[4]])
def p_boolean_value(p): """ boolean_value : boolean_constant | identifier | '(' boolean_expression ')' identifier relation_symbol identifier | '(' arithmetic_expression ')' relation_symbol '(' arithmetic_expression ')' """ if len(p) == 2: p[0] = Node(type='boolean_value', children=[p[1]]) elif len(p) == 7: p[0] = Node(type='boolean_value', children=[p[2], p[4], p[5], p[6]], leaf=[p[1], p[3]]) else: p[0] = Node(type="boolean_value", children=[p[2], p[4], p[6]], leaf=[p[1], p[3], p[5], p[7]])
def p_structured_clause(p): """ structured_clause : compound_clause | if_clause | while_clause | for_clause | repeat_clause """ p[0] = Node(type='structured_clause', children=[p[1]])
def p_constant(p): """ constant : integer | boolean_constant | character_constant | constant_identifier | real_number """ p[0] = Node(type="constant", children=[p[1]])
def p_relation_symbol(p): """ relation_symbol : '<' | NEQ | LE | GE | '>' | '=' """ p[0] = Node(type='relation_symbol', leaf=p[1])
def p_program(p): """ program : PROGRAM identifier subroutine """ p[0] = Node(type='program', children=[p[2], p[3]], leaf=p[1])
def p_subroutine(p): """ subroutine : constant_declaration variable_declaration procedure_declaration compound_clause """ p[0] = Node(type='subroutine', children=[p[1], p[2], p[3], p[4]])
def p_write_clause(p): """ write_clause : WRITE '(' expression_table ')' """ p[0] = Node(type='write_clause', children=[p[3]], leaf=[p[1], p[2], p[4]])
def p_for_clause(p): """ for_clause : FOR variable ASSIGNMENT expression TO expression DO execution_clause """ p[0] = Node(type='for_clause', children=[p[2], p[4], p[6], p[8]], leaf=[p[1], p[3], p[5], p[7]])
def p_repeat_clause(p): """ repeat_clause : REPEAT execution_clause UNTIL boolean_expression """ p[0] = Node(type='repeat_clause', children=[p[2], p[4]], leaf=[p[1], p[3]])
def p_while_clause(p): """ while_clause : WHILE boolean_expression DO execution_clause """ p[0] = Node(type='while_clause', children=[p[2], p[4]], leaf=[p[1], p[3]])
def p_compound_clause(p): """ compound_clause : BEGIN clause_table END """ p[0] = Node(type='compound_clause', children=[p[2]], leaf=[p[1], p[3]])