Exemplo n.º 1
0
    def __init__(self, lp, lex_ws, ptrees, sen=None):
        self.lex = Lexer.parse(open(lp, 'r').read())
        self.lex_ws = lex_ws
        self.ptrees = ptrees
        self.sen = sen
        self.amb_type = 'horizontal'
        for l in iter(self.ptrees.splitlines()):
            if re.match(VERTICAL_AMBIGUITY, l):
                self.amb_type = 'vertical'
                break

        amb1, amb2 = None, None
        if self.amb_type == 'vertical':
            amb1, amb2 = self.parse_vamb()
        else:
            amb1, amb2 = self.parse_hamb()

        # extract the ambiguous string from sentence
        amb1s = self.ambiguous_string(amb1)
        amb2s = self.ambiguous_string(amb2)
        assert amb1s == amb2s
        self.amb_str = amb1s

        # extract the ambiguous grammar rules from parse trees
        _cfg = self.ambiguous_cfg_subset(amb1, amb2)
        # first, minimise the lex based on the cfg
        self.sym_toks, self.toks = self.minimise_lex(_cfg)
        tp = tempfile.mktemp()
        Lexer.write(self.sym_toks, self.toks, self.lex_ws, tp)
        lex = Lexer.parse(open(tp, 'r').read())

        # convert _cfg to a CFG instance.
        self.min_cfg = self.to_CFG(_cfg, lex)
Exemplo n.º 2
0
 def write_cfg_lex(self, ambi_parse, gp, lp):
     CFG.write(ambi_parse.min_cfg, gp)
     Lexer.write(ambi_parse.sym_toks, ambi_parse.toks, self._sin.lex_ws, lp)