Ejemplo n.º 1
0
 def simplifyCFG(self, cfg):
     """ Base Normal Form : epsilon-free Grammar """
     self._loadCFG(cfg)
     self._reduceCFG()
     self._removeUnitProductins()
     self._removeNullProductins()
     return CFG().create(self._V, self._SIGMA, self._S, self._P)
Ejemplo n.º 2
0
 def convertToNF(self, cfg):
     ChomskyNF.convertToNF(self, cfg)
     self._renameCFG()
     self._orderProductions()
     self._terminateFirstSymbol()
     self._renameBackCFG()
     return CFG().create(self._V, self._SIGMA, self._S, self._P)
Ejemplo n.º 3
0
 def convertToNF(self, cfg):
     self._loadCFG(cfg)
     self._reduceCFG()
     self._removeNullProductins()
     self._replaceMixedTerminals()
     self._splitNonTerminalSequences()
     self._removeUnitProductins()
     return CFG().create(self._V, self._SIGMA, self._S, self._P)
Ejemplo n.º 4
0
                _v = {0: v}
                for j in range(1, n):
                    if j != n - 1:
                        _v[j] = self._createVariable('X')
                        self._V.append(_v[j])
                    else:
                        _v[j] = p[j]
                    if _v[j] not in _P.keys():
                        _P[_v[j]] = []
                    _P[_v[j - 1]].append({0: p[j - 1], 1: _v[j]})
        self._P = _P


if __name__ == "__main__":
    print("Chomsky Normal Form")
    G = CFG()

    print('\nTest : check normal form (test/ChomskyNF.txt)')
    G.loadFromFile('tests/ChomskyNF.txt')
    result = ChomskyNF().isInNF(G)
    print(G)

    if result:
        print('\ngrammar is in Chomsky normal form')
    else:
        print('\ngrammar is not in Chomsky normal form\n')
        g = ChomskyNF().convertToNF(G)
        print(g)

        if ChomskyNF().isInNF(g):
            print('\ngrammar is now in Chomsky normal form')