def decode_grammar(self, P): g=SubGrammar(self.G) vd = VaultDistribution() iterp = iter(P) stack = ['G'] done = [] while stack: head = stack.pop() assert head not in done done.append(head) p = iterp.next() n = vd.decode_vault_size(head, p) #print "RuleSizeDecoding:", head, n t_set = [] for x in range(n): rhs = self.decode(head, iterp.next()) #print "Decoding:", stack, head, '==>', rhs if rhs != '__totoal__': r = filter(lambda x: x not in done+stack, self.G.get_actual_NonTlist(head, rhs)) if r: for x in r: if (x not in t_set): t_set.append(x) g.add_rule(head, rhs) t_set.reverse() stack.extend(t_set) g.finalize() # fixes the freq and some other book keepings return g