Exemplo n.º 1
0
def parse(starting_rule, text):
    # text_with_indexes = enumerate([ None ] + text.lower().split())
    import string

    splitted = string.replace(text.lower(), ",", " , ").split()
    text_with_indexes = enumerate([None] + splitted)

    table = [Column(i, token) for i, token in text_with_indexes]

    gamma_prod = ConstrainedProduction(constraint.new_empty_avm(), starting_rule)
    table[0].add(ConstrainedState(GAMMA_RULE, gamma_prod, 0, gamma_prod.avm, table[0]))

    for i, column in enumerate(table):
        for state in column:
            if state.is_completed():
                complete(column, state)
            else:
                term = state.get_next_term()
                if isinstance(term, Rule):
                    predict(column, term)
                elif i + 1 < len(table):
                    scan(table[i + 1], state, term)

        # XXX(sandello): You can uncomment this line to see full dump of
        # the chart table.
        #
        # column.dump(only_completed = False)

    # Find Gamma rule in the last table column or fail otherwise.
    for state in table[-1]:
        if state.name == GAMMA_RULE and state.is_completed():
            return [tree for tree in build_trees(state, table)]
    else:
        return []
Exemplo n.º 2
0
    def process_rule(self, in_number, in_line):
        line = in_line
        n = in_number
        parts = line.strip().split()

        for part in parts:
            if part != part.upper() and part != part.lower():
                raise RuntimeError, "Malformed line #{0}: Mixed-case for term '{1}'".format(n + 1, part)

        if len(parts) == 0:
            return

        if len(parts) == 1:
            if parts[0] not in self.non_terminals:
                raise RuntimeError, "Malformed line #{0}: Unknown non-terminal '{1}'".format(n + 1, parts[0])
            else:
                self.starting_rule = parts[0]
                return

        if parts[1] != "->":
            raise RuntimeError, "Malformed line #{0}: Second part have to be '->'".format(n + 1)

        lhs = self.get_term(parts[0])
        rhs = map(self.get_term, parts[2:])

        if not isinstance(lhs, Rule):
            raise RuntimeError, "Malformed line #{0}: Left-hand side have to be a non-terminal".format(n + 1)

        rhs_prod = ConstrainedProduction(constraint.new_empty_avm(), *rhs)
        lhs.add(rhs_prod)
        self.last_production = rhs_prod