Verified Commit e9dcabb2 authored by Vladimír Štill's avatar Vladimír Štill
Browse files

CFG: Fix an overzealous assertion in production counting

parent 9d4203ee
Loading
Loading
Loading
Loading
+4 −4
Original line number Original line Diff line number Diff line
@@ -723,15 +723,12 @@ class WordGenerator:
        self.counts.append({n: 0 for n in self.cfg.nonterminals})
        self.counts.append({n: 0 for n in self.cfg.nonterminals})


        for src, prod in self.cfg.productions():
        for src, prod in self.cfg.productions():
            count = self._materialize_prod(prod, length)
            count = self._materialize_prod_count(prod, length)
            self.counts[length][src] += count
            self.counts[length][src] += count


    def _materialize_prod(self, prod: CFG.Production, length: int, prefix=""):
    def _materialize_prod(self, prod: CFG.Production, length: int, prefix=""):
        """Assumes smaller length are already computed"""
        """Assumes smaller length are already computed"""


        assert len(self.prod_counts) >= length, \
            "smaller production lengths must be already computed"

        if isinstance(prod, Eps):
        if isinstance(prod, Eps):
            return Eps() in self.cfg.rules.get(self.cfg.init, [])
            return Eps() in self.cfg.rules.get(self.cfg.init, [])


@@ -743,6 +740,9 @@ class WordGenerator:
        if CFG.all_terminal(prod):
        if CFG.all_terminal(prod):
            return len(prod) == length
            return len(prod) == length


        assert len(self.prod_counts) >= length, \
            "smaller production lengths must be already computed"

        if len(self.prod_counts) == length:
        if len(self.prod_counts) == length:
            self.prod_counts.append(dict())
            self.prod_counts.append(dict())