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')
Ejemplo n.º 5
0
    def _updateW(self, SET, _prev=None):
        if _prev is not None:
            W = [x for x in _prev]
        else:
            W = []
        for v in self._P:
            for p in self._P[v]:
                for n, _v in p.items():
                    if _v in SET and v not in W:
                        W.append(v)
        return W


if __name__ == "__main__":
    G = CFG()

    print('\nTest : reduce CFG (test/GenericNF_reduce.txt.txt)')
    G.loadFromFile('tests/GenericNF_reduce.txt')
    g = GenericNF().simplifyCFG(G.__copy__())
    print(G, '\n', g)

    print('\nTest : remove unit productins (tests/GenericNF_unit.txt)')
    G.loadFromFile('tests/GenericNF_unit.txt')
    g = GenericNF().simplifyCFG(G.__copy__())
    print(G, '\n', g)

    print('\nTest : remove null productins (tests/GenericNF_null.txt)')
    G.loadFromFile('tests/GenericNF_null.txt')
    g = GenericNF().simplifyCFG(G.__copy__())
    print(G, '\n', g)
Ejemplo n.º 6
0
            p0[len(p0)] = _v
            _Ps[v] = [x for x in [p0] if x not in _Ps[v]] + _Ps[v]
        p1 = {}
        p2 = {}
        for i, s in p.items():
            if i != 0:
                p1[i - 1] = s
                p2[i - 1] = s
        p2[len(p2)] = _v
        _Ps[_v] = [x for x in [p1, p2] if x not in _Ps[_v]] + _Ps[_v]
        return _Ps


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

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

    if result:
        print('\ngrammar is in Greibach normal form')
    else:
        print('\ngrammar is not in Greibach normal form\n')
        g = GreibachNF().convertToNF(G)
        print(
            g.__str__([
                'S', 'A', 'B', 'X\\_1', 'A\\_9', 'A\\_8', 'X\\_0', 'S\\_0',
                'B\\_0', 'B\\_1'