コード例 #1
0
 def test_apply_remove_connection_from_edge(self):
     mother_graph = Graph()
     m_n1 = Vertex()
     m_e1 = Edge(m_n1)
     mother_graph.add_elements([m_n1, m_e1])
     daughter_graph = Graph()
     d_e1 = Edge()
     daughter_graph.add_elements([d_e1])
     mother_daughter_mapping = Mapping()
     mother_daughter_mapping[m_e1] = d_e1
     prod_option = ProductionOption(mother_graph, mother_daughter_mapping,
                                    daughter_graph)
     production = Production(mother_graph, [prod_option])
     host_graph = Graph()
     h_n1 = Vertex()
     h_e1 = Edge(None, h_n1)
     host_graph.add_elements([h_n1, h_e1])
     mother_host_mapping = Mapping()
     mother_host_mapping[m_n1] = h_n1
     mother_host_mapping[m_e1] = h_e1
     result = production.apply(host_graph, mother_host_mapping)
     # Test if the vertex was deleted
     assert len(result.vertices) == 0
     assert len(result.edges) == 1
     # Test if the vertex was removed from the edges connection field
     assert result.edges[0].vertex2 is None
コード例 #2
0
 def test_apply_vertex_new_connection(self):
     mother_graph = Graph()
     m_n1 = Vertex()
     m_e1 = Edge(m_n1)
     mother_graph.add_elements([m_n1, m_e1])
     daughter_graph = Graph()
     d_n1 = Vertex()
     d_e1 = Edge(d_n1)
     daughter_graph.add_elements([d_n1, d_e1])
     mother_daughter_mapping = Mapping()
     mother_daughter_mapping[m_n1] = d_n1
     prod_option = ProductionOption(mother_graph, mother_daughter_mapping,
                                    daughter_graph)
     production = Production(mother_graph, [prod_option])
     host_graph = Graph()
     h_n1 = Vertex()
     h_e1 = Edge(h_n1)
     host_graph.add_elements([h_n1, h_e1])
     mother_host_mapping = Mapping()
     mother_host_mapping[m_n1] = h_n1
     mother_host_mapping[m_e1] = h_e1
     result = production.apply(host_graph, mother_host_mapping)
     # Test if there are no superfluous elements in the graph
     assert len(result.vertices) == 1
     assert len(result.edges) == 1
     # Test if graph has the correct connections and element count
     assert result.is_isomorph(daughter_graph)
     # Test if there are no extra neighbours introduced to the vertex.
     assert len(result.vertices[0].edges) == 1
     assert result.edges[0] in result.vertices[0].edges
     # Test if the new edge was correctly connected to the vertex.
     assert result.edges[0].vertex1 == result.vertices[0]
コード例 #3
0
 def test_apply_delete_single_element(self):
     mother_graph = Graph()
     m_n1 = Vertex()
     mother_graph.add(m_n1)
     daughter_graph = Graph()
     mother_daughter_mapping = Mapping()
     prod_option = ProductionOption(mother_graph, mother_daughter_mapping,
                                    daughter_graph)
     production = Production(mother_graph, [prod_option])
     host_graph = Graph()
     h_n1 = Vertex()
     host_graph.add(h_n1)
     host_mother_mapping = Mapping()
     host_mother_mapping[m_n1] = h_n1
     result = production.apply(host_graph, host_mother_mapping)
     assert len(result) == 0
コード例 #4
0
class SLR_item:
    def __init__(self, text, point_position):
        self.production = Production(text)
        self.point_position = point_position

    def shift(self):
        if self.point_position < len(self.production.get_Right()):
            self.point_position += 1
            return True
        return False

    def get_next_token(self):
        if self.point_position == len(self.production.get_Right()): return '$'
        else: return self.production.get_Right()[self.point_position]

    def reduce(self):
        if self.point_position > 0:
            self.point_position -= 1

    def __eq__(self, item):
        if self.production == item.production and self.point_position == item.point_position:
            return True
        return False

    def __str__(self):
        r = self.production.get_Left() + " -> "
        for i in range(0, len(self.production.get_Right())):
            if i == self.point_position:
                r += '.'
            r += self.production.get_Right()[i]
        if self.point_position == len(self.production.get_Right()):
            r += '.'
        return r
コード例 #5
0
 def test_apply_no_change(self):
     mother_graph = Graph()
     m_n1 = Vertex()
     mother_graph.add(m_n1)
     daughter_graph = Graph()
     d_n1 = Vertex()
     mother_daughter_mapping = Mapping()
     mother_daughter_mapping[m_n1] = d_n1
     prod_option = ProductionOption(mother_graph, mother_daughter_mapping,
                                    daughter_graph)
     production = Production(mother_graph, [prod_option])
     host_graph = Graph()
     h_n1 = Vertex()
     h_n1.attr['test'] = 1
     host_graph.add(h_n1)
     host_mother_mapping = Mapping()
     host_mother_mapping[m_n1] = h_n1
     result = production.apply(host_graph, host_mother_mapping)
     assert len(result) == 1
     assert result.vertices[0].attr['test'] == 1
コード例 #6
0
ファイル: grammar.py プロジェクト: jr638091/LR-Parser
 def __init__(self, productions, distinguished):
     self.distinguished = distinguished
     self.productions = []
     for i in productions.split('\n'):
         self.productions.append(Production(i))
     self.not_terminals = []
     self.terminals = []
     self.taking_not_terminals()
     self.taking_terminals()
     self.epsilon = "@"
     self.EOF = "$"
コード例 #7
0
 def __init__(self, grammar):
     self.states = []
     production_prim = Production("S_prim -> " + grammar.distinguished)
     show = str(production_prim) + " \n " + str(grammar)
     self.G = Grammar(show, "S_prim")
     self.insert_state(SLR_state([production_prim], 0))
     self.states[0].expand(self.states[0].items, self.G)
     self.goto = {}
     self.queue = Queue()
     self.queue.put(self.states[0])
     self.firsts = first(self.G)
     self.follows = follow(self.G, self.firsts)
     self.move_action()
コード例 #8
0
 def __init__(self, text, point_position):
     self.production = Production(text)
     self.point_position = point_position