def nont_freq(G): freq = [0 for i in xi(G)] for i in xi(G): for nont, e in G[i]: if nont: freq[e] += 1 return freq
def calculate_paths(self, i=-1, j=-1): if i == -1: i = self.i if j == -1: j = self.j for x in xi(self.dist): self.dist[x] = x + 1 self.dist[0] = 0 for x in xrange(i, j + 1): for prev, sym in self.g[x]: if prev < i: continue if prev == self.i and x == self.j: continue dx = self.dist[x - self.i] dp = self.dist[prev - self.i] if dx < dp + 1: continue elif dx == dp + 1: self.ties[x - self.i].append(prev - self.i) self.sym[x - self.i].append(sym) elif dx > dp + 1: del self.ties[x - self.i][:] self.ties[x - self.i].append(prev - self.i) self.dist[x - self.i] = dp + 1 del self.sym[x - self.i][:] self.sym[x - self.i].append(sym)
def __getitem__(self, x): """ Iterate over the edges arriving to x. Returns pairs (prev_index,symbol_label) that represent and edge between prev_index and x with label symbol label """ for i in xi(self.prev[x]): yield self.prev[x][i], self.psym[x][i]
def __init__(self, context): self.ctx = context self.G = None self.q = [] rw = self.ctx.alphabet.get_rewrite() self.graph = reordering_graph(len(rw)) for i in xi(rw): self.graph.add(i, i + 1, rw[i]) self.paths = [reordering_paths(self.graph, 0, len(rw))] self.minimize()
def get_a_bracket_for_nonterminals(G): start = [None for i in xi(G)] end = [None for i in xi(G)] root = G.get_S() start[root] = 0 globalpos = 0 q = [root] for nont, e in iterate_in_codification_order(G, root): if nont: if e is None: end[q.pop()] = globalpos elif end[e] is not None: globalpos += end[e] - start[e] else: q.append(e) start[e] = globalpos else: globalpos += 1 end[root] = globalpos ijs = [] for i in xi(start): ijs.append((start[i], end[i])) return ijs
def calculate_dists(self, i=-1, j=-1): if i == -1: i = self.i if j == -1: j = self.j for x in xi(self.dist): self.dist[x] = x + 1 self.dist[0] = 0 for x in xrange(i, j + 1): for prev in self.g.prevs(x): if prev < i: continue self.dist[x - self.i] = min(self.dist[x - self.i], self.dist[prev - self.i])
def step_bottomup(R, sizef=size_tesis): cons = R.get_context().cons R.minimize() size = sizef(R) q = set(R.get_q()) answer = None for c in xi(cons): if c in q: continue R.push(c) R.minimize() newsize = sizef(R) R.pop() if newsize <= size: size = newsize answer = c return answer, size
def get_yields(G): ys = [None for i in xi(G)] root = G.get_S() ys[root] = [] q = [root] for nont, e in iterate_in_codification_order(G): if nont: if e is None: last = q.pop() ys[q[-1]].extend(ys[last]) elif ys[e] is not None: ys[q[-1]].extend(ys[e]) else: q.append(e) ys[e] = [] else: ys[q[-1]].append(e) return ys
def iterate_in_codification_order(G, root=None, end_rule_symbol=None): if root is None: root = G.get_S() done = [False for i in xi(G)] done[root] = True q = [(root, 0)] while len(q) != 0: current, pos = q.pop() body = G[current] i = pos while i < len(body): nont, e = body[i] yield nont, e if nont and not done[e]: q.append((current, i + 1)) q.append((e, 0)) break i += 1 if i == len(body): done[current] = True if current != root: yield True, end_rule_symbol
def parse_grammar(s, alphac): # The "trick" in this implementation is that alphacode assigns increasing # values from 0 and tree_grammar too. This is postcondition of both. lines = s.split("\n") nont_alpha = generic_alphacode() G = tree_grammar() rules = {} for line in lines: if line == "": continue nt, body = line.split(" -> ") nt = int(nt) nont_alpha.code(nt) body = parse_body(body, nont_alpha.code, alphac.code) rules[nt] = body assert len(rules) == len(nont_alpha) for i in xi(rules): nt = nont_alpha.decode(i) j = G.add_body(rules[nt]) assert i == j S = search_S(G) assert S is not None G.set_S(S) return G
def clear_to_1(self): for i in xi(self.prev): del self.prev[i][1:] del self.psym[i][1:]
def search_S(G): freq = nont_freq(G) for i in xi(freq): if freq[i] == 0: return i return None