def label_declarations(node): global current_lexem current_node = Node('<label_declarations>') if current_lexem['lexem'] == 'LABEL': add_current_item(current_node) next_lexem() if unsigned_integer(current_node): node.add(current_node) error_print('NOT_FOUND', 'unsigned_integer') return True if label_list(current_node): return True if current_lexem['code'] != 59: node.add(current_node) error_print('NOT_FOUND', ';') return True add_current_item(current_node) node.add(current_node) next_lexem() elif current_lexem['lexem'] == 'BEGIN': current_node.add(Node('<empty>')) node.add(current_node) return False else: node.add(current_node) error_print('NOT_FOUND', 'LABEL') return True
def block(node): global current_lexem current_node = Node('<block>') if declarations(current_node): node.add(current_node) return True if current_lexem['lexem'] != 'BEGIN': node.add(current_node) error_print('NOT_FOUND', 'BEGIN') return True add_current_item(current_node) next_lexem() if statements_list(current_node): node.add(current_node) return True if current_lexem['lexem'] != 'END': node.add(current_node) error_print('NOT_FOUND', 'END') return True else: add_current_item(current_node) node.add(current_node) next_lexem()
def procedure_identifier(node): current_node = Node('<procedure_identifier>') if identifier(current_node): node.add(current_node) return True return False
def alternative_part(node): global current_lexem current_node = Node('<alternative_part>') if current_lexem['lexem'] == 'ELSE': add_current_item(current_node) next_lexem() if statements_list(current_node): return True else: next = take_next_item() if current_lexem['lexem'] == 'ENDIF': current_node.add(Node('<empty>')) node.add(current_node) return False
def statements_list(node): global statements_stack current_node = Node('<statements_list>') if not statement(current_node): statements_list(current_node) node.add(current_node) return False if current_lexem['lexem'] in empty_authorized: current_node.add(Node('<empty>')) node.add(current_node) return False error_print('UNEXPECTED') node.add(current_node) return True
def statement(node): global current_lexem current_node = Node('<statement>') if not unsigned_integer(current_node): if current_lexem['lexem'] != ':': node.add(current_node) error_print('NOT_FOUND', ':') return True add_current_item(current_node) next_lexem() if statement(current_node): return True node.add(current_node) return False if current_lexem['lexem'] == 'GOTO': add_current_item(current_node) next_lexem() if unsigned_integer(current_node): return True if current_lexem['lexem'] != ';': node.add(current_node) error_print('NOT_FOUND', ';') return True add_current_item(current_node) # ; next_lexem() node.add(current_node) return False if not condition_statement(current_node): if current_lexem['lexem'] != 'ENDIF': node.add(current_node) error_print('NOT_FOUND', 'ENDIF') return True add_current_item(current_node) next_lexem() if current_lexem['lexem'] != ';': node.add(current_node) error_print('NOT_FOUND', ';') return True add_current_item(current_node) node.add(current_node) next_lexem() return False if current_lexem['lexem'] == ';': add_current_item(current_node) node.add(current_node) next_lexem() return False return True
def variable_identifier(node): global current_lexem current_node = Node('<variable_identifier>') if not identifier(current_node): node.add(current_node) error_print('NOT_FOUND', 'identifier') return True node.add(current_node) return False
def identifier(node): global current_lexem if current_lexem['code'] < 1001: # error return False current_node = Node('<identifier>') add_current_item(current_node) node.add(current_node) return True
def declarations(node): global current_lexem current_node = Node('<declarations>') if label_declarations(current_node): node.add(current_node) return True node.add(current_node)
def add_current_item(current_node): global current_lexem current_node.add( Node({ 'lexem': current_lexem['lexem'], 'code': current_lexem['code'], 'line': current_lexem['line'], 'pos': current_lexem['pos'] }))
def unsigned_integer(node): global current_lexem current_node = Node('<unsigned_integer>') # const if current_lexem['code'] < 501 or current_lexem['code'] > 1000: return True else: add_current_item(current_node) node.add(current_node) next_lexem()
def condition_statement(node): global current_lexem current_node = Node('<condition_statement>') if incomplete_condition_statement(current_node): return True if alternative_part(current_node): return True node.add(current_node) return False
def label_list(node): global current_lexem current_node = Node('<label_list>') if current_lexem['lexem'] == ',': add_current_item(current_node) next_lexem() if unsigned_integer(current_node): error_print('NOT_FOUND', 'unsigned_integer') return True if label_list(current_node): return True node.add(current_node) else: if not unsigned_integer(current_node): error_print('NOT_FOUND') return True else: # print next_item current_node.add(Node('<empty>')) node.add(current_node)
def condition_expression(node): current_node = Node('<condition_expression>') if variable_identifier(current_node): return True next_lexem() if current_lexem['lexem'] != '=': node.add(current_node) error_print('NOT_FOUND', '=') return True add_current_item(current_node) next_lexem() if unsigned_integer(current_node): return True node.add(current_node) return False
def program(node): global current_lexem global parse_tree curr_node = Node('<program>') if current_lexem['lexem'] != 'PROGRAM': error_print('NOT_FOUND', 'PROGRAM') parse_tree.add(curr_node) parse_tree.view() else: add_current_item(curr_node) next_lexem() if procedure_identifier(curr_node): next_lexem() if current_lexem['lexem'] != ';': error_print('NOT_FOUND', ';') parse_tree.add(curr_node) return parse_tree # parse_tree.view() # return False else: next_lexem() if block(curr_node): parse_tree.add(curr_node) return parse_tree # parse_tree.view() # return False if current_lexem['lexem'] != '.': error_print('NOT_FOUND', '.') parse_tree.add(curr_node) # parse_tree.view() # return False else: add_current_item(curr_node) parse_tree.add(curr_node) # view all parse tree # parse_tree.view() return parse_tree else: error_print('NOT_FOUND', 'identifier') parse_tree.add(curr_node) # parse_tree.view() return parse_tree
def incomplete_condition_statement(node): global current_lexem current_node = Node('<incomplite_condition_statement>') if current_lexem['lexem'] != 'IF': node.add(current_node) # error_print('NOT_FOUND', 'IF') return True add_current_item(current_node) next_lexem() if condition_expression(current_node): return True if current_lexem['lexem'] != 'THEN': node.add(current_node) error_print('NOT_FOUND', 'THEN') return True add_current_item(current_node) next_lexem() if statements_list(current_node): return True node.add(current_node) return False
def setUp(self): # wierzchołek drzewa (root) self.root = Node(5) # Poziom 1 self.root.left = Node(3) self.root.right = Node(7) # poziom 2 self.root.left.left = Node(2) self.root.left.right = Node(5) self.root.right.left = Node(1) self.root.right.right = Node(0) # poziom 3 self.root.right.right.left = Node(2) self.root.right.right.right = Node(8) # poziom 4 self.root.right.right.right.right = Node(5)
from tree_struct import Node, bc lexem_table = None current_lexem = None parse_tree = Node('<signal_program>') empty_authorized = ['ENDIF', 'END', 'ELSE'] error_stack = [] def add_current_item(current_node): global current_lexem current_node.add( Node({ 'lexem': current_lexem['lexem'], 'code': current_lexem['code'], 'line': current_lexem['line'], 'pos': current_lexem['pos'] })) def next_lexem(): global lexem_table global current_lexem try: current_lexem = lexem_table.pop() except: pass
class TestTree(unittest.TestCase): """Klasa testująca struktórę drzewa z wariantu 1 __5__ / \ 3 7__ / \ / \ 2 5 1 0 / \ 2 8 \ 5 """ def setUp(self): # wierzchołek drzewa (root) self.root = Node(5) # Poziom 1 self.root.left = Node(3) self.root.right = Node(7) # poziom 2 self.root.left.left = Node(2) self.root.left.right = Node(5) self.root.right.left = Node(1) self.root.right.right = Node(0) # poziom 3 self.root.right.right.left = Node(2) self.root.right.right.right = Node(8) # poziom 4 self.root.right.right.right.right = Node(5) def test_is_instance(self): self.assertIsInstance(self.root.left, Node) def test_sum_in_subtree(self): """test sumy wartosci we wskazanym poddrzewie 3 7 / \ && / \ 2 5 1 0 """ self.assertEqual(self.root.left.sum_in_subtree(), sum([3, 2, 5])) self.assertEqual(self.root.right.sum_in_subtree(), sum([7, 1, 0])) def test_average_in_subtree(self): """test średniej wartosci we wskazanym poddrzewie 3 7 / \ && / \ 2 5 1 0 """ self.assertAlmostEqual(self.root.left.average_in_subtree(), sum([3, 2, 5]) / 3) self.assertEqual(self.root.right.average_in_subtree(), sum([7, 1, 0]) / 3) def test_mediane_in_subtree(self): """test mediany we wskazanym poddrzewie 3 7 / \ && / \ 2 5 1 0 """ self.assertEqual(self.root.left.median_in_subtree, 3) self.assertEqual(self.root.right.median_in_subtree, 1) def test_sum_in_all_subtree(self): """ Test sumy wszystkich wartości zaczynając od poddrzewa o wartości 7 7__ / \ 1 0 / \ 2 8 \ 5 """ self.assertEqual(self.root.sum_in_all_subtree(self.root.right), sum([7, 1, 0, 2, 8, 5])) def test_average_in_all_subtree(self): """ Test mediany wartości zaczynając od poddrzewa o wartości 7 7__ / \ 1 0 / \ 2 8 \ 5 """ self.assertEqual(self.root.average_in_all_subtree(self.root.right), sum([7, 1, 0, 2, 8, 5]) / 6) def test_median_in_all_subtree(self): """ Test mediany wartości zaczynając od poddrzewa o wartości 7 7__ / \ 1 0 / \ 2 8 \ 5 """ self.assertEqual(self.root.median_in_all_subtree(self.root.right), 3.5)